src/Pure/Isar/generic_target.ML
author wenzelm
Fri Sep 07 15:28:48 2012 +0200 (2012-09-07)
changeset 49198 38af9102ee75
parent 47313 6a0ee401b899
child 52153 f5773a46cf05
permissions -rw-r--r--
merged
     1 (*  Title:      Pure/Isar/generic_target.ML
     2     Author:     Makarius
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     5 Common target infrastructure.
     6 *)
     7 
     8 signature GENERIC_TARGET =
     9 sig
    10   val define: (((binding * typ) * mixfix) * (binding * term) ->
    11       term list * term list -> local_theory -> (term * thm) * local_theory) ->
    12     bool -> (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    13     (term * (string * thm)) * local_theory
    14   val notes:
    15     (string -> (Attrib.binding * (thm list * Args.src list) list) list ->
    16       (Attrib.binding * (thm list * Args.src list) list) list -> local_theory -> local_theory) ->
    17     string -> (Attrib.binding * (thm list * Args.src list) list) list -> local_theory ->
    18     (string * thm list) list * local_theory
    19   val locale_notes: string -> string ->
    20     (Attrib.binding * (thm list * Args.src list) list) list ->
    21     (Attrib.binding * (thm list * Args.src list) list) list ->
    22     local_theory -> local_theory
    23   val abbrev: (string * bool -> binding * mixfix -> term * term ->
    24       term list -> local_theory -> local_theory) ->
    25     string * bool -> (binding * mixfix) * term -> local_theory -> (term * term) * local_theory
    26   val background_declaration: declaration -> local_theory -> local_theory
    27   val locale_declaration: string -> bool -> declaration -> local_theory -> local_theory
    28   val standard_declaration: (int -> bool) -> declaration -> local_theory -> local_theory
    29   val generic_const: bool -> Syntax.mode -> (binding * mixfix) * term ->
    30     Context.generic -> Context.generic
    31   val const_declaration: (int -> bool) -> Syntax.mode -> (binding * mixfix) * term ->
    32     local_theory -> local_theory
    33   val background_foundation: ((binding * typ) * mixfix) * (binding * term) ->
    34     term list * term list -> local_theory -> (term * thm) * local_theory
    35   val theory_foundation: ((binding * typ) * mixfix) * (binding * term) ->
    36     term list * term list -> local_theory -> (term * thm) * local_theory
    37   val theory_notes: string ->
    38     (Attrib.binding * (thm list * Args.src list) list) list ->
    39     (Attrib.binding * (thm list * Args.src list) list) list ->
    40     local_theory -> local_theory
    41   val theory_abbrev: Syntax.mode -> (binding * mixfix) -> term * term -> term list ->
    42     local_theory -> local_theory
    43   val theory_declaration: declaration -> local_theory -> local_theory
    44 end
    45 
    46 structure Generic_Target: GENERIC_TARGET =
    47 struct
    48 
    49 (** lifting primitive to target operations **)
    50 
    51 (* mixfix syntax *)
    52 
    53 fun check_mixfix ctxt (b, extra_tfrees) mx =
    54   if null extra_tfrees then mx
    55   else
    56     (Context_Position.if_visible ctxt warning
    57       ("Additional type variable(s) in specification of " ^ Binding.print b ^ ": " ^
    58         commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^
    59         (if mx = NoSyn then ""
    60          else "\nDropping mixfix syntax " ^ Pretty.string_of (Mixfix.pretty_mixfix mx)));
    61       NoSyn);
    62 
    63 fun check_mixfix_global (b, no_params) mx =
    64   if no_params orelse mx = NoSyn then mx
    65   else (warning ("Dropping global mixfix syntax: " ^ Binding.print b ^ " " ^
    66     Pretty.string_of (Mixfix.pretty_mixfix mx)); NoSyn);
    67 
    68 
    69 (* define *)
    70 
    71 fun define foundation internal ((b, mx), ((b_def, atts), rhs)) lthy =
    72   let
    73     val thy = Proof_Context.theory_of lthy;
    74     val thy_ctxt = Proof_Context.init_global thy;
    75 
    76     (*term and type parameters*)
    77     val ((defs, _), rhs') = Thm.cterm_of thy rhs
    78       |> Local_Defs.export_cterm lthy thy_ctxt ||> Thm.term_of;
    79 
    80     val xs = Variable.add_fixed lthy rhs' [];
    81     val T = Term.fastype_of rhs;
    82     val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
    83     val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
    84     val mx' = check_mixfix lthy (b, extra_tfrees) mx;
    85 
    86     val type_params = map (Logic.mk_type o TFree) extra_tfrees;
    87     val term_params = map Free (sort (Variable.fixed_ord lthy o pairself #1) xs);
    88     val params = type_params @ term_params;
    89 
    90     val U = map Term.fastype_of params ---> T;
    91 
    92     (*foundation*)
    93     val ((lhs', global_def), lthy2) = lthy
    94       |> foundation (((b, U), mx'), (b_def, rhs')) (type_params, term_params);
    95 
    96     (*local definition*)
    97     val ((lhs, local_def), lthy3) = lthy2
    98       |> Local_Defs.add_def ((b, NoSyn), lhs');
    99 
   100     (*result*)
   101     val def =
   102       Thm.transitive local_def global_def
   103       |> Local_Defs.contract defs
   104           (Thm.cterm_of (Proof_Context.theory_of lthy3) (Logic.mk_equals (lhs, rhs)));
   105     val ([(res_name, [res])], lthy4) = lthy3
   106       |> Local_Theory.notes [((if internal then Binding.empty else b_def, atts), [([def], [])])];
   107   in ((lhs, (res_name, res)), lthy4) end;
   108 
   109 
   110 (* notes *)
   111 
   112 local
   113 
   114 fun import_export_proof ctxt (name, raw_th) =
   115   let
   116     val thy = Proof_Context.theory_of ctxt;
   117     val thy_ctxt = Proof_Context.init_global thy;
   118     val certT = Thm.ctyp_of thy;
   119     val cert = Thm.cterm_of thy;
   120 
   121     (*export assumes/defines*)
   122     val th = Goal.norm_result raw_th;
   123     val ((defs, asms), th') = Local_Defs.export ctxt thy_ctxt th;
   124     val asms' = map (Raw_Simplifier.rewrite_rule (Drule.norm_hhf_eqs @ defs)) asms;
   125 
   126     (*export fixes*)
   127     val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
   128     val frees = map Free (Thm.fold_terms Term.add_frees th' []);
   129     val (th'' :: vs) =
   130       (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
   131       |> Variable.export ctxt thy_ctxt
   132       |> Drule.zero_var_indexes_list;
   133 
   134     (*thm definition*)
   135     val result = Global_Theory.name_thm true true name th'';
   136 
   137     (*import fixes*)
   138     val (tvars, vars) =
   139       chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
   140       |>> map Logic.dest_type;
   141 
   142     val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
   143     val inst = filter (is_Var o fst) (vars ~~ frees);
   144     val cinstT = map (pairself certT o apfst TVar) instT;
   145     val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
   146     val result' = Thm.instantiate (cinstT, cinst) result;
   147 
   148     (*import assumes/defines*)
   149     val result'' =
   150       (fold (curry op COMP) asms' result'
   151         handle THM _ => raise THM ("Failed to re-import result", 0, result' :: asms'))
   152       |> Local_Defs.contract defs (Thm.cprop_of th)
   153       |> Goal.norm_result
   154       |> Global_Theory.name_thm false false name;
   155 
   156   in (result'', result) end;
   157 
   158 fun standard_facts lthy ctxt =
   159   Element.transform_facts (Local_Theory.standard_morphism lthy ctxt);
   160 
   161 in
   162 
   163 fun notes target_notes kind facts lthy =
   164   let
   165     val facts' = facts
   166       |> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi
   167           (Local_Theory.full_name lthy (fst a))) bs))
   168       |> Global_Theory.map_facts (import_export_proof lthy);
   169     val local_facts = Global_Theory.map_facts #1 facts';
   170     val global_facts = Global_Theory.map_facts #2 facts';
   171   in
   172     lthy
   173     |> target_notes kind global_facts (Attrib.partial_evaluation lthy local_facts)
   174     |> Attrib.local_notes kind local_facts
   175   end;
   176 
   177 fun locale_notes locale kind global_facts local_facts =
   178   Local_Theory.background_theory
   179     (Attrib.global_notes kind (Attrib.map_facts (K []) global_facts) #> snd) #>
   180   (fn lthy => lthy |>
   181     Local_Theory.target (fn ctxt => ctxt |>
   182       Locale.add_thmss locale kind (standard_facts lthy ctxt local_facts))) #>
   183   (fn lthy => lthy |>
   184     Local_Theory.map_contexts (fn level => fn ctxt =>
   185       if level = 0 orelse level = Local_Theory.level lthy then ctxt
   186       else ctxt |> Attrib.local_notes kind (standard_facts lthy ctxt local_facts) |> snd));
   187 
   188 end;
   189 
   190 
   191 (* abbrev *)
   192 
   193 fun abbrev target_abbrev prmode ((b, mx), t) lthy =
   194   let
   195     val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
   196 
   197     val t' = Assumption.export_term lthy (Local_Theory.target_of lthy) t;
   198     val xs = map Free (sort (Variable.fixed_ord lthy o pairself #1) (Variable.add_fixed lthy t' []));
   199     val u = fold_rev lambda xs t';
   200     val global_rhs = singleton (Variable.polymorphic thy_ctxt) u;
   201 
   202     val extra_tfrees =
   203       subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
   204     val mx' = check_mixfix lthy (b, extra_tfrees) mx;
   205   in
   206     lthy
   207     |> target_abbrev prmode (b, mx') (global_rhs, t') xs
   208     |> Proof_Context.add_abbrev Print_Mode.internal (b, t) |> snd
   209     |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
   210   end;
   211 
   212 
   213 (* declaration *)
   214 
   215 fun background_declaration decl lthy =
   216   let
   217     val theory_decl =
   218       Local_Theory.standard_form lthy
   219         (Proof_Context.init_global (Proof_Context.theory_of lthy)) decl;
   220   in Local_Theory.background_theory (Context.theory_map theory_decl) lthy end;
   221 
   222 fun locale_declaration locale syntax decl lthy = lthy
   223   |> Local_Theory.target (fn ctxt => ctxt |>
   224     Locale.add_declaration locale syntax
   225       (Morphism.transform (Local_Theory.standard_morphism lthy ctxt) decl));
   226 
   227 fun standard_declaration pred decl lthy =
   228   Local_Theory.map_contexts (fn level => fn ctxt =>
   229     if pred level then Context.proof_map (Local_Theory.standard_form lthy ctxt decl) ctxt
   230     else ctxt) lthy;
   231 
   232 
   233 (* const declaration *)
   234 
   235 fun generic_const same_shape prmode ((b, mx), t) context =
   236   let
   237     val const_alias =
   238       if same_shape then
   239         (case t of
   240           Const (c, T) =>
   241             let
   242               val thy = Context.theory_of context;
   243               val ctxt = Context.proof_of context;
   244             in
   245               (case Type_Infer_Context.const_type ctxt c of
   246                 SOME T' => if Sign.typ_equiv thy (T, T') then SOME c else NONE
   247               | NONE => NONE)
   248             end
   249         | _ => NONE)
   250       else NONE;
   251   in
   252     (case const_alias of
   253       SOME c =>
   254         context
   255         |> Context.mapping (Sign.const_alias b c) (Proof_Context.const_alias b c)
   256         |> Morphism.form (Proof_Context.generic_notation true prmode [(t, mx)])
   257     | NONE =>
   258         context
   259         |> Proof_Context.generic_add_abbrev Print_Mode.internal (b, Term.close_schematic_term t)
   260         |-> (fn (const as Const (c, _), _) => same_shape ?
   261               (Proof_Context.generic_revert_abbrev (#1 prmode) c #>
   262                Morphism.form (Proof_Context.generic_notation true prmode [(const, mx)]))))
   263   end;
   264 
   265 fun const_declaration pred prmode ((b, mx), rhs) =
   266   standard_declaration pred (fn phi =>
   267     let
   268       val b' = Morphism.binding phi b;
   269       val rhs' = Morphism.term phi rhs;
   270       val same_shape = Term.aconv_untyped (rhs, rhs');
   271     in generic_const same_shape prmode ((b', mx), rhs') end);
   272 
   273 
   274 
   275 (** primitive theory operations **)
   276 
   277 fun background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
   278   let
   279     val params = type_params @ term_params;
   280     val mx' = check_mixfix_global (b, null params) mx;
   281 
   282     val (const, lthy2) = lthy
   283       |> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx'));
   284     val lhs = Term.list_comb (const, params);
   285 
   286     val ((_, def), lthy3) = lthy2
   287       |> Local_Theory.background_theory_result
   288         (Thm.add_def lthy2 false false
   289           (Thm.def_binding_optional b b_def, Logic.mk_equals (lhs, rhs)));
   290   in ((lhs, def), lthy3) end;
   291 
   292 fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
   293   background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params)
   294   #-> (fn (lhs, def) => fn lthy' => lthy' |>
   295         const_declaration (fn level => level <> Local_Theory.level lthy')
   296           Syntax.mode_default ((b, mx), lhs)
   297     |> pair (lhs, def));
   298 
   299 fun theory_notes kind global_facts local_facts =
   300   Local_Theory.background_theory (Attrib.global_notes kind global_facts #> snd) #>
   301   (fn lthy => lthy |> Local_Theory.map_contexts (fn level => fn ctxt =>
   302     if level = Local_Theory.level lthy then ctxt
   303     else
   304       ctxt |> Attrib.local_notes kind
   305         (Element.transform_facts (Local_Theory.standard_morphism lthy ctxt) local_facts) |> snd));
   306 
   307 fun theory_abbrev prmode (b, mx) (t, _) xs =
   308   Local_Theory.background_theory_result
   309     (Sign.add_abbrev (#1 prmode) (b, t) #->
   310       (fn (lhs, _) =>  (* FIXME type_params!? *)
   311         Sign.notation true prmode [(lhs, check_mixfix_global (b, null xs) mx)] #> pair lhs))
   312   #-> (fn lhs => fn lthy' => lthy' |>
   313         const_declaration (fn level => level <> Local_Theory.level lthy') prmode
   314           ((b, if null xs then NoSyn else mx), Term.list_comb (Logic.unvarify_global lhs, xs)));
   315 
   316 fun theory_declaration decl =
   317   background_declaration decl #> standard_declaration (K true) decl;
   318 
   319 end;