src/Pure/Isar/generic_target.ML
author wenzelm
Sat, 29 Oct 2011 12:57:43 +0200
changeset 46169 aa35859c8741
parent 46164 57def0b39696
child 46181 adaf2184b79d
permissions -rw-r--r--
uniform treatment of syntax declaration wrt. aux. context (NB: notation avoids duplicate mixfix internally);
     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     (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    13     (term * (string * thm)) * local_theory
    14   val notes: (string -> (Attrib.binding * (thm list * Args.src list) list) list ->
    15       (Attrib.binding * (thm list * Args.src list) list) list -> local_theory -> local_theory) ->
    16     string -> (Attrib.binding * (thm list * Args.src list) list) list -> local_theory ->
    17     (string * thm list) list * local_theory
    18   val abbrev: (string * bool -> binding * mixfix -> term * term ->
    19       term list -> local_theory -> local_theory) ->
    20     string * bool -> (binding * mixfix) * term -> local_theory ->
    21     (term * term) * local_theory
    22 
    23   val theory_declaration: bool -> declaration -> local_theory -> local_theory
    24   val theory_foundation: ((binding * typ) * mixfix) * (binding * term) ->
    25     term list * term list -> local_theory -> (term * thm) * local_theory
    26   val theory_notes: string -> (Attrib.binding * (thm list * Args.src list) list) list ->
    27     local_theory -> local_theory
    28   val theory_abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory -> local_theory
    29 end;
    30 
    31 structure Generic_Target: GENERIC_TARGET =
    32 struct
    33 
    34 (** lifting primitive to target operations **)
    35 
    36 (* mixfix syntax *)
    37 
    38 fun check_mixfix ctxt (b, extra_tfrees) mx =
    39   if null extra_tfrees then mx
    40   else
    41     (Context_Position.if_visible ctxt warning
    42       ("Additional type variable(s) in specification of " ^ Binding.print b ^ ": " ^
    43         commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^
    44         (if mx = NoSyn then ""
    45          else "\nDropping mixfix syntax " ^ Pretty.string_of (Mixfix.pretty_mixfix mx)));
    46       NoSyn);
    47 
    48 
    49 (* define *)
    50 
    51 fun define foundation ((b, mx), ((proto_b_def, atts), rhs)) lthy =
    52   let
    53     val thy = Proof_Context.theory_of lthy;
    54     val thy_ctxt = Proof_Context.init_global thy;
    55 
    56     val b_def = Thm.def_binding_optional b proto_b_def;
    57 
    58     (*term and type parameters*)
    59     val crhs = Thm.cterm_of thy rhs;
    60     val (defs, rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
    61     val rhs_conv = Raw_Simplifier.rewrite true defs crhs;
    62 
    63     val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
    64     val T = Term.fastype_of rhs;
    65     val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
    66     val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
    67     val mx' = check_mixfix lthy (b, extra_tfrees) mx;
    68 
    69     val type_params = map (Logic.mk_type o TFree) extra_tfrees;
    70     val target_ctxt = Local_Theory.target_of lthy;
    71     val term_params =
    72       filter (Variable.is_fixed target_ctxt o #1) xs
    73       |> sort (Variable.fixed_ord target_ctxt o pairself #1)
    74       |> map Free;
    75     val params = type_params @ term_params;
    76 
    77     val U = map Term.fastype_of params ---> T;
    78 
    79     (*foundation*)
    80     val ((lhs', global_def), lthy2) = foundation
    81       (((b, U), mx'), (b_def, rhs')) (type_params, term_params) lthy;
    82 
    83     (*local definition*)
    84     val ((lhs, local_def), lthy3) = lthy2
    85       |> Local_Defs.add_def ((b, NoSyn), lhs');
    86     val def = Local_Defs.trans_terms lthy3
    87       [(*c == global.c xs*)     local_def,
    88        (*global.c xs == rhs'*)  global_def,
    89        (*rhs' == rhs*)          Thm.symmetric rhs_conv];
    90 
    91     (*note*)
    92     val ([(res_name, [res])], lthy4) = lthy3
    93       |> Local_Theory.notes_kind "" [((b_def, atts), [([def], [])])];
    94   in ((lhs, (res_name, res)), lthy4) end;
    95 
    96 
    97 (* notes *)
    98 
    99 fun import_export_proof ctxt (name, raw_th) =
   100   let
   101     val thy = Proof_Context.theory_of ctxt;
   102     val thy_ctxt = Proof_Context.init_global thy;
   103     val certT = Thm.ctyp_of thy;
   104     val cert = Thm.cterm_of thy;
   105 
   106     (*export assumes/defines*)
   107     val th = Goal.norm_result raw_th;
   108     val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
   109     val assms = map (Raw_Simplifier.rewrite_rule defs o Thm.assume)
   110       (Assumption.all_assms_of ctxt);
   111     val nprems = Thm.nprems_of th' - Thm.nprems_of th;
   112 
   113     (*export fixes*)
   114     val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
   115     val frees = map Free (Thm.fold_terms Term.add_frees th' []);
   116     val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
   117       |> Variable.export ctxt thy_ctxt
   118       |> Drule.zero_var_indexes_list;
   119 
   120     (*thm definition*)
   121     val result = Global_Theory.name_thm true true name (Thm.compress th'');
   122 
   123     (*import fixes*)
   124     val (tvars, vars) =
   125       chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
   126       |>> map Logic.dest_type;
   127 
   128     val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
   129     val inst = filter (is_Var o fst) (vars ~~ frees);
   130     val cinstT = map (pairself certT o apfst TVar) instT;
   131     val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
   132     val result' = Thm.instantiate (cinstT, cinst) result;
   133 
   134     (*import assumes/defines*)
   135     val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
   136     val result'' =
   137       (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
   138         NONE => raise THM ("Failed to re-import result", 0, [result'])
   139       | SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
   140       |> Goal.norm_result
   141       |> Global_Theory.name_thm false false name;
   142 
   143   in (result'', result) end;
   144 
   145 fun notes target_notes kind facts lthy =
   146   let
   147     val thy = Proof_Context.theory_of lthy;
   148     val facts' = facts
   149       |> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi
   150           (Local_Theory.full_name lthy (fst a))) bs))
   151       |> Global_Theory.map_facts (import_export_proof lthy);
   152     val local_facts = Global_Theory.map_facts #1 facts';
   153     val global_facts = Global_Theory.map_facts #2 facts';
   154   in
   155     lthy
   156     |> target_notes kind global_facts local_facts
   157     |> Proof_Context.note_thmss kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts)
   158   end;
   159 
   160 
   161 (* abbrev *)
   162 
   163 fun abbrev target_abbrev prmode ((b, mx), t) lthy =
   164   let
   165     val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
   166     val target_ctxt = Local_Theory.target_of lthy;
   167 
   168     val t' = Assumption.export_term lthy target_ctxt t;
   169     val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
   170     val u = fold_rev lambda xs t';
   171 
   172     val extra_tfrees =
   173       subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
   174     val mx' = check_mixfix lthy (b, extra_tfrees) mx;
   175 
   176     val global_rhs =
   177       singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
   178   in
   179     lthy
   180     |> target_abbrev prmode (b, mx') (global_rhs, t') xs
   181     |> Proof_Context.add_abbrev Print_Mode.internal (b, t) |> snd
   182     |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
   183   end;
   184 
   185 
   186 (** primitive theory operations **)
   187 
   188 fun theory_declaration syntax decl lthy =
   189   let
   190     val global_decl = Morphism.form
   191       (Morphism.transform (Local_Theory.global_morphism lthy) decl);
   192   in
   193     lthy
   194     |> Local_Theory.background_theory (Context.theory_map global_decl)
   195     |> Local_Theory.target (Context.proof_map global_decl)
   196     |> Context.proof_map (Morphism.form decl)
   197   end;
   198 
   199 fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
   200   let
   201     val (const, lthy2) = lthy
   202       |> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx));
   203     val lhs = list_comb (const, type_params @ term_params);
   204     val ((_, def), lthy3) = lthy2
   205       |> Local_Theory.background_theory_result
   206         (Thm.add_def lthy2 false false (b_def, Logic.mk_equals (lhs, rhs)));
   207   in ((lhs, def), lthy3) end;
   208 
   209 fun theory_notes kind global_facts lthy =
   210   let
   211     val thy = Proof_Context.theory_of lthy;
   212     val global_facts' = Attrib.map_facts (Attrib.attribute_i thy) global_facts;
   213   in
   214     lthy
   215     |> Local_Theory.background_theory (Global_Theory.note_thmss kind global_facts' #> snd)
   216     |> Local_Theory.target (Proof_Context.note_thmss kind global_facts' #> snd)
   217   end;
   218 
   219 fun theory_abbrev prmode ((b, mx), t) =
   220   Local_Theory.background_theory
   221     (Sign.add_abbrev (#1 prmode) (b, t) #->
   222       (fn (lhs, _) => Sign.notation true prmode [(lhs, mx)]));
   223 
   224 end;