src/Pure/Isar/specification.ML
author wenzelm
Wed Oct 10 17:31:56 2007 +0200 (2007-10-10)
changeset 24949 5f00e3532418
parent 24927 48e08f37ce92
child 24986 1f902ded7f70
permissions -rw-r--r--
generalized notation interface (add or del);
     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 check_specification: (string * typ option * mixfix) list ->
    14     ((string * Attrib.src list) * term list) list list -> Proof.context ->
    15     (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    16     Proof.context
    17   val read_specification: (string * string option * mixfix) list ->
    18     ((string * Attrib.src list) * string list) list list -> Proof.context ->
    19     (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    20     Proof.context
    21   val check_free_specification: (string * typ option * mixfix) list ->
    22     ((string * Attrib.src list) * term list) list -> Proof.context ->
    23     (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    24     Proof.context
    25   val read_free_specification: (string * string option * mixfix) list ->
    26     ((string * Attrib.src list) * string list) list -> Proof.context ->
    27     (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    28     Proof.context
    29   val axiomatization: (string * typ option * mixfix) list ->
    30     ((bstring * Attrib.src list) * term list) list -> local_theory ->
    31     (term list * (bstring * thm list) list) * local_theory
    32   val axiomatization_cmd: (string * string option * mixfix) list ->
    33     ((bstring * Attrib.src list) * string list) list -> local_theory ->
    34     (term list * (bstring * thm list) list) * local_theory
    35   val definition:
    36     (string * typ option * mixfix) option * ((string * Attrib.src list) * term) ->
    37     local_theory -> (term * (bstring * thm)) * local_theory
    38   val definition_cmd:
    39     (string * string option * mixfix) option * ((string * Attrib.src list) * string) ->
    40     local_theory -> (term * (bstring * thm)) * local_theory
    41   val abbreviation: Syntax.mode -> (string * typ option * mixfix) option * term ->
    42     local_theory -> local_theory
    43   val abbreviation_cmd: Syntax.mode -> (string * string option * mixfix) option * string ->
    44     local_theory -> local_theory
    45   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    46   val notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    47   val theorems: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list
    48     -> local_theory -> (bstring * thm list) list * local_theory
    49   val theorems_cmd: string -> ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list
    50     -> local_theory -> (bstring * thm list) list * local_theory
    51   val theorem: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
    52     string * Attrib.src list -> Element.context_i Locale.element list -> Element.statement_i ->
    53     bool -> local_theory -> Proof.state
    54   val theorem_cmd: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
    55     string * Attrib.src list -> Element.context Locale.element list -> Element.statement ->
    56     bool -> local_theory -> Proof.state
    57   val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic
    58 end;
    59 
    60 structure Specification: SPECIFICATION =
    61 struct
    62 
    63 
    64 (* diagnostics *)
    65 
    66 val quiet_mode = ref false;
    67 
    68 fun print_consts _ _ [] = ()
    69   | print_consts ctxt pred cs =
    70       if ! quiet_mode then () else Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs);
    71 
    72 fun present_results ctxt res =
    73   if ! quiet_mode then () else ProofDisplay.present_results ctxt res;
    74 
    75 fun present_results' ctxt kind res = present_results ctxt ((kind, ""), res);
    76 
    77 
    78 (* prepare specification *)
    79 
    80 local
    81 
    82 fun close_forms ctxt i xs As =
    83   let
    84     fun add_free (Free (x, _)) = if Variable.is_fixed ctxt x then I else insert (op =) x
    85       | add_free _ = I;
    86 
    87     val commons = map #1 xs;
    88     val _ =
    89       (case duplicates (op =) commons of [] => ()
    90       | dups => error ("Duplicate local variables " ^ commas_quote dups));
    91     val frees = rev ((fold o fold_aterms) add_free As (rev commons));
    92     val types = map (TypeInfer.param i o rpair []) (Name.invents Name.context Name.aT (length frees));
    93     val uniform_typing = the o AList.lookup (op =) (frees ~~ types);
    94 
    95     fun abs_body lev y (Abs (x, T, b)) = Abs (x, T, abs_body (lev + 1) y b)
    96       | abs_body lev y (t $ u) = abs_body lev y t $ abs_body lev y u
    97       | abs_body lev y (t as Free (x, T)) =
    98           if x = y then TypeInfer.constrain (uniform_typing x) (TypeInfer.constrain T (Bound lev))
    99           else t
   100       | abs_body _ _ a = a;
   101     fun close (y, U) B =
   102       let val B' = abs_body 0 y (Term.incr_boundvars 1 B)
   103       in if Term.loose_bvar1 (B', 0) then Term.all dummyT $ Abs (y, U, B') else B end;
   104     fun close_form A =
   105       let
   106         val occ_frees = rev (fold_aterms add_free A []);
   107         val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees);
   108       in fold_rev close bounds A end;
   109   in map close_form As end;
   110 
   111 fun prep_spec prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
   112   let
   113     val thy = ProofContext.theory_of ctxt;
   114 
   115     val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
   116     val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
   117 
   118     val Asss = (map o map) (map (parse_prop params_ctxt) o snd) raw_specss;
   119     val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
   120       |> fold Name.declare xs;
   121     val Asss' = #1 ((fold_map o fold_map o fold_map) Term.free_dummy_patterns Asss names);
   122     val idx = (fold o fold o fold) Term.maxidx_term Asss' ~1 + 1;
   123     val specs =
   124       (if do_close then
   125         #1 (fold_map
   126             (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
   127       else Asss')
   128       |> flat |> burrow (Syntax.check_props params_ctxt);
   129     val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
   130 
   131     val vs = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
   132     val params = vs ~~ map #3 vars;
   133     val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
   134   in ((params, name_atts ~~ specs), specs_ctxt) end;
   135 
   136 fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src x;
   137 fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) x;
   138 
   139 in
   140 
   141 fun read_specification x = read_spec true x;
   142 fun check_specification x = check_spec true x;
   143 fun read_free_specification vars specs = read_spec false vars [specs];
   144 fun check_free_specification vars specs = check_spec false vars [specs];
   145 
   146 end;
   147 
   148 
   149 (* axiomatization *)
   150 
   151 fun gen_axioms prep raw_vars raw_specs lthy =
   152   let
   153     val (vars, specs) = fst (prep raw_vars [raw_specs] lthy);
   154     val cs = map fst vars;
   155     val spec_frees = member (op =) ((fold (fold Term.add_frees o snd) specs []));
   156 
   157     val ((constants, axioms), lthy') = lthy
   158       |> LocalTheory.consts spec_frees vars
   159       ||>> LocalTheory.axioms Thm.axiomK specs;
   160     val consts = map #1 constants;
   161     val consts' = map (Morphism.term (LocalTheory.target_morphism lthy')) consts;
   162 
   163     (* FIXME generic target!? *)
   164     val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) constants;
   165     val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
   166 
   167     val _ = print_consts lthy' (member (op =) (fold Term.add_frees consts' [])) cs;
   168   in ((consts, axioms), lthy'') end;
   169 
   170 val axiomatization = gen_axioms check_specification;
   171 val axiomatization_cmd = gen_axioms read_specification;
   172 
   173 
   174 (* definition *)
   175 
   176 fun gen_def prep (raw_var, (raw_a, raw_prop)) lthy =
   177   let
   178     val (vars, [((raw_name, atts), [prop])]) =
   179       fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy);
   180     val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop;
   181     val name = Thm.def_name_optional x raw_name;
   182     val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
   183       if x = x' then mx
   184       else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
   185     val ((lhs, (_, th)), lthy2) = lthy
   186       |> LocalTheory.def Thm.definitionK ((x, mx), ((name ^ "_raw", []), rhs));
   187     val ((b, [th']), lthy3) = lthy2
   188       |> LocalTheory.note Thm.definitionK
   189           ((name, Code.add_default_func_attr :: atts), [prove lthy2 th]);
   190 
   191     val lhs' = Morphism.term (LocalTheory.target_morphism lthy3) lhs;
   192     val _ = print_consts lthy3 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
   193   in ((lhs, (b, th')), lthy3) end;
   194 
   195 val definition = gen_def check_free_specification;
   196 val definition_cmd = gen_def read_free_specification;
   197 
   198 
   199 (* abbreviation *)
   200 
   201 fun gen_abbrev prep mode (raw_var, raw_prop) lthy =
   202   let
   203     val ((vars, [(_, [prop])]), _) =
   204       prep (the_list raw_var) [(("", []), [raw_prop])]
   205         (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
   206     val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop));
   207     val mx = (case vars of [] => NoSyn | [((y, _), mx)] =>
   208       if x = y then mx
   209       else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y));
   210     val lthy' = lthy
   211       |> ProofContext.set_syntax_mode mode    (* FIXME !? *)
   212       |> LocalTheory.abbrev mode ((x, mx), rhs) |> snd
   213       |> ProofContext.restore_syntax_mode lthy;
   214 
   215     val _ = print_consts lthy' (K false) [(x, T)]
   216   in lthy' end;
   217 
   218 val abbreviation = gen_abbrev check_free_specification;
   219 val abbreviation_cmd = gen_abbrev read_free_specification;
   220 
   221 
   222 (* notation *)
   223 
   224 fun gen_notation prep_const add mode args lthy =
   225   lthy |> LocalTheory.notation add mode (map (apfst (prep_const lthy)) args);
   226 
   227 val notation = gen_notation (K I);
   228 val notation_cmd = gen_notation ProofContext.read_const;
   229 
   230 
   231 (* fact statements *)
   232 
   233 fun gen_theorems prep_thms prep_att kind raw_facts lthy =
   234   let
   235     val attrib = prep_att (ProofContext.theory_of lthy);
   236     val facts = raw_facts |> map (fn ((name, atts), bs) =>
   237       ((name, map attrib atts),
   238         bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts))));
   239     val (res, lthy') = lthy |> LocalTheory.notes kind facts;
   240     val _ = present_results' lthy' kind res;
   241   in (res, lthy') end;
   242 
   243 val theorems = gen_theorems (K I) (K I);
   244 val theorems_cmd = gen_theorems ProofContext.get_thms Attrib.intern_src;
   245 
   246 
   247 (* complex goal statements *)
   248 
   249 local
   250 
   251 fun subtract_prems ctxt1 ctxt2 =
   252   Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2);
   253 
   254 fun prep_statement prep_att prep_stmt elems concl ctxt =
   255   (case concl of
   256     Element.Shows shows =>
   257       let
   258         val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt;
   259         val prems = subtract_prems loc_ctxt elems_ctxt;
   260         val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
   261         val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
   262       in ((prems, stmt, []), goal_ctxt) end
   263   | Element.Obtains obtains =>
   264       let
   265         val case_names = obtains |> map_index
   266           (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
   267         val constraints = obtains |> map (fn (_, (vars, _)) =>
   268           Locale.Elem (Element.Constrains
   269             (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
   270 
   271         val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
   272         val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
   273 
   274         val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
   275 
   276         fun assume_case ((name, (vars, _)), asms) ctxt' =
   277           let
   278             val xs = map fst vars;
   279             val props = map fst asms;
   280             val (parms, _) = ctxt'
   281               |> fold Variable.declare_term props
   282               |> fold_map ProofContext.inferred_param xs;
   283             val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
   284           in
   285             ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
   286             ctxt' |> Variable.auto_fixes asm
   287             |> ProofContext.add_assms_i Assumption.assume_export
   288               [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
   289             |>> (fn [(_, [th])] => th)
   290           end;
   291 
   292         val atts = map (Attrib.internal o K)
   293           [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
   294         val prems = subtract_prems loc_ctxt elems_ctxt;
   295         val stmt = [(("", atts), [(thesis, [])])];
   296 
   297         val (facts, goal_ctxt) = elems_ctxt
   298           |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
   299           |> fold_map assume_case (obtains ~~ propp)
   300           |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
   301                 [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
   302       in ((prems, stmt, facts), goal_ctxt) end);
   303 
   304 structure TheoremHook = GenericDataFun
   305 (
   306   type T = ((bool -> Proof.state -> Proof.state) * stamp) list;
   307   val empty = [];
   308   val extend = I;
   309   fun merge _ hooks : T = Library.merge (eq_snd op =) hooks;
   310 );
   311 
   312 fun gen_theorem prep_att prep_stmt
   313     kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy0 =
   314   let
   315     val _ = LocalTheory.affirm lthy0;
   316     val thy = ProofContext.theory_of lthy0;
   317 
   318     val (loc, ctxt, lthy) =
   319       (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
   320         (SOME loc, true) => (* workaround non-modularity of in/includes *)  (* FIXME *)
   321           (SOME loc, ProofContext.init thy, LocalTheory.restore lthy0)
   322       | _ => (NONE, lthy0, lthy0));
   323 
   324     val attrib = prep_att thy;
   325     val atts = map attrib raw_atts;
   326 
   327     val elems = raw_elems |> map (Locale.map_elem (Element.map_ctxt_attrib attrib));
   328     val ((prems, stmt, facts), goal_ctxt) =
   329       prep_statement attrib (prep_stmt loc) elems raw_concl ctxt;
   330 
   331     fun after_qed' results goal_ctxt' =
   332       let val results' =
   333         burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
   334       in
   335         lthy
   336         |> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
   337         |> (fn (res, lthy') =>
   338           (present_results lthy' ((kind, name), res);
   339             if name = "" andalso null atts then lthy'
   340             else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy')))
   341         |> after_qed results'
   342       end;
   343   in
   344     goal_ctxt
   345     |> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])]
   346     |> snd
   347     |> Proof.theorem_i before_qed after_qed' (map snd stmt)
   348     |> Proof.refine_insert facts
   349     |> Library.apply (map (fn (f, _) => f int) (rev (TheoremHook.get (Context.Proof goal_ctxt))))
   350   end;
   351 
   352 in
   353 
   354 val theorem = gen_theorem (K I) Locale.cert_context_statement;
   355 val theorem_cmd = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
   356 
   357 fun add_theorem_hook f = TheoremHook.map (cons (f, stamp ()));
   358 
   359 end;
   360 
   361 end;