src/HOL/Tools/transfer.ML
author huffman
Fri Mar 30 12:32:35 2012 +0200 (2012-03-30)
changeset 47220 52426c62b5d0
parent 46497 89ccf66aa73d
permissions -rw-r--r--
replace lemmas eval_nat_numeral with a simpler reformulation
haftmann@37744
     1
(*  Title:      HOL/Tools/transfer.ML
haftmann@37744
     2
    Author:     Amine Chaieb, University of Cambridge, 2009
haftmann@37744
     3
                Jeremy Avigad, Carnegie Mellon University
haftmann@37744
     4
                Florian Haftmann, TU Muenchen
haftmann@35648
     5
haftmann@35648
     6
Simple transfer principle on theorems.
huffman@31706
     7
*)
huffman@31706
     8
haftmann@32557
     9
signature TRANSFER =
huffman@31706
    10
sig
haftmann@35648
    11
  datatype selection = Direction of term * term | Hints of string list | Prop
haftmann@35674
    12
  val transfer: Context.generic -> selection -> string list -> thm -> thm list
huffman@31706
    13
  type entry
haftmann@35676
    14
  val add: thm -> bool -> entry -> Context.generic -> Context.generic
haftmann@35676
    15
  val del: thm -> entry -> Context.generic -> Context.generic
haftmann@35676
    16
  val drop: thm -> Context.generic -> Context.generic
huffman@31706
    17
  val setup: theory -> theory
huffman@31706
    18
end;
huffman@31706
    19
haftmann@32557
    20
structure Transfer : TRANSFER =
huffman@31706
    21
struct
haftmann@32557
    22
haftmann@35638
    23
(* data administration *)
haftmann@35638
    24
haftmann@35647
    25
val direction_of = Thm.dest_binop o Thm.dest_arg o cprop_of;
haftmann@35647
    26
haftmann@35821
    27
val transfer_morphism_key = Drule.strip_imp_concl (Thm.cprop_of @{thm transfer_morphismI});
haftmann@35821
    28
haftmann@35645
    29
fun check_morphism_key ctxt key =
haftmann@35645
    30
  let
haftmann@35821
    31
    val _ = Thm.match (transfer_morphism_key, Thm.cprop_of key)
haftmann@35821
    32
      handle Pattern.MATCH => error ("Transfer: expected theorem of the form "
haftmann@35821
    33
        ^ quote (Syntax.string_of_term ctxt (Thm.term_of transfer_morphism_key)));
haftmann@35647
    34
  in direction_of key end;
haftmann@35638
    35
haftmann@35674
    36
type entry = { inj : thm list, embed : thm list, return : thm list, cong : thm list,
haftmann@35675
    37
  hints : string list };
haftmann@33321
    38
haftmann@35676
    39
val empty_entry = { inj = [], embed = [], return = [], cong = [], hints = [] };
haftmann@35675
    40
fun merge_entry ({ inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 } : entry,
haftmann@35675
    41
  { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } : entry) =
haftmann@35674
    42
    { inj = merge Thm.eq_thm (inj1, inj2), embed = merge Thm.eq_thm (embed1, embed2),
haftmann@35674
    43
      return = merge Thm.eq_thm (return1, return2), cong = merge Thm.eq_thm (cong1, cong2),
haftmann@35675
    44
      hints = merge (op =) (hints1, hints2) };
haftmann@33321
    45
wenzelm@33519
    46
structure Data = Generic_Data
huffman@31706
    47
(
haftmann@35638
    48
  type T = (thm * entry) list;
haftmann@35638
    49
  val empty = [];
haftmann@35647
    50
  val extend = I;
haftmann@35638
    51
  val merge = AList.join Thm.eq_thm (K merge_entry);
huffman@31706
    52
);
huffman@31706
    53
haftmann@35647
    54
haftmann@35647
    55
(* data lookup *)
haftmann@35647
    56
haftmann@35708
    57
fun transfer_rules_of ({ inj, embed, return, cong, ... } : entry) =
haftmann@35675
    58
  (inj, embed, return, cong);
haftmann@35675
    59
haftmann@35647
    60
fun get_by_direction context (a, D) =
haftmann@35647
    61
  let
haftmann@35647
    62
    val ctxt = Context.proof_of context;
haftmann@35647
    63
    val certify = Thm.cterm_of (Context.theory_of context);
haftmann@35647
    64
    val a0 = certify a;
haftmann@35647
    65
    val D0 = certify D;
haftmann@35647
    66
    fun eq_direction ((a, D), thm') =
haftmann@35647
    67
      let
haftmann@35647
    68
        val (a', D') = direction_of thm';
haftmann@35674
    69
      in a aconvc a' andalso D aconvc D' end;
haftmann@35674
    70
  in case AList.lookup eq_direction (Data.get context) (a0, D0) of
haftmann@35675
    71
      SOME e => ((a0, D0), transfer_rules_of e)
haftmann@35647
    72
    | NONE => error ("Transfer: no such instance: ("
haftmann@35647
    73
        ^ Syntax.string_of_term ctxt a ^ ", " ^ Syntax.string_of_term ctxt D ^ ")")
haftmann@35647
    74
  end;
huffman@31706
    75
haftmann@35647
    76
fun get_by_hints context hints =
haftmann@35647
    77
  let
haftmann@35647
    78
    val insts = map_filter (fn (k, e) => if exists (member (op =) (#hints e)) hints
haftmann@35675
    79
      then SOME (direction_of k, transfer_rules_of e) else NONE) (Data.get context);
wenzelm@45430
    80
    val _ = if null insts then error ("Transfer: no such labels: " ^ commas_quote hints) else ();
haftmann@35647
    81
  in insts end;
haftmann@35647
    82
haftmann@35647
    83
fun splits P [] = []
haftmann@35647
    84
  | splits P (xs as (x :: _)) =
haftmann@35647
    85
      let
haftmann@35647
    86
        val (pss, qss) = List.partition (P x) xs;
haftmann@35647
    87
      in if null pss then [qss] else if null qss then [pss] else pss :: splits P qss end;
huffman@31706
    88
haftmann@35647
    89
fun get_by_prop context t =
haftmann@35647
    90
  let
haftmann@35647
    91
    val tys = map snd (Term.add_vars t []);
haftmann@35647
    92
    val _ = if null tys then error "Transfer: unable to guess instance" else ();
haftmann@35647
    93
    val tyss = splits (curry Type.could_unify) tys;
haftmann@35647
    94
    val get_ty = typ_of o ctyp_of_term o fst o direction_of;
haftmann@35675
    95
    val insts = map_filter (fn tys => get_first (fn (k, e) =>
haftmann@35647
    96
      if Type.could_unify (hd tys, range_type (get_ty k))
haftmann@35675
    97
      then SOME (direction_of k, transfer_rules_of e)
haftmann@35647
    98
      else NONE) (Data.get context)) tyss;
haftmann@35647
    99
    val _ = if null insts then
haftmann@35647
   100
      error "Transfer: no instances, provide direction or hints explicitly" else ();
haftmann@35647
   101
  in insts end;
huffman@31706
   102
huffman@31706
   103
haftmann@35638
   104
(* applying transfer data *)
huffman@31706
   105
haftmann@35684
   106
fun transfer_thm ((raw_a, raw_D), (inj, embed, return, cong)) leave ctxt1 thm =
haftmann@35645
   107
  let
haftmann@35684
   108
    (* identify morphism function *)
haftmann@35684
   109
    val ([a, D], ctxt2) = ctxt1
haftmann@35684
   110
      |> Variable.import true (map Drule.mk_term [raw_a, raw_D])
haftmann@35684
   111
      |>> map Drule.dest_term o snd;
wenzelm@46497
   112
    val transform = Thm.apply @{cterm "Trueprop"} o Thm.apply D;
haftmann@35684
   113
    val T = Thm.typ_of (Thm.ctyp_of_term a);
haftmann@35684
   114
    val (aT, bT) = (Term.range_type T, Term.domain_type T);
haftmann@35684
   115
    
haftmann@35684
   116
    (* determine variables to transfer *)
haftmann@35684
   117
    val ctxt3 = ctxt2
haftmann@35684
   118
      |> Variable.declare_thm thm
haftmann@35684
   119
      |> Variable.declare_term (term_of a)
haftmann@35684
   120
      |> Variable.declare_term (term_of D);
wenzelm@42361
   121
    val certify = Thm.cterm_of (Proof_Context.theory_of ctxt3);
haftmann@35684
   122
    val vars = filter (fn ((v, _), T) => Type.could_unify (T, aT) andalso
haftmann@35684
   123
      not (member (op =) leave v)) (Term.add_vars (Thm.prop_of thm) []);
haftmann@35684
   124
    val c_vars = map (certify o Var) vars;
haftmann@35684
   125
    val (vs', ctxt4) = Variable.variant_fixes (map (fst o fst) vars) ctxt3;
haftmann@35684
   126
    val c_vars' = map (certify o (fn v => Free (v, bT))) vs';
wenzelm@46497
   127
    val c_exprs' = map (Thm.apply a) c_vars';
huffman@31706
   128
haftmann@35684
   129
    (* transfer *)
haftmann@35684
   130
    val (hyps, ctxt5) = ctxt4
haftmann@35684
   131
      |> Assumption.add_assumes (map transform c_vars');
wenzelm@45620
   132
    val simpset =
wenzelm@45620
   133
      Simplifier.context ctxt5 HOL_ss addsimps (inj @ embed @ return)
wenzelm@45620
   134
      |> fold Simplifier.add_cong cong;
haftmann@35684
   135
    val thm' = thm
haftmann@35684
   136
      |> Drule.cterm_instantiate (c_vars ~~ c_exprs')
haftmann@35684
   137
      |> fold_rev Thm.implies_intr (map cprop_of hyps)
haftmann@35684
   138
      |> Simplifier.asm_full_simplify simpset
haftmann@35684
   139
  in singleton (Variable.export ctxt5 ctxt1) thm' end;
haftmann@35684
   140
haftmann@35684
   141
fun transfer_thm_multiple insts leave ctxt thm =
haftmann@35684
   142
  map (fn inst => transfer_thm inst leave ctxt thm) insts;
huffman@31706
   143
haftmann@35648
   144
datatype selection = Direction of term * term | Hints of string list | Prop;
haftmann@35647
   145
haftmann@35648
   146
fun insts_for context thm (Direction direction) = [get_by_direction context direction]
haftmann@35648
   147
  | insts_for context thm (Hints hints) = get_by_hints context hints
haftmann@35648
   148
  | insts_for context thm Prop = get_by_prop context (Thm.prop_of thm);
huffman@31706
   149
haftmann@35648
   150
fun transfer context selection leave thm =
haftmann@35684
   151
  transfer_thm_multiple (insts_for context thm selection) leave (Context.proof_of context) thm;
huffman@31706
   152
huffman@31706
   153
haftmann@35647
   154
(* maintaining transfer data *)
huffman@31706
   155
haftmann@35676
   156
fun extend_entry ctxt (a, D) guess
haftmann@35676
   157
    { inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 }
haftmann@35676
   158
    { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
haftmann@35638
   159
  let
haftmann@35676
   160
    fun add_del eq del add = union eq add #> subtract eq del;
haftmann@35676
   161
    val guessed = if guess
haftmann@35684
   162
      then map (fn thm => transfer_thm
haftmann@35676
   163
        ((a, D), (if null inj1 then inj2 else inj1, [], [], cong1)) [] ctxt thm RS sym) embed1
haftmann@35676
   164
      else [];
haftmann@35638
   165
  in
haftmann@35676
   166
    { inj = union Thm.eq_thm inj1 inj2, embed = union Thm.eq_thm embed1 embed2,
haftmann@35676
   167
      return = union Thm.eq_thm guessed (union Thm.eq_thm return1 return2),
haftmann@35676
   168
      cong = union Thm.eq_thm cong1 cong2, hints = union (op =) hints1 hints2 }
haftmann@35638
   169
  end;
huffman@31706
   170
haftmann@35676
   171
fun diminish_entry 
haftmann@35676
   172
    { inj = inj0, embed = embed0, return = return0, cong = cong0, hints = hints0 }
haftmann@35676
   173
    { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
haftmann@35676
   174
  { inj = subtract Thm.eq_thm inj0 inj2, embed = subtract Thm.eq_thm embed0 embed2,
haftmann@35676
   175
    return = subtract Thm.eq_thm return0 return2, cong = subtract Thm.eq_thm cong0 cong2,
haftmann@35676
   176
    hints = subtract (op =) hints0 hints2 };
haftmann@35638
   177
haftmann@35676
   178
fun add key guess entry context =
haftmann@35676
   179
  let
haftmann@35676
   180
    val ctxt = Context.proof_of context;
haftmann@35676
   181
    val a_D = check_morphism_key ctxt key;
haftmann@35676
   182
  in
haftmann@35676
   183
    context
haftmann@35676
   184
    |> Data.map (AList.map_default Thm.eq_thm
haftmann@35676
   185
         (key, empty_entry) (extend_entry ctxt a_D guess entry))
haftmann@35676
   186
  end;
haftmann@35676
   187
haftmann@35676
   188
fun del key entry = Data.map (AList.map_entry Thm.eq_thm key (diminish_entry entry));
haftmann@35676
   189
haftmann@35676
   190
fun drop key = Data.map (AList.delete Thm.eq_thm key);
haftmann@35647
   191
huffman@31706
   192
haftmann@35638
   193
(* syntax *)
huffman@31706
   194
huffman@31706
   195
local
huffman@31706
   196
haftmann@35645
   197
fun these scan = Scan.optional scan [];
haftmann@35645
   198
fun these_pair scan = Scan.optional scan ([], []);
haftmann@35645
   199
haftmann@35645
   200
fun keyword k = Scan.lift (Args.$$$ k) >> K ();
haftmann@35645
   201
fun keyword_colon k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
huffman@31706
   202
haftmann@35645
   203
val addN = "add";
haftmann@35645
   204
val delN = "del";
haftmann@35684
   205
val keyN = "key";
haftmann@35645
   206
val modeN = "mode";
haftmann@35645
   207
val automaticN = "automatic";
haftmann@35645
   208
val manualN = "manual";
haftmann@35674
   209
val injN = "inj";
haftmann@35674
   210
val embedN = "embed";
haftmann@35674
   211
val returnN = "return";
haftmann@35674
   212
val congN = "cong";
haftmann@35645
   213
val labelsN = "labels";
huffman@31706
   214
haftmann@35674
   215
val leavingN = "leaving";
haftmann@35674
   216
val directionN = "direction";
haftmann@35674
   217
haftmann@35684
   218
val any_keyword = keyword_colon addN || keyword_colon delN || keyword_colon keyN
haftmann@35684
   219
  || keyword_colon modeN || keyword_colon injN || keyword_colon embedN || keyword_colon returnN
haftmann@35674
   220
  || keyword_colon congN || keyword_colon labelsN
haftmann@35674
   221
  || keyword_colon leavingN || keyword_colon directionN;
haftmann@35645
   222
haftmann@35645
   223
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
haftmann@35645
   224
val names = Scan.repeat (Scan.unless any_keyword (Scan.lift Args.name))
huffman@31706
   225
haftmann@35645
   226
val mode = keyword_colon modeN |-- ((Scan.lift (Args.$$$ manualN) >> K false)
haftmann@35645
   227
  || (Scan.lift (Args.$$$ automaticN) >> K true));
haftmann@35645
   228
val inj = (keyword_colon injN |-- thms) -- these (keyword_colon delN |-- thms);
haftmann@35645
   229
val embed = (keyword_colon embedN |-- thms) -- these (keyword_colon delN |-- thms);
haftmann@35645
   230
val return = (keyword_colon returnN |-- thms) -- these (keyword_colon delN |-- thms);
haftmann@35645
   231
val cong = (keyword_colon congN |-- thms) -- these (keyword_colon delN |-- thms);
haftmann@35645
   232
val labels = (keyword_colon labelsN |-- names) -- these (keyword_colon delN |-- names);
huffman@31706
   233
haftmann@35675
   234
val entry_pair = these_pair inj -- these_pair embed
haftmann@35648
   235
  -- these_pair return -- these_pair cong -- these_pair labels
haftmann@35675
   236
  >> (fn (((((inja, injd), (embeda, embedd)), (returna, returnd)), (conga, congd)),
haftmann@35674
   237
       (hintsa, hintsd)) =>
haftmann@35684
   238
      ({ inj = inja, embed = embeda, return = returna, cong = conga, hints = hintsa },
haftmann@35684
   239
        { inj = injd, embed = embedd, return = returnd, cong = congd, hints = hintsd }));
haftmann@35645
   240
haftmann@35648
   241
val selection = (keyword_colon directionN |-- (Args.term -- Args.term) >> Direction)
haftmann@35648
   242
  || these names >> (fn hints => if null hints then Prop else Hints hints);
haftmann@35645
   243
huffman@31706
   244
in
huffman@31706
   245
haftmann@35684
   246
val transfer_attribute = keyword delN >> K (Thm.declaration_attribute drop)
haftmann@35684
   247
  || keyword addN |-- Scan.optional mode true -- entry_pair
haftmann@35676
   248
    >> (fn (guess, (entry_add, entry_del)) => Thm.declaration_attribute
haftmann@35684
   249
      (fn thm => add thm guess entry_add #> del thm entry_del))
haftmann@35684
   250
  || keyword_colon keyN |-- Attrib.thm
haftmann@35684
   251
    >> (fn key => Thm.declaration_attribute
haftmann@35684
   252
      (fn thm => add key false
haftmann@35684
   253
        { inj = [], embed = [], return = [thm], cong = [], hints = [] }));
huffman@31706
   254
haftmann@35648
   255
val transferred_attribute = selection -- these (keyword_colon leavingN |-- names)
haftmann@35674
   256
  >> (fn (selection, leave) => Thm.rule_attribute (fn context =>
haftmann@35674
   257
      Conjunction.intr_balanced o transfer context selection leave));
huffman@31706
   258
huffman@31706
   259
end;
huffman@31706
   260
huffman@31706
   261
huffman@31706
   262
(* theory setup *)
huffman@31706
   263
huffman@31706
   264
val setup =
haftmann@35648
   265
  Attrib.setup @{binding transfer} transfer_attribute
huffman@31706
   266
    "Installs transfer data" #>
haftmann@35648
   267
  Attrib.setup @{binding transferred} transferred_attribute
huffman@31706
   268
    "Transfers theorems";
huffman@31706
   269
huffman@31706
   270
end;