src/HOL/Tools/transfer.ML
author haftmann
Tue, 09 Mar 2010 15:47:16 +0100
changeset 35674 e69013c25b74
parent 35648 4b01ddafc8a9
child 35675 189b4a932cfe
permissions -rw-r--r--
consistent field names; tuned interface
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
     1
(*  Author:   Amine Chaieb, University of Cambridge, 2009
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
     2
              Jeremy Avigad, Carnegie Mellon University
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
     3
              Florian Haftmann, TU Muenchen
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
     4
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
     5
Simple transfer principle on theorems.
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
     6
*)
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
     7
32557
3cfe4c13aa6e plain structure name; signature constraint; shorter lines
haftmann
parents: 32476
diff changeset
     8
signature TRANSFER =
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
     9
sig
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
    10
  datatype selection = Direction of term * term | Hints of string list | Prop
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    11
  val transfer: Context.generic -> selection -> string list -> thm -> thm list
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    12
  type entry
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
    13
  val add: entry * entry -> thm -> Context.generic -> Context.generic
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
    14
  val del: thm -> Context.generic -> Context.generic
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    15
  val setup: theory -> theory
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    16
end;
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    17
32557
3cfe4c13aa6e plain structure name; signature constraint; shorter lines
haftmann
parents: 32476
diff changeset
    18
structure Transfer : TRANSFER =
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    19
struct
32557
3cfe4c13aa6e plain structure name; signature constraint; shorter lines
haftmann
parents: 32476
diff changeset
    20
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
    21
(* data administration *)
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
    22
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    23
val direction_of = Thm.dest_binop o Thm.dest_arg o cprop_of;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    24
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
    25
fun check_morphism_key ctxt key =
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
    26
  let
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
    27
    val _ = (Thm.match o pairself Thm.cprop_of) (@{thm transfer_morphismI}, key)
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
    28
      handle Pattern.MATCH => error
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    29
        ("Transfer: expected theorem of the form " ^ quote (Display.string_of_thm ctxt @{thm transfer_morphismI}));
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    30
  in direction_of key end;
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
    31
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    32
type entry = { inj : thm list, embed : thm list, return : thm list, cong : thm list,
33321
28e3ce50a5a1 join entries properly on theory merge
haftmann
parents: 33042
diff changeset
    33
  guess : bool, hints : string list };
28e3ce50a5a1 join entries properly on theory merge
haftmann
parents: 33042
diff changeset
    34
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    35
fun merge_entry ({ inj = inj1, embed = embed1, return = return1, cong = cong1, guess = guess1, hints = hints1 } : entry,
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    36
  { inj = inj2, embed = embed2, return = return2, cong = cong2, guess = guess2, hints = hints2 } : entry) =
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    37
    { inj = merge Thm.eq_thm (inj1, inj2), embed = merge Thm.eq_thm (embed1, embed2),
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    38
      return = merge Thm.eq_thm (return1, return2), cong = merge Thm.eq_thm (cong1, cong2),
33321
28e3ce50a5a1 join entries properly on theory merge
haftmann
parents: 33042
diff changeset
    39
      guess = guess1 andalso guess2, hints = merge (op =) (hints1, hints2) };
28e3ce50a5a1 join entries properly on theory merge
haftmann
parents: 33042
diff changeset
    40
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33321
diff changeset
    41
structure Data = Generic_Data
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    42
(
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
    43
  type T = (thm * entry) list;
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
    44
  val empty = [];
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    45
  val extend = I;
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
    46
  val merge = AList.join Thm.eq_thm (K merge_entry);
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    47
);
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    48
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    49
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    50
(* data lookup *)
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    51
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    52
fun get_by_direction context (a, D) =
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    53
  let
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    54
    val ctxt = Context.proof_of context;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    55
    val certify = Thm.cterm_of (Context.theory_of context);
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    56
    val a0 = certify a;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    57
    val D0 = certify D;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    58
    fun eq_direction ((a, D), thm') =
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    59
      let
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    60
        val (a', D') = direction_of thm';
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    61
      in a aconvc a' andalso D aconvc D' end;
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    62
  in case AList.lookup eq_direction (Data.get context) (a0, D0) of
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    63
      SOME e => ((a0, D0), e)
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    64
    | NONE => error ("Transfer: no such instance: ("
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    65
        ^ Syntax.string_of_term ctxt a ^ ", " ^ Syntax.string_of_term ctxt D ^ ")")
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    66
  end;
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    67
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    68
fun get_by_hints context hints =
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    69
  let
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    70
    val insts = map_filter (fn (k, e) => if exists (member (op =) (#hints e)) hints
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    71
      then SOME (direction_of k, e) else NONE) (Data.get context);
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    72
    val _ = if null insts then error ("Transfer: no such labels: " ^ commas (map quote hints)) else ();
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    73
  in insts end;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    74
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    75
fun splits P [] = []
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    76
  | splits P (xs as (x :: _)) =
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    77
      let
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    78
        val (pss, qss) = List.partition (P x) xs;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    79
      in if null pss then [qss] else if null qss then [pss] else pss :: splits P qss end;
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    80
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    81
fun get_by_prop context t =
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    82
  let
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    83
    val tys = map snd (Term.add_vars t []);
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    84
    val _ = if null tys then error "Transfer: unable to guess instance" else ();
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    85
    val tyss = splits (curry Type.could_unify) tys;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    86
    val get_ty = typ_of o ctyp_of_term o fst o direction_of;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    87
    val insts = map_filter (fn tys => get_first (fn (k, ss) =>
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    88
      if Type.could_unify (hd tys, range_type (get_ty k))
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    89
      then SOME (direction_of k, ss)
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    90
      else NONE) (Data.get context)) tyss;
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    91
    val _ = if null insts then
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    92
      error "Transfer: no instances, provide direction or hints explicitly" else ();
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    93
  in insts end;
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    94
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    95
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
    96
(* applying transfer data *)
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
    97
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
    98
fun transfer_thm inj_only a0 D0 { inj, embed, return, cong, guess = _, hints = _ }
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
    99
    leave ctxt0 th =
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   100
  let
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   101
    val ([a, D], ctxt) = apfst (map Drule.dest_term o snd)
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   102
      (Variable.import true (map Drule.mk_term [a0, D0]) ctxt0);
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   103
    val (aT, bT) =
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   104
      let val T = typ_of (ctyp_of_term a)
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   105
      in (Term.range_type T, Term.domain_type T) end;
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   106
    val ctxt' = (Variable.declare_term (term_of a) o Variable.declare_term (term_of D)
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   107
      o Variable.declare_thm th) ctxt;
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   108
    val ns = filter (fn i => Type.could_unify (snd i, aT) andalso
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   109
      not (member (op =) leave (fst (fst i)))) (Term.add_vars (prop_of th) []);
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   110
    val (ins, ctxt'') = Variable.variant_fixes (map (fst o fst) ns) ctxt';
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   111
    val certify = Thm.cterm_of (ProofContext.theory_of ctxt'');
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   112
    val cns = map (certify o Var) ns;
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   113
    val cfis = map (certify o (fn n => Free (n, bT))) ins;
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   114
    val cis = map (Thm.capply a) cfis;
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   115
    val (hs, ctxt''') = Assumption.add_assumes (map (fn ct =>
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   116
      Thm.capply @{cterm "Trueprop"} (Thm.capply D ct)) cfis) ctxt'';
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   117
    val th1 = Drule.cterm_instantiate (cns ~~ cis) th;
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   118
    val th2 = fold Thm.elim_implies hs (fold_rev implies_intr (map cprop_of hs) th1);
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
   119
    val simpset = (Simplifier.context ctxt''' HOL_ss)
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   120
      addsimps inj addsimps (if inj_only then [] else embed @ return) addcongs cong;
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
   121
    val th3 = Simplifier.asm_full_simplify simpset
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
   122
      (fold_rev implies_intr (map cprop_of hs) th2);
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   123
  in hd (Variable.export ctxt''' ctxt0 [th3]) end;
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   124
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
   125
fun transfer_thm_multiple inj_only insts leave ctxt thm =
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   126
  map (fn ((a, D), e) => transfer_thm false a D e leave ctxt thm) insts;
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   127
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   128
datatype selection = Direction of term * term | Hints of string list | Prop;
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
   129
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   130
fun insts_for context thm (Direction direction) = [get_by_direction context direction]
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   131
  | insts_for context thm (Hints hints) = get_by_hints context hints
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   132
  | insts_for context thm Prop = get_by_prop context (Thm.prop_of thm);
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   133
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   134
fun transfer context selection leave thm =
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   135
  transfer_thm_multiple false (insts_for context thm selection) leave (Context.proof_of context) thm;
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   136
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   137
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
   138
(* maintaining transfer data *)
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   139
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   140
fun merge_update eq m (k, v) [] = [(k, v)]
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   141
  | merge_update eq m (k, v) ((k', v') :: al) =
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   142
      if eq (k, k') then (k', m (v, v')) :: al else (k', v') :: merge_update eq m (k, v) al;
32813
dac196e23093 handle Pattern.MATCH, not arbitrary exceptions;
wenzelm
parents: 32557
diff changeset
   143
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   144
(*? fun merge_update eq m (k, v) = AList.map_entry eq k (fn v' => m (v, v'));*)
32813
dac196e23093 handle Pattern.MATCH, not arbitrary exceptions;
wenzelm
parents: 32557
diff changeset
   145
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   146
fun merge_entries {inj = inj0, embed = embed0, return = return0, cong = cong0, guess = guess0, hints = hints0}
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   147
    ({inj = inj1, embed = embed1, return = return1, cong = cong1, guess = guess1, hints = hints1},
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   148
     {inj = inj2, embed = embed2, return = return2, cong = cong2, guess = guess2, hints = hints2} : entry) =
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   149
  let
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   150
    fun h xs0 xs ys = subtract Thm.eq_thm xs0 (merge Thm.eq_thm (xs, ys))
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   151
  in
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   152
    {inj = h inj0 inj1 inj2, embed = h embed0 embed1 embed2,
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   153
     return = h return0 return1 return2, cong = h cong0 cong1 cong2, guess = guess1 andalso guess2,
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   154
     hints = subtract (op =) hints0 (union (op =) hints1 hints2) }
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   155
  end;
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   156
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   157
fun add (e0 as {inj = inja, embed = embeda, return = returna, cong = conga, guess = guess, hints = hintsa},
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   158
  ed as {inj = injd, embed = embedd, return = returnd, cong = congd, guess = _, hints = hintsd}) key context =
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   159
  context
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   160
  |> Data.map (fn al =>
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   161
    let
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   162
      val ctxt = Context.proof_of context;
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   163
      val (a0, D0) = check_morphism_key ctxt key;
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   164
      val entry = if guess then
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   165
        let
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   166
          val inj' = if null inja then #inj
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   167
            (case AList.lookup Thm.eq_thm al key of SOME e => e
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   168
              | NONE => error "Transfer: cannot generate return rules on the fly, either add injectivity axiom or force manual mode with mode: manual")
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   169
            else inja
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   170
          val return' = merge Thm.eq_thm (returna, map
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   171
            (fn th => transfer_thm true a0 D0 {inj = inj', embed = [], return = [], cong = conga,
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   172
              guess = guess, hints = hintsa} [] ctxt th RS sym) embeda);
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   173
        in {inj = inja, embed = embeda, return = return', cong = conga, guess = guess, hints = hintsa} end
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   174
        else e0;
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   175
    in merge_update Thm.eq_thm (merge_entries ed) (key, entry) al end);
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   176
35647
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
   177
fun del key = Data.map (remove (eq_fst Thm.eq_thm) (key, []));
8f4b2e8543e7 code simplification and tuning
haftmann
parents: 35645
diff changeset
   178
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   179
35638
50655e2ebc85 dropped dead code; adhere more closely to standard coding conventions
haftmann
parents: 33519
diff changeset
   180
(* syntax *)
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   181
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   182
local
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   183
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   184
fun these scan = Scan.optional scan [];
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   185
fun these_pair scan = Scan.optional scan ([], []);
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   186
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   187
fun keyword k = Scan.lift (Args.$$$ k) >> K ();
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   188
fun keyword_colon k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   189
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   190
val addN = "add";
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   191
val delN = "del";
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   192
val modeN = "mode";
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   193
val automaticN = "automatic";
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   194
val manualN = "manual";
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   195
val injN = "inj";
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   196
val embedN = "embed";
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   197
val returnN = "return";
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   198
val congN = "cong";
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   199
val labelsN = "labels";
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   200
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   201
val leavingN = "leaving";
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   202
val directionN = "direction";
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   203
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   204
val any_keyword = keyword_colon addN || keyword_colon delN || keyword_colon modeN
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   205
  || keyword_colon injN || keyword_colon embedN || keyword_colon returnN
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   206
  || keyword_colon congN || keyword_colon labelsN
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   207
  || keyword_colon leavingN || keyword_colon directionN;
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   208
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   209
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   210
val names = Scan.repeat (Scan.unless any_keyword (Scan.lift Args.name))
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   211
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   212
val mode = keyword_colon modeN |-- ((Scan.lift (Args.$$$ manualN) >> K false)
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   213
  || (Scan.lift (Args.$$$ automaticN) >> K true));
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   214
val inj = (keyword_colon injN |-- thms) -- these (keyword_colon delN |-- thms);
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   215
val embed = (keyword_colon embedN |-- thms) -- these (keyword_colon delN |-- thms);
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   216
val return = (keyword_colon returnN |-- thms) -- these (keyword_colon delN |-- thms);
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   217
val cong = (keyword_colon congN |-- thms) -- these (keyword_colon delN |-- thms);
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   218
val labels = (keyword_colon labelsN |-- names) -- these (keyword_colon delN |-- names);
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   219
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   220
val entry_pair = Scan.optional mode true -- these_pair inj -- these_pair embed
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   221
  -- these_pair return -- these_pair cong -- these_pair labels
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   222
  >> (fn (((((g, (inja, injd)), (embeda, embedd)), (returna, returnd)), (conga, congd)),
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   223
       (hintsa, hintsd)) =>
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   224
      ({inj = inja, embed = embeda, return = returna, cong = conga, guess = g, hints = hintsa},
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   225
        {inj = injd, embed = embedd, return = returnd, cong = congd, guess = g, hints = hintsd}));
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   226
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   227
val selection = (keyword_colon directionN |-- (Args.term -- Args.term) >> Direction)
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   228
  || these names >> (fn hints => if null hints then Prop else Hints hints);
35645
74e4542d0a4a transfer: avoid camel case, more standard coding conventions, misc tuning
haftmann
parents: 35638
diff changeset
   229
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   230
in
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   231
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   232
val transfer_attribute = Scan.lift (Args.$$$ delN >> K (Thm.declaration_attribute del))
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   233
  || Scan.unless any_keyword (keyword addN) |-- entry_pair
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   234
    >> (fn entry_pair => Thm.declaration_attribute (add entry_pair))
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   235
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   236
val transferred_attribute = selection -- these (keyword_colon leavingN |-- names)
35674
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   237
  >> (fn (selection, leave) => Thm.rule_attribute (fn context =>
e69013c25b74 consistent field names; tuned interface
haftmann
parents: 35648
diff changeset
   238
      Conjunction.intr_balanced o transfer context selection leave));
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   239
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   240
end;
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   241
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   242
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   243
(* theory setup *)
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   244
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   245
val setup =
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   246
  Attrib.setup @{binding transfer} transfer_attribute
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   247
    "Installs transfer data" #>
35648
4b01ddafc8a9 proper ML interface; further polishing
haftmann
parents: 35647
diff changeset
   248
  Attrib.setup @{binding transferred} transferred_attribute
31706
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   249
    "Transfers theorems";
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   250
1db0c8f235fb new GCD library, courtesy of Jeremy Avigad
huffman
parents:
diff changeset
   251
end;