unused;
authorwenzelm
Tue Jun 04 13:44:59 2019 +0200 (6 weeks ago)
changeset 703179fced5690f4e
parent 70316 c61b7af108a6
child 70318 9eff9e2dc177
unused;
src/HOL/Tools/legacy_transfer.ML
     1.1 --- a/src/HOL/Tools/legacy_transfer.ML	Tue Jun 04 13:14:17 2019 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,261 +0,0 @@
     1.4 -(*  Title:      HOL/Tools/legacy_transfer.ML
     1.5 -    Author:     Amine Chaieb, University of Cambridge, 2009
     1.6 -    Author:     Jeremy Avigad, Carnegie Mellon University
     1.7 -    Author:     Florian Haftmann, TU Muenchen
     1.8 -
     1.9 -Simple transfer principle on theorems.
    1.10 -*)
    1.11 -
    1.12 -signature LEGACY_TRANSFER =
    1.13 -sig
    1.14 -  datatype selection = Direction of term * term | Hints of string list | Prop
    1.15 -  val transfer: Context.generic -> selection -> string list -> thm -> thm list
    1.16 -  type entry
    1.17 -  val add: thm -> bool -> entry -> Context.generic -> Context.generic
    1.18 -  val del: thm -> entry -> Context.generic -> Context.generic
    1.19 -  val drop: thm -> Context.generic -> Context.generic
    1.20 -end;
    1.21 -
    1.22 -structure Legacy_Transfer : LEGACY_TRANSFER =
    1.23 -struct
    1.24 -
    1.25 -(* data administration *)
    1.26 -
    1.27 -val direction_of = Thm.dest_binop o Thm.dest_arg o Thm.cprop_of;
    1.28 -
    1.29 -val transfer_morphism_key = Drule.strip_imp_concl (Thm.cprop_of @{thm transfer_morphismI});
    1.30 -
    1.31 -fun check_morphism_key ctxt key =
    1.32 -  let
    1.33 -    val _ = Thm.match (transfer_morphism_key, Thm.cprop_of key)
    1.34 -      handle Pattern.MATCH => error ("Transfer: expected theorem of the form "
    1.35 -        ^ quote (Syntax.string_of_term ctxt (Thm.term_of transfer_morphism_key)));
    1.36 -  in direction_of key end;
    1.37 -
    1.38 -type entry = { inj : thm list, embed : thm list, return : thm list, cong : thm list,
    1.39 -  hints : string list };
    1.40 -
    1.41 -val empty_entry = { inj = [], embed = [], return = [], cong = [], hints = [] };
    1.42 -fun merge_entry ({ inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 } : entry,
    1.43 -  { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } : entry) =
    1.44 -    { inj = merge Thm.eq_thm (inj1, inj2), embed = merge Thm.eq_thm (embed1, embed2),
    1.45 -      return = merge Thm.eq_thm (return1, return2), cong = merge Thm.eq_thm (cong1, cong2),
    1.46 -      hints = merge (op =) (hints1, hints2) };
    1.47 -
    1.48 -structure Data = Generic_Data
    1.49 -(
    1.50 -  type T = (thm * entry) list;
    1.51 -  val empty = [];
    1.52 -  val extend = I;
    1.53 -  val merge = AList.join Thm.eq_thm (K merge_entry);
    1.54 -);
    1.55 -
    1.56 -
    1.57 -(* data lookup *)
    1.58 -
    1.59 -fun transfer_rules_of ({ inj, embed, return, cong, ... } : entry) =
    1.60 -  (inj, embed, return, cong);
    1.61 -
    1.62 -fun get_by_direction context (a, D) =
    1.63 -  let
    1.64 -    val ctxt = Context.proof_of context;
    1.65 -    val a0 = Thm.cterm_of ctxt a;
    1.66 -    val D0 = Thm.cterm_of ctxt D;
    1.67 -    fun eq_direction ((a, D), thm') =
    1.68 -      let
    1.69 -        val (a', D') = direction_of thm';
    1.70 -      in a aconvc a' andalso D aconvc D' end;
    1.71 -  in case AList.lookup eq_direction (Data.get context) (a0, D0) of
    1.72 -      SOME e => ((a0, D0), transfer_rules_of e)
    1.73 -    | NONE => error ("Transfer: no such instance: ("
    1.74 -        ^ Syntax.string_of_term ctxt a ^ ", " ^ Syntax.string_of_term ctxt D ^ ")")
    1.75 -  end;
    1.76 -
    1.77 -fun get_by_hints context hints =
    1.78 -  let
    1.79 -    val insts = map_filter (fn (k, e) => if exists (member (op =) (#hints e)) hints
    1.80 -      then SOME (direction_of k, transfer_rules_of e) else NONE) (Data.get context);
    1.81 -    val _ = if null insts then error ("Transfer: no such labels: " ^ commas_quote hints) else ();
    1.82 -  in insts end;
    1.83 -
    1.84 -fun splits P [] = []
    1.85 -  | splits P (xs as (x :: _)) =
    1.86 -      let
    1.87 -        val (pss, qss) = List.partition (P x) xs;
    1.88 -      in if null pss then [qss] else if null qss then [pss] else pss :: splits P qss end;
    1.89 -
    1.90 -fun get_by_prop context t =
    1.91 -  let
    1.92 -    val tys = map snd (Term.add_vars t []);
    1.93 -    val _ = if null tys then error "Transfer: unable to guess instance" else ();
    1.94 -    val tyss = splits (curry Type.could_unify) tys;
    1.95 -    val get_ty = Thm.typ_of_cterm o fst o direction_of;
    1.96 -    val insts = map_filter (fn tys => get_first (fn (k, e) =>
    1.97 -      if Type.could_unify (hd tys, range_type (get_ty k))
    1.98 -      then SOME (direction_of k, transfer_rules_of e)
    1.99 -      else NONE) (Data.get context)) tyss;
   1.100 -    val _ = if null insts then
   1.101 -      error "Transfer: no instances, provide direction or hints explicitly" else ();
   1.102 -  in insts end;
   1.103 -
   1.104 -
   1.105 -(* applying transfer data *)
   1.106 -
   1.107 -fun transfer_thm ((raw_a, raw_D), (inj, embed, return, cong)) leave ctxt1 thm =
   1.108 -  let
   1.109 -    (* identify morphism function *)
   1.110 -    val ([a, D], ctxt2) = ctxt1
   1.111 -      |> Variable.import true (map Drule.mk_term [raw_a, raw_D])
   1.112 -      |>> map Drule.dest_term o snd;
   1.113 -    val transform = Thm.apply @{cterm "Trueprop"} o Thm.apply D;
   1.114 -    val T = Thm.typ_of_cterm a;
   1.115 -    val (aT, bT) = (Term.range_type T, Term.domain_type T);
   1.116 -
   1.117 -    (* determine variables to transfer *)
   1.118 -    val ctxt3 = ctxt2
   1.119 -      |> Variable.declare_thm thm
   1.120 -      |> Variable.declare_term (Thm.term_of a)
   1.121 -      |> Variable.declare_term (Thm.term_of D);
   1.122 -    val vars = filter (fn ((v, _), T) => Type.could_unify (T, aT) andalso
   1.123 -      not (member (op =) leave v)) (Term.add_vars (Thm.prop_of thm) []);
   1.124 -    val c_vars = map (Thm.cterm_of ctxt3 o Var) vars;
   1.125 -    val (vs', ctxt4) = Variable.variant_fixes (map (fst o fst) vars) ctxt3;
   1.126 -    val c_vars' = map (Thm.cterm_of ctxt3 o (fn v => Free (v, bT))) vs';
   1.127 -    val c_exprs' = map (Thm.apply a) c_vars';
   1.128 -
   1.129 -    (* transfer *)
   1.130 -    val (hyps, ctxt5) = ctxt4
   1.131 -      |> Assumption.add_assumes (map transform c_vars');
   1.132 -    val simpset =
   1.133 -      put_simpset HOL_ss ctxt5 addsimps (inj @ embed @ return)
   1.134 -      |> fold Simplifier.add_cong cong;
   1.135 -    val thm' = thm
   1.136 -      |> infer_instantiate ctxt5 (map (#1 o dest_Var o Thm.term_of) c_vars ~~ c_exprs')
   1.137 -      |> fold_rev Thm.implies_intr (map Thm.cprop_of hyps)
   1.138 -      |> Simplifier.asm_full_simplify simpset
   1.139 -  in singleton (Variable.export ctxt5 ctxt1) thm' end;
   1.140 -
   1.141 -fun transfer_thm_multiple insts leave ctxt thm =
   1.142 -  map (fn inst => transfer_thm inst leave ctxt thm) insts;
   1.143 -
   1.144 -datatype selection = Direction of term * term | Hints of string list | Prop;
   1.145 -
   1.146 -fun insts_for context thm (Direction direction) = [get_by_direction context direction]
   1.147 -  | insts_for context thm (Hints hints) = get_by_hints context hints
   1.148 -  | insts_for context thm Prop = get_by_prop context (Thm.prop_of thm);
   1.149 -
   1.150 -fun transfer context selection leave thm =
   1.151 -  transfer_thm_multiple (insts_for context thm selection) leave (Context.proof_of context) thm;
   1.152 -
   1.153 -
   1.154 -(* maintaining transfer data *)
   1.155 -
   1.156 -fun extend_entry ctxt (a, D) guess
   1.157 -    { inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 }
   1.158 -    { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
   1.159 -  let
   1.160 -    val guessed = if guess
   1.161 -      then map (fn thm => transfer_thm
   1.162 -        ((a, D), (if null inj1 then inj2 else inj1, [], [], cong1)) [] ctxt thm RS sym) embed1
   1.163 -      else [];
   1.164 -  in
   1.165 -    { inj = union Thm.eq_thm inj1 inj2, embed = union Thm.eq_thm embed1 embed2,
   1.166 -      return = union Thm.eq_thm guessed (union Thm.eq_thm return1 return2),
   1.167 -      cong = union Thm.eq_thm cong1 cong2, hints = union (op =) hints1 hints2 }
   1.168 -  end;
   1.169 -
   1.170 -fun diminish_entry
   1.171 -    { inj = inj0, embed = embed0, return = return0, cong = cong0, hints = hints0 }
   1.172 -    { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
   1.173 -  { inj = subtract Thm.eq_thm inj0 inj2, embed = subtract Thm.eq_thm embed0 embed2,
   1.174 -    return = subtract Thm.eq_thm return0 return2, cong = subtract Thm.eq_thm cong0 cong2,
   1.175 -    hints = subtract (op =) hints0 hints2 };
   1.176 -
   1.177 -fun add key guess entry context =
   1.178 -  let
   1.179 -    val ctxt = Context.proof_of context;
   1.180 -    val a_D = check_morphism_key ctxt key;
   1.181 -  in
   1.182 -    context
   1.183 -    |> Data.map (AList.map_default Thm.eq_thm
   1.184 -         (key, empty_entry) (extend_entry ctxt a_D guess entry))
   1.185 -  end;
   1.186 -
   1.187 -fun del key entry = Data.map (AList.map_entry Thm.eq_thm key (diminish_entry entry));
   1.188 -
   1.189 -fun drop key = Data.map (AList.delete Thm.eq_thm key);
   1.190 -
   1.191 -
   1.192 -(* syntax *)
   1.193 -
   1.194 -local
   1.195 -
   1.196 -fun these scan = Scan.optional scan [];
   1.197 -fun these_pair scan = Scan.optional scan ([], []);
   1.198 -
   1.199 -fun keyword k = Scan.lift (Args.$$$ k) >> K ();
   1.200 -fun keyword_colon k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
   1.201 -
   1.202 -val addN = "add";
   1.203 -val delN = "del";
   1.204 -val keyN = "key";
   1.205 -val modeN = "mode";
   1.206 -val automaticN = "automatic";
   1.207 -val manualN = "manual";
   1.208 -val injN = "inj";
   1.209 -val embedN = "embed";
   1.210 -val returnN = "return";
   1.211 -val congN = "cong";
   1.212 -val labelsN = "labels";
   1.213 -
   1.214 -val leavingN = "leaving";
   1.215 -val directionN = "direction";
   1.216 -
   1.217 -val any_keyword = keyword_colon addN || keyword_colon delN || keyword_colon keyN
   1.218 -  || keyword_colon modeN || keyword_colon injN || keyword_colon embedN || keyword_colon returnN
   1.219 -  || keyword_colon congN || keyword_colon labelsN
   1.220 -  || keyword_colon leavingN || keyword_colon directionN;
   1.221 -
   1.222 -val thms = Scan.repeats (Scan.unless any_keyword Attrib.multi_thm);
   1.223 -val names = Scan.repeat (Scan.unless any_keyword (Scan.lift Args.name))
   1.224 -
   1.225 -val mode = keyword_colon modeN |-- ((Scan.lift (Args.$$$ manualN) >> K false)
   1.226 -  || (Scan.lift (Args.$$$ automaticN) >> K true));
   1.227 -val inj = (keyword_colon injN |-- thms) -- these (keyword_colon delN |-- thms);
   1.228 -val embed = (keyword_colon embedN |-- thms) -- these (keyword_colon delN |-- thms);
   1.229 -val return = (keyword_colon returnN |-- thms) -- these (keyword_colon delN |-- thms);
   1.230 -val cong = (keyword_colon congN |-- thms) -- these (keyword_colon delN |-- thms);
   1.231 -val labels = (keyword_colon labelsN |-- names) -- these (keyword_colon delN |-- names);
   1.232 -
   1.233 -val entry_pair = these_pair inj -- these_pair embed
   1.234 -  -- these_pair return -- these_pair cong -- these_pair labels
   1.235 -  >> (fn (((((inja, injd), (embeda, embedd)), (returna, returnd)), (conga, congd)),
   1.236 -       (hintsa, hintsd)) =>
   1.237 -      ({ inj = inja, embed = embeda, return = returna, cong = conga, hints = hintsa },
   1.238 -        { inj = injd, embed = embedd, return = returnd, cong = congd, hints = hintsd }));
   1.239 -
   1.240 -val selection = (keyword_colon directionN |-- (Args.term -- Args.term) >> Direction)
   1.241 -  || these names >> (fn hints => if null hints then Prop else Hints hints);
   1.242 -
   1.243 -in
   1.244 -
   1.245 -val _ =
   1.246 -  Theory.setup
   1.247 -   (Attrib.setup @{binding transfer}
   1.248 -      (keyword delN >> K (Thm.declaration_attribute drop)
   1.249 -    || keyword addN |-- Scan.optional mode true -- entry_pair
   1.250 -      >> (fn (guess, (entry_add, entry_del)) =>
   1.251 -          Thm.declaration_attribute (fn thm => add thm guess entry_add #> del thm entry_del))
   1.252 -    || keyword_colon keyN |-- Attrib.thm
   1.253 -      >> (fn key => Thm.declaration_attribute (fn thm =>
   1.254 -            add key false { inj = [], embed = [], return = [thm], cong = [], hints = [] })))
   1.255 -    "install transfer data" #>
   1.256 -    Attrib.setup @{binding transferred}
   1.257 -      (selection -- these (keyword_colon leavingN |-- names)
   1.258 -        >> (fn (selection, leave) => Thm.rule_attribute [] (fn context =>
   1.259 -              Conjunction.intr_balanced o transfer context selection leave)))
   1.260 -    "transfer theorems");
   1.261 -
   1.262 -end;
   1.263 -
   1.264 -end;