src/Pure/Isar/theory_target.ML
author wenzelm
Thu Oct 11 21:10:43 2007 +0200 (2007-10-11)
changeset 24987 50b07326da38
parent 24983 f2f4ba67cef1
child 24989 e656aeaa8b28
permissions -rw-r--r--
local_theory: incorporated consts into axioms;
LocalDefs.expand_cterm;
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 
    20 (** locale targets **)
    21 
    22 (* context data *)
    23 
    24 structure Data = ProofDataFun
    25 (
    26   type T = string option;
    27   fun init _ = NONE;
    28 );
    29 
    30 val peek = Data.get;
    31 
    32 
    33 (* pretty *)
    34 
    35 fun pretty loc ctxt =
    36   let
    37     val thy = ProofContext.theory_of ctxt;
    38     val loc_kind = if is_some (Class.class_of_locale thy loc) then "class" else "locale";
    39     val loc_name = Locale.extern thy loc;
    40 
    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 loc = "" then []
    49       else if null elems then [Pretty.str (loc_kind ^ " " ^ loc_name)]
    50       else [Pretty.big_list (loc_kind ^ " " ^ loc_name ^ " =")
    51         (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)])
    52   end;
    53 
    54 
    55 (* target declarations *)
    56 
    57 fun target_decl add loc d lthy =
    58   let
    59     val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
    60     val d0 = Morphism.form d';
    61   in
    62     if loc = "" 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 loc 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 loc lthy =
    76   (if loc = "" 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 local_notes loc kind facts lthy =
   135   let
   136     val is_loc = loc <> "";
   137     val thy = ProofContext.theory_of lthy;
   138     val full = LocalTheory.full_name lthy;
   139 
   140     val facts' = facts
   141       |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
   142       |> PureThy.map_facts (import_export_proof lthy);
   143     val local_facts = PureThy.map_facts #1 facts'
   144       |> Attrib.map_facts (Attrib.attribute_i thy);
   145     val target_facts = PureThy.map_facts #1 facts'
   146       |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
   147     val global_facts = PureThy.map_facts #2 facts'
   148       |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
   149   in
   150     lthy |> LocalTheory.theory
   151       (Sign.qualified_names
   152         #> PureThy.note_thmss_i kind global_facts #> snd
   153         #> Sign.restore_naming thy)
   154 
   155     |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
   156 
   157     |> ProofContext.qualified_names
   158     |> ProofContext.note_thmss_i kind local_facts
   159     ||> ProofContext.restore_naming lthy
   160   end;
   161 
   162 
   163 (* consts *)
   164 
   165 fun target_abbrev prmode ((c, mx), rhs) phi =
   166   let
   167     val c' = Morphism.name phi c;
   168     val rhs' = Morphism.term phi rhs;
   169     val arg' = (c', rhs');
   170     val eq_heads =
   171       (case pairself Term.head_of (rhs, rhs') of
   172         (Const (a, _), Const (a', _)) => a = a'
   173       | _ => false);
   174   in
   175     eq_heads ? (Context.mapping_result
   176       (Sign.add_abbrev Syntax.internalM [] arg') (ProofContext.add_abbrev Syntax.internalM [] arg')
   177     #-> (fn (lhs, _) =>
   178       Type.similar_types (rhs, rhs') ?
   179         Morphism.form (ProofContext.target_notation true prmode [(lhs, mx)])))
   180   end;
   181 
   182 fun internal_abbrev loc prmode ((c, mx), t) lthy = lthy
   183   (* FIXME really permissive *)
   184   |> term_syntax loc (perhaps o try o target_abbrev prmode ((c, mx), t))
   185   |> ProofContext.add_abbrev Syntax.internalM (ContextPosition.properties_of lthy) (c, t)
   186   |> snd;
   187 
   188 fun declare_consts loc depends decls lthy =
   189   let
   190     val thy = ProofContext.theory_of lthy;
   191     val is_loc = loc <> "";
   192     val some_class = Class.class_of_locale thy loc;
   193 
   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_loc some_class mx;
   200         val mx3 = if is_loc 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     fun hide_abbrev (SOME class) abbrs thy =
   209           let
   210             val raw_cs = map (fst o fst) abbrs;
   211             val prfx = (Logic.const_of_class o NameSpace.base) class;
   212             fun mk_name c =
   213               let
   214                 val n1 = Sign.full_name thy c;
   215                 val n2 = NameSpace.qualifier n1;
   216                 val n3 = NameSpace.base n1;
   217               in NameSpace.implode [n2, prfx, prfx, n3] end;
   218             val cs = map mk_name raw_cs;
   219           in
   220             Sign.hide_consts_i true cs thy
   221           end
   222       | hide_abbrev NONE _ thy = thy;
   223 
   224     val (abbrs, lthy') = lthy
   225       |> LocalTheory.theory_result (fold_map const decls)
   226     val defs = map (apsnd (pair ("", []))) abbrs;
   227 
   228   in
   229     lthy'
   230     |> fold2 (const_class some_class) decls abbrs
   231     |> is_loc ? fold (internal_abbrev loc Syntax.mode_default) abbrs
   232     |> LocalTheory.raw_theory (hide_abbrev some_class abbrs)
   233         (*FIXME abbreviations should never occur*)
   234     |> LocalDefs.add_defs defs
   235     |>> map (apsnd snd)
   236   end;
   237 
   238 
   239 (* abbrev *)
   240 
   241 fun occ_params ctxt ts =
   242   #1 (ProofContext.inferred_fixes ctxt)
   243   |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
   244 
   245 fun local_abbrev (x, rhs) =
   246   Variable.add_fixes [x] #-> (fn [x'] =>
   247     let
   248       val T = Term.fastype_of rhs;
   249       val lhs = Free (x', T);
   250     in
   251       Variable.declare_term lhs #>
   252       Variable.declare_term rhs #>
   253       Assumption.add_assms (K (K (I, Envir.expand_term_frees [((x', T), rhs)]))) [] #> snd #>
   254       pair (lhs, rhs)
   255     end);
   256 
   257 fun abbrev loc prmode ((raw_c, mx), raw_t) lthy =
   258   let
   259     val thy = ProofContext.theory_of lthy;
   260     val is_loc = loc <> "";
   261     val some_class = Class.class_of_locale thy loc;
   262 
   263     val thy_ctxt = ProofContext.init thy;
   264     val target = LocalTheory.target_of lthy;
   265     val target_morphism = LocalTheory.target_morphism lthy;
   266 
   267     val c = Morphism.name target_morphism raw_c;
   268     val t = Morphism.term target_morphism raw_t;
   269     val xs = map Free (occ_params target [t]);
   270     val u = fold_rev Term.lambda xs t;
   271     val U = Term.fastype_of u;
   272     val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
   273     val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
   274     val mx3 = if is_loc then NoSyn else mx1;
   275     fun add_abbrev_in_class (SOME class) abbr =
   276           LocalTheory.raw_theory_result (Class.add_abbrev_in_class class prmode abbr)
   277           #-> (fn c => Class.remove_constraint [class] c)
   278       | add_abbrev_in_class NONE _ = I;
   279   in
   280     lthy
   281     |> LocalTheory.theory_result
   282         (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'))
   283     |-> (fn (lhs as Const (full_c, _), rhs) =>
   284           LocalTheory.theory (Sign.notation true prmode [(lhs, mx3)])
   285           #> is_loc ? internal_abbrev loc prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
   286           #> add_abbrev_in_class some_class ((c, Term.list_comb (Const (full_c, U), xs)), mx1)
   287           #> local_abbrev (c, rhs))
   288   end;
   289 
   290 
   291 (* defs *)
   292 
   293 fun local_def loc kind ((c, mx), ((name, atts), rhs)) lthy =
   294   let
   295     val thy = ProofContext.theory_of lthy;
   296     val thy_ctxt = ProofContext.init thy;
   297 
   298     val (rhs', rhs_conv) =
   299       LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
   300     val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
   301     val T = Term.fastype_of rhs;
   302 
   303     (*consts*)
   304     val ([(lhs, lhs_def)], lthy2) = lthy
   305       |> declare_consts loc (member (op =) xs) [((c, T), mx)];
   306     val (_, lhs') = Logic.dest_equals (Thm.prop_of lhs_def);
   307 
   308     (*def*)
   309     val name' = Thm.def_name_optional c name;
   310     val (def, lthy3) = lthy2
   311       |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
   312     val eq = LocalDefs.trans_terms lthy3
   313       [(*c == loc.c xs*) lhs_def,
   314        (*lhs' == rhs'*)  def,
   315        (*rhs' == rhs*)   Thm.symmetric rhs_conv];
   316 
   317     (*notes*)
   318     val ([(res_name, [res])], lthy4) = lthy3
   319       |> local_notes loc kind [((name', atts), [([eq], [])])];
   320   in ((lhs, (res_name, res)), lthy4) end;
   321 
   322 
   323 (* axioms *)
   324 
   325 fun local_axioms loc kind (vars, specs) lthy =
   326   let
   327     val hyps = map Thm.term_of (Assumption.assms_of lthy);
   328     fun axiom ((name, atts), props) thy = thy
   329       |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props)
   330       |-> (fn ths => pair ((name, atts), [(ths, [])]));
   331 
   332     val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
   333     val (consts, lthy') = declare_consts loc spec_frees vars lthy;
   334     val heads = map (Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
   335   in
   336     lthy'
   337     |> LocalTheory.theory (Theory.add_finals_i false heads)
   338     |> fold (fold Variable.declare_term o snd) specs
   339     |> LocalTheory.theory_result (fold_map axiom specs)
   340     |-> local_notes loc kind
   341     |>> pair (map #1 consts)
   342   end;
   343 
   344 
   345 (* init and exit *)
   346 
   347 fun begin loc ctxt =
   348   let
   349     val thy = ProofContext.theory_of ctxt;
   350     val is_loc = loc <> "";
   351   in
   352     ctxt
   353     |> Data.put (if is_loc then SOME loc else NONE)
   354     |> fold Class.init (the_list (Class.class_of_locale thy loc))
   355     |> LocalTheory.init (NameSpace.base loc)
   356      {pretty = pretty loc,
   357       axioms = local_axioms loc,
   358       abbrev = abbrev loc,
   359       def = local_def loc,
   360       notes = local_notes loc,
   361       type_syntax = type_syntax loc,
   362       term_syntax = term_syntax loc,
   363       declaration = declaration loc,
   364       target_naming = target_naming loc,
   365       reinit = fn _ =>
   366         (if is_loc then Locale.init loc else ProofContext.init)
   367         #> begin loc,
   368       exit = LocalTheory.target_of}
   369   end;
   370 
   371 fun init NONE thy = begin "" (ProofContext.init thy)
   372   | init (SOME loc) thy = begin loc (Locale.init loc thy);
   373 
   374 fun init_cmd (SOME "-") thy = init NONE thy
   375   | init_cmd loc thy = init (Option.map (Locale.intern thy) loc) thy;
   376 
   377 end;