src/Pure/Isar/theory_target.ML
author wenzelm
Sun Apr 15 14:31:49 2007 +0200 (2007-04-15)
changeset 22692 1e057a3f087d
parent 22673 4e2aa12af7ed
child 22846 fb79144af9a3
permissions -rw-r--r--
proper ProofContext.infer_types;
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    ID:         $Id$
wenzelm@20894
     3
    Author:     Makarius
wenzelm@20894
     4
wenzelm@21594
     5
Common theory/locale targets.
wenzelm@20894
     6
*)
wenzelm@20894
     7
wenzelm@20894
     8
signature THEORY_TARGET =
wenzelm@20894
     9
sig
wenzelm@21006
    10
  val peek: local_theory -> string option
haftmann@22353
    11
  val begin: bstring -> Proof.context -> local_theory
wenzelm@20894
    12
  val init: xstring option -> theory -> local_theory
wenzelm@20894
    13
  val init_i: string option -> theory -> local_theory
wenzelm@20894
    14
end;
wenzelm@20894
    15
wenzelm@20894
    16
structure TheoryTarget: THEORY_TARGET =
wenzelm@20894
    17
struct
wenzelm@20894
    18
wenzelm@20894
    19
(** locale targets **)
wenzelm@20894
    20
wenzelm@21006
    21
(* context data *)
wenzelm@21006
    22
wenzelm@21006
    23
structure Data = ProofDataFun
wenzelm@21006
    24
(
wenzelm@21006
    25
  val name = "Isar/theory_target";
wenzelm@21006
    26
  type T = string option;
wenzelm@21006
    27
  fun init _ = NONE;
wenzelm@21006
    28
  fun print _ _ = ();
wenzelm@21006
    29
);
wenzelm@21006
    30
wenzelm@21006
    31
val _ = Context.add_setup Data.init;
wenzelm@21006
    32
val peek = Data.get;
wenzelm@21006
    33
wenzelm@21006
    34
wenzelm@20894
    35
(* pretty *)
wenzelm@20894
    36
wenzelm@20894
    37
fun pretty loc ctxt =
wenzelm@20894
    38
  let
wenzelm@20894
    39
    val thy = ProofContext.theory_of ctxt;
wenzelm@20894
    40
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@21585
    41
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
wenzelm@20894
    42
    val elems =
wenzelm@20894
    43
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    44
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    45
  in
wenzelm@20984
    46
    if loc = "" then
wenzelm@20984
    47
      [Pretty.block
wenzelm@20984
    48
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@20984
    49
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
wenzelm@20984
    50
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
wenzelm@20984
    51
    else
wenzelm@20984
    52
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
wenzelm@20984
    53
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    54
  end;
wenzelm@20894
    55
wenzelm@20894
    56
wenzelm@21804
    57
(* consts *)
wenzelm@21804
    58
wenzelm@21825
    59
fun internal_abbrev prmode ((c, mx), t) =
wenzelm@22380
    60
  (* FIXME really permissive *)
wenzelm@22380
    61
  LocalTheory.term_syntax (perhaps o try o ProofContext.target_abbrev prmode ((c, mx), t)) #>
wenzelm@21825
    62
  ProofContext.add_abbrev Syntax.internalM (c, t) #> snd;
wenzelm@21804
    63
haftmann@22353
    64
fun consts is_loc some_class depends decls lthy =
wenzelm@21804
    65
  let
wenzelm@21804
    66
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@21747
    67
wenzelm@21804
    68
    fun const ((c, T), mx) thy =
wenzelm@21804
    69
      let
wenzelm@21804
    70
        val U = map #2 xs ---> T;
wenzelm@21804
    71
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
haftmann@22353
    72
        val (mx1, mx2) = ClassPackage.fork_mixfix is_loc some_class mx;
wenzelm@21804
    73
        val thy' = Sign.add_consts_authentic [(c, U, mx1)] thy;
wenzelm@21804
    74
      in (((c, mx2), t), thy') end;
wenzelm@21804
    75
wenzelm@21804
    76
    val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls);
wenzelm@21804
    77
    val defs = map (apsnd (pair ("", []))) abbrs;
wenzelm@21804
    78
  in
wenzelm@21804
    79
    lthy'
wenzelm@21825
    80
    |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
wenzelm@21804
    81
    |> LocalDefs.add_defs defs |>> map (apsnd snd)
wenzelm@21804
    82
  end;
wenzelm@21804
    83
wenzelm@21804
    84
wenzelm@21804
    85
(* abbrev *)
wenzelm@21747
    86
wenzelm@21747
    87
fun occ_params ctxt ts =
wenzelm@21747
    88
  #1 (ProofContext.inferred_fixes ctxt)
wenzelm@21747
    89
  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
wenzelm@21747
    90
haftmann@22353
    91
fun abbrev is_loc some_class  prmode ((raw_c, mx), raw_t) lthy =
wenzelm@21747
    92
  let
wenzelm@21747
    93
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@21747
    94
    val target = LocalTheory.target_of lthy;
wenzelm@21747
    95
    val target_morphism = LocalTheory.target_morphism lthy;
wenzelm@21747
    96
wenzelm@21747
    97
    val c = Morphism.name target_morphism raw_c;
wenzelm@21747
    98
    val t = Morphism.term target_morphism raw_t;
wenzelm@21747
    99
    val xs = map Free (occ_params target [t]);
wenzelm@21747
   100
    val u = fold_rev Term.lambda xs t;
wenzelm@21804
   101
    val U = Term.fastype_of u;
wenzelm@21747
   102
    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
wenzelm@21747
   103
wenzelm@21804
   104
    val ((lhs as Const (full_c, _), rhs), lthy1) = lthy
wenzelm@21747
   105
      |> LocalTheory.theory_result (Sign.add_abbrev (#1 prmode) (c, u'));
haftmann@22353
   106
    val (mx1, mx2) = ClassPackage.fork_mixfix is_loc some_class mx;
wenzelm@21747
   107
  in
wenzelm@21747
   108
    lthy1
wenzelm@21804
   109
    |> LocalTheory.theory (Sign.add_notation prmode [(lhs, mx1)])
wenzelm@21825
   110
    |> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
wenzelm@21804
   111
    |> ProofContext.local_abbrev (c, rhs)
wenzelm@21747
   112
  end;
wenzelm@21747
   113
wenzelm@21747
   114
wenzelm@20894
   115
(* defs *)
wenzelm@20894
   116
wenzelm@21845
   117
local
wenzelm@21570
   118
wenzelm@21594
   119
fun expand_term ctxt t =
wenzelm@21570
   120
  let
wenzelm@21570
   121
    val thy = ProofContext.theory_of ctxt;
wenzelm@21570
   122
    val thy_ctxt = ProofContext.init thy;
wenzelm@21570
   123
    val ct = Thm.cterm_of thy t;
wenzelm@21594
   124
    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
wenzelm@21708
   125
  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
wenzelm@21570
   126
wenzelm@20894
   127
fun add_def (name, prop) =
wenzelm@20894
   128
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
   129
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   130
wenzelm@20894
   131
in
wenzelm@20894
   132
haftmann@22353
   133
fun defs loc some_class kind args lthy0 =
wenzelm@20894
   134
  let
wenzelm@21533
   135
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   136
      let
wenzelm@21594
   137
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
wenzelm@21570
   138
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
wenzelm@20894
   139
wenzelm@21570
   140
        val ([(lhs, lhs_def)], lthy2) = lthy1
wenzelm@21570
   141
          |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)];
wenzelm@21570
   142
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
wenzelm@21533
   143
wenzelm@21570
   144
        val name' = Thm.def_name_optional c name;
wenzelm@21570
   145
        val (def, lthy3) = lthy2
wenzelm@20894
   146
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@21845
   147
        val eq = LocalDefs.trans_terms lthy3
wenzelm@21845
   148
          [(*c == loc.c xs*) lhs_def,
wenzelm@21845
   149
           (*lhs' == rhs'*)  def,
wenzelm@21845
   150
           (*rhs' == rhs*)   Thm.symmetric rhs_conv];
haftmann@22353
   151
        val lthy4 = case some_class
haftmann@22353
   152
         of SOME class => 
haftmann@22424
   153
              lthy3
haftmann@22424
   154
              |> LocalTheory.raw_theory
haftmann@22353
   155
                (ClassPackage.add_def_in_class lthy3 class
haftmann@22424
   156
                  ((c, mx), ((name, atts), (rhs, eq))))
haftmann@22353
   157
          | _ => lthy3;
haftmann@22301
   158
      in ((lhs, ((name', atts), [([eq], [])])), lthy4) end;
wenzelm@20894
   159
wenzelm@21570
   160
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
wenzelm@21433
   161
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
haftmann@22424
   162
wenzelm@20894
   163
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
wenzelm@20894
   164
wenzelm@20894
   165
end;
wenzelm@20894
   166
wenzelm@20894
   167
wenzelm@21611
   168
(* axioms *)
wenzelm@21611
   169
wenzelm@21611
   170
local
wenzelm@21611
   171
wenzelm@21611
   172
fun add_axiom hyps (name, prop) thy =
wenzelm@21611
   173
  let
wenzelm@21611
   174
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@21611
   175
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@21611
   176
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@21611
   177
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@21611
   178
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@21611
   179
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@21611
   180
wenzelm@21611
   181
in
wenzelm@21611
   182
wenzelm@21611
   183
fun axioms kind specs lthy =
wenzelm@21611
   184
  let
wenzelm@21611
   185
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
wenzelm@21611
   186
    fun axiom ((name, atts), props) thy = thy
wenzelm@21611
   187
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@21611
   188
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@21611
   189
  in
wenzelm@21611
   190
    lthy
wenzelm@21611
   191
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@21611
   192
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@21611
   193
    |-> LocalTheory.notes kind
wenzelm@21611
   194
  end;
wenzelm@21611
   195
wenzelm@21611
   196
end;
wenzelm@21611
   197
wenzelm@21611
   198
wenzelm@20894
   199
(* notes *)
wenzelm@20894
   200
wenzelm@21611
   201
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   202
  let
wenzelm@21611
   203
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
   204
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
   205
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   206
    val cert = Thm.cterm_of thy;
wenzelm@21594
   207
wenzelm@21611
   208
    (*export assumes/defines*)
wenzelm@21611
   209
    val th = Goal.norm_result raw_th;
wenzelm@21611
   210
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
wenzelm@21708
   211
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
wenzelm@21708
   212
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
wenzelm@21611
   213
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   214
wenzelm@21611
   215
    (*export fixes*)
wenzelm@22692
   216
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
wenzelm@22692
   217
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
wenzelm@21611
   218
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   219
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   220
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   221
wenzelm@21611
   222
    (*thm definition*)
wenzelm@21644
   223
    val result = th''
wenzelm@21644
   224
      |> PureThy.name_thm true true ""
wenzelm@21644
   225
      |> Goal.close_result
wenzelm@21644
   226
      |> PureThy.name_thm true true name;
wenzelm@21611
   227
wenzelm@21611
   228
    (*import fixes*)
wenzelm@21611
   229
    val (tvars, vars) =
wenzelm@21611
   230
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   231
      |>> map Logic.dest_type;
wenzelm@21611
   232
wenzelm@21611
   233
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   234
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   235
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   236
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   237
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   238
wenzelm@21611
   239
    (*import assumes/defines*)
wenzelm@21594
   240
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   241
    val result'' =
wenzelm@21611
   242
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   243
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21845
   244
      | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
wenzelm@21644
   245
      |> Goal.norm_result
wenzelm@21644
   246
      |> PureThy.name_thm false false name;
wenzelm@21611
   247
wenzelm@21611
   248
  in (result'', result) end;
wenzelm@21611
   249
wenzelm@21594
   250
fun notes loc kind facts lthy =
wenzelm@21585
   251
  let
wenzelm@21594
   252
    val is_loc = loc <> "";
wenzelm@21594
   253
    val thy = ProofContext.theory_of lthy;
wenzelm@21615
   254
    val full = LocalTheory.full_name lthy;
wenzelm@21585
   255
wenzelm@21594
   256
    val facts' = facts
wenzelm@21615
   257
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
wenzelm@21615
   258
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   259
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   260
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   261
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   262
      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   263
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@21594
   264
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
wenzelm@21594
   265
  in
wenzelm@21611
   266
    lthy |> LocalTheory.theory
wenzelm@21611
   267
      (Sign.qualified_names
wenzelm@21611
   268
        #> PureThy.note_thmss_i kind global_facts #> snd
wenzelm@21611
   269
        #> Sign.restore_naming thy)
wenzelm@21585
   270
wenzelm@21611
   271
    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
wenzelm@21594
   272
wenzelm@21433
   273
    |> ProofContext.set_stmt true
wenzelm@20894
   274
    |> ProofContext.qualified_names
wenzelm@21611
   275
    |> ProofContext.note_thmss_i kind local_facts
wenzelm@20894
   276
    ||> ProofContext.restore_naming lthy
wenzelm@21433
   277
    ||> ProofContext.restore_stmt lthy
wenzelm@20894
   278
  end;
wenzelm@20894
   279
wenzelm@20894
   280
wenzelm@21533
   281
(* target declarations *)
wenzelm@20894
   282
wenzelm@22673
   283
fun target_decl add loc d lthy =
wenzelm@22673
   284
  let
wenzelm@22673
   285
    val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
wenzelm@22673
   286
    val d0 = Morphism.form d';
wenzelm@22673
   287
  in
wenzelm@22673
   288
    if loc = "" then
wenzelm@22673
   289
      lthy
wenzelm@22673
   290
      |> LocalTheory.theory (Context.theory_map d0)
wenzelm@22673
   291
      |> LocalTheory.target (Context.proof_map d0)
wenzelm@22673
   292
    else
wenzelm@22673
   293
      lthy
wenzelm@22673
   294
      |> LocalTheory.target (add loc d')
wenzelm@22673
   295
  end;
wenzelm@20894
   296
wenzelm@21611
   297
val type_syntax = target_decl Locale.add_type_syntax;
wenzelm@21611
   298
val term_syntax = target_decl Locale.add_term_syntax;
wenzelm@21611
   299
val declaration = target_decl Locale.add_declaration;
wenzelm@20894
   300
wenzelm@21533
   301
fun target_morphism loc lthy =
wenzelm@21594
   302
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21613
   303
  Morphism.thm_morphism Goal.norm_result;
wenzelm@21533
   304
wenzelm@22203
   305
fun target_naming loc lthy =
wenzelm@21668
   306
  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21668
   307
   else ProofContext.naming_of (LocalTheory.target_of lthy))
wenzelm@22203
   308
  |> NameSpace.qualified_names;
wenzelm@21533
   309
wenzelm@20894
   310
wenzelm@20894
   311
(* init and exit *)
wenzelm@20894
   312
haftmann@22353
   313
fun begin loc ctxt =
haftmann@22353
   314
  let
haftmann@22353
   315
    val thy = ProofContext.theory_of ctxt;
haftmann@22353
   316
    val is_loc = loc <> "";
haftmann@22353
   317
    val some_class = ClassPackage.class_of_locale thy loc;
haftmann@22353
   318
  in
haftmann@22353
   319
    ctxt
haftmann@22353
   320
    |> Data.put (if is_loc then SOME loc else NONE)
haftmann@22353
   321
    |> LocalTheory.init (NameSpace.base loc)
wenzelm@21747
   322
     {pretty = pretty loc,
haftmann@22353
   323
      consts = consts is_loc some_class,
wenzelm@21747
   324
      axioms = axioms,
haftmann@22353
   325
      abbrev = abbrev is_loc some_class,
haftmann@22353
   326
      defs = defs loc some_class,
wenzelm@21747
   327
      notes = notes loc,
wenzelm@21747
   328
      type_syntax = type_syntax loc,
wenzelm@21747
   329
      term_syntax = term_syntax loc,
wenzelm@21747
   330
      declaration = declaration loc,
wenzelm@21747
   331
      target_morphism = target_morphism loc,
wenzelm@22203
   332
      target_naming = target_naming loc,
wenzelm@21804
   333
      reinit = fn _ =>
haftmann@22353
   334
        begin loc o (if is_loc then Locale.init loc else ProofContext.init),
wenzelm@21747
   335
      exit = LocalTheory.target_of}
wenzelm@21747
   336
  end;
wenzelm@20894
   337
haftmann@22353
   338
fun init_i NONE thy = begin "" (ProofContext.init thy)
haftmann@22353
   339
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   340
wenzelm@21276
   341
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   342
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   343
wenzelm@20894
   344
end;