src/Pure/Isar/specification.ML
author wenzelm
Thu Nov 30 14:17:25 2006 +0100 (2006-11-30)
changeset 21602 cb13024d0e36
parent 21532 8c162b766cef
child 21658 5e31241e1e3c
permissions -rw-r--r--
Goal.norm/close_result;
     1 (*  Title:      Pure/Isar/specification.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Derived local theory specifications --- with type-inference and
     6 toplevel polymorphism.
     7 *)
     8 
     9 signature SPECIFICATION =
    10 sig
    11   val quiet_mode: bool ref
    12   val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
    13   val read_specification: (string * string option * mixfix) list ->
    14     ((string * Attrib.src list) * string list) list -> local_theory ->
    15     (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    16     local_theory
    17   val cert_specification: (string * typ option * mixfix) list ->
    18     ((string * Attrib.src list) * term list) list -> local_theory ->
    19     (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    20     local_theory
    21   val axiomatization: (string * string option * mixfix) list ->
    22     ((bstring * Attrib.src list) * string list) list -> local_theory ->
    23     (term list * (bstring * thm list) list) * local_theory
    24   val axiomatization_i: (string * typ option * mixfix) list ->
    25     ((bstring * Attrib.src list) * term list) list -> local_theory ->
    26     (term list * (bstring * thm list) list) * local_theory
    27   val definition:
    28     ((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list ->
    29     local_theory -> (term * (bstring * thm)) list * local_theory
    30   val definition_i:
    31     ((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list ->
    32     local_theory -> (term * (bstring * thm)) list * local_theory
    33   val abbreviation: Syntax.mode -> ((string * string option * mixfix) option * string) list ->
    34     local_theory -> local_theory
    35   val abbreviation_i: Syntax.mode -> ((string * typ option * mixfix) option * term) list ->
    36     local_theory -> local_theory
    37   val notation: Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    38   val notation_i: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    39   val theorems: string -> ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list
    40     -> local_theory -> (bstring * thm list) list * local_theory
    41   val theorems_i: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list
    42     -> local_theory -> (bstring * thm list) list * local_theory
    43   val theorem: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
    44     string * Attrib.src list -> Element.context Locale.element list -> Element.statement ->
    45     local_theory -> Proof.state
    46   val theorem_i: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
    47     string * Attrib.src list -> Element.context_i Locale.element list -> Element.statement_i ->
    48     local_theory -> Proof.state
    49 end;
    50 
    51 structure Specification: SPECIFICATION =
    52 struct
    53 
    54 
    55 (* diagnostics *)
    56 
    57 val quiet_mode = ref false;
    58 
    59 fun print_consts _ _ [] = ()
    60   | print_consts ctxt pred cs =
    61       if ! quiet_mode then () else Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs);
    62 
    63 fun present_results ctxt res =
    64   if ! quiet_mode then () else ProofDisplay.present_results ctxt res;
    65 
    66 fun present_results' ctxt kind res = present_results ctxt ((kind, ""), res);
    67 
    68 
    69 (* prepare specification *)
    70 
    71 fun prep_specification prep_vars prep_propp prep_att raw_vars raw_specs ctxt =
    72   let
    73     val thy = ProofContext.theory_of ctxt;
    74 
    75     val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
    76     val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
    77     val ((specs, vs), specs_ctxt) =
    78       prep_propp (params_ctxt, map (map (rpair []) o snd) raw_specs)
    79       |> swap |>> map (map fst)
    80       ||>> fold_map ProofContext.inferred_param xs;
    81 
    82     val params = vs ~~ map #3 vars;
    83     val names = map (fst o fst) raw_specs;
    84     val atts = map (map (prep_att thy) o snd o fst) raw_specs;
    85   in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
    86 
    87 fun read_specification x =
    88   prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
    89 
    90 fun cert_specification x =
    91   prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
    92 
    93 
    94 (* axiomatization *)
    95 
    96 fun gen_axioms prep raw_vars raw_specs lthy =
    97   let
    98     val (vars, specs) = fst (prep raw_vars raw_specs lthy);
    99     val cs = map fst vars;
   100     val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
   101 
   102     val ((consts, axioms), lthy') = lthy
   103       |> LocalTheory.consts spec_frees vars
   104       ||> fold (fold Variable.auto_fixes o snd) specs   (* FIXME !? *)
   105       ||>> LocalTheory.axioms Thm.axiomK specs;
   106 
   107     (* FIXME generic target!? *)
   108     val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) consts;
   109     val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
   110 
   111     val _ = print_consts lthy' spec_frees cs;
   112   in ((map #1 consts, axioms), lthy'') end;
   113 
   114 val axiomatization = gen_axioms read_specification;
   115 val axiomatization_i = gen_axioms cert_specification;
   116 
   117 
   118 (* definition *)
   119 
   120 fun gen_defs prep args lthy =
   121   let
   122     fun define (raw_var, (raw_a, raw_prop)) lthy1 =
   123       let
   124         val (vars, [((raw_name, atts), [prop])]) =
   125           fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy1);
   126         val (((x, T), rhs), prove) = LocalDefs.derived_def lthy1 true prop;
   127         val name = Thm.def_name_optional x raw_name;
   128         val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
   129           if x = x' then mx
   130           else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
   131         val ((lhs, (_, th)), lthy2) = lthy1
   132 (*          |> LocalTheory.def ((x, mx), ((name ^ "_raw", []), rhs));  FIXME *)
   133           |> LocalTheory.def Thm.definitionK ((x, mx), ((name, []), rhs));
   134         val ((b, [th']), lthy3) = lthy2
   135           |> LocalTheory.note Thm.definitionK ((name, atts), [prove lthy2 th]);
   136       in (((x, T), (lhs, (b, th'))), LocalTheory.restore lthy3) end;
   137 
   138     val ((cs, defs), lthy') = lthy |> fold_map define args |>> split_list;
   139     val def_frees = member (op =) (fold (Term.add_frees o fst) defs []);
   140     val _ = print_consts lthy' def_frees cs;
   141   in (defs, lthy') end;
   142 
   143 val definition = gen_defs read_specification;
   144 val definition_i = gen_defs cert_specification;
   145 
   146 
   147 (* abbreviation *)
   148 
   149 fun gen_abbrevs prep mode args lthy =
   150   let
   151     fun abbrev (raw_var, raw_prop) lthy1 =
   152       let
   153         val ((vars, [(_, [prop])]), _) =
   154           prep (the_list raw_var) [(("", []), [raw_prop])]
   155             (lthy1 |> ProofContext.expand_abbrevs false);
   156         val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy1 prop));
   157         val mx = (case vars of [] => NoSyn | [((y, _), mx)] =>
   158           if x = y then mx
   159           else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y));
   160         val lthy2 = lthy1 |> LocalTheory.abbrevs mode [((x, mx), rhs)];
   161       in ((x, T), LocalTheory.restore lthy2) end;
   162 
   163     val (cs, lthy') = lthy
   164       |> ProofContext.set_syntax_mode mode
   165       |> fold_map abbrev args
   166       ||> ProofContext.restore_syntax_mode lthy;
   167     val _ = print_consts lthy' (K false) cs;
   168   in lthy' end;
   169 
   170 val abbreviation = gen_abbrevs read_specification;
   171 val abbreviation_i = gen_abbrevs cert_specification;
   172 
   173 
   174 (* notation *)
   175 
   176 fun gen_notation prep_const mode args lthy =
   177   lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args);
   178 
   179 val notation = gen_notation ProofContext.read_const;
   180 val notation_i = gen_notation (K I);
   181 
   182 
   183 (* fact statements *)
   184 
   185 fun gen_theorems prep_thms prep_att kind raw_facts lthy =
   186   let
   187     val attrib = prep_att (ProofContext.theory_of lthy);
   188     val facts = raw_facts |> map (fn ((name, atts), bs) =>
   189       ((name, map attrib atts),
   190         bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts))));
   191     val (res, lthy') = lthy |> LocalTheory.notes kind facts;
   192     val _ = present_results' lthy' kind res;
   193   in (res, lthy') end;
   194 
   195 val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
   196 val theorems_i = gen_theorems (K I) (K I);
   197 
   198 
   199 (* complex goal statements *)
   200 
   201 local
   202 
   203 fun subtract_prems ctxt1 ctxt2 =
   204   Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2);
   205 
   206 fun prep_statement prep_att prep_stmt elems concl ctxt =
   207   (case concl of
   208     Element.Shows shows =>
   209       let
   210         val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt;
   211         val prems = subtract_prems loc_ctxt elems_ctxt;
   212         val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
   213         val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
   214       in ((prems, stmt, []), goal_ctxt) end
   215   | Element.Obtains obtains =>
   216       let
   217         val case_names = obtains |> map_index
   218           (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
   219         val constraints = obtains |> map (fn (_, (vars, _)) =>
   220           Locale.Elem (Element.Constrains
   221             (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
   222 
   223         val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
   224         val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
   225 
   226         val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
   227 
   228         fun assume_case ((name, (vars, _)), asms) ctxt' =
   229           let
   230             val xs = map fst vars;
   231             val props = map fst asms;
   232             val (parms, _) = ctxt'
   233               |> fold Variable.declare_term props
   234               |> fold_map ProofContext.inferred_param xs;
   235             val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
   236           in
   237             ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
   238             ctxt' |> Variable.auto_fixes asm
   239             |> ProofContext.add_assms_i Assumption.assume_export
   240               [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
   241             |>> (fn [(_, [th])] => th)
   242           end;
   243 
   244         val atts = map Attrib.internal
   245           [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
   246         val prems = subtract_prems loc_ctxt elems_ctxt;
   247         val stmt = [(("", atts), [(thesis, [])])];
   248 
   249         val (facts, goal_ctxt) = elems_ctxt
   250           |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
   251           |> fold_map assume_case (obtains ~~ propp)
   252           |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
   253                 [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
   254       in ((prems, stmt, facts), goal_ctxt) end);
   255 
   256 fun gen_theorem prep_att prep_stmt
   257     kind before_qed after_qed (name, raw_atts) raw_elems raw_concl lthy0 =
   258   let
   259     val _ = LocalTheory.assert lthy0;
   260     val thy = ProofContext.theory_of lthy0;
   261 
   262     val (loc, ctxt, lthy) =
   263       (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
   264         (SOME loc, true) => (* workaround non-modularity of in/includes *)  (* FIXME *)
   265           (SOME loc, ProofContext.init thy, LocalTheory.restore lthy0)
   266       | _ => (NONE, lthy0, lthy0));
   267 
   268     val attrib = prep_att thy;
   269     val atts = map attrib raw_atts;
   270 
   271     val elems = raw_elems |> map (Locale.map_elem (Element.map_ctxt_attrib attrib));
   272     val ((prems, stmt, facts), goal_ctxt) =
   273       prep_statement attrib (prep_stmt loc) elems raw_concl ctxt;
   274 
   275     fun after_qed' results goal_ctxt' =
   276       let val results' =
   277         burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
   278       in
   279         lthy
   280         |> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
   281         |> (fn (res, lthy') =>
   282           (present_results lthy' ((kind, name), res);
   283             if name = "" andalso null atts then lthy'
   284             else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy')))
   285         |> after_qed results'
   286       end;
   287   in
   288     goal_ctxt
   289     |> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])]
   290     |> snd
   291     |> Proof.theorem_i before_qed after_qed' (map snd stmt)
   292     |> Proof.refine_insert facts
   293   end;
   294 
   295 in
   296 
   297 val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
   298 val theorem_i = gen_theorem (K I) Locale.cert_context_statement;
   299 
   300 end;
   301 
   302 end;