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