src/Pure/Isar/theory_target.ML
author wenzelm
Wed, 29 Nov 2006 04:11:18 +0100
changeset 21585 2444f1d1127b
parent 21570 f20f9304ab48
child 21594 2859c94d67d4
permissions -rw-r--r--
Assumption.assms_of: cterm; added tmp version of export_standard_facts;

(*  Title:      Pure/Isar/theory_target.ML
    ID:         $Id$
    Author:     Makarius

Common theory targets.
*)

signature THEORY_TARGET =
sig
  val peek: local_theory -> string option
  val begin: bstring -> Proof.context -> local_theory
  val init: xstring option -> theory -> local_theory
  val init_i: string option -> theory -> local_theory
end;

structure TheoryTarget: THEORY_TARGET =
struct

(** locale targets **)

(* context data *)

structure Data = ProofDataFun
(
  val name = "Isar/theory_target";
  type T = string option;
  fun init _ = NONE;
  fun print _ _ = ();
);

val _ = Context.add_setup Data.init;
val peek = Data.get;


(* pretty *)

fun pretty loc ctxt =
  let
    val thy = ProofContext.theory_of ctxt;
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
    val elems =
      (if null fixes then [] else [Element.Fixes fixes]) @
      (if null assumes then [] else [Element.Assumes assumes]);
  in
    if loc = "" then
      [Pretty.block
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
    else
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
  end;


(* consts *)

fun consts loc depends decls lthy =
  let
    val is_loc = loc <> "";
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));

    fun const ((c, T), mx) thy =
      let
        val U = map #2 xs ---> T;
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
        val thy' = Sign.add_consts_authentic [(c, U, Syntax.unlocalize_mixfix is_loc mx)] thy;
      in (((c, mx), t), thy') end;

    val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls);
    val defs = abbrs |> map (fn (x, t) => (x, (("", []), t)));
  in
    lthy'
    |> is_loc ? LocalTheory.abbrevs Syntax.default_mode abbrs
    |> LocalDefs.add_defs defs |>> map (apsnd snd)
  end;


(* axioms *)

local

fun add_axiom hyps (name, prop) thy =
  let
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
    val prop' = Logic.list_implies (hyps, prop);
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
  in (Drule.implies_elim_list axm prems, thy') end;

in

fun axioms kind specs lthy =
  let
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
    fun axiom ((name, atts), props) thy = thy
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
  in
    lthy
    |> fold (fold Variable.declare_term o snd) specs
    |> LocalTheory.theory_result (fold_map axiom specs)
    |-> LocalTheory.notes kind
  end;

end;


(* defs *)

infix also;
fun eq1 also eq2 =
  eq2 COMP (eq1 COMP (Drule.incr_indexes2 eq1 eq2 transitive_thm));

local

fun expand_defs ctxt t =
  let
    val thy = ProofContext.theory_of ctxt;
    val thy_ctxt = ProofContext.init thy;
    val ct = Thm.cterm_of thy t;
    val (defs, ct') = LocalDefs.expand_defs ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
  in (Thm.term_of ct', Tactic.rewrite true defs ct) end;

fun add_def (name, prop) =
  Theory.add_defs_i false false [(name, prop)] #>
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));

in

fun defs kind args lthy0 =
  let
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
      let
        val (rhs', rhs_conv) = expand_defs lthy0 rhs;
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];

        val ([(lhs, lhs_def)], lthy2) = lthy1
          |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)];
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));

        val name' = Thm.def_name_optional c name;
        val (def, lthy3) = lthy2
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));

        val eq =
          (*c == loc.c xs*) lhs_def
          (*lhs' == rhs'*)  also def
          (*rhs' == rhs*)   also Thm.symmetric rhs_conv;
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;

    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;

end;


(* notes *)

(* FIXME tmp *)

val maxidx_atts = fold Args.maxidx_values;

fun export_standard_facts inner outer facts =
  let
    val thy = ProofContext.theory_of inner;
    val maxidx =
      fold (fn ((_, atts), bs) => maxidx_atts atts #> fold (maxidx_atts o #2) bs) facts ~1;
    val exp_fact = map (Thm.adjust_maxidx_thm maxidx) #> ProofContext.export_standard inner outer;
    val exp_term = Drule.term_rule thy (singleton exp_fact);
    val exp_typ = Logic.type_map exp_term;
    val morphism =
      Morphism.morphism {name = I, var = I, typ = exp_typ, term = exp_term, fact = exp_fact};
  in Element.facts_map (Element.morph_ctxt morphism) facts end;


fun context_notes kind facts lthy =
  let
    val facts' = facts
      |> export_standard_facts lthy lthy
      |> Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of lthy));
  in
    lthy
    |> ProofContext.set_stmt true
    |> ProofContext.qualified_names
    |> ProofContext.note_thmss_i kind facts'
    ||> ProofContext.restore_naming lthy
    ||> ProofContext.restore_stmt lthy
  end;

fun theory_notes keep_atts kind facts lthy = lthy |> LocalTheory.theory (fn thy =>
  let
    val facts' = facts
      |> export_standard_facts lthy (ProofContext.init thy)
      |> Attrib.map_facts (if keep_atts then Attrib.attribute_i thy else K I);
  in
    thy
    |> Sign.qualified_names
    |> PureThy.note_thmss_i kind facts' |> #2
    |> Sign.restore_naming thy
  end);

fun locale_notes loc kind facts lthy = lthy |> LocalTheory.target (fn ctxt =>
  Locale.add_thmss loc kind (export_standard_facts lthy ctxt facts) ctxt);

fun notes "" kind facts = theory_notes true kind facts #> context_notes kind facts
  | notes loc kind facts = theory_notes false kind facts #>
      locale_notes loc kind facts #> context_notes kind facts;


(* target declarations *)

fun decl _ "" f =
      LocalTheory.theory (Context.theory_map (f Morphism.identity)) #>
      LocalTheory.target (Context.proof_map (f Morphism.identity))
  | decl add loc f =
      LocalTheory.target (add loc (Context.proof_map o f));

val type_syntax = decl Locale.add_type_syntax;
val term_syntax = decl Locale.add_term_syntax;
val declaration = decl Locale.add_declaration;

fun target_morphism loc lthy =
  ProofContext.export_standard_morphism lthy (LocalTheory.target_of lthy) $>
  Morphism.name_morphism (NameSpace.qualified loc);

fun target_name "" lthy = Sign.full_name (ProofContext.theory_of lthy)
  | target_name _ lthy = ProofContext.full_name (LocalTheory.target_of lthy);


(* init and exit *)

fun begin loc =
  Data.put (if loc = "" then NONE else SOME loc) #>
  LocalTheory.init (NameSpace.base loc)
   {pretty = pretty loc,
    consts = consts loc,
    axioms = axioms,
    defs = defs,
    notes = notes loc,
    type_syntax = type_syntax loc,
    term_syntax = term_syntax loc,
    declaration = declaration loc,
    target_morphism = target_morphism loc,
    target_name = target_name loc,
    reinit = fn _ => begin loc o (if loc = "" then ProofContext.init else Locale.init loc),
    exit = LocalTheory.target_of};

fun init_i NONE thy = begin "" (ProofContext.init thy)
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);

fun init (SOME "-") thy = init_i NONE thy
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;

end;