src/Pure/Isar/theory_target.ML
author wenzelm
Sat Oct 13 17:16:45 2007 +0200 (2007-10-13)
changeset 25022 bb0dcb603a13
parent 25012 448af76a1ba3
child 25028 e0f74efc210f
permissions -rw-r--r--
abbrev: return hypothetical def;
replaced obsolete Theory.add_finals_i by Theory.add_deps;
misc cleanup;
     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 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')
   180         (ProofContext.add_abbrev Syntax.internalM [] arg')
   181       #-> (fn (lhs, _) =>
   182         Type.similar_types (rhs, rhs') ?
   183           Morphism.form (ProofContext.target_notation true prmode [(lhs, mx)])))
   184   end;
   185 
   186 fun internal_abbrev ta prmode ((c, mx), t) lthy = lthy
   187   (* FIXME really permissive *)
   188   |> term_syntax ta (perhaps o try o target_abbrev prmode ((c, mx), t))
   189   |> ProofContext.add_abbrev Syntax.internalM (ContextPosition.properties_of lthy) (c, t)
   190   |> snd;
   191 
   192 fun declare_consts (ta as Target {target, is_locale, is_class}) depends decls lthy =
   193   let
   194     val thy = ProofContext.theory_of lthy;
   195     val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   196 
   197     fun const ((c, T), mx) thy =
   198       let
   199         val U = map #2 xs ---> T;
   200         val (mx1, mx2) = Class.fork_mixfix is_locale is_class mx;
   201         val mx3 = if is_locale then NoSyn else mx1;
   202         val (const, thy') = Sign.declare_const (ContextPosition.properties_of lthy) (c, U, mx3) thy;
   203         val t = Term.list_comb (const, map Free xs);
   204       in (((c, mx2), t), thy') end;
   205     fun const_class ((c, _), mx) (_, t) =
   206       LocalTheory.raw_theory_result (Class.add_const_in_class target ((c, t), mx))
   207       #-> Class.remove_constraint target;
   208 
   209     val (abbrs, lthy') = lthy
   210       |> LocalTheory.theory_result (fold_map const decls)
   211   in
   212     lthy'
   213     |> is_class ? fold2 const_class decls abbrs
   214     |> is_locale ? fold (internal_abbrev ta Syntax.mode_default) abbrs
   215     |> LocalDefs.add_defs (map (apsnd (pair ("", []))) abbrs) |>> map (apsnd snd)
   216   end;
   217 
   218 
   219 (* abbrev *)
   220 
   221 fun abbrev (ta as Target {target, is_locale, is_class}) prmode ((raw_c, mx), raw_t) lthy =
   222   let
   223     val thy = ProofContext.theory_of lthy;
   224     val thy_ctxt = ProofContext.init thy;
   225     val target_ctxt = LocalTheory.target_of lthy;
   226     val target_morphism = LocalTheory.target_morphism lthy;
   227 
   228     val c = Morphism.name target_morphism raw_c;
   229     val t = Morphism.term target_morphism raw_t;
   230     val xs = map Free (Variable.add_fixed target_ctxt t []);
   231     val u = fold_rev lambda xs t;
   232     val U = Term.fastype_of u;
   233 
   234     val u' = singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
   235     val (mx1, mx2) = Class.fork_mixfix is_locale is_class mx;
   236     val mx3 = if is_locale then NoSyn else mx1;
   237     fun add_abbrev_in_class abbr =
   238       LocalTheory.raw_theory_result (Class.add_abbrev_in_class target prmode abbr)
   239       #-> Class.remove_constraint target;
   240   in
   241     lthy
   242     |> LocalTheory.theory_result
   243         (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'))
   244     |-> (fn (lhs, rhs) =>
   245           LocalTheory.theory (Sign.notation true prmode [(lhs, mx3)])
   246           #> is_locale ? internal_abbrev ta prmode ((c, mx2), Term.list_comb (lhs, xs))
   247           #> is_class ? add_abbrev_in_class ((c, Term.list_comb (lhs, xs)), mx1))
   248     |> LocalDefs.add_defs [((c, NoSyn), (("", []), raw_t))] |>> the_single
   249   end;
   250 
   251 
   252 (* define *)
   253 
   254 fun define (ta as Target {target, is_locale, is_class})
   255     kind ((c, mx), ((name, atts), rhs)) lthy =
   256   let
   257     val thy = ProofContext.theory_of lthy;
   258     val thy_ctxt = ProofContext.init thy;
   259 
   260     val name' = Thm.def_name_optional c name;
   261     val (rhs', rhs_conv) =
   262       LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
   263     val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
   264     val T = Term.fastype_of rhs;
   265 
   266     (*consts*)
   267     val ([(lhs, local_def)], lthy2) = lthy
   268       |> declare_consts ta (member (op =) xs) [((c, T), mx)];
   269     val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
   270 
   271     (*def*)
   272     val (global_def, lthy3) = lthy2
   273       |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
   274     val def = LocalDefs.trans_terms lthy3
   275       [(*c == global.c xs*)     local_def,
   276        (*global.c xs == rhs'*)  global_def,
   277        (*rhs' == rhs*)          Thm.symmetric rhs_conv];
   278 
   279     (*notes*)
   280     val ([(res_name, [res])], lthy4) = lthy3
   281       |> notes ta kind [((name', atts), [([def], [])])];
   282   in ((lhs, (res_name, res)), lthy4) end;
   283 
   284 
   285 (* axioms *)
   286 
   287 fun axioms ta kind (vars, specs) lthy =
   288   let
   289     val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   290     val expanded_props = map (Assumption.export_term lthy thy_ctxt) (maps snd specs);
   291     val xs = fold Term.add_frees expanded_props [];
   292 
   293     (*consts*)
   294     val (consts, lthy') = declare_consts ta (member (op =) xs) vars lthy;
   295     val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
   296 
   297     (*axioms*)
   298     val hyps = map Thm.term_of (Assumption.assms_of lthy');
   299     fun axiom ((name, atts), props) thy = thy
   300       |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props)
   301       |-> (fn ths => pair ((name, atts), [(ths, [])]));
   302   in
   303     lthy'
   304     |> fold Variable.declare_term expanded_props
   305     |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts)
   306     |> LocalTheory.theory_result (fold_map axiom specs)
   307     |-> notes ta kind
   308     |>> pair (map #1 consts)
   309   end;
   310 
   311 
   312 (* init and exit *)
   313 
   314 fun begin target ctxt =
   315   let
   316     val thy = ProofContext.theory_of ctxt;
   317     val is_locale = target <> "";
   318     val is_class = is_some (Class.class_of_locale thy target);
   319     val ta = Target {target = target, is_locale = is_locale, is_class = is_class};
   320   in
   321     ctxt
   322     |> Data.put ta
   323     |> is_class ? Class.init target
   324     |> LocalTheory.init (NameSpace.base target)
   325      {pretty = pretty ta,
   326       axioms = axioms ta,
   327       abbrev = abbrev ta,
   328       define = define ta,
   329       notes = notes ta,
   330       type_syntax = type_syntax ta,
   331       term_syntax = term_syntax ta,
   332       declaration = declaration ta,
   333       target_naming = target_naming ta,
   334       reinit = fn _ =>
   335         (if is_locale then Locale.init target else ProofContext.init)
   336         #> begin target,
   337       exit = LocalTheory.target_of}
   338   end;
   339 
   340 fun init NONE thy = begin "" (ProofContext.init thy)
   341   | init (SOME target) thy = begin target (Locale.init target thy);
   342 
   343 fun init_cmd (SOME "-") thy = init NONE thy
   344   | init_cmd target thy = init (Option.map (Locale.intern thy) target) thy;
   345 
   346 end;