src/Pure/Isar/theory_target.ML
author wenzelm
Sat Nov 10 18:36:10 2007 +0100 (2007-11-10)
changeset 25381 c100bf5bd6b8
parent 25372 a718f1ccaf1a
child 25392 bb8d225dcf05
permissions -rw-r--r--
removed LocalTheory.target_naming/name;
     1 (*  Title:      Pure/Isar/theory_target.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Common theory/locale/class targets.
     6 *)
     7 
     8 signature THEORY_TARGET =
     9 sig
    10   val peek: local_theory -> {target: string, is_locale: bool, is_class: bool}
    11   val init: string option -> theory -> local_theory
    12   val begin: string -> Proof.context -> local_theory
    13   val context: xstring -> theory -> local_theory
    14 end;
    15 
    16 structure TheoryTarget: THEORY_TARGET =
    17 struct
    18 
    19 (* context data *)
    20 
    21 datatype target = Target of {target: string, is_locale: bool, is_class: bool};
    22 
    23 fun make_target target is_locale is_class =
    24   Target {target = target, is_locale = is_locale, is_class = is_class};
    25 
    26 val global_target = make_target "" false false;
    27 
    28 structure Data = ProofDataFun
    29 (
    30   type T = target;
    31   fun init _ = global_target;
    32 );
    33 
    34 val peek = (fn Target args => args) o Data.get;
    35 
    36 
    37 (* pretty *)
    38 
    39 fun pretty (Target {target, is_locale, is_class}) ctxt =
    40   let
    41     val thy = ProofContext.theory_of ctxt;
    42     val target_name = (if is_class then "class " else "locale ") ^ Locale.extern thy target;
    43     val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
    44     val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
    45     val elems =
    46       (if null fixes then [] else [Element.Fixes fixes]) @
    47       (if null assumes then [] else [Element.Assumes assumes]);
    48   in
    49     Pretty.block [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy)] ::
    50      (if target = "" then []
    51       else if null elems then [Pretty.str target_name]
    52       else [Pretty.big_list (target_name ^ " =")
    53         (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)])
    54   end;
    55 
    56 
    57 (* target declarations *)
    58 
    59 fun target_decl add (Target {target, ...}) d lthy =
    60   let
    61     val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
    62     val d0 = Morphism.form d';
    63   in
    64     if target = "" then
    65       lthy
    66       |> LocalTheory.theory (Context.theory_map d0)
    67       |> LocalTheory.target (Context.proof_map d0)
    68     else
    69       lthy
    70       |> LocalTheory.target (add target d')
    71   end;
    72 
    73 val type_syntax = target_decl Locale.add_type_syntax;
    74 val term_syntax = target_decl Locale.add_term_syntax;
    75 val declaration = target_decl Locale.add_declaration;
    76 
    77 fun class_target (Target {target, ...}) f =
    78   LocalTheory.raw_theory f #>
    79   LocalTheory.target (Class.refresh_syntax target);
    80 
    81 
    82 (* notes *)
    83 
    84 fun import_export_proof ctxt (name, raw_th) =
    85   let
    86     val thy = ProofContext.theory_of ctxt;
    87     val thy_ctxt = ProofContext.init thy;
    88     val certT = Thm.ctyp_of thy;
    89     val cert = Thm.cterm_of thy;
    90 
    91     (*export assumes/defines*)
    92     val th = Goal.norm_result raw_th;
    93     val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
    94     val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
    95     val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
    96     val nprems = Thm.nprems_of th' - Thm.nprems_of th;
    97 
    98     (*export fixes*)
    99     val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
   100     val frees = map Free (Thm.fold_terms Term.add_frees th' []);
   101     val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
   102       |> Variable.export ctxt thy_ctxt
   103       |> Drule.zero_var_indexes_list;
   104 
   105     (*thm definition*)
   106     val result = th''
   107       |> PureThy.name_thm true true ""
   108       |> Goal.close_result
   109       |> fold PureThy.tag_rule (ContextPosition.properties_of ctxt)
   110       |> PureThy.name_thm true true name;
   111 
   112     (*import fixes*)
   113     val (tvars, vars) =
   114       chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
   115       |>> map Logic.dest_type;
   116 
   117     val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
   118     val inst = filter (is_Var o fst) (vars ~~ frees);
   119     val cinstT = map (pairself certT o apfst TVar) instT;
   120     val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
   121     val result' = Thm.instantiate (cinstT, cinst) result;
   122 
   123     (*import assumes/defines*)
   124     val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
   125     val result'' =
   126       (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
   127         NONE => raise THM ("Failed to re-import result", 0, [result'])
   128       | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
   129       |> Goal.norm_result
   130       |> PureThy.name_thm false false name;
   131 
   132   in (result'', result) end;
   133 
   134 fun notes (Target {target, is_locale, ...}) kind facts lthy =
   135   let
   136     val thy = ProofContext.theory_of lthy;
   137     val full = LocalTheory.full_name lthy;
   138 
   139     val facts' = facts
   140       |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
   141       |> PureThy.map_facts (import_export_proof lthy);
   142     val local_facts = PureThy.map_facts #1 facts'
   143       |> Attrib.map_facts (Attrib.attribute_i thy);
   144     val target_facts = PureThy.map_facts #1 facts'
   145       |> is_locale ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
   146     val global_facts = PureThy.map_facts #2 facts'
   147       |> Attrib.map_facts (if is_locale then K I else Attrib.attribute_i thy);
   148   in
   149     lthy |> LocalTheory.theory
   150       (Sign.qualified_names
   151         #> PureThy.note_thmss_i kind global_facts #> snd
   152         #> Sign.restore_naming thy)
   153 
   154     |> is_locale ? LocalTheory.target (Locale.add_thmss target kind target_facts)
   155 
   156     |> ProofContext.qualified_names
   157     |> ProofContext.note_thmss_i kind local_facts
   158     ||> ProofContext.restore_naming lthy
   159   end;
   160 
   161 
   162 (* declare_const *)
   163 
   164 fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
   165   if not is_locale then (NoSyn, NoSyn, mx)
   166   else if not is_class then (NoSyn, mx, NoSyn)
   167   else (mx, NoSyn, NoSyn);
   168 
   169 fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) pos ((c, mx), rhs) phi =
   170   let
   171     val c' = Morphism.name phi c;
   172     val rhs' = Morphism.term phi rhs;
   173     val legacy_arg = (c', Term.close_schematic_term (Logic.legacy_varify rhs'));
   174     val arg = (c', Term.close_schematic_term rhs');
   175     val similar_body = Type.similar_types (rhs, rhs');
   176     (* FIXME workaround based on educated guess *)
   177     val class_global = c' = NameSpace.qualified (Class.class_prefix target) c;
   178   in
   179     not (is_class andalso (similar_body orelse class_global)) ?
   180       (Context.mapping_result
   181         (Sign.add_abbrev PrintMode.internal pos legacy_arg)
   182         (ProofContext.add_abbrev PrintMode.internal pos arg)
   183       #-> (fn (lhs' as Const (d, _), _) =>
   184           similar_body ?
   185             (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
   186              Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
   187   end;
   188 
   189 fun declare_const (ta as Target {target, is_locale, is_class}) depends ((c, T), mx) lthy =
   190   let
   191     val pos = ContextPosition.properties_of lthy;
   192     val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   193     val U = map #2 xs ---> T;
   194     val (mx1, mx2, mx3) = fork_mixfix ta mx;
   195     val (const, lthy') = lthy |> LocalTheory.theory_result (Sign.declare_const pos (c, U, mx3));
   196     val t = Term.list_comb (const, map Free xs);
   197   in
   198     lthy'
   199     |> is_locale ? term_syntax ta (locale_const ta Syntax.mode_default pos ((c, mx2), t))
   200     |> is_class ? class_target ta (Class.add_logical_const target pos ((c, mx1), t))
   201     |> LocalDefs.add_def ((c, NoSyn), t)
   202   end;
   203 
   204 
   205 (* abbrev *)
   206 
   207 fun abbrev (ta as Target {target, is_locale, is_class}) prmode ((c, mx), t) lthy =
   208   let
   209     val pos = ContextPosition.properties_of lthy;
   210     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   211     val target_ctxt = LocalTheory.target_of lthy;
   212 
   213     val (mx1, mx2, mx3) = fork_mixfix ta mx;
   214     val t' = Assumption.export_term lthy target_ctxt t;
   215     val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
   216     val u = fold_rev lambda xs t';
   217     val global_rhs =
   218       singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
   219   in
   220     lthy |>
   221      (if is_locale then
   222         LocalTheory.theory_result (Sign.add_abbrev PrintMode.internal pos (c, global_rhs))
   223         #-> (fn (lhs, _) =>
   224           let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
   225             term_syntax ta (locale_const ta prmode pos ((c, mx2), lhs')) #>
   226             is_class ? class_target ta (Class.add_syntactic_const target prmode pos ((c, mx1), t'))
   227           end)
   228       else
   229         LocalTheory.theory
   230           (Sign.add_abbrev (#1 prmode) pos (c, global_rhs) #-> (fn (lhs, _) =>
   231            Sign.notation true prmode [(lhs, mx3)])))
   232     |> ProofContext.add_abbrev PrintMode.internal pos (c, t) |> snd
   233     |> LocalDefs.fixed_abbrev ((c, NoSyn), t)
   234   end;
   235 
   236 
   237 (* define *)
   238 
   239 fun define (ta as Target {target, is_locale, is_class})
   240     kind ((c, mx), ((name, atts), rhs)) lthy =
   241   let
   242     val thy = ProofContext.theory_of lthy;
   243     val thy_ctxt = ProofContext.init thy;
   244 
   245     val name' = Thm.def_name_optional c name;
   246     val (rhs', rhs_conv) =
   247       LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
   248     val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
   249     val T = Term.fastype_of rhs;
   250 
   251     (*const*)
   252     val ((lhs, local_def), lthy2) = lthy |> declare_const ta (member (op =) xs) ((c, T), mx);
   253     val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
   254 
   255     (*def*)
   256     val (global_def, lthy3) = lthy2
   257       |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
   258     val def = LocalDefs.trans_terms lthy3
   259       [(*c == global.c xs*)     local_def,
   260        (*global.c xs == rhs'*)  global_def,
   261        (*rhs' == rhs*)          Thm.symmetric rhs_conv];
   262 
   263     (*note*)
   264     val ([(res_name, [res])], lthy4) = lthy3
   265       |> notes ta kind [((name', atts), [([def], [])])];
   266   in ((lhs, (res_name, res)), lthy4) end;
   267 
   268 
   269 (* axioms *)
   270 
   271 fun axioms ta kind (vars, specs) lthy =
   272   let
   273     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   274     val expanded_props = map (Assumption.export_term lthy thy_ctxt) (maps snd specs);
   275     val xs = fold Term.add_frees expanded_props [];
   276 
   277     (*consts*)
   278     val (consts, lthy') = fold_map (declare_const ta (member (op =) xs)) vars lthy;
   279     val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
   280 
   281     (*axioms*)
   282     val hyps = map Thm.term_of (Assumption.assms_of lthy');
   283     fun axiom ((name, atts), props) thy = thy
   284       |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props)
   285       |-> (fn ths => pair ((name, atts), [(ths, [])]));
   286   in
   287     lthy'
   288     |> fold Variable.declare_term expanded_props
   289     |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts)
   290     |> LocalTheory.theory_result (fold_map axiom specs)
   291     |-> notes ta kind
   292     |>> pair (map #1 consts)
   293   end;
   294 
   295 
   296 (* init *)
   297 
   298 local
   299 
   300 fun init_target _ NONE = global_target
   301   | init_target thy (SOME target) = make_target target true (Class.is_class thy target);
   302 
   303 fun init_ctxt (Target {target, is_locale, is_class}) =
   304   if not is_locale then ProofContext.init
   305   else if not is_class then Locale.init target
   306   else Class.init target;
   307 
   308 fun init_lthy (ta as Target {target, ...}) =
   309   Data.put ta #>
   310   LocalTheory.init (NameSpace.base target)
   311    {pretty = pretty ta,
   312     axioms = axioms ta,
   313     abbrev = abbrev ta,
   314     define = define ta,
   315     notes = notes ta,
   316     type_syntax = type_syntax ta,
   317     term_syntax = term_syntax ta,
   318     declaration = declaration ta,
   319     reinit = fn lthy => init_lthy_ctxt ta (ProofContext.theory_of lthy),
   320     exit = LocalTheory.target_of}
   321 and init_lthy_ctxt ta = init_lthy ta o init_ctxt ta;
   322 
   323 in
   324 
   325 fun init target thy = init_lthy_ctxt (init_target thy target) thy;
   326 fun begin target ctxt = init_lthy (init_target (ProofContext.theory_of ctxt) (SOME target)) ctxt;
   327 
   328 fun context "-" thy = init NONE thy
   329   | context target thy = init (SOME (Locale.intern thy target)) thy;
   330 
   331 end;
   332 
   333 end;
   334