src/Pure/Isar/theory_target.ML
author wenzelm
Wed Oct 11 00:27:37 2006 +0200 (2006-10-11)
changeset 20962 e404275bff33
parent 20915 dcb0a3e2f1bd
child 20984 d09f388fa9db
permissions -rw-r--r--
added begin;
     1 (*  Title:      Pure/Isar/theory_target.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Common theory targets.
     6 *)
     7 
     8 signature THEORY_TARGET =
     9 sig
    10   val begin: bstring -> Proof.context -> local_theory
    11   val init: xstring option -> theory -> local_theory
    12   val init_i: string option -> theory -> local_theory
    13   val exit: local_theory -> Proof.context * theory
    14   val mapping: xstring option -> (local_theory -> local_theory) ->
    15     theory -> Proof.context * theory
    16 end;
    17 
    18 structure TheoryTarget: THEORY_TARGET =
    19 struct
    20 
    21 (** locale targets **)
    22 
    23 (* pretty *)
    24 
    25 fun pretty loc ctxt =
    26   let
    27     val thy = ProofContext.theory_of ctxt;
    28     val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
    29     val assumes = map (fn A => (("", []), [(A, [])])) (Assumption.assms_of ctxt);
    30     val elems =
    31       (if null fixes then [] else [Element.Fixes fixes]) @
    32       (if null assumes then [] else [Element.Assumes assumes]);
    33   in
    34     [Pretty.block
    35       [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
    36         Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]] @
    37       (if loc = "" then []
    38        else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
    39        else
    40          [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
    41            (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)])
    42   end;
    43 
    44 
    45 (* consts *)
    46 
    47 fun consts loc depends decls lthy =
    48   let
    49     val xs = filter depends (#1 (ProofContext.inferred_fixes lthy));
    50     val ys = filter (Variable.newly_fixed lthy (LocalTheory.target_of lthy) o #1) xs;
    51 
    52     fun const ((c, T), mx) thy =
    53       let
    54         val U = map #2 xs ---> T;
    55         val mx' = if null ys then mx else NoSyn;
    56         val mx'' = Syntax.unlocalize_mixfix (loc <> "") mx';
    57 
    58         val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
    59         val defn = ((c, if loc <> "" then mx else NoSyn (* FIXME !? *)), (("", []), t));
    60         val abbr = ((c, mx'), fold_rev (lambda o Free) ys t);
    61         val thy' = Sign.add_consts_authentic [(c, U, mx'')] thy;
    62       in ((defn, abbr), thy') end;
    63 
    64     val ((defns, abbrs), lthy') = lthy
    65       |> LocalTheory.theory_result (fold_map const decls) |>> split_list;
    66   in
    67     lthy'
    68     |> K (loc <> "") ? LocalTheory.abbrevs Syntax.default_mode abbrs
    69     |> LocalDefs.add_defs defns |>> map (apsnd snd)
    70   end;
    71 
    72 
    73 (* axioms *)
    74 
    75 local
    76 
    77 fun add_axiom hyps (name, prop) thy =
    78   let
    79     val name' = if name = "" then "axiom_" ^ serial_string () else name;
    80     val prop' = Logic.list_implies (hyps, prop);
    81     val thy' = thy |> Theory.add_axioms_i [(name', prop')];
    82     val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
    83     val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
    84   in (Drule.implies_elim_list axm prems, thy') end;
    85 
    86 in
    87 
    88 fun axioms specs lthy =
    89   let
    90     val hyps = Assumption.assms_of lthy;
    91     fun axiom ((name, atts), props) thy = thy
    92       |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
    93       |-> (fn ths => pair ((name, atts), [(ths, [])]));
    94   in
    95     lthy
    96     |> LocalTheory.theory_result (fold_map axiom specs)
    97     |-> LocalTheory.notes
    98   end;
    99 
   100 end;
   101 
   102 
   103 (* defs *)
   104 
   105 local
   106 
   107 fun add_def (name, prop) =
   108   Theory.add_defs_i false false [(name, prop)] #>
   109   (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
   110 
   111 fun expand_defs lthy =
   112   Drule.term_rule (ProofContext.theory_of lthy)
   113     (Assumption.export false lthy (LocalTheory.target_of lthy));
   114 
   115 in
   116 
   117 fun defs args lthy =
   118   let
   119     fun def ((x, mx), ((name, atts), rhs)) lthy1 =
   120       let
   121         val name' = Thm.def_name_optional x name;
   122         val T = Term.fastype_of rhs;
   123         val rhs' = expand_defs lthy1 rhs;
   124         val depends = member (op =) (Term.add_frees rhs' []);
   125         val defined = filter_out depends (Term.add_frees rhs []);
   126 
   127         val rhs_conv = rhs
   128           |> Thm.cterm_of (ProofContext.theory_of lthy1)
   129           |> Tactic.rewrite true (map_filter (LocalDefs.find_def lthy1 o #1) defined);
   130 
   131         val ([(lhs, local_def)], lthy2) = lthy1
   132           |> LocalTheory.consts depends [((x, T), mx)];
   133         val lhs' = #2 (Logic.dest_equals (Thm.prop_of local_def));
   134 
   135         val (global_def, lthy3) = lthy2
   136           |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
   137 
   138         val eq = Thm.transitive (Thm.transitive local_def global_def) (Thm.symmetric rhs_conv);
   139       in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
   140 
   141     val ((lhss, facts), lthy') = lthy |> fold_map def args |>> split_list;
   142     val (res, lthy'') = lthy' |> LocalTheory.notes facts;
   143   in (lhss ~~ map (apsnd the_single) res, lthy'') end;
   144 
   145 end;
   146 
   147 
   148 (* notes *)
   149 
   150 fun context_notes facts lthy =
   151   let
   152     val facts' = facts
   153       |> Element.export_standard_facts lthy lthy
   154       |> Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of lthy));
   155   in
   156     lthy
   157     |> ProofContext.qualified_names
   158     |> ProofContext.note_thmss_i facts'
   159     ||> ProofContext.restore_naming lthy
   160   end;
   161 
   162 fun theory_notes keep_atts facts lthy = lthy |> LocalTheory.theory (fn thy =>
   163   let
   164     val facts' = facts
   165       |> Element.export_standard_facts lthy (ProofContext.init thy)
   166       |> Attrib.map_facts (if keep_atts then Attrib.attribute_i thy else K I);
   167   in
   168     thy
   169     |> Sign.qualified_names
   170     |> PureThy.note_thmss_i "" facts' |> #2
   171     |> Sign.restore_naming thy
   172   end);
   173 
   174 fun locale_notes loc facts lthy = lthy |> LocalTheory.target (fn ctxt =>
   175   #2 (Locale.add_thmss "" loc (Element.export_standard_facts lthy ctxt facts) ctxt));
   176 
   177 fun notes "" facts = theory_notes true facts #> context_notes facts
   178   | notes loc facts = theory_notes false facts #> locale_notes loc facts #> context_notes facts;
   179 
   180 
   181 (* declarations *)
   182 
   183 fun term_syntax "" f = LocalTheory.theory (Context.theory_map f)
   184   | term_syntax loc f = LocalTheory.target (Locale.add_term_syntax loc (Context.proof_map f));
   185 
   186 fun declaration "" f = LocalTheory.theory (Context.theory_map f)
   187   | declaration loc f = I;    (* FIXME *)
   188 
   189 
   190 
   191 (* init and exit *)
   192 
   193 fun begin loc =
   194   LocalTheory.init (SOME (NameSpace.base loc))
   195    {pretty = pretty loc,
   196     consts = consts loc,
   197     axioms = axioms,
   198     defs = defs,
   199     notes = notes loc,
   200     term_syntax = term_syntax loc,
   201     declaration = declaration loc,
   202     exit = I};
   203 
   204 fun init_i NONE thy = begin "" (ProofContext.init thy)
   205   | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
   206 
   207 fun init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
   208 
   209 val exit = LocalTheory.exit #> (fn lthy => (lthy, ProofContext.theory_of lthy));
   210 
   211 fun mapping loc f = init loc #> f #> exit;
   212 
   213 end;