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