src/Pure/Isar/specification.ML
author wenzelm
Tue Nov 07 21:30:03 2006 +0100 (2006-11-07)
changeset 21236 890fafbcf8b0
parent 21230 abfdce60b371
child 21275 e4cb9c7a7482
permissions -rw-r--r--
complex goal statements: misc cleanup;
     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 fun cert_specification x =
    90   prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
    91 
    92 
    93 (* axiomatization *)
    94 
    95 fun gen_axioms prep raw_vars raw_specs lthy =
    96   let
    97     val (vars, specs) = fst (prep raw_vars raw_specs lthy);
    98     val cs = map fst vars;
    99     val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
   100 
   101     val ((consts, axioms), lthy') = lthy
   102       |> LocalTheory.consts spec_frees vars
   103       ||> fold (fold Variable.fix_frees o snd) specs   (* FIXME !? *)
   104       ||>> LocalTheory.axioms specs;
   105 
   106     (* FIXME generic target!? *)
   107     val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) consts;
   108     val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
   109 
   110     val _ = print_consts lthy' spec_frees cs;
   111   in ((map #1 consts, axioms), lthy'') end;
   112 
   113 val axiomatization = gen_axioms read_specification;
   114 val axiomatization_i = gen_axioms cert_specification;
   115 
   116 
   117 (* definition *)
   118 
   119 fun gen_defs prep args lthy =
   120   let
   121     fun define (raw_var, (raw_a, raw_prop)) lthy1 =
   122       let
   123         val (vars, [((raw_name, atts), [prop])]) =
   124           fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy1);
   125         val (((x, T), rhs), prove) = LocalDefs.derived_def lthy1 true prop;
   126         val name = Thm.def_name_optional x raw_name;
   127         val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
   128           if x = x' then mx
   129           else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
   130         val ((lhs, (_, th)), lthy2) = lthy1
   131 (*          |> LocalTheory.def ((x, mx), ((name ^ "_raw", []), rhs));  FIXME *)
   132           |> LocalTheory.def ((x, mx), ((name, []), rhs));
   133         val ((b, [th']), lthy3) = lthy2
   134           |> LocalTheory.note ((name, atts), [prove lthy2 th]);
   135       in (((x, T), (lhs, (b, th'))), LocalTheory.reinit lthy3) end;
   136 
   137     val ((cs, defs), lthy') = lthy |> fold_map define args |>> split_list;
   138     val def_frees = member (op =) (fold (Term.add_frees o fst) defs []);
   139     val _ = print_consts lthy' def_frees cs;
   140   in (defs, lthy') end;
   141 
   142 val definition = gen_defs read_specification;
   143 val definition_i = gen_defs cert_specification;
   144 
   145 
   146 (* abbreviation *)
   147 
   148 fun gen_abbrevs prep mode args lthy =
   149   let
   150     fun abbrev (raw_var, raw_prop) lthy1 =
   151       let
   152         val ((vars, [(_, [prop])]), _) =
   153           prep (the_list raw_var) [(("", []), [raw_prop])]
   154             (lthy1 |> ProofContext.expand_abbrevs false);
   155         val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy1 prop));
   156         val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
   157           if x = x' then mx
   158           else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote x'));
   159       in
   160         lthy1
   161         |> LocalTheory.abbrevs mode [((x, mx), rhs)]
   162         |> pair (x, T)
   163       end;
   164 
   165     val (cs, lthy1) = lthy
   166       |> ProofContext.set_syntax_mode mode
   167       |> fold_map abbrev args
   168       ||> ProofContext.restore_syntax_mode lthy;
   169     val _ = print_consts lthy1 (K false) cs;
   170   in lthy1 end;
   171 
   172 val abbreviation = gen_abbrevs read_specification;
   173 val abbreviation_i = gen_abbrevs cert_specification;
   174 
   175 
   176 (* notation *)
   177 
   178 fun gen_notation prep_const mode args lthy =
   179   lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args);
   180 
   181 val notation = gen_notation ProofContext.read_const;
   182 val notation_i = gen_notation (K I);
   183 
   184 
   185 (* fact statements *)
   186 
   187 fun gen_theorems prep_thms prep_att kind raw_facts lthy =
   188   let
   189     val k = if kind = "" then [] else [Attrib.kind kind];
   190     val attrib = prep_att (ProofContext.theory_of lthy);
   191     val facts = raw_facts |> map (fn ((name, atts), bs) =>
   192       ((name, map attrib atts),
   193         bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts @ k))));
   194     val (res, lthy') = lthy |> LocalTheory.notes facts;
   195     val _ = present_results' lthy' kind res;
   196   in (res, lthy') end;
   197 
   198 val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
   199 val theorems_i = gen_theorems (K I) (K I);
   200 
   201 
   202 (* complex goal statements *)
   203 
   204 local
   205 
   206 fun prep_statement prep_att prep_stmt elems concl ctxt =
   207   (case concl of
   208     Element.Shows shows =>
   209       let
   210         val (_, _, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt;
   211         val goal_ctxt = fold (fold (Variable.fix_frees o fst)) propp elems_ctxt;
   212         val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
   213       in ((stmt, []), goal_ctxt) end
   214   | Element.Obtains obtains =>
   215       let
   216         val case_names = obtains |> map_index
   217           (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
   218         val constraints = obtains |> map (fn (_, (vars, _)) =>
   219           Locale.Elem (Element.Constrains
   220             (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
   221 
   222         val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
   223         val (_, _, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
   224 
   225         val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
   226 
   227         fun assume_case ((name, (vars, _)), asms) ctxt' =
   228           let
   229             val xs = map fst vars;
   230             val props = map fst asms;
   231             val (parms, _) = ctxt'
   232               |> fold Variable.declare_term props
   233               |> fold_map ProofContext.inferred_param xs;
   234             val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
   235           in
   236             ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
   237             ctxt' |> Variable.fix_frees asm
   238             |> ProofContext.add_assms_i Assumption.assume_export
   239               [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
   240             |>> (fn [(_, [th])] => th)
   241           end;
   242 
   243         val atts = map Attrib.internal
   244           [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
   245         val stmt = [(("", atts), [(thesis, [])])];
   246 
   247         val (facts, goal_ctxt) = elems_ctxt
   248           |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
   249           |> fold_map assume_case (obtains ~~ propp)
   250           |-> (fn ths =>
   251             ProofContext.note_thmss_i [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
   252       in ((stmt, facts), goal_ctxt) end);
   253 
   254 fun gen_theorem prep_att prep_stmt
   255     kind before_qed after_qed (name, raw_atts) raw_elems raw_concl lthy0 =
   256   let
   257     val _ = LocalTheory.assert lthy0;
   258     val thy = ProofContext.theory_of lthy0;
   259     val attrib = prep_att thy;
   260 
   261     val (loc, ctxt, lthy) =
   262       (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
   263         (SOME loc, true) => (* workaround non-modularity of in/includes *)  (* FIXME *)
   264           (SOME loc, ProofContext.init thy, LocalTheory.reinit lthy0)
   265       | _ => (NONE, lthy0, lthy0));
   266 
   267     val elems = raw_elems |> (map o Locale.map_elem)
   268       (Element.map_ctxt {name = I, var = I, typ = I, term = I, fact = I, attrib = attrib});
   269     val ((stmt, facts), goal_ctxt) = prep_statement attrib (prep_stmt loc) elems raw_concl ctxt;
   270 
   271     val k = if kind = "" then [] else [Attrib.kind kind];
   272     val names = map (fst o fst) stmt;
   273     val attss = map (fn ((_, atts), _) => atts @ k) stmt;
   274     val atts = map attrib raw_atts @ k;
   275 
   276     fun after_qed' results goal_ctxt' =
   277       let val results' = burrow (ProofContext.export_standard goal_ctxt' lthy) results in
   278         lthy
   279         |> LocalTheory.notes ((names ~~ attss) ~~ map (fn ths => [(ths, [])]) results')
   280         |> (fn (res, lthy') =>
   281           (present_results lthy' ((kind, name), res);
   282             if name = "" andalso null raw_atts then lthy'
   283             else #2 (LocalTheory.notes [((name, atts), [(maps #2 res, [])])] lthy')))
   284         |> after_qed results'
   285       end;
   286   in
   287     goal_ctxt
   288     |> Proof.global_goal (K (K ())) Attrib.attribute_i ProofContext.bind_propp_schematic_i
   289       kind before_qed after_qed' NONE (name, []) stmt
   290     |> Proof.refine_insert facts
   291   end;
   292 
   293 in
   294 
   295 val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
   296 val theorem_i = gen_theorem (K I) Locale.cert_context_statement;
   297 
   298 end;
   299 
   300 end;