src/Pure/Isar/theory_target.ML
author haftmann
Thu Oct 18 09:20:59 2007 +0200 (2007-10-18)
changeset 25079 db5fdc34f3af
parent 25068 a11d25316c3d
child 25083 765528b4b419
permissions -rw-r--r--
moved fork_mixfix to theory_target
     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 fork_mixfix false _ mx = ((NoSyn, NoSyn), mx)
   165   | fork_mixfix true false mx = ((NoSyn, mx), NoSyn)
   166   | fork_mixfix true true mx = ((mx, NoSyn), NoSyn);
   167 
   168 fun locale_const (prmode as (mode, _)) pos ((c, mx), rhs) phi =
   169   let
   170     val c' = Morphism.name phi c;
   171     val rhs' = Morphism.term phi rhs;
   172     val legacy_arg = (c', Term.close_schematic_term (Logic.legacy_varify rhs'));
   173     val arg = (c', Term.close_schematic_term rhs');
   174   in
   175     Context.mapping_result
   176       (Sign.add_abbrev Syntax.internalM pos legacy_arg)
   177       (ProofContext.add_abbrev Syntax.internalM pos arg)
   178     #-> (fn (lhs' as Const (d, _), _) =>
   179         Type.similar_types (rhs, rhs') ?
   180           (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
   181            Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)])))
   182   end;
   183 
   184 fun declare_consts (ta as Target {target, is_locale, is_class}) depends decls lthy =
   185   let
   186     val pos = ContextPosition.properties_of lthy;
   187     val thy = ProofContext.theory_of lthy;
   188     val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   189 
   190     fun const ((c, T), mx) thy =
   191       let
   192         val U = map #2 xs ---> T;
   193         val (mx12, mx3) = fork_mixfix is_locale is_class mx;
   194         val (const, thy') = Sign.declare_const pos (c, U, mx3) thy;
   195         val t = Term.list_comb (const, map Free xs);
   196       in (((c, mx12), t), thy') end;
   197     fun class_const ((c, _), _) ((_, (mx1, _)), t) =
   198       LocalTheory.raw_theory_result (Class.add_const_in_class target ((c, mx1), t))
   199       #-> LocalTheory.target o Class.remove_constraint target;
   200 
   201     val (abbrs, lthy') = lthy
   202       |> LocalTheory.theory_result (fold_map const decls)
   203     val abbrs' = (map o apfst o apsnd) snd abbrs;
   204   in
   205     lthy'
   206     |> is_locale ? fold (term_syntax ta o locale_const Syntax.mode_default pos) abbrs'
   207     |> is_class ? fold2 class_const decls abbrs
   208     |> fold_map (apfst (apsnd snd) oo LocalDefs.add_def) abbrs'
   209   end;
   210 
   211 
   212 (* abbrev *)
   213 
   214 local
   215 
   216 fun context_abbrev pos (c, t) lthy = lthy
   217   |> ProofContext.add_abbrev Syntax.internalM pos (c, t) |> snd
   218   |> LocalDefs.add_def ((c, NoSyn), t);
   219 
   220 fun class_abbrev target prmode ((c, mx), rhs) lthy = lthy   (* FIXME pos *)
   221   |> LocalTheory.raw_theory_result (Class.add_abbrev_in_class target prmode
   222       ((c, mx), rhs))
   223   |-> LocalTheory.target o Class.remove_constraint target;
   224 
   225 in
   226 
   227 fun abbrev (ta as Target {target, is_locale, is_class}) prmode ((raw_c, mx), raw_t) lthy =
   228   let
   229     val pos = ContextPosition.properties_of lthy;
   230     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   231     val target_ctxt = LocalTheory.target_of lthy;
   232     val target_morphism = LocalTheory.target_morphism lthy;
   233     val c = Morphism.name target_morphism raw_c;
   234     val t = Morphism.term target_morphism raw_t;
   235 
   236     val xs = map Free (rev (Variable.add_fixed target_ctxt t []));
   237     val ((mx1, mx2), mx3) = fork_mixfix is_locale is_class mx;
   238 
   239     val global_rhs =
   240       singleton (Variable.export_terms (Variable.declare_term t target_ctxt) thy_ctxt)
   241         (fold_rev lambda xs t);
   242   in
   243     if is_locale then
   244       lthy
   245       |> LocalTheory.theory_result (Sign.add_abbrev Syntax.internalM pos (c, global_rhs))
   246       |-> (fn (lhs, _) =>
   247         let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
   248           term_syntax ta (locale_const prmode pos ((c, mx2), lhs')) #>
   249           is_class ? class_abbrev target prmode ((c, mx1), lhs')
   250         end)
   251       |> context_abbrev pos (c, raw_t)
   252     else
   253       lthy
   254       |> LocalTheory.theory
   255         (Sign.add_abbrev (#1 prmode) pos (c, global_rhs)
   256           #-> (fn (lhs, _) => Sign.notation true prmode [(lhs, mx3)]))
   257       |> context_abbrev pos (c, raw_t)
   258   end;
   259 
   260 end;
   261 
   262 
   263 (* define *)
   264 
   265 fun define (ta as Target {target, is_locale, is_class})
   266     kind ((c, mx), ((name, atts), rhs)) lthy =
   267   let
   268     val thy = ProofContext.theory_of lthy;
   269     val thy_ctxt = ProofContext.init thy;
   270 
   271     val name' = Thm.def_name_optional c name;
   272     val (rhs', rhs_conv) =
   273       LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
   274     val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
   275     val T = Term.fastype_of rhs;
   276 
   277     (*consts*)
   278     val ([(lhs, local_def)], lthy2) = lthy
   279       |> declare_consts ta (member (op =) xs) [((c, T), mx)];
   280     val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
   281 
   282     (*def*)
   283     val (global_def, lthy3) = lthy2
   284       |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
   285     val def = LocalDefs.trans_terms lthy3
   286       [(*c == global.c xs*)     local_def,
   287        (*global.c xs == rhs'*)  global_def,
   288        (*rhs' == rhs*)          Thm.symmetric rhs_conv];
   289 
   290     (*notes*)
   291     val ([(res_name, [res])], lthy4) = lthy3
   292       |> notes ta kind [((name', atts), [([def], [])])];
   293   in ((lhs, (res_name, res)), lthy4) end;
   294 
   295 
   296 (* axioms *)
   297 
   298 fun axioms ta kind (vars, specs) lthy =
   299   let
   300     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   301     val expanded_props = map (Assumption.export_term lthy thy_ctxt) (maps snd specs);
   302     val xs = fold Term.add_frees expanded_props [];
   303 
   304     (*consts*)
   305     val (consts, lthy') = declare_consts ta (member (op =) xs) vars lthy;
   306     val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
   307 
   308     (*axioms*)
   309     val hyps = map Thm.term_of (Assumption.assms_of lthy');
   310     fun axiom ((name, atts), props) thy = thy
   311       |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props)
   312       |-> (fn ths => pair ((name, atts), [(ths, [])]));
   313   in
   314     lthy'
   315     |> fold Variable.declare_term expanded_props
   316     |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts)
   317     |> LocalTheory.theory_result (fold_map axiom specs)
   318     |-> notes ta kind
   319     |>> pair (map #1 consts)
   320   end;
   321 
   322 
   323 (* init and exit *)
   324 
   325 fun begin target ctxt =
   326   let
   327     val thy = ProofContext.theory_of ctxt;
   328     val is_locale = target <> "";
   329     val is_class = Class.is_class thy target;
   330     val ta = Target {target = target, is_locale = is_locale, is_class = is_class};
   331   in
   332     ctxt
   333     |> Data.put ta
   334     |> is_class ? Class.init target
   335     |> LocalTheory.init (NameSpace.base target)
   336      {pretty = pretty ta,
   337       axioms = axioms ta,
   338       abbrev = abbrev ta,
   339       define = define ta,
   340       notes = notes ta,
   341       type_syntax = type_syntax ta,
   342       term_syntax = term_syntax ta,
   343       declaration = declaration ta,
   344       target_naming = target_naming ta,
   345       reinit = fn _ =>
   346         begin target o (if is_locale then Locale.init target else ProofContext.init),
   347       exit = LocalTheory.target_of}
   348   end;
   349 
   350 fun init NONE thy = begin "" (ProofContext.init thy)
   351   | init (SOME target) thy = begin target (Locale.init target thy);
   352 
   353 fun init_cmd (SOME "-") thy = init NONE thy
   354   | init_cmd target thy = init (Option.map (Locale.intern thy) target) thy;
   355 
   356 end;