# HG changeset patch # User haftmann # Date 1268147719 -3600 # Node ID 189b4a932cfe64ae9f562748e5a6cc02992cea27 # Parent e69013c25b743d513dff5d2461c4a01e4dc13ef7 tuned data structures; using AList.map_default diff -r e69013c25b74 -r 189b4a932cfe src/HOL/Tools/transfer.ML --- a/src/HOL/Tools/transfer.ML Tue Mar 09 15:47:16 2010 +0100 +++ b/src/HOL/Tools/transfer.ML Tue Mar 09 16:15:19 2010 +0100 @@ -10,7 +10,7 @@ datatype selection = Direction of term * term | Hints of string list | Prop val transfer: Context.generic -> selection -> string list -> thm -> thm list type entry - val add: entry * entry -> thm -> Context.generic -> Context.generic + val add: bool -> entry * entry -> thm -> Context.generic -> Context.generic val del: thm -> Context.generic -> Context.generic val setup: theory -> theory end; @@ -30,13 +30,13 @@ in direction_of key end; type entry = { inj : thm list, embed : thm list, return : thm list, cong : thm list, - guess : bool, hints : string list }; + hints : string list }; -fun merge_entry ({ inj = inj1, embed = embed1, return = return1, cong = cong1, guess = guess1, hints = hints1 } : entry, - { inj = inj2, embed = embed2, return = return2, cong = cong2, guess = guess2, hints = hints2 } : entry) = +fun merge_entry ({ inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 } : entry, + { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } : entry) = { inj = merge Thm.eq_thm (inj1, inj2), embed = merge Thm.eq_thm (embed1, embed2), return = merge Thm.eq_thm (return1, return2), cong = merge Thm.eq_thm (cong1, cong2), - guess = guess1 andalso guess2, hints = merge (op =) (hints1, hints2) }; + hints = merge (op =) (hints1, hints2) }; structure Data = Generic_Data ( @@ -49,6 +49,9 @@ (* data lookup *) +fun transfer_rules_of { inj, embed, return, cong, ... } = + (inj, embed, return, cong); + fun get_by_direction context (a, D) = let val ctxt = Context.proof_of context; @@ -60,7 +63,7 @@ val (a', D') = direction_of thm'; in a aconvc a' andalso D aconvc D' end; in case AList.lookup eq_direction (Data.get context) (a0, D0) of - SOME e => ((a0, D0), e) + SOME e => ((a0, D0), transfer_rules_of e) | NONE => error ("Transfer: no such instance: (" ^ Syntax.string_of_term ctxt a ^ ", " ^ Syntax.string_of_term ctxt D ^ ")") end; @@ -68,7 +71,7 @@ fun get_by_hints context hints = let val insts = map_filter (fn (k, e) => if exists (member (op =) (#hints e)) hints - then SOME (direction_of k, e) else NONE) (Data.get context); + then SOME (direction_of k, transfer_rules_of e) else NONE) (Data.get context); val _ = if null insts then error ("Transfer: no such labels: " ^ commas (map quote hints)) else (); in insts end; @@ -84,9 +87,9 @@ val _ = if null tys then error "Transfer: unable to guess instance" else (); val tyss = splits (curry Type.could_unify) tys; val get_ty = typ_of o ctyp_of_term o fst o direction_of; - val insts = map_filter (fn tys => get_first (fn (k, ss) => + val insts = map_filter (fn tys => get_first (fn (k, e) => if Type.could_unify (hd tys, range_type (get_ty k)) - then SOME (direction_of k, ss) + then SOME (direction_of k, transfer_rules_of e) else NONE) (Data.get context)) tyss; val _ = if null insts then error "Transfer: no instances, provide direction or hints explicitly" else (); @@ -95,8 +98,7 @@ (* applying transfer data *) -fun transfer_thm inj_only a0 D0 { inj, embed, return, cong, guess = _, hints = _ } - leave ctxt0 th = +fun transfer_thm inj_only ((a0, D0), (inj, embed, return, cong)) leave ctxt0 th = let val ([a, D], ctxt) = apfst (map Drule.dest_term o snd) (Variable.import true (map Drule.mk_term [a0, D0]) ctxt0); @@ -116,14 +118,14 @@ Thm.capply @{cterm "Trueprop"} (Thm.capply D ct)) cfis) ctxt''; val th1 = Drule.cterm_instantiate (cns ~~ cis) th; val th2 = fold Thm.elim_implies hs (fold_rev implies_intr (map cprop_of hs) th1); - val simpset = (Simplifier.context ctxt''' HOL_ss) + val simpset = Simplifier.context ctxt''' HOL_ss addsimps inj addsimps (if inj_only then [] else embed @ return) addcongs cong; val th3 = Simplifier.asm_full_simplify simpset (fold_rev implies_intr (map cprop_of hs) th2); in hd (Variable.export ctxt''' ctxt0 [th3]) end; fun transfer_thm_multiple inj_only insts leave ctxt thm = - map (fn ((a, D), e) => transfer_thm false a D e leave ctxt thm) insts; + map (fn inst => transfer_thm false inst leave ctxt thm) insts; datatype selection = Direction of term * term | Hints of string list | Prop; @@ -137,25 +139,22 @@ (* maintaining transfer data *) -fun merge_update eq m (k, v) [] = [(k, v)] - | merge_update eq m (k, v) ((k', v') :: al) = - if eq (k, k') then (k', m (v, v')) :: al else (k', v') :: merge_update eq m (k, v) al; +fun merge_update eq m (k, v) = AList.map_default eq + (k, { inj = [], embed = [], return = [], cong = [], hints = [] }) (fn v' => m (v, v')); -(*? fun merge_update eq m (k, v) = AList.map_entry eq k (fn v' => m (v, v'));*) - -fun merge_entries {inj = inj0, embed = embed0, return = return0, cong = cong0, guess = guess0, hints = hints0} - ({inj = inj1, embed = embed1, return = return1, cong = cong1, guess = guess1, hints = hints1}, - {inj = inj2, embed = embed2, return = return2, cong = cong2, guess = guess2, hints = hints2} : entry) = +fun merge_entries { inj = inj0, embed = embed0, return = return0, cong = cong0, hints = hints0 } + ({ inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 }, + { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } : entry) = let fun h xs0 xs ys = subtract Thm.eq_thm xs0 (merge Thm.eq_thm (xs, ys)) in - {inj = h inj0 inj1 inj2, embed = h embed0 embed1 embed2, - return = h return0 return1 return2, cong = h cong0 cong1 cong2, guess = guess1 andalso guess2, - hints = subtract (op =) hints0 (union (op =) hints1 hints2) } + { inj = h inj0 inj1 inj2, embed = h embed0 embed1 embed2, + return = h return0 return1 return2, cong = h cong0 cong1 cong2, + hints = subtract (op =) hints0 (union (op =) hints1 hints2) } end; -fun add (e0 as {inj = inja, embed = embeda, return = returna, cong = conga, guess = guess, hints = hintsa}, - ed as {inj = injd, embed = embedd, return = returnd, cong = congd, guess = _, hints = hintsd}) key context = +fun add guess (e0 as { inj = inja, embed = embeda, return = returna, cong = conga, hints = hintsa }, + ed as { inj = injd, embed = embedd, return = returnd, cong = congd, hints = hintsd }) key context = context |> Data.map (fn al => let @@ -168,9 +167,8 @@ | NONE => error "Transfer: cannot generate return rules on the fly, either add injectivity axiom or force manual mode with mode: manual") else inja val return' = merge Thm.eq_thm (returna, map - (fn th => transfer_thm true a0 D0 {inj = inj', embed = [], return = [], cong = conga, - guess = guess, hints = hintsa} [] ctxt th RS sym) embeda); - in {inj = inja, embed = embeda, return = return', cong = conga, guess = guess, hints = hintsa} end + (fn th => transfer_thm true ((a0, D0), (inj', [], [], conga)) [] ctxt th RS sym) embeda); + in { inj = inja, embed = embeda, return = return', cong = conga, hints = hintsa } end else e0; in merge_update Thm.eq_thm (merge_entries ed) (key, entry) al end); @@ -217,12 +215,12 @@ val cong = (keyword_colon congN |-- thms) -- these (keyword_colon delN |-- thms); val labels = (keyword_colon labelsN |-- names) -- these (keyword_colon delN |-- names); -val entry_pair = Scan.optional mode true -- these_pair inj -- these_pair embed +val entry_pair = these_pair inj -- these_pair embed -- these_pair return -- these_pair cong -- these_pair labels - >> (fn (((((g, (inja, injd)), (embeda, embedd)), (returna, returnd)), (conga, congd)), + >> (fn (((((inja, injd), (embeda, embedd)), (returna, returnd)), (conga, congd)), (hintsa, hintsd)) => - ({inj = inja, embed = embeda, return = returna, cong = conga, guess = g, hints = hintsa}, - {inj = injd, embed = embedd, return = returnd, cong = congd, guess = g, hints = hintsd})); + ({inj = inja, embed = embeda, return = returna, cong = conga, hints = hintsa}, + {inj = injd, embed = embedd, return = returnd, cong = congd, hints = hintsd})); val selection = (keyword_colon directionN |-- (Args.term -- Args.term) >> Direction) || these names >> (fn hints => if null hints then Prop else Hints hints); @@ -230,8 +228,8 @@ in val transfer_attribute = Scan.lift (Args.$$$ delN >> K (Thm.declaration_attribute del)) - || Scan.unless any_keyword (keyword addN) |-- entry_pair - >> (fn entry_pair => Thm.declaration_attribute (add entry_pair)) + || Scan.unless any_keyword (keyword addN) |-- Scan.optional mode true -- entry_pair + >> (fn (guess, entry_pair) => Thm.declaration_attribute (add guess entry_pair)) val transferred_attribute = selection -- these (keyword_colon leavingN |-- names) >> (fn (selection, leave) => Thm.rule_attribute (fn context =>