src/Pure/Isar/specification.ML
author wenzelm
Mon, 01 Mar 2010 17:09:42 +0100
changeset 35418 4c7cba1f7ce9
parent 35399 3881972fcfca
child 35624 c4e29a0bb8c1
permissions -rw-r--r--
added type_notation command;
     1 (*  Title:      Pure/Isar/specification.ML
     2     Author:     Makarius
     3 
     4 Derived local theory specifications --- with type-inference and
     5 toplevel polymorphism.
     6 *)
     7 
     8 signature SPECIFICATION =
     9 sig
    10   val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
    11   val check_spec:
    12     (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
    13     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
    14   val read_spec:
    15     (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
    16     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
    17   val check_free_spec:
    18     (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
    19     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
    20   val read_free_spec:
    21     (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
    22     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
    23   val check_specification: (binding * typ option * mixfix) list ->
    24     (Attrib.binding * term list) list -> Proof.context ->
    25     (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
    26   val read_specification: (binding * string option * mixfix) list ->
    27     (Attrib.binding * string list) list -> Proof.context ->
    28     (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
    29   val axiomatization: (binding * typ option * mixfix) list ->
    30     (Attrib.binding * term list) list -> theory ->
    31     (term list * thm list list) * theory
    32   val axiomatization_cmd: (binding * string option * mixfix) list ->
    33     (Attrib.binding * string list) list -> theory ->
    34     (term list * thm list list) * theory
    35   val definition:
    36     (binding * typ option * mixfix) option * (Attrib.binding * term) ->
    37     local_theory -> (term * (string * thm)) * local_theory
    38   val definition_cmd:
    39     (binding * string option * mixfix) option * (Attrib.binding * string) ->
    40     local_theory -> (term * (string * thm)) * local_theory
    41   val abbreviation: Syntax.mode -> (binding * typ option * mixfix) option * term ->
    42     local_theory -> local_theory
    43   val abbreviation_cmd: Syntax.mode -> (binding * string option * mixfix) option * string ->
    44     local_theory -> local_theory
    45   val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
    46   val type_notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    47   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    48   val notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    49   val theorems: string ->
    50     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    51     local_theory -> (string * thm list) list * local_theory
    52   val theorems_cmd: string ->
    53     (Attrib.binding * (Facts.ref * Attrib.src list) list) list ->
    54     local_theory -> (string * thm list) list * local_theory
    55   val theorem: string -> Method.text option ->
    56     (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
    57     Element.context_i list -> Element.statement_i ->
    58     bool -> local_theory -> Proof.state
    59   val theorem_cmd: string -> Method.text option ->
    60     (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
    61     Element.context list -> Element.statement ->
    62     bool -> local_theory -> Proof.state
    63   val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic
    64 end;
    65 
    66 structure Specification: SPECIFICATION =
    67 struct
    68 
    69 (* diagnostics *)
    70 
    71 fun print_consts _ _ [] = ()
    72   | print_consts ctxt pred cs = Pretty.writeln (Proof_Display.pretty_consts ctxt pred cs);
    73 
    74 
    75 (* prepare specification *)
    76 
    77 local
    78 
    79 fun close_forms ctxt i xs As =
    80   let
    81     fun add_free (Free (x, _)) = if Variable.is_fixed ctxt x then I else insert (op =) x
    82       | add_free _ = I;
    83 
    84     val commons = map #1 xs;
    85     val _ =
    86       (case duplicates (op =) commons of [] => ()
    87       | dups => error ("Duplicate local variables " ^ commas_quote dups));
    88     val frees = rev ((fold o fold_aterms) add_free As (rev commons));
    89     val types = map (TypeInfer.param i o rpair []) (Name.invents Name.context Name.aT (length frees));
    90     val uniform_typing = the o AList.lookup (op =) (frees ~~ types);
    91 
    92     fun abs_body lev y (Abs (x, T, b)) = Abs (x, T, abs_body (lev + 1) y b)
    93       | abs_body lev y (t $ u) = abs_body lev y t $ abs_body lev y u
    94       | abs_body lev y (t as Free (x, T)) =
    95           if x = y then TypeInfer.constrain (uniform_typing x) (TypeInfer.constrain T (Bound lev))
    96           else t
    97       | abs_body _ _ a = a;
    98     fun close (y, U) B =
    99       let val B' = abs_body 0 y (Term.incr_boundvars 1 B)
   100       in if Term.loose_bvar1 (B', 0) then Term.all dummyT $ Abs (y, U, B') else B end;
   101     fun close_form A =
   102       let
   103         val occ_frees = rev (fold_aterms add_free A []);
   104         val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees);
   105       in fold_rev close bounds A end;
   106   in map close_form As end;
   107 
   108 fun prepare prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
   109   let
   110     val thy = ProofContext.theory_of ctxt;
   111 
   112     val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
   113     val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes vars;
   114 
   115     val Asss = (map o map) (map (parse_prop params_ctxt) o snd) raw_specss;
   116     val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
   117       |> fold Name.declare xs;
   118     val Asss' = #1 ((fold_map o fold_map o fold_map) Term.free_dummy_patterns Asss names);
   119     val idx = (fold o fold o fold) Term.maxidx_term Asss' ~1 + 1;
   120     val specs =
   121       (if do_close then
   122         #1 (fold_map
   123             (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
   124       else Asss')
   125       |> flat |> burrow (Syntax.check_props params_ctxt);
   126     val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
   127 
   128     val Ts = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
   129     val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
   130     val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
   131   in ((params, name_atts ~~ specs), specs_ctxt) end;
   132 
   133 
   134 fun single_spec (a, prop) = [(a, [prop])];
   135 fun the_spec (a, [prop]) = (a, prop);
   136 
   137 fun prep_spec prep_vars parse_prop prep_att do_close vars specs =
   138   prepare prep_vars parse_prop prep_att do_close
   139     vars (map single_spec specs) #>> apsnd (map the_spec);
   140 
   141 in
   142 
   143 fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) true x;
   144 fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true x;
   145 
   146 fun check_free_spec x = prep_spec ProofContext.cert_vars (K I) (K I) false x;
   147 fun read_free_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src false x;
   148 
   149 fun check_specification vars specs =
   150   prepare ProofContext.cert_vars (K I) (K I) true vars [specs];
   151 
   152 fun read_specification vars specs =
   153   prepare ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true vars [specs];
   154 
   155 end;
   156 
   157 
   158 (* axiomatization -- within global theory *)
   159 
   160 fun gen_axioms do_print prep raw_vars raw_specs thy =
   161   let
   162     val ((vars, specs), _) = prep raw_vars raw_specs (ProofContext.init thy);
   163     val xs = map (fn ((b, T), _) => (Name.of_binding b, T)) vars;
   164 
   165     (*consts*)
   166     val (consts, consts_thy) = thy |> fold_map Theory.specify_const vars;
   167     val subst = Term.subst_atomic (map Free xs ~~ consts);
   168 
   169     (*axioms*)
   170     val (axioms, axioms_thy) = (specs, consts_thy) |-> fold_map (fn ((b, atts), props) =>
   171         fold_map Thm.add_axiom
   172           (map (apfst (fn a => Binding.map_name (K a) b))
   173             (PureThy.name_multi (Name.of_binding b) (map subst props)))
   174         #>> (fn ths => ((b, atts), [(ths, [])])));
   175 
   176     (*facts*)
   177     val (facts, facts_lthy) = axioms_thy
   178       |> Theory_Target.init NONE
   179       |> Spec_Rules.add Spec_Rules.Unknown (consts, maps (maps #1 o #2) axioms)
   180       |> Local_Theory.notes axioms;
   181 
   182     val _ =
   183       if not do_print then ()
   184       else print_consts facts_lthy (K false) xs;
   185   in ((consts, map #2 facts), Local_Theory.exit_global facts_lthy) end;
   186 
   187 val axiomatization = gen_axioms false check_specification;
   188 val axiomatization_cmd = gen_axioms true read_specification;
   189 
   190 
   191 (* definition *)
   192 
   193 fun gen_def do_print prep (raw_var, raw_spec) lthy =
   194   let
   195     val (vars, [((raw_name, atts), prop)]) = fst (prep (the_list raw_var) [raw_spec] lthy);
   196     val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop;
   197     val var as (b, _) =
   198       (case vars of
   199         [] => (Binding.name x, NoSyn)
   200       | [((b, _), mx)] =>
   201           let
   202             val y = Name.of_binding b;
   203             val _ = x = y orelse
   204               error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote y ^
   205                 Position.str_of (Binding.pos_of b));
   206           in (b, mx) end);
   207     val name = Thm.def_binding_optional b raw_name;
   208     val ((lhs, (_, raw_th)), lthy2) = lthy
   209       |> Local_Theory.define (var, ((Binding.suffix_name "_raw" name, []), rhs));
   210 
   211     val th = prove lthy2 raw_th;
   212     val lthy3 = lthy2 |> Spec_Rules.add Spec_Rules.Equational ([lhs], [th]);
   213 
   214     val ([(def_name, [th'])], lthy4) = lthy3
   215       |> Local_Theory.notes_kind Thm.definitionK
   216         [((name, Code.add_default_eqn_attrib :: atts), [([th], [])])];
   217 
   218     val lhs' = Morphism.term (Local_Theory.target_morphism lthy4) lhs;
   219     val _ =
   220       if not do_print then ()
   221       else print_consts lthy4 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
   222   in ((lhs, (def_name, th')), lthy4) end;
   223 
   224 val definition = gen_def false check_free_spec;
   225 val definition_cmd = gen_def true read_free_spec;
   226 
   227 
   228 (* abbreviation *)
   229 
   230 fun gen_abbrev do_print prep mode (raw_var, raw_prop) lthy =
   231   let
   232     val ((vars, [(_, prop)]), _) =
   233       prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
   234         (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
   235     val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop));
   236     val var =
   237       (case vars of
   238         [] => (Binding.name x, NoSyn)
   239       | [((b, _), mx)] =>
   240           let
   241             val y = Name.of_binding b;
   242             val _ = x = y orelse
   243               error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y ^
   244                 Position.str_of (Binding.pos_of b));
   245           in (b, mx) end);
   246     val lthy' = lthy
   247       |> ProofContext.set_syntax_mode mode    (* FIXME ?!? *)
   248       |> Local_Theory.abbrev mode (var, rhs) |> snd
   249       |> ProofContext.restore_syntax_mode lthy;
   250 
   251     val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
   252   in lthy' end;
   253 
   254 val abbreviation = gen_abbrev false check_free_spec;
   255 val abbreviation_cmd = gen_abbrev true read_free_spec;
   256 
   257 
   258 (* notation *)
   259 
   260 local
   261 
   262 fun gen_type_notation prep_type add mode args lthy =
   263   lthy |> Local_Theory.type_notation add mode (map (apfst (prep_type lthy)) args);
   264 
   265 fun gen_notation prep_const add mode args lthy =
   266   lthy |> Local_Theory.notation add mode (map (apfst (prep_const lthy)) args);
   267 
   268 in
   269 
   270 val type_notation = gen_type_notation (K I);
   271 val type_notation_cmd = gen_type_notation (fn ctxt => ProofContext.read_type_name ctxt false);
   272 
   273 val notation = gen_notation (K I);
   274 val notation_cmd = gen_notation (fn ctxt => ProofContext.read_const ctxt false);
   275 
   276 end;
   277 
   278 
   279 (* fact statements *)
   280 
   281 fun gen_theorems prep_fact prep_att kind raw_facts lthy =
   282   let
   283     val attrib = prep_att (ProofContext.theory_of lthy);
   284     val facts = raw_facts |> map (fn ((name, atts), bs) =>
   285       ((name, map attrib atts),
   286         bs |> map (fn (b, more_atts) => (prep_fact lthy b, map attrib more_atts))));
   287     val (res, lthy') = lthy |> Local_Theory.notes_kind kind facts;
   288     val _ = Proof_Display.print_results true lthy' ((kind, ""), res);
   289   in (res, lthy') end;
   290 
   291 val theorems = gen_theorems (K I) (K I);
   292 val theorems_cmd = gen_theorems ProofContext.get_fact Attrib.intern_src;
   293 
   294 
   295 (* complex goal statements *)
   296 
   297 local
   298 
   299 fun prep_statement prep_att prep_stmt elems concl ctxt =
   300   (case concl of
   301     Element.Shows shows =>
   302       let
   303         val (propp, elems_ctxt) = prep_stmt elems (map snd shows) ctxt;
   304         val prems = Assumption.local_prems_of elems_ctxt ctxt;
   305         val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
   306         val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
   307       in ((prems, stmt, NONE), goal_ctxt) end
   308   | Element.Obtains obtains =>
   309       let
   310         val case_names = obtains |> map_index (fn (i, (b, _)) =>
   311           if Binding.is_empty b then string_of_int (i + 1) else Name.of_binding b);
   312         val constraints = obtains |> map (fn (_, (vars, _)) =>
   313           Element.Constrains
   314             (vars |> map_filter (fn (x, SOME T) => SOME (Name.of_binding x, T) | _ => NONE)));
   315 
   316         val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
   317         val (propp, elems_ctxt) = prep_stmt (elems @ constraints) raw_propp ctxt;
   318 
   319         val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) Auto_Bind.thesisN;
   320 
   321         fun assume_case ((name, (vars, _)), asms) ctxt' =
   322           let
   323             val bs = map fst vars;
   324             val xs = map Name.of_binding bs;
   325             val props = map fst asms;
   326             val (Ts, _) = ctxt'
   327               |> fold Variable.declare_term props
   328               |> fold_map ProofContext.inferred_param xs;
   329             val asm = Term.list_all_free (xs ~~ Ts, Logic.list_implies (props, thesis));
   330           in
   331             ctxt' |> (snd o ProofContext.add_fixes (map (fn b => (b, NONE, NoSyn)) bs));
   332             ctxt' |> Variable.auto_fixes asm
   333             |> ProofContext.add_assms_i Assumption.assume_export
   334               [((name, [Context_Rules.intro_query NONE]), [(asm, [])])]
   335             |>> (fn [(_, [th])] => th)
   336           end;
   337 
   338         val atts = map (Attrib.internal o K)
   339           [Rule_Cases.consumes (~ (length obtains)), Rule_Cases.case_names case_names];
   340         val prems = Assumption.local_prems_of elems_ctxt ctxt;
   341         val stmt = [((Binding.empty, atts), [(thesis, [])])];
   342 
   343         val (facts, goal_ctxt) = elems_ctxt
   344           |> (snd o ProofContext.add_fixes [(Binding.name Auto_Bind.thesisN, NONE, NoSyn)])
   345           |> fold_map assume_case (obtains ~~ propp)
   346           |-> (fn ths =>
   347             ProofContext.note_thmss "" [((Binding.name Obtain.thatN, []), [(ths, [])])] #>
   348             #2 #> pair ths);
   349       in ((prems, stmt, SOME facts), goal_ctxt) end);
   350 
   351 structure TheoremHook = Generic_Data
   352 (
   353   type T = ((bool -> Proof.state -> Proof.state) * stamp) list;
   354   val empty = [];
   355   val extend = I;
   356   fun merge hooks : T = Library.merge (eq_snd op =) hooks;
   357 );
   358 
   359 fun gen_theorem prep_att prep_stmt
   360     kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy =
   361   let
   362     val _ = Local_Theory.affirm lthy;
   363     val thy = ProofContext.theory_of lthy;
   364 
   365     val attrib = prep_att thy;
   366     val atts = map attrib raw_atts;
   367     val elems = raw_elems |> map (Element.map_ctxt_attrib attrib);
   368     val ((prems, stmt, facts), goal_ctxt) =
   369       prep_statement attrib prep_stmt elems raw_concl lthy;
   370 
   371     fun after_qed' results goal_ctxt' =
   372       let val results' =
   373         burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
   374       in
   375         lthy
   376         |> Local_Theory.notes_kind kind
   377           (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
   378         |> (fn (res, lthy') =>
   379           if Binding.is_empty name andalso null atts then
   380             (Proof_Display.print_results true lthy' ((kind, ""), res); lthy')
   381           else
   382             let
   383               val ([(res_name, _)], lthy'') = lthy'
   384                 |> Local_Theory.notes_kind kind [((name, atts), [(maps #2 res, [])])];
   385               val _ = Proof_Display.print_results true lthy' ((kind, res_name), res);
   386             in lthy'' end)
   387         |> after_qed results'
   388       end;
   389   in
   390     goal_ctxt
   391     |> ProofContext.note_thmss "" [((Binding.name Auto_Bind.assmsN, []), [(prems, [])])]
   392     |> snd
   393     |> Proof.theorem_i before_qed after_qed' (map snd stmt)
   394     |> (case facts of NONE => I | SOME ths => Proof.refine_insert ths)
   395     |> Library.apply (map (fn (f, _) => f int) (rev (TheoremHook.get (Context.Proof goal_ctxt))))
   396   end;
   397 
   398 in
   399 
   400 val theorem = gen_theorem (K I) Expression.cert_statement;
   401 val theorem_cmd = gen_theorem Attrib.intern_src Expression.read_statement;
   402 
   403 fun add_theorem_hook f = TheoremHook.map (cons (f, stamp ()));
   404 
   405 end;
   406 
   407 end;