40582
|
1 |
(* Title: HOL/Tools/functorial-mappers.ML
|
|
2 |
Author: Florian Haftmann, TU Muenchen
|
|
3 |
|
|
4 |
Functorial mappers on types.
|
|
5 |
*)
|
|
6 |
|
|
7 |
signature FUNCTORIAL_MAPPERS =
|
|
8 |
sig
|
|
9 |
val find_atomic: theory -> typ -> (typ * (bool * bool)) list
|
|
10 |
val construct_mapper: theory -> (string * bool -> term)
|
|
11 |
-> bool -> typ -> typ -> term
|
|
12 |
val register: string -> string -> (sort * (bool * bool)) list -> thm -> thm
|
|
13 |
-> theory -> theory
|
|
14 |
type entry
|
|
15 |
val entries: theory -> entry Symtab.table
|
|
16 |
end;
|
|
17 |
|
|
18 |
structure Functorial_Mappers : FUNCTORIAL_MAPPERS =
|
|
19 |
struct
|
|
20 |
|
|
21 |
(** functorial mappers and their properties **)
|
|
22 |
|
|
23 |
(* bookkeeping *)
|
|
24 |
|
|
25 |
type entry = { mapper: string, variances: (sort * (bool * bool)) list,
|
|
26 |
concatenate: thm, identity: thm };
|
|
27 |
|
|
28 |
structure Data = Theory_Data(
|
|
29 |
type T = entry Symtab.table
|
|
30 |
val empty = Symtab.empty
|
|
31 |
val merge = Symtab.merge (K true)
|
|
32 |
val extend = I
|
|
33 |
);
|
|
34 |
|
|
35 |
val entries = Data.get;
|
|
36 |
|
|
37 |
|
|
38 |
(* type analysis *)
|
|
39 |
|
|
40 |
fun find_atomic thy T =
|
|
41 |
let
|
|
42 |
val variances_of = Option.map #variances o Symtab.lookup (Data.get thy);
|
|
43 |
fun add_variance is_contra T =
|
|
44 |
AList.map_default (op =) (T, (false, false))
|
|
45 |
((if is_contra then apsnd else apfst) (K true));
|
|
46 |
fun analyze' is_contra (_, (co, contra)) T =
|
|
47 |
(if co then analyze is_contra T else I)
|
|
48 |
#> (if contra then analyze (not is_contra) T else I)
|
|
49 |
and analyze is_contra (T as Type (tyco, Ts)) = (case variances_of tyco
|
|
50 |
of NONE => add_variance is_contra T
|
|
51 |
| SOME variances => fold2 (analyze' is_contra) variances Ts)
|
|
52 |
| analyze is_contra T = add_variance is_contra T;
|
|
53 |
in analyze false T [] end;
|
|
54 |
|
|
55 |
fun construct_mapper thy atomic =
|
|
56 |
let
|
|
57 |
val lookup = the o Symtab.lookup (Data.get thy);
|
|
58 |
fun constructs is_contra (_, (co, contra)) T T' =
|
|
59 |
(if co then [construct is_contra T T'] else [])
|
|
60 |
@ (if contra then [construct (not is_contra) T T'] else [])
|
|
61 |
and construct is_contra (T as Type (tyco, Ts)) (T' as Type (_, Ts')) =
|
|
62 |
let
|
|
63 |
val { mapper, variances, ... } = lookup tyco;
|
|
64 |
val args = maps (fn (arg_pattern, (T, T')) =>
|
|
65 |
constructs is_contra arg_pattern T T')
|
|
66 |
(variances ~~ (Ts ~~ Ts'));
|
|
67 |
val (U, U') = if is_contra then (T', T) else (T, T');
|
|
68 |
in list_comb (Const (mapper, map fastype_of args ---> U --> U'), args) end
|
|
69 |
| construct is_contra (TFree (v, _)) (TFree _) = atomic (v, is_contra);
|
|
70 |
in construct end;
|
|
71 |
|
|
72 |
|
|
73 |
(* mapper properties *)
|
|
74 |
|
|
75 |
fun make_concatenate_prop variances (tyco, mapper) =
|
|
76 |
let
|
|
77 |
fun invents n k nctxt =
|
|
78 |
let
|
|
79 |
val names = Name.invents nctxt n k;
|
|
80 |
in (names, fold Name.declare names nctxt) end;
|
|
81 |
val (((vs1, vs2), vs3), _) = Name.context
|
|
82 |
|> invents Name.aT (length variances)
|
|
83 |
||>> invents Name.aT (length variances)
|
|
84 |
||>> invents Name.aT (length variances);
|
|
85 |
fun mk_Ts vs = map2 (fn v => fn (sort, _) => TFree (v, sort))
|
|
86 |
vs variances;
|
|
87 |
val (Ts1, Ts2, Ts3) = (mk_Ts vs1, mk_Ts vs2, mk_Ts vs3);
|
|
88 |
fun mk_argT ((T, T'), (_, (co, contra))) =
|
|
89 |
(if co then [(T --> T')] else [])
|
|
90 |
@ (if contra then [(T' --> T)] else []);
|
|
91 |
val contras = maps (fn (_, (co, contra)) =>
|
|
92 |
(if co then [false] else []) @ (if contra then [true] else [])) variances;
|
|
93 |
val Ts21 = maps mk_argT ((Ts2 ~~ Ts1) ~~ variances);
|
|
94 |
val Ts32 = maps mk_argT ((Ts3 ~~ Ts2) ~~ variances);
|
|
95 |
val ((names21, names32), nctxt) = Name.context
|
|
96 |
|> invents "f" (length Ts21)
|
|
97 |
||>> invents "f" (length Ts32);
|
|
98 |
val T1 = Type (tyco, Ts1);
|
|
99 |
val T2 = Type (tyco, Ts2);
|
|
100 |
val T3 = Type (tyco, Ts3);
|
|
101 |
val x = Free (the_single (Name.invents nctxt "a" 1), T3);
|
|
102 |
val (args21, args32) = (names21 ~~ Ts21, names32 ~~ Ts32);
|
|
103 |
val args31 = map2 (fn is_contra => fn ((f21, T21), (f32, T32)) =>
|
|
104 |
if not is_contra then
|
|
105 |
Abs ("x", domain_type T32, Free (f21, T21) $ (Free (f32, T32) $ Bound 0))
|
|
106 |
else
|
|
107 |
Abs ("x", domain_type T21, Free (f32, T32) $ (Free (f21, T21) $ Bound 0))
|
|
108 |
) contras (args21 ~~ args32)
|
|
109 |
fun mk_mapper T T' args = list_comb (Const (mapper,
|
|
110 |
map fastype_of args ---> T --> T'), args);
|
|
111 |
val lhs = mk_mapper T2 T1 (map Free args21) $
|
|
112 |
(mk_mapper T3 T2 (map Free args32) $ x);
|
|
113 |
val rhs = mk_mapper T3 T1 args31 $ x;
|
|
114 |
in (x, (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhs, rhs)) end;
|
|
115 |
|
|
116 |
fun make_identity_prop variances (tyco, mapper) =
|
|
117 |
let
|
|
118 |
val vs = Name.invents Name.context Name.aT (length variances);
|
|
119 |
val Ts = map2 (fn v => fn (sort, _) => TFree (v, sort)) vs variances;
|
|
120 |
fun bool_num b = if b then 1 else 0;
|
|
121 |
fun mk_argT (T, (_, (co, contra))) =
|
|
122 |
replicate (bool_num co + bool_num contra) (T --> T)
|
|
123 |
val Ts' = maps mk_argT (Ts ~~ variances)
|
|
124 |
val T = Type (tyco, Ts);
|
|
125 |
val x = Free ("a", T);
|
|
126 |
val lhs = list_comb (Const (mapper, Ts' ---> T --> T),
|
|
127 |
map (fn T => Abs ("x", domain_type T, Bound 0)) Ts') $ x;
|
|
128 |
in (x, (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhs, x)) end;
|
|
129 |
|
|
130 |
|
|
131 |
(* registering primitive mappers *)
|
|
132 |
|
|
133 |
fun register tyco mapper variances raw_concatenate raw_identity thy =
|
|
134 |
let
|
|
135 |
val (_, concatenate_prop) = make_concatenate_prop variances (tyco, mapper);
|
|
136 |
val (_, identity_prop) = make_identity_prop variances (tyco, mapper);
|
|
137 |
val concatenate = Goal.prove_global thy
|
|
138 |
(Term.add_free_names concatenate_prop []) [] concatenate_prop
|
|
139 |
(K (ALLGOALS (ProofContext.fact_tac [raw_concatenate])));
|
|
140 |
val identity = Goal.prove_global thy
|
|
141 |
(Term.add_free_names identity_prop []) [] identity_prop
|
|
142 |
(K (ALLGOALS (ProofContext.fact_tac [raw_identity])));
|
|
143 |
in
|
|
144 |
thy
|
|
145 |
|> Data.map (Symtab.update (tyco, { mapper = mapper,
|
|
146 |
variances = variances,
|
|
147 |
concatenate = concatenate, identity = identity }))
|
|
148 |
end;
|
|
149 |
|
|
150 |
end;
|