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