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