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