src/Pure/Isar/generic_target.ML
author wenzelm
Tue, 03 Apr 2012 10:59:20 +0200
changeset 48170 392c4cd97e5c
parent 48169 ca4cf5de366c
child 48172 b79bf8288b29
permissions -rw-r--r--
more uniform theory_abbrev with const_declaration;
     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 
    64 (* define *)
    65 
    66 fun define foundation internal ((b, mx), ((b_def, atts), rhs)) lthy =
    67   let
    68     val thy = Proof_Context.theory_of lthy;
    69     val thy_ctxt = Proof_Context.init_global thy;
    70 
    71     (*term and type parameters*)
    72     val ((defs, _), rhs') = Thm.cterm_of thy rhs
    73       |> Local_Defs.export_cterm lthy thy_ctxt ||> Thm.term_of;
    74 
    75     val xs = Variable.add_fixed lthy rhs' [];
    76     val T = Term.fastype_of rhs;
    77     val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
    78     val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
    79     val mx' = check_mixfix lthy (b, extra_tfrees) mx;
    80 
    81     val type_params = map (Logic.mk_type o TFree) extra_tfrees;
    82     val term_params = map Free (sort (Variable.fixed_ord lthy o pairself #1) xs);
    83     val params = type_params @ term_params;
    84 
    85     val U = map Term.fastype_of params ---> T;
    86 
    87     (*foundation*)
    88     val ((lhs', global_def), lthy2) = lthy
    89       |> foundation (((b, U), mx'), (b_def, rhs')) (type_params, term_params);
    90 
    91     (*local definition*)
    92     val ((lhs, local_def), lthy3) = lthy2
    93       |> Local_Defs.add_def ((b, NoSyn), lhs');
    94 
    95     (*result*)
    96     val def =
    97       Thm.transitive local_def global_def
    98       |> Local_Defs.contract defs
    99           (Thm.cterm_of (Proof_Context.theory_of lthy3) (Logic.mk_equals (lhs, rhs)));
   100     val ([(res_name, [res])], lthy4) = lthy3
   101       |> Local_Theory.notes [((if internal then Binding.empty else b_def, atts), [([def], [])])];
   102   in ((lhs, (res_name, res)), lthy4) end;
   103 
   104 
   105 (* notes *)
   106 
   107 local
   108 
   109 fun import_export_proof ctxt (name, raw_th) =
   110   let
   111     val thy = Proof_Context.theory_of ctxt;
   112     val thy_ctxt = Proof_Context.init_global thy;
   113     val certT = Thm.ctyp_of thy;
   114     val cert = Thm.cterm_of thy;
   115 
   116     (*export assumes/defines*)
   117     val th = Goal.norm_result raw_th;
   118     val ((defs, asms), th') = Local_Defs.export ctxt thy_ctxt th;
   119     val asms' = map (Raw_Simplifier.rewrite_rule defs) asms;
   120 
   121     (*export fixes*)
   122     val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
   123     val frees = map Free (Thm.fold_terms Term.add_frees th' []);
   124     val (th'' :: vs) =
   125       (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
   126       |> Variable.export ctxt thy_ctxt
   127       |> Drule.zero_var_indexes_list;
   128 
   129     (*thm definition*)
   130     val result = Global_Theory.name_thm true true name th'';
   131 
   132     (*import fixes*)
   133     val (tvars, vars) =
   134       chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
   135       |>> map Logic.dest_type;
   136 
   137     val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
   138     val inst = filter (is_Var o fst) (vars ~~ frees);
   139     val cinstT = map (pairself certT o apfst TVar) instT;
   140     val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
   141     val result' = Thm.instantiate (cinstT, cinst) result;
   142 
   143     (*import assumes/defines*)
   144     val result'' =
   145       (fold (curry op COMP) asms' result'
   146         handle THM _ => raise THM ("Failed to re-import result", 0, result' :: asms'))
   147       |> Local_Defs.contract defs (Thm.cprop_of th)
   148       |> Goal.norm_result
   149       |> Global_Theory.name_thm false false name;
   150 
   151   in (result'', result) end;
   152 
   153 fun standard_facts lthy ctxt =
   154   Element.transform_facts (Local_Theory.standard_morphism lthy ctxt);
   155 
   156 in
   157 
   158 fun notes target_notes kind facts lthy =
   159   let
   160     val facts' = facts
   161       |> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi
   162           (Local_Theory.full_name lthy (fst a))) bs))
   163       |> Global_Theory.map_facts (import_export_proof lthy);
   164     val local_facts = Global_Theory.map_facts #1 facts';
   165     val global_facts = Global_Theory.map_facts #2 facts';
   166   in
   167     lthy
   168     |> target_notes kind global_facts (Attrib.partial_evaluation lthy local_facts)
   169     |> Attrib.local_notes kind local_facts
   170   end;
   171 
   172 fun locale_notes locale kind global_facts local_facts =
   173   Local_Theory.background_theory
   174     (Attrib.global_notes kind (Attrib.map_facts (K []) global_facts) #> snd) #>
   175   (fn lthy => lthy |>
   176     Local_Theory.target (fn ctxt => ctxt |>
   177       Locale.add_thmss locale kind (standard_facts lthy ctxt local_facts))) #>
   178   (fn lthy => lthy |>
   179     Local_Theory.map_contexts (fn level => fn ctxt =>
   180       if level = 0 orelse level = Local_Theory.level lthy then ctxt
   181       else ctxt |> Attrib.local_notes kind (standard_facts lthy ctxt local_facts) |> snd));
   182 
   183 end;
   184 
   185 
   186 (* abbrev *)
   187 
   188 fun abbrev target_abbrev prmode ((b, mx), t) lthy =
   189   let
   190     val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
   191     val target_ctxt = Local_Theory.target_of lthy;
   192 
   193     val t' = Assumption.export_term lthy target_ctxt t;
   194     val xs = map Free (rev (Variable.add_fixed lthy t' []));
   195     val u = fold_rev lambda xs t';
   196 
   197     val extra_tfrees =
   198       subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
   199     val mx' = check_mixfix lthy (b, extra_tfrees) mx;
   200 
   201     val global_rhs =
   202       singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
   203   in
   204     lthy
   205     |> target_abbrev prmode (b, mx') (global_rhs, t') xs
   206     |> Proof_Context.add_abbrev Print_Mode.internal (b, t) |> snd
   207     |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
   208   end;
   209 
   210 
   211 (* declaration *)
   212 
   213 fun background_declaration decl lthy =
   214   let
   215     val theory_decl =
   216       Local_Theory.standard_form lthy
   217         (Proof_Context.init_global (Proof_Context.theory_of lthy)) decl;
   218   in Local_Theory.background_theory (Context.theory_map theory_decl) lthy end;
   219 
   220 fun locale_declaration locale syntax decl lthy = lthy
   221   |> Local_Theory.target (fn ctxt => ctxt |>
   222     Locale.add_declaration locale syntax
   223       (Morphism.transform (Local_Theory.standard_morphism lthy ctxt) decl));
   224 
   225 fun standard_declaration pred decl lthy =
   226   Local_Theory.map_contexts (fn level => fn ctxt =>
   227     if pred level then Context.proof_map (Local_Theory.standard_form lthy ctxt decl) ctxt
   228     else ctxt) lthy;
   229 
   230 
   231 (* const declaration *)
   232 
   233 fun generic_const same_shape prmode ((b, mx), t) context =
   234   let
   235     val const_alias =
   236       if same_shape then
   237         (case t of
   238           Const (c, T) =>
   239             let
   240               val thy = Context.theory_of context;
   241               val ctxt = Context.proof_of context;
   242               val t' = Syntax.check_term ctxt (Const (c, dummyT))
   243                 |> singleton (Variable.polymorphic ctxt);
   244             in
   245               (case t' of
   246                 Const (c', T') =>
   247                   if c = c' andalso Sign.typ_equiv thy (T, T') then SOME c else NONE
   248               | _ => NONE)
   249             end
   250         | _ => NONE)
   251       else NONE;
   252   in
   253     (case const_alias of
   254       SOME c =>
   255         context
   256         |> Context.mapping (Sign.const_alias b c) (Proof_Context.const_alias b c)
   257         |> Morphism.form (Proof_Context.generic_notation true prmode [(t, mx)])
   258     | NONE =>
   259         context
   260         |> Proof_Context.generic_add_abbrev Print_Mode.internal (b, t)
   261         |-> (fn (const as Const (c, _), _) => same_shape ?
   262               (Proof_Context.generic_revert_abbrev (#1 prmode) c #>
   263                Morphism.form (Proof_Context.generic_notation true prmode [(const, mx)]))))
   264   end;
   265 
   266 fun const_declaration pred prmode ((b, mx), rhs) =
   267   standard_declaration pred (fn phi =>
   268     let
   269       val b' = Morphism.binding phi b;
   270       val rhs' = Morphism.term phi rhs;
   271       val same_shape = Term.aconv_untyped (rhs, rhs');
   272     in generic_const same_shape prmode ((b', mx), Term.close_schematic_term rhs') end);
   273 
   274 
   275 
   276 (** primitive theory operations **)
   277 
   278 fun background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
   279   let
   280     val (const, lthy2) = lthy
   281       |> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx));
   282     val lhs = list_comb (const, type_params @ term_params);
   283     val ((_, def), lthy3) = lthy2
   284       |> Local_Theory.background_theory_result
   285         (Thm.add_def lthy2 false false
   286           (Thm.def_binding_optional b b_def, Logic.mk_equals (lhs, rhs)));
   287   in ((lhs, def), lthy3) end;
   288 
   289 fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
   290   background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params)
   291   #-> (fn (lhs, def) => fn lthy' => lthy' |>
   292         const_declaration (fn level => level <> Local_Theory.level lthy')
   293           Syntax.mode_default ((b, mx), lhs)
   294     |> pair (lhs, def));
   295 
   296 fun theory_notes kind global_facts local_facts =
   297   Local_Theory.background_theory (Attrib.global_notes kind global_facts #> snd) #>
   298   (fn lthy => lthy |> Local_Theory.map_contexts (fn level => fn ctxt =>
   299     if level = Local_Theory.level lthy then ctxt
   300     else
   301       ctxt |> Attrib.local_notes kind
   302         (Element.transform_facts (Local_Theory.standard_morphism lthy ctxt) local_facts) |> snd));
   303 
   304 fun theory_abbrev prmode (b, mx) (t, _) xs =
   305   Local_Theory.background_theory_result
   306     (Sign.add_abbrev (#1 prmode) (b, t) #->
   307       (fn (lhs, _) => Sign.notation true prmode [(lhs, mx)] #> pair lhs))
   308   #-> (fn lhs => fn lthy' => lthy' |>
   309         const_declaration (fn level => level <> Local_Theory.level lthy') prmode
   310           ((b, mx), Term.list_comb (Logic.unvarify_global lhs, xs)));
   311 
   312 fun theory_declaration decl =
   313   background_declaration decl #> standard_declaration (K true) decl;
   314 
   315 end;