src/Pure/Isar/theory_target.ML
author haftmann
Tue Oct 16 23:12:58 2007 +0200 (2007-10-16)
changeset 25064 c6664770ef6c
parent 25053 2b86fac03ec5
child 25068 a11d25316c3d
permissions -rw-r--r--
clarified fork_mixfix
     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 begin: string -> Proof.context -> local_theory
    12   val init: string option -> theory -> local_theory
    13   val init_cmd: xstring option -> 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 structure Data = ProofDataFun
    27 (
    28   type T = target;
    29   fun init _ = make_target "" false false;
    30 );
    31 
    32 val peek = (fn Target args => args) o Data.get;
    33 
    34 
    35 (* pretty *)
    36 
    37 fun pretty (Target {target, is_locale, is_class}) ctxt =
    38   let
    39     val thy = ProofContext.theory_of ctxt;
    40     val target_name = (if is_class then "class " else "locale ") ^ Locale.extern thy target;
    41     val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
    42     val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
    43     val elems =
    44       (if null fixes then [] else [Element.Fixes fixes]) @
    45       (if null assumes then [] else [Element.Assumes assumes]);
    46   in
    47     Pretty.block [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy)] ::
    48      (if target = "" then []
    49       else if null elems then [Pretty.str target_name]
    50       else [Pretty.big_list (target_name ^ " =")
    51         (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)])
    52   end;
    53 
    54 
    55 (* target declarations *)
    56 
    57 fun target_decl add (Target {target, ...}) d lthy =
    58   let
    59     val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
    60     val d0 = Morphism.form d';
    61   in
    62     if target = "" then
    63       lthy
    64       |> LocalTheory.theory (Context.theory_map d0)
    65       |> LocalTheory.target (Context.proof_map d0)
    66     else
    67       lthy
    68       |> LocalTheory.target (add target d')
    69   end;
    70 
    71 val type_syntax = target_decl Locale.add_type_syntax;
    72 val term_syntax = target_decl Locale.add_term_syntax;
    73 val declaration = target_decl Locale.add_declaration;
    74 
    75 fun target_naming (Target {target, ...}) lthy =
    76   (if target = "" then Sign.naming_of (ProofContext.theory_of lthy)
    77    else ProofContext.naming_of (LocalTheory.target_of lthy))
    78   |> NameSpace.qualified_names;
    79 
    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 (* consts *)
   163 
   164 fun locale_const (prmode as (mode, _)) pos ((c, mx), rhs) phi =
   165   let
   166     val c' = Morphism.name phi c;
   167     val rhs' = Morphism.term phi rhs;
   168     val legacy_arg = (c', Term.close_schematic_term (Logic.legacy_varify rhs'));
   169     val arg = (c', Term.close_schematic_term rhs');
   170   in
   171     Context.mapping_result
   172       (Sign.add_abbrev Syntax.internalM pos legacy_arg)
   173       (ProofContext.add_abbrev Syntax.internalM pos arg)
   174     #-> (fn (lhs' as Const (d, _), _) =>
   175         Type.similar_types (rhs, rhs') ?
   176           (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
   177            Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)])))
   178   end;
   179 
   180 fun declare_consts (ta as Target {target, is_locale, is_class}) depends decls lthy =
   181   let
   182     val pos = ContextPosition.properties_of lthy;
   183     val thy = ProofContext.theory_of lthy;
   184     val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   185 
   186     fun const ((c, T), mx) thy =
   187       let
   188         val U = map #2 xs ---> T;
   189         val ((mx1, mx2), mx3) = Class.fork_mixfix is_locale is_class mx;
   190         val (const, thy') = Sign.declare_const pos (c, U, mx3) thy;
   191         val t = Term.list_comb (const, map Free xs);
   192       in (((c, mx2), t), thy') end;
   193     fun class_const ((c, _), mx) (_, t) =
   194       LocalTheory.raw_theory_result (Class.add_const_in_class target ((c, mx), t))
   195       #-> LocalTheory.target o Class.remove_constraint target;
   196 
   197     val (abbrs, lthy') = lthy
   198       |> LocalTheory.theory_result (fold_map const decls)
   199   in
   200     lthy'
   201     |> is_locale ? fold (term_syntax ta o locale_const Syntax.mode_default pos) abbrs
   202     |> is_class ? fold2 class_const decls abbrs
   203     |> fold_map (apfst (apsnd snd) oo LocalDefs.add_def) abbrs
   204   end;
   205 
   206 
   207 (* abbrev *)
   208 
   209 local
   210 
   211 fun context_abbrev pos (c, t) lthy = lthy
   212   |> ProofContext.add_abbrev Syntax.internalM pos (c, t) |> snd
   213   |> LocalDefs.add_def ((c, NoSyn), t);
   214 
   215 fun class_abbrev target prmode ((c, mx), rhs) lthy = lthy   (* FIXME pos *)
   216   |> LocalTheory.raw_theory_result (Class.add_abbrev_in_class target prmode
   217       ((c, mx), rhs))
   218   |-> LocalTheory.target o Class.remove_constraint target;
   219 
   220 in
   221 
   222 fun abbrev (ta as Target {target, is_locale, is_class}) prmode ((raw_c, mx), raw_t) lthy =
   223   let
   224     val pos = ContextPosition.properties_of lthy;
   225     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   226     val target_ctxt = LocalTheory.target_of lthy;
   227     val target_morphism = LocalTheory.target_morphism lthy;
   228     val c = Morphism.name target_morphism raw_c;
   229     val t = Morphism.term target_morphism raw_t;
   230 
   231     val xs = map Free (Variable.add_fixed target_ctxt t []);
   232     val ((mx1, mx2), mx3) = Class.fork_mixfix is_locale is_class mx;
   233 
   234     val global_rhs =
   235       singleton (Variable.export_terms (Variable.declare_term t target_ctxt) thy_ctxt)
   236         (fold_rev lambda xs t);
   237   in
   238     if is_locale then
   239       lthy
   240       |> LocalTheory.theory_result (Sign.add_abbrev Syntax.internalM pos (c, global_rhs))
   241       |-> (fn (lhs, _) =>
   242         let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
   243           term_syntax ta (locale_const prmode pos ((c, mx2), lhs')) #>
   244           is_class ? class_abbrev target prmode ((c, mx1), lhs')
   245         end)
   246       |> context_abbrev pos (c, raw_t)
   247     else
   248       lthy
   249       |> LocalTheory.theory
   250         (Sign.add_abbrev (#1 prmode) pos (c, global_rhs)
   251           #-> (fn (lhs, _) => Sign.notation true prmode [(lhs, mx3)]))
   252       |> context_abbrev pos (c, raw_t)
   253   end;
   254 
   255 end;
   256 
   257 
   258 (* define *)
   259 
   260 fun define (ta as Target {target, is_locale, is_class})
   261     kind ((c, mx), ((name, atts), rhs)) lthy =
   262   let
   263     val thy = ProofContext.theory_of lthy;
   264     val thy_ctxt = ProofContext.init thy;
   265 
   266     val name' = Thm.def_name_optional c name;
   267     val (rhs', rhs_conv) =
   268       LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
   269     val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
   270     val T = Term.fastype_of rhs;
   271 
   272     (*consts*)
   273     val ([(lhs, local_def)], lthy2) = lthy
   274       |> declare_consts ta (member (op =) xs) [((c, T), mx)];
   275     val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
   276 
   277     (*def*)
   278     val (global_def, lthy3) = lthy2
   279       |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
   280     val def = LocalDefs.trans_terms lthy3
   281       [(*c == global.c xs*)     local_def,
   282        (*global.c xs == rhs'*)  global_def,
   283        (*rhs' == rhs*)          Thm.symmetric rhs_conv];
   284 
   285     (*notes*)
   286     val ([(res_name, [res])], lthy4) = lthy3
   287       |> notes ta kind [((name', atts), [([def], [])])];
   288   in ((lhs, (res_name, res)), lthy4) end;
   289 
   290 
   291 (* axioms *)
   292 
   293 fun axioms ta kind (vars, specs) lthy =
   294   let
   295     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   296     val expanded_props = map (Assumption.export_term lthy thy_ctxt) (maps snd specs);
   297     val xs = fold Term.add_frees expanded_props [];
   298 
   299     (*consts*)
   300     val (consts, lthy') = declare_consts ta (member (op =) xs) vars lthy;
   301     val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
   302 
   303     (*axioms*)
   304     val hyps = map Thm.term_of (Assumption.assms_of lthy');
   305     fun axiom ((name, atts), props) thy = thy
   306       |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props)
   307       |-> (fn ths => pair ((name, atts), [(ths, [])]));
   308   in
   309     lthy'
   310     |> fold Variable.declare_term expanded_props
   311     |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts)
   312     |> LocalTheory.theory_result (fold_map axiom specs)
   313     |-> notes ta kind
   314     |>> pair (map #1 consts)
   315   end;
   316 
   317 
   318 (* init and exit *)
   319 
   320 fun begin target ctxt =
   321   let
   322     val thy = ProofContext.theory_of ctxt;
   323     val is_locale = target <> "";
   324     val is_class = Class.is_class thy target;
   325     val ta = Target {target = target, is_locale = is_locale, is_class = is_class};
   326   in
   327     ctxt
   328     |> Data.put ta
   329     |> is_class ? Class.init target
   330     |> LocalTheory.init (NameSpace.base target)
   331      {pretty = pretty ta,
   332       axioms = axioms ta,
   333       abbrev = abbrev ta,
   334       define = define ta,
   335       notes = notes ta,
   336       type_syntax = type_syntax ta,
   337       term_syntax = term_syntax ta,
   338       declaration = declaration ta,
   339       target_naming = target_naming ta,
   340       reinit = fn _ =>
   341         begin target o (if is_locale then Locale.init target else ProofContext.init),
   342       exit = LocalTheory.target_of}
   343   end;
   344 
   345 fun init NONE thy = begin "" (ProofContext.init thy)
   346   | init (SOME target) thy = begin target (Locale.init target thy);
   347 
   348 fun init_cmd (SOME "-") thy = init NONE thy
   349   | init_cmd target thy = init (Option.map (Locale.intern thy) target) thy;
   350 
   351 end;