src/Pure/Isar/theory_target.ML
author haftmann
Thu May 24 08:37:37 2007 +0200 (2007-05-24)
changeset 23086 12320f6e2523
parent 22846 fb79144af9a3
child 23087 ad7244663431
permissions -rw-r--r--
tuned Pure/General/name_space.ML
     1 (*  Title:      Pure/Isar/theory_target.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Common theory/locale targets.
     6 *)
     7 
     8 signature THEORY_TARGET =
     9 sig
    10   val peek: local_theory -> string option
    11   val begin: bstring -> Proof.context -> local_theory
    12   val init: xstring option -> theory -> local_theory
    13   val init_i: string 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 (* consts *)
    55 
    56 fun internal_abbrev prmode ((c, mx), t) =
    57   (* FIXME really permissive *)
    58   LocalTheory.term_syntax (perhaps o try o ProofContext.target_abbrev prmode ((c, mx), t)) #>
    59   ProofContext.add_abbrev Syntax.internalM (c, t) #> snd;
    60 
    61 fun consts is_loc some_class depends decls lthy =
    62   let
    63     val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
    64 
    65     fun const ((c, T), mx) thy =
    66       let
    67         val U = map #2 xs ---> T;
    68         val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
    69         val (mx1, mx2) = ClassPackage.fork_mixfix is_loc some_class mx;
    70         val thy' = Sign.add_consts_authentic [(c, U, mx1)] thy;
    71       in (((c, mx2), t), thy') end;
    72 
    73     val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls);
    74     val defs = map (apsnd (pair ("", []))) abbrs;
    75   in
    76     lthy'
    77     |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
    78     |> LocalDefs.add_defs defs |>> map (apsnd snd)
    79   end;
    80 
    81 
    82 (* abbrev *)
    83 
    84 fun occ_params ctxt ts =
    85   #1 (ProofContext.inferred_fixes ctxt)
    86   |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
    87 
    88 fun abbrev is_loc some_class  prmode ((raw_c, mx), raw_t) lthy =
    89   let
    90     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
    91     val target = LocalTheory.target_of lthy;
    92     val target_morphism = LocalTheory.target_morphism lthy;
    93 
    94     val c = Morphism.name target_morphism raw_c;
    95     val t = Morphism.term target_morphism raw_t;
    96     val xs = map Free (occ_params target [t]);
    97     val u = fold_rev Term.lambda xs t;
    98     val U = Term.fastype_of u;
    99     val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
   100 
   101     val ((lhs as Const (full_c, _), rhs), lthy1) = lthy
   102       |> LocalTheory.theory_result (Sign.add_abbrev (#1 prmode) (c, u'));
   103     val (mx1, mx2) = ClassPackage.fork_mixfix is_loc some_class mx;
   104   in
   105     lthy1
   106     |> LocalTheory.theory (Sign.add_notation prmode [(lhs, mx1)])
   107     |> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
   108     |> ProofContext.local_abbrev (c, rhs)
   109   end;
   110 
   111 
   112 (* defs *)
   113 
   114 local
   115 
   116 fun expand_term ctxt t =
   117   let
   118     val thy = ProofContext.theory_of ctxt;
   119     val thy_ctxt = ProofContext.init thy;
   120     val ct = Thm.cterm_of thy t;
   121     val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
   122   in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
   123 
   124 fun add_def (name, prop) =
   125   Theory.add_defs_i false false [(name, prop)] #>
   126   (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
   127 
   128 in
   129 
   130 fun defs loc some_class kind args lthy0 =
   131   let
   132     fun def ((c, mx), ((name, atts), rhs)) lthy1 =
   133       let
   134         val (rhs', rhs_conv) = expand_term lthy0 rhs;
   135         val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
   136 
   137         val ([(lhs, lhs_def)], lthy2) = lthy1
   138           |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)];
   139         val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
   140 
   141         val name' = Thm.def_name_optional c name;
   142         val (def, lthy3) = lthy2
   143           |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
   144         val eq = LocalDefs.trans_terms lthy3
   145           [(*c == loc.c xs*) lhs_def,
   146            (*lhs' == rhs'*)  def,
   147            (*rhs' == rhs*)   Thm.symmetric rhs_conv];
   148         val lthy4 = case some_class
   149          of SOME class => 
   150               lthy3
   151               |> LocalTheory.raw_theory
   152                 (ClassPackage.add_def_in_class lthy3 class
   153                   ((c, mx), ((name, atts), (rhs, eq))))
   154           | _ => lthy3;
   155       in ((lhs, ((name', atts), [([eq], [])])), lthy4) end;
   156 
   157     val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
   158     val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
   159 
   160   in (lhss ~~ map (apsnd the_single) res, lthy'') end;
   161 
   162 end;
   163 
   164 
   165 (* axioms *)
   166 
   167 local
   168 
   169 fun add_axiom hyps (name, prop) thy =
   170   let
   171     val name' = if name = "" then "axiom_" ^ serial_string () else name;
   172     val prop' = Logic.list_implies (hyps, prop);
   173     val thy' = thy |> Theory.add_axioms_i [(name', prop')];
   174     val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
   175     val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
   176   in (Drule.implies_elim_list axm prems, thy') end;
   177 
   178 in
   179 
   180 fun axioms kind specs lthy =
   181   let
   182     val hyps = map Thm.term_of (Assumption.assms_of lthy);
   183     fun axiom ((name, atts), props) thy = thy
   184       |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
   185       |-> (fn ths => pair ((name, atts), [(ths, [])]));
   186   in
   187     lthy
   188     |> fold (fold Variable.declare_term o snd) specs
   189     |> LocalTheory.theory_result (fold_map axiom specs)
   190     |-> LocalTheory.notes kind
   191   end;
   192 
   193 end;
   194 
   195 
   196 (* notes *)
   197 
   198 fun import_export_proof ctxt (name, raw_th) =
   199   let
   200     val thy = ProofContext.theory_of ctxt;
   201     val thy_ctxt = ProofContext.init thy;
   202     val certT = Thm.ctyp_of thy;
   203     val cert = Thm.cterm_of thy;
   204 
   205     (*export assumes/defines*)
   206     val th = Goal.norm_result raw_th;
   207     val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
   208     val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
   209     val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
   210     val nprems = Thm.nprems_of th' - Thm.nprems_of th;
   211 
   212     (*export fixes*)
   213     val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
   214     val frees = map Free (Thm.fold_terms Term.add_frees th' []);
   215     val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
   216       |> Variable.export ctxt thy_ctxt
   217       |> Drule.zero_var_indexes_list;
   218 
   219     (*thm definition*)
   220     val result = th''
   221       |> PureThy.name_thm true true ""
   222       |> Goal.close_result
   223       |> PureThy.name_thm true true name;
   224 
   225     (*import fixes*)
   226     val (tvars, vars) =
   227       chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
   228       |>> map Logic.dest_type;
   229 
   230     val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
   231     val inst = filter (is_Var o fst) (vars ~~ frees);
   232     val cinstT = map (pairself certT o apfst TVar) instT;
   233     val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
   234     val result' = Thm.instantiate (cinstT, cinst) result;
   235 
   236     (*import assumes/defines*)
   237     val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
   238     val result'' =
   239       (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
   240         NONE => raise THM ("Failed to re-import result", 0, [result'])
   241       | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
   242       |> Goal.norm_result
   243       |> PureThy.name_thm false false name;
   244 
   245   in (result'', result) end;
   246 
   247 fun notes loc kind facts lthy =
   248   let
   249     val is_loc = loc <> "";
   250     val thy = ProofContext.theory_of lthy;
   251     val full = LocalTheory.full_name lthy;
   252 
   253     val facts' = facts
   254       |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
   255       |> PureThy.map_facts (import_export_proof lthy);
   256     val local_facts = PureThy.map_facts #1 facts'
   257       |> Attrib.map_facts (Attrib.attribute_i thy);
   258     val target_facts = PureThy.map_facts #1 facts'
   259       |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
   260     val global_facts = PureThy.map_facts #2 facts'
   261       |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
   262   in
   263     lthy |> LocalTheory.theory
   264       (Sign.qualified_names
   265         #> PureThy.note_thmss_i kind global_facts #> snd
   266         #> Sign.restore_naming thy)
   267 
   268     |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
   269 
   270     |> ProofContext.set_stmt true
   271     |> ProofContext.qualified_names
   272     |> ProofContext.note_thmss_i kind local_facts
   273     ||> ProofContext.restore_naming lthy
   274     ||> ProofContext.restore_stmt lthy
   275   end;
   276 
   277 
   278 (* target declarations *)
   279 
   280 fun target_decl add loc d lthy =
   281   let
   282     val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
   283     val d0 = Morphism.form d';
   284   in
   285     if loc = "" then
   286       lthy
   287       |> LocalTheory.theory (Context.theory_map d0)
   288       |> LocalTheory.target (Context.proof_map d0)
   289     else
   290       lthy
   291       |> LocalTheory.target (add loc d')
   292   end;
   293 
   294 val type_syntax = target_decl Locale.add_type_syntax;
   295 val term_syntax = target_decl Locale.add_term_syntax;
   296 val declaration = target_decl Locale.add_declaration;
   297 
   298 fun target_morphism loc lthy =
   299   ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
   300   Morphism.thm_morphism Goal.norm_result;
   301 
   302 fun target_naming loc lthy =
   303   (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
   304    else ProofContext.naming_of (LocalTheory.target_of lthy))
   305   |> NameSpace.qualified_names;
   306 
   307 
   308 (* init and exit *)
   309 
   310 fun begin loc ctxt =
   311   let
   312     val thy = ProofContext.theory_of ctxt;
   313     val is_loc = loc <> "";
   314     val some_class = ClassPackage.class_of_locale thy loc;
   315   in
   316     ctxt
   317     |> Data.put (if is_loc then SOME loc else NONE)
   318     |> LocalTheory.init (NameSpace.base loc)
   319      {pretty = pretty loc,
   320       consts = consts is_loc some_class,
   321       axioms = axioms,
   322       abbrev = abbrev is_loc some_class,
   323       defs = defs loc some_class,
   324       notes = notes loc,
   325       type_syntax = type_syntax loc,
   326       term_syntax = term_syntax loc,
   327       declaration = declaration loc,
   328       target_morphism = target_morphism loc,
   329       target_naming = target_naming loc,
   330       reinit = fn _ =>
   331         begin loc o (if is_loc then Locale.init loc else ProofContext.init),
   332       exit = LocalTheory.target_of}
   333   end;
   334 
   335 fun init_i NONE thy = begin "" (ProofContext.init thy)
   336   | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
   337 
   338 fun init (SOME "-") thy = init_i NONE thy
   339   | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
   340 
   341 end;