src/Pure/Isar/theory_target.ML
author wenzelm
Tue Oct 09 17:10:49 2007 +0200 (2007-10-09)
changeset 24935 a033971c63a0
parent 24914 95cda5dd58d5
child 24939 6dd60d1191bf
permissions -rw-r--r--
removed LocalTheory.defs/target_morphism operations;
moved target_morphism to local_theory.ML;
renamed init_i to init, and init to init_cmd;
tuned;
     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 -> string option
    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 (** locale targets **)
    20 
    21 (* context data *)
    22 
    23 structure Data = ProofDataFun
    24 (
    25   type T = string option;
    26   fun init _ = NONE;
    27 );
    28 
    29 val peek = Data.get;
    30 
    31 
    32 (* pretty *)
    33 
    34 fun pretty loc ctxt =
    35   let
    36     val thy = ProofContext.theory_of ctxt;
    37     val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
    38     val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
    39     val elems =
    40       (if null fixes then [] else [Element.Fixes fixes]) @
    41       (if null assumes then [] else [Element.Assumes assumes]);
    42   in
    43     if loc = "" then
    44       [Pretty.block
    45         [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
    46           Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
    47     else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
    48     else
    49       [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
    50         (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
    51   end;
    52 
    53 
    54 (* target declarations *)
    55 
    56 fun target_decl add loc d lthy =
    57   let
    58     val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
    59     val d0 = Morphism.form d';
    60   in
    61     if loc = "" then
    62       lthy
    63       |> LocalTheory.theory (Context.theory_map d0)
    64       |> LocalTheory.target (Context.proof_map d0)
    65     else
    66       lthy
    67       |> LocalTheory.target (add loc d')
    68   end;
    69 
    70 val type_syntax = target_decl Locale.add_type_syntax;
    71 val term_syntax = target_decl Locale.add_term_syntax;
    72 val declaration = target_decl Locale.add_declaration;
    73 
    74 fun target_naming loc lthy =
    75   (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
    76    else ProofContext.naming_of (LocalTheory.target_of lthy))
    77   |> NameSpace.qualified_names;
    78 
    79 
    80 (* consts *)
    81 
    82 fun target_abbrev prmode ((c, mx), rhs) phi =
    83   let
    84     val c' = Morphism.name phi c;
    85     val rhs' = Morphism.term phi rhs;
    86     val arg' = (c', rhs');
    87     val eq_heads =
    88       (case pairself Term.head_of (rhs, rhs') of
    89         (Const (a, _), Const (a', _)) => a = a'
    90       | _ => false);
    91   in
    92     eq_heads ? (Context.mapping_result
    93       (Sign.add_abbrev Syntax.internalM [] arg') (ProofContext.add_abbrev Syntax.internalM [] arg')
    94     #-> (fn (lhs, _) =>
    95       Term.equiv_types (rhs, rhs') ?
    96         Morphism.form (ProofContext.target_notation prmode [(lhs, mx)])))
    97   end;
    98 
    99 fun internal_abbrev prmode ((c, mx), t) lthy = lthy
   100   (* FIXME really permissive *)
   101   |> LocalTheory.term_syntax (perhaps o try o target_abbrev prmode ((c, mx), t))
   102   |> ProofContext.add_abbrev Syntax.internalM (ContextPosition.properties_of lthy) (c, t)
   103   |> snd;
   104 
   105 fun consts is_loc some_class depends decls lthy =
   106   let
   107     val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   108 
   109     fun const ((c, T), mx) thy =
   110       let
   111         val U = map #2 xs ---> T;
   112         val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
   113         val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
   114         val mx3 = if is_loc then NoSyn else mx1;
   115         val thy' = Sign.add_consts_authentic (ContextPosition.properties_of lthy) [(c, U, mx3)] thy;
   116       in (((c, mx2), t), thy') end;
   117     fun const_class (SOME class) ((c, _), mx) (_, t) =
   118           LocalTheory.raw_theory_result
   119             (Class.add_const_in_class class ((c, t), mx))
   120           #-> (fn c => Class.remove_constraint [class] c)
   121       | const_class NONE _ _ = I;
   122     fun hide_abbrev (SOME class) abbrs thy =
   123           let
   124             val raw_cs = map (fst o fst) abbrs;
   125             val prfx = (Logic.const_of_class o NameSpace.base) class;
   126             fun mk_name c =
   127               let
   128                 val n1 = Sign.full_name thy c;
   129                 val n2 = NameSpace.qualifier n1;
   130                 val n3 = NameSpace.base n1;
   131               in NameSpace.implode [n2, prfx, prfx, n3] end;
   132             val cs = map mk_name raw_cs;
   133           in
   134             Sign.hide_consts_i true cs thy
   135           end
   136       | hide_abbrev NONE _ thy = thy;
   137 
   138     val (abbrs, lthy') = lthy
   139       |> LocalTheory.theory_result (fold_map const decls)
   140     val defs = map (apsnd (pair ("", []))) abbrs;
   141 
   142   in
   143     lthy'
   144     |> fold2 (const_class some_class) decls abbrs
   145     |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
   146     |> LocalTheory.raw_theory (hide_abbrev some_class abbrs)
   147         (*FIXME abbreviations should never occur*)
   148     |> LocalDefs.add_defs defs
   149     |>> map (apsnd snd)
   150   end;
   151 
   152 
   153 (* abbrev *)
   154 
   155 fun occ_params ctxt ts =
   156   #1 (ProofContext.inferred_fixes ctxt)
   157   |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
   158 
   159 fun local_abbrev (x, rhs) =
   160   Variable.add_fixes [x] #-> (fn [x'] =>
   161     let
   162       val T = Term.fastype_of rhs;
   163       val lhs = Free (x', T);
   164     in
   165       Variable.declare_term lhs #>
   166       Variable.declare_term rhs #>
   167       Assumption.add_assms (K (K (I, Envir.expand_term_frees [((x', T), rhs)]))) [] #> snd #>
   168       pair (lhs, rhs)
   169     end);
   170 
   171 fun abbrev is_loc some_class prmode ((raw_c, mx), raw_t) lthy =
   172   let
   173     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   174     val target = LocalTheory.target_of lthy;
   175     val target_morphism = LocalTheory.target_morphism lthy;
   176 
   177     val c = Morphism.name target_morphism raw_c;
   178     val t = Morphism.term target_morphism raw_t;
   179     val xs = map Free (occ_params target [t]);
   180     val u = fold_rev Term.lambda xs t;
   181     val U = Term.fastype_of u;
   182     val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
   183     val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
   184     val mx3 = if is_loc then NoSyn else mx1;
   185     fun add_abbrev_in_class (SOME class) abbr =
   186           LocalTheory.raw_theory_result (Class.add_abbrev_in_class class prmode abbr)
   187           #-> (fn c => Class.remove_constraint [class] c)
   188       | add_abbrev_in_class NONE _ = I;
   189   in
   190     lthy
   191     |> LocalTheory.theory_result
   192         (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'))
   193     |-> (fn (lhs as Const (full_c, _), rhs) => LocalTheory.theory (Sign.add_notation prmode [(lhs, mx3)])
   194     #> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
   195     #> add_abbrev_in_class some_class ((c, Term.list_comb (Const (full_c, U), xs)), mx1)
   196     #> local_abbrev (c, rhs))
   197   end;
   198 
   199 
   200 (* defs *)
   201 
   202 local
   203 
   204 fun expand_term ctxt t =
   205   let
   206     val thy = ProofContext.theory_of ctxt;
   207     val thy_ctxt = ProofContext.init thy;
   208     val ct = Thm.cterm_of thy t;
   209     val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
   210   in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
   211 
   212 fun add_def (name, prop) thy =
   213   let
   214     val tfrees = rev (map TFree (Term.add_tfrees prop []));
   215     val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
   216     val strip_sorts = tfrees ~~ tfrees';
   217     val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
   218 
   219     val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
   220     val thy' = Theory.add_defs_i false false [(name, prop')] thy;
   221     val axm' = Thm.get_axiom_i thy' (Sign.full_name thy' name);
   222     val def = Thm.instantiate (recover_sorts, []) axm';
   223   in (Drule.unvarify def, thy') end;
   224 
   225 in
   226 
   227 fun local_def kind ((c, mx), ((name, atts), rhs)) lthy1 =
   228   let
   229     val (rhs', rhs_conv) = expand_term lthy1 rhs;
   230     val xs = Variable.add_fixed (LocalTheory.target_of lthy1) rhs' [];
   231     val T = Term.fastype_of rhs;
   232 
   233     (*consts*)
   234     val ([(lhs, lhs_def)], lthy2) = lthy1
   235       |> LocalTheory.consts (member (op =) xs) [((c, T), mx)];
   236     val (_, lhs') = Logic.dest_equals (Thm.prop_of lhs_def);
   237 
   238     (*def*)
   239     val name' = Thm.def_name_optional c name;
   240     val (def, lthy3) = lthy2
   241       |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
   242     val eq = LocalDefs.trans_terms lthy3
   243       [(*c == loc.c xs*) lhs_def,
   244        (*lhs' == rhs'*)  def,
   245        (*rhs' == rhs*)   Thm.symmetric rhs_conv];
   246 
   247     (*notes*)
   248     val ([(res_name, [res])], lthy4) = lthy3
   249       |> LocalTheory.notes kind [((name', atts), [([eq], [])])];
   250   in ((lhs, (res_name, res)), lthy4) end;
   251 
   252 end;
   253 
   254 
   255 (* axioms *)
   256 
   257 local
   258 
   259 fun add_axiom hyps (name, prop) thy =
   260   let
   261     val name' = if name = "" then "axiom_" ^ serial_string () else name;
   262     val prop' = Logic.list_implies (hyps, prop);
   263     val thy' = thy |> Theory.add_axioms_i [(name', prop')];
   264     val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
   265     val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
   266   in (Drule.implies_elim_list axm prems, thy') end;
   267 
   268 in
   269 
   270 fun axioms kind specs lthy =
   271   let
   272     val hyps = map Thm.term_of (Assumption.assms_of lthy);
   273     fun axiom ((name, atts), props) thy = thy
   274       |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
   275       |-> (fn ths => pair ((name, atts), [(ths, [])]));
   276   in
   277     lthy
   278     |> fold (fold Variable.declare_term o snd) specs
   279     |> LocalTheory.theory_result (fold_map axiom specs)
   280     |-> LocalTheory.notes kind
   281   end;
   282 
   283 end;
   284 
   285 
   286 (* notes *)
   287 
   288 fun import_export_proof ctxt (name, raw_th) =
   289   let
   290     val thy = ProofContext.theory_of ctxt;
   291     val thy_ctxt = ProofContext.init thy;
   292     val certT = Thm.ctyp_of thy;
   293     val cert = Thm.cterm_of thy;
   294 
   295     (*export assumes/defines*)
   296     val th = Goal.norm_result raw_th;
   297     val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
   298     val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
   299     val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
   300     val nprems = Thm.nprems_of th' - Thm.nprems_of th;
   301 
   302     (*export fixes*)
   303     val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
   304     val frees = map Free (Thm.fold_terms Term.add_frees th' []);
   305     val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
   306       |> Variable.export ctxt thy_ctxt
   307       |> Drule.zero_var_indexes_list;
   308 
   309     (*thm definition*)
   310     val result = th''
   311       |> PureThy.name_thm true true ""
   312       |> Goal.close_result
   313       |> fold PureThy.tag_rule (ContextPosition.properties_of ctxt)
   314       |> PureThy.name_thm true true name;
   315 
   316     (*import fixes*)
   317     val (tvars, vars) =
   318       chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
   319       |>> map Logic.dest_type;
   320 
   321     val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
   322     val inst = filter (is_Var o fst) (vars ~~ frees);
   323     val cinstT = map (pairself certT o apfst TVar) instT;
   324     val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
   325     val result' = Thm.instantiate (cinstT, cinst) result;
   326 
   327     (*import assumes/defines*)
   328     val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
   329     val result'' =
   330       (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
   331         NONE => raise THM ("Failed to re-import result", 0, [result'])
   332       | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
   333       |> Goal.norm_result
   334       |> PureThy.name_thm false false name;
   335 
   336   in (result'', result) end;
   337 
   338 fun notes loc kind facts lthy =
   339   let
   340     val is_loc = loc <> "";
   341     val thy = ProofContext.theory_of lthy;
   342     val full = LocalTheory.full_name lthy;
   343 
   344     val facts' = facts
   345       |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
   346       |> PureThy.map_facts (import_export_proof lthy);
   347     val local_facts = PureThy.map_facts #1 facts'
   348       |> Attrib.map_facts (Attrib.attribute_i thy);
   349     val target_facts = PureThy.map_facts #1 facts'
   350       |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
   351     val global_facts = PureThy.map_facts #2 facts'
   352       |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
   353   in
   354     lthy |> LocalTheory.theory
   355       (Sign.qualified_names
   356         #> PureThy.note_thmss_i kind global_facts #> snd
   357         #> Sign.restore_naming thy)
   358 
   359     |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
   360 
   361     |> ProofContext.qualified_names
   362     |> ProofContext.note_thmss_i kind local_facts
   363     ||> ProofContext.restore_naming lthy
   364   end;
   365 
   366 
   367 (* init and exit *)
   368 
   369 fun begin loc ctxt =
   370   let
   371     val thy = ProofContext.theory_of ctxt;
   372     val is_loc = loc <> "";
   373     val some_class = Class.class_of_locale thy loc;
   374   in
   375     ctxt
   376     |> Data.put (if is_loc then SOME loc else NONE)
   377     |> fold Class.init (the_list some_class)
   378     |> LocalTheory.init (NameSpace.base loc)
   379      {pretty = pretty loc,
   380       consts = consts is_loc some_class,
   381       axioms = axioms,
   382       abbrev = abbrev is_loc some_class,
   383       def = local_def,
   384       notes = notes loc,
   385       type_syntax = type_syntax loc,
   386       term_syntax = term_syntax loc,
   387       declaration = declaration loc,
   388       target_naming = target_naming loc,
   389       reinit = fn _ =>
   390         (if is_loc then Locale.init loc else ProofContext.init)
   391         #> begin loc,
   392       exit = LocalTheory.target_of}
   393   end;
   394 
   395 fun init NONE thy = begin "" (ProofContext.init thy)
   396   | init (SOME loc) thy = begin loc (Locale.init loc thy);
   397 
   398 fun init_cmd (SOME "-") thy = init NONE thy
   399   | init_cmd loc thy = init (Option.map (Locale.intern thy) loc) thy;
   400 
   401 end;