1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
1 |
theory Fv
|
1178
|
2 |
imports "Nominal2_Atoms"
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
3 |
begin
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
4 |
|
1169
|
5 |
(* Bindings are given as a list which has a length being equal
|
1172
|
6 |
to the length of the number of constructors.
|
|
7 |
|
|
8 |
Each element is a list whose length is equal to the number
|
|
9 |
of arguents.
|
|
10 |
|
|
11 |
Every element specifies bindings of this argument given as
|
|
12 |
a tuple: function, bound argument.
|
1169
|
13 |
|
|
14 |
Eg:
|
|
15 |
nominal_datatype
|
|
16 |
|
|
17 |
C1
|
|
18 |
| C2 x y z bind x in z
|
1172
|
19 |
| C3 x y z bind f x in z bind g y in z
|
1169
|
20 |
|
|
21 |
yields:
|
1172
|
22 |
[
|
|
23 |
[],
|
|
24 |
[[], [], [(NONE, 0)]],
|
1173
|
25 |
[[], [], [(SOME (Const f), 0), (Some (Const g), 1)]]]
|
1185
|
26 |
|
|
27 |
A SOME binding has to have a function returning an atom set,
|
|
28 |
and a NONE binding has to be on an argument that is an atom
|
|
29 |
or an atom set.
|
|
30 |
|
1169
|
31 |
*)
|
|
32 |
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
33 |
ML {*
|
1175
|
34 |
open Datatype_Aux; (* typ_of_dtyp, DtRec, ... *);
|
1178
|
35 |
(* TODO: It is the same as one in 'nominal_atoms' *)
|
1175
|
36 |
fun mk_atom ty = Const (@{const_name atom}, ty --> @{typ atom});
|
|
37 |
val noatoms = @{term "{} :: atom set"};
|
|
38 |
fun mk_single_atom x = HOLogic.mk_set @{typ atom} [mk_atom (type_of x) $ x];
|
|
39 |
fun mk_union sets =
|
|
40 |
fold (fn a => fn b =>
|
|
41 |
if a = noatoms then b else
|
|
42 |
if b = noatoms then a else
|
|
43 |
HOLogic.mk_binop @{const_name union} (a, b)) (rev sets) noatoms;
|
|
44 |
fun mk_diff a b =
|
|
45 |
if b = noatoms then a else
|
|
46 |
if b = a then noatoms else
|
|
47 |
HOLogic.mk_binop @{const_name minus} (a, b);
|
1185
|
48 |
fun mk_atoms t =
|
|
49 |
let
|
|
50 |
val ty = fastype_of t;
|
|
51 |
val atom_ty = HOLogic.dest_setT ty --> @{typ atom};
|
|
52 |
val img_ty = atom_ty --> ty --> @{typ "atom set"};
|
|
53 |
in
|
|
54 |
(Const (@{const_name image}, img_ty) $ Const (@{const_name atom}, atom_ty) $ t)
|
|
55 |
end;
|
|
56 |
(* Copy from Term *)
|
|
57 |
fun is_funtype (Type ("fun", [_, _])) = true
|
|
58 |
| is_funtype _ = false;
|
1175
|
59 |
*}
|
|
60 |
|
|
61 |
ML {*
|
1178
|
62 |
(* Currently needs just one full_tname to access Datatype *)
|
|
63 |
fun define_raw_fv full_tname bindsall lthy =
|
|
64 |
let
|
1185
|
65 |
val thy = ProofContext.theory_of lthy;
|
1178
|
66 |
val {descr, ...} = Datatype.the_info thy full_tname;
|
|
67 |
val sorts = []; (* TODO *)
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
68 |
fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
69 |
val fv_names = Datatype_Prop.indexify_names (map (fn (i, _) =>
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
70 |
"fv_" ^ name_of_typ (nth_dtyp i)) descr);
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
71 |
val fv_types = map (fn (i, _) => nth_dtyp i --> @{typ "atom set"}) descr;
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
72 |
val fv_frees = map Free (fv_names ~~ fv_types);
|
1173
|
73 |
fun fv_eq_constr i (cname, dts) bindcs =
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
74 |
let
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
75 |
val Ts = map (typ_of_dtyp descr sorts) dts;
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
76 |
val names = Name.variant_list ["pi"] (Datatype_Prop.make_tnames Ts);
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
77 |
val args = map Free (names ~~ Ts);
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
78 |
val c = Const (cname, Ts ---> (nth_dtyp i));
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
79 |
val fv_c = Free (nth fv_names i, (nth_dtyp i) --> @{typ "atom set"});
|
1177
|
80 |
fun fv_bind (NONE, i) =
|
|
81 |
if is_rec_type (nth dts i) then (nth fv_frees (body_index (nth dts i))) $ (nth args i) else
|
|
82 |
(* TODO we assume that all can be 'atomized' *)
|
1185
|
83 |
if (is_funtype o fastype_of) (nth args i) then mk_atoms (nth args i) else
|
1177
|
84 |
mk_single_atom (nth args i)
|
1174
|
85 |
| fv_bind (SOME f, i) = f $ (nth args i);
|
1173
|
86 |
fun fv_arg ((dt, x), bindxs) =
|
1172
|
87 |
let
|
|
88 |
val arg =
|
1175
|
89 |
if is_rec_type dt then nth fv_frees (body_index dt) $ x else
|
1174
|
90 |
(* TODO: we just assume everything can be 'atomized' *)
|
1185
|
91 |
if (is_funtype o fastype_of) x then mk_atoms x else
|
|
92 |
HOLogic.mk_set @{typ atom} [mk_atom (fastype_of x) $ x]
|
1173
|
93 |
val sub = mk_union (map fv_bind bindxs)
|
1172
|
94 |
in
|
1173
|
95 |
mk_diff arg sub
|
|
96 |
end;
|
1176
|
97 |
val _ = tracing ("d" ^ string_of_int (length dts));
|
|
98 |
val _ = tracing (string_of_int (length args));
|
|
99 |
val _ = tracing (string_of_int (length bindcs));
|
1173
|
100 |
in
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
101 |
(Attrib.empty_binding, HOLogic.mk_Trueprop (HOLogic.mk_eq
|
1173
|
102 |
(fv_c $ list_comb (c, args), mk_union (map fv_arg (dts ~~ args ~~ bindcs)))))
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
103 |
end;
|
1176
|
104 |
fun fv_eq (i, (_, _, constrs)) binds = map2 (fv_eq_constr i) constrs binds;
|
|
105 |
val fv_eqs = flat (map2 fv_eq descr bindsall)
|
1178
|
106 |
in
|
|
107 |
snd (Primrec.add_primrec
|
|
108 |
(map (fn s => (Binding.name s, NONE, NoSyn)) fv_names) fv_eqs lthy)
|
|
109 |
end
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
110 |
*}
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
111 |
|
1185
|
112 |
(* tests:
|
|
113 |
|
1178
|
114 |
atom_decl name
|
|
115 |
|
1185
|
116 |
datatype ty =
|
|
117 |
Var "name set"
|
|
118 |
|
|
119 |
ML {* Syntax.check_term @{context} (mk_atoms @{term "a :: name set"}) *}
|
|
120 |
|
|
121 |
local_setup {* define_raw_fv "Fv.ty" [[[[]]]] *}
|
|
122 |
print_theorems
|
|
123 |
|
1178
|
124 |
datatype rtrm1 =
|
|
125 |
rVr1 "name"
|
|
126 |
| rAp1 "rtrm1" "rtrm1"
|
|
127 |
| rLm1 "name" "rtrm1" --"name is bound in trm1"
|
|
128 |
| rLt1 "bp" "rtrm1" "rtrm1" --"all variables in bp are bound in the 2nd trm1"
|
|
129 |
and bp =
|
|
130 |
BUnit
|
|
131 |
| BVr "name"
|
|
132 |
| BPr "bp" "bp"
|
|
133 |
|
|
134 |
(* to be given by the user *)
|
|
135 |
|
|
136 |
primrec
|
|
137 |
bv1
|
|
138 |
where
|
|
139 |
"bv1 (BUnit) = {}"
|
|
140 |
| "bv1 (BVr x) = {atom x}"
|
|
141 |
| "bv1 (BPr bp1 bp2) = (bv1 bp1) \<union> (bv1 bp1)"
|
|
142 |
|
|
143 |
local_setup {* define_raw_fv "Fv.rtrm1"
|
|
144 |
[[[[]], [[], []], [[(NONE, 0)], [(NONE, 0)]], [[(NONE, 0)], [], [(SOME @{term bv1}, 0)]]],
|
|
145 |
[[], [[]], [[], []]]] *}
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
146 |
print_theorems
|
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
147 |
|
1180
|
148 |
*)
|
|
149 |
|
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
150 |
end
|