src/HOL/Tools/transfer_data.ML
author haftmann
Mon, 24 Aug 2009 08:31:41 +0200
changeset 32475 0d7e8d858b44
parent 31794 71af1fd6a5e4
permissions -rw-r--r--
avoid long line
huffman@31704
     1
(*  Title:      Tools/transfer.ML
huffman@31704
     2
    Author:     Amine Chaieb, University of Cambridge, 2009
huffman@31704
     3
                Jeremy Avigad, Carnegie Mellon University
huffman@31704
     4
*)
huffman@31704
     5
huffman@31704
     6
signature TRANSFER_DATA =
huffman@31704
     7
sig
huffman@31704
     8
  type data
huffman@31704
     9
  type entry
huffman@31704
    10
  val get: Proof.context -> data
huffman@31704
    11
  val del: attribute
huffman@31704
    12
  val add: attribute 
huffman@31704
    13
  val setup: theory -> theory
huffman@31704
    14
end;
huffman@31704
    15
huffman@31704
    16
structure TransferData (* : TRANSFER_DATA*) =
huffman@31704
    17
struct
huffman@31704
    18
type entry = {inj : thm list , emb : thm list , ret : thm list , cong : thm list, guess : bool, hints : string list}; 
huffman@31704
    19
type data = simpset * (thm * entry) list;
huffman@31704
    20
huffman@31704
    21
val eq_key = Thm.eq_thm;
huffman@31704
    22
fun eq_data arg = eq_fst eq_key arg;
huffman@31704
    23
huffman@31704
    24
structure Data = GenericDataFun
huffman@31704
    25
(
huffman@31704
    26
  type T = data;
huffman@31704
    27
  val empty = (HOL_ss, []);
huffman@31704
    28
  val extend  = I;
huffman@31704
    29
  fun merge _ ((ss, e), (ss', e')) =
huffman@31704
    30
    (merge_ss (ss, ss'), AList.merge eq_key (K true) (e, e'));
huffman@31704
    31
);
huffman@31704
    32
huffman@31704
    33
val get = Data.get o Context.Proof;
huffman@31704
    34
huffman@31704
    35
fun del_data key = apsnd (remove eq_data (key, []));
huffman@31704
    36
huffman@31704
    37
val del = Thm.declaration_attribute (Data.map o del_data);
huffman@31704
    38
val add_ss = Thm.declaration_attribute 
huffman@31704
    39
   (fn th => Data.map (fn (ss,data) => (ss addsimps [th], data)));
huffman@31704
    40
huffman@31704
    41
val del_ss = Thm.declaration_attribute 
huffman@31704
    42
   (fn th => Data.map (fn (ss,data) => (ss delsimps [th], data)));
huffman@31704
    43
huffman@31704
    44
val transM_pat = (Thm.dest_arg1 o Thm.dest_arg o cprop_of) @{thm TransferMorphism_def};
huffman@31704
    45
huffman@31704
    46
fun merge_update eq m (k,v) [] = [(k,v)]
huffman@31704
    47
  | merge_update eq m (k,v) ((k',v')::al) = 
huffman@31704
    48
           if eq (k,k') then (k',m (v,v')):: al else (k',v') :: merge_update eq m (k,v) al
huffman@31704
    49
huffman@31704
    50
fun C f x y = f y x
huffman@31704
    51
huffman@31704
    52
fun simpset_of_entry injonly {inj = inj, emb = emb, ret = ret, cong = cg, guess = g, hints = hints} = 
huffman@31704
    53
 HOL_ss addsimps inj addsimps (if injonly then [] else emb@ret) addcongs cg;
huffman@31704
    54
huffman@31704
    55
fun basic_transfer_rule injonly a0 D0 e leave ctxt0 th = 
huffman@31704
    56
 let 
wenzelm@31794
    57
  val ([a,D], ctxt) = apfst (map Drule.dest_term o snd) (Variable.import true (map Drule.mk_term [a0, D0]) ctxt0)
huffman@31704
    58
  val (aT,bT) = 
huffman@31704
    59
     let val T = typ_of (ctyp_of_term a) 
huffman@31704
    60
     in (Term.range_type T, Term.domain_type T)
huffman@31704
    61
     end
huffman@31704
    62
  val ctxt' = (Variable.declare_term (term_of a) o Variable.declare_term (term_of D) o Variable.declare_thm th) ctxt
huffman@31704
    63
  val ns = filter (fn i => Type.could_unify (snd i, aT) andalso not (fst (fst i) mem_string leave)) (Term.add_vars (prop_of th) [])
huffman@31704
    64
  val (ins, ctxt'') = Variable.variant_fixes (map (fst o fst) ns) ctxt'
huffman@31704
    65
  val cns = map ((cterm_of o ProofContext.theory_of) ctxt'' o Var) ns
huffman@31704
    66
  val cfis = map ((cterm_of o ProofContext.theory_of) ctxt'' o (fn n => Free (n, bT))) ins
huffman@31704
    67
  val cis = map (Thm.capply a) cfis
huffman@31704
    68
  val (hs,ctxt''') = Assumption.add_assumes (map (fn ct => Thm.capply @{cterm "Trueprop"} (Thm.capply D ct)) cfis) ctxt''
huffman@31704
    69
  val th1 = Drule.cterm_instantiate (cns~~ cis) th
huffman@31704
    70
  val th2 = fold (C implies_elim) hs (fold_rev implies_intr (map cprop_of hs) th1)
huffman@31704
    71
  val th3 = Simplifier.asm_full_simplify (Simplifier.context ctxt''' (simpset_of_entry injonly e)) 
huffman@31704
    72
                                         (fold_rev implies_intr (map cprop_of hs) th2)
huffman@31704
    73
in hd (Variable.export ctxt''' ctxt0 [th3]) end;
huffman@31704
    74
huffman@31704
    75
local
huffman@31704
    76
fun transfer_ruleh a D leave ctxt th = 
huffman@31704
    77
 let val (ss,al) = get ctxt
huffman@31704
    78
     val a0 = cterm_of (ProofContext.theory_of ctxt) a
huffman@31704
    79
     val D0 = cterm_of (ProofContext.theory_of ctxt) D
huffman@31704
    80
     fun h (th', e) = let val (a',D') = (Thm.dest_binop o Thm.dest_arg o cprop_of) th' 
huffman@31704
    81
                 in if a0 aconvc a' andalso D0 aconvc D' then SOME e else NONE
huffman@31704
    82
                 end
huffman@31704
    83
 in case get_first h al of
huffman@31704
    84
      SOME e => basic_transfer_rule false a0 D0 e leave ctxt th
huffman@31704
    85
    | NONE => error "Transfer: corresponding instance not found in context-data"
huffman@31704
    86
 end
huffman@31704
    87
in fun transfer_rule (a,D) leave (gctxt,th) = 
huffman@31704
    88
   (gctxt, transfer_ruleh a D leave (Context.proof_of gctxt) th)
huffman@31704
    89
end;
huffman@31704
    90
huffman@31704
    91
fun  splits P [] = []
huffman@31704
    92
   | splits P (xxs as (x::xs)) = 
huffman@31704
    93
    let val pss = filter (P x) xxs
huffman@31704
    94
        val qss = filter_out (P x) xxs
huffman@31704
    95
    in if null pss then [qss] else if null qss then [pss] else pss:: splits P qss
huffman@31704
    96
    end
huffman@31704
    97
huffman@31704
    98
fun all_transfers leave (gctxt,th) = 
huffman@31704
    99
 let 
huffman@31704
   100
  val ctxt = Context.proof_of gctxt
huffman@31704
   101
  val tys = map snd (Term.add_vars (prop_of th) [])
huffman@31704
   102
  val _ = if null tys then error "transfer: Unable to guess instance" else ()
huffman@31704
   103
  val tyss = splits (curry Type.could_unify) tys 
huffman@31704
   104
  val get_ty = typ_of o ctyp_of_term o fst o Thm.dest_binop o Thm.dest_arg o cprop_of
huffman@31704
   105
  val get_aD = Thm.dest_binop o Thm.dest_arg o cprop_of
huffman@31704
   106
  val insts = 
huffman@31704
   107
    map_filter (fn tys => 
huffman@31704
   108
          get_first (fn (k,ss) => if Type.could_unify (hd tys, range_type (get_ty k)) 
huffman@31704
   109
                                  then SOME (get_aD k, ss) 
huffman@31704
   110
                                  else NONE) (snd (get ctxt))) tyss
huffman@31704
   111
  val _ = if null insts then error "Transfer guesser: there were no possible instances, use direction: in order to provide a direction" else ()
huffman@31704
   112
  val ths = map  (fn ((a,D),e) => basic_transfer_rule false a D e leave ctxt th) insts
huffman@31704
   113
  val cth = Conjunction.intr_balanced ths
huffman@31704
   114
 in (gctxt, cth)
huffman@31704
   115
 end;
huffman@31704
   116
huffman@31704
   117
fun transfer_rule_by_hint ls leave (gctxt,th) = 
huffman@31704
   118
 let 
huffman@31704
   119
  val ctxt = Context.proof_of gctxt
huffman@31704
   120
  val get_aD = Thm.dest_binop o Thm.dest_arg o cprop_of
huffman@31704
   121
  val insts = 
huffman@31704
   122
    map_filter (fn (k,e) => if exists (fn l => l mem_string (#hints e)) ls 
huffman@31704
   123
			    then SOME (get_aD k, e) else NONE)
huffman@31704
   124
        (snd (get ctxt))
huffman@31704
   125
  val _ = if null insts then error "Transfer: No labels provided are stored in the context" else ()
huffman@31704
   126
  val ths = map  (fn ((a,D),e) => basic_transfer_rule false a D e leave ctxt th) insts
huffman@31704
   127
  val cth = Conjunction.intr_balanced ths
huffman@31704
   128
 in (gctxt, cth)
huffman@31704
   129
 end;
huffman@31704
   130
huffman@31704
   131
huffman@31704
   132
fun transferred_attribute ls NONE leave = 
huffman@31704
   133
         if null ls then all_transfers leave else transfer_rule_by_hint ls leave
huffman@31704
   134
  | transferred_attribute _ (SOME (a,D)) leave = transfer_rule (a,D) leave
huffman@31704
   135
huffman@31704
   136
                                                    (* Add data to the context *)
huffman@31704
   137
fun gen_merge_entries {inj = inj0, emb = emb0, ret = ret0, cong = cg0, guess = g0, hints = hints0}
huffman@31704
   138
                      ({inj = inj1, emb = emb1, ret = ret1, cong = cg1, guess = g1, hints = hints1}, 
huffman@31704
   139
                       {inj = inj2, emb = emb2, ret = ret2, cong = cg2, guess = g2, hints = hints2})
huffman@31704
   140
 = 
huffman@31704
   141
 let fun h xs0 xs ys = subtract Thm.eq_thm xs0 (merge Thm.eq_thm (xs,ys)) in
huffman@31704
   142
 {inj = h inj0 inj1 inj2, emb = h emb0 emb1 emb2, 
huffman@31704
   143
  ret = h ret0 ret1 ret2, cong = h cg0 cg1 cg2, guess = g1 andalso g2,
huffman@31704
   144
  hints = subtract (op = : string*string -> bool) hints0 
huffman@31704
   145
            (hints1 union_string hints2)}
huffman@31704
   146
 end;
huffman@31704
   147
huffman@31704
   148
local
huffman@31704
   149
 val h = curry (merge Thm.eq_thm)
huffman@31704
   150
in
huffman@31704
   151
fun merge_entries ({inj = inj1, emb = emb1, ret = ret1, cong = cg1, guess = g1, hints = hints1}, 
huffman@31704
   152
                   {inj = inj2, emb = emb2, ret = ret2, cong = cg2, guess = g2, hints = hints2}) = 
huffman@31704
   153
    {inj = h inj1 inj2, emb = h emb1 emb2, ret = h ret1 ret2, cong = h cg1 cg2, guess = g1 andalso g2, hints = hints1 union_string hints2}
huffman@31704
   154
end; 
huffman@31704
   155
huffman@31704
   156
fun add ((inja,injd), (emba,embd), (reta,retd), (cga,cgd), g, (hintsa, hintsd)) =
huffman@31704
   157
  Thm.declaration_attribute (fn key => fn context => context |> Data.map
huffman@31704
   158
   (fn (ss, al) => 
huffman@31704
   159
     let
huffman@31704
   160
      val _ = ((let val _ = Thm.match (transM_pat, (Thm.dest_arg o cprop_of) key) 
huffman@31704
   161
                in 0 end) 
huffman@31704
   162
                handle MATCH => error "Attribute expected Theorem of the form : TransferMorphism A a B b")
huffman@31704
   163
      val e0 = {inj = inja, emb = emba, ret = reta, cong = cga, guess = g, hints = hintsa}
huffman@31704
   164
      val ed = {inj = injd, emb = embd, ret = retd, cong = cgd, guess = g, hints = hintsd}
huffman@31704
   165
      val entry = 
huffman@31704
   166
        if g then 
huffman@31704
   167
         let val (a0,D0) = (Thm.dest_binop o Thm.dest_arg o cprop_of) key
huffman@31704
   168
             val ctxt0 = ProofContext.init (Thm.theory_of_thm key)
huffman@31704
   169
             val inj' = if null inja then #inj (case AList.lookup eq_key al key of SOME e => e | NONE => error "Transfer: can not generate return rules on the fly, either add injectivity axiom or force manual mode with mode: manual") 
huffman@31704
   170
                        else inja
huffman@31704
   171
             val ret' = merge Thm.eq_thm (reta,  map (fn th => basic_transfer_rule true a0 D0 {inj = inj', emb = [], ret = [], cong = cga, guess = g, hints = hintsa} [] ctxt0 th RS sym) emba)
huffman@31704
   172
         in {inj = inja, emb = emba, ret = ret', cong = cga, guess = g, hints = hintsa} end 
huffman@31704
   173
        else e0
huffman@31704
   174
    in (ss, merge_update eq_key (gen_merge_entries ed) (key, entry) al)
huffman@31704
   175
    end));
huffman@31704
   176
huffman@31704
   177
huffman@31704
   178
huffman@31704
   179
(* concrete syntax *)
huffman@31704
   180
huffman@31704
   181
local
huffman@31704
   182
huffman@31704
   183
fun keyword k = Scan.lift (Args.$$$ k) >> K ()
huffman@31704
   184
fun keywordC k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
huffman@31704
   185
huffman@31704
   186
val congN = "cong"
huffman@31704
   187
val injN = "inj"
huffman@31704
   188
val embedN = "embed"
huffman@31704
   189
val returnN = "return"
huffman@31704
   190
val addN = "add"
huffman@31704
   191
val delN = "del"
huffman@31704
   192
val modeN = "mode"
huffman@31704
   193
val automaticN = "automatic"
huffman@31704
   194
val manualN = "manual"
huffman@31704
   195
val directionN = "direction"
huffman@31704
   196
val labelsN = "labels"
huffman@31704
   197
val leavingN = "leaving"
huffman@31704
   198
huffman@31704
   199
val any_keyword = keywordC congN || keywordC injN || keywordC embedN || keywordC returnN || keywordC directionN || keywordC modeN || keywordC delN || keywordC labelsN || keywordC leavingN
huffman@31704
   200
huffman@31704
   201
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat
huffman@31704
   202
val terms = thms >> map Drule.dest_term
huffman@31704
   203
val types = thms >> (Logic.dest_type o HOLogic.dest_Trueprop o prop_of o hd) 
huffman@31704
   204
val name = Scan.lift Args.name
huffman@31704
   205
val names = Scan.repeat (Scan.unless any_keyword name)
huffman@31704
   206
fun optional scan = Scan.optional scan []
huffman@31704
   207
fun optional2 scan = Scan.optional scan ([],[])
huffman@31704
   208
huffman@31704
   209
val mode = keywordC modeN |-- ((Scan.lift (Args.$$$ manualN) >> K false) || (Scan.lift (Args.$$$ automaticN) >> K true))
huffman@31704
   210
val inj = (keywordC injN |-- thms) -- optional (keywordC delN |-- thms)
huffman@31704
   211
val embed = (keywordC embedN |-- thms) -- optional (keywordC delN |-- thms)
huffman@31704
   212
val return = (keywordC returnN |-- thms) -- optional (keywordC delN |-- thms)
huffman@31704
   213
val cong = (keywordC congN |-- thms) -- optional (keywordC delN |-- thms)
huffman@31704
   214
val addscan = Scan.unless any_keyword (keyword addN)
huffman@31704
   215
val labels = (keywordC labelsN |-- names) -- optional (keywordC delN |-- names)
huffman@31704
   216
val entry = Scan.optional mode true -- optional2 inj -- optional2 embed -- optional2 return -- optional2 cong -- optional2 labels
huffman@31704
   217
huffman@31704
   218
val transf_add = addscan |-- entry
huffman@31704
   219
in
huffman@31704
   220
huffman@31704
   221
val install_att_syntax =
huffman@31704
   222
  (Scan.lift (Args.$$$ delN >> K del) ||
huffman@31704
   223
    transf_add
huffman@31704
   224
    >> (fn (((((g, inj), embed), ret), cg), hints) => add (inj, embed, ret, cg, g, hints)))
huffman@31704
   225
haftmann@32475
   226
val transferred_att_syntax = (optional names -- Scan.option (keywordC directionN |-- (Args.term -- Args.term))
haftmann@32475
   227
  -- optional (keywordC leavingN |-- names) >> (fn ((hints, aD),leave) => transferred_attribute hints aD leave));
huffman@31704
   228
huffman@31704
   229
end;
huffman@31704
   230
huffman@31704
   231
huffman@31704
   232
(* theory setup *)
huffman@31704
   233
huffman@31704
   234
huffman@31704
   235
val setup =
huffman@31704
   236
  Attrib.setup @{binding transfer} install_att_syntax
huffman@31704
   237
    "Installs transfer data" #>
huffman@31704
   238
  Attrib.setup @{binding transfer_simps} (Attrib.add_del add_ss del_ss)
huffman@31704
   239
    "simp rules for transfer" #>
huffman@31704
   240
  Attrib.setup @{binding transferred} transferred_att_syntax
huffman@31704
   241
    "Transfers theorems";
huffman@31704
   242
huffman@31704
   243
end;