57634
|
1 |
(* Title: HOL/BNF_Examples/Compat.thy
|
|
2 |
Author: Jasmin Blanchette, TU Muenchen
|
|
3 |
Copyright 2014
|
|
4 |
|
|
5 |
Tests for compatibility with the old datatype package.
|
|
6 |
*)
|
|
7 |
|
58125
|
8 |
header \<open> Tests for Compatibility with the Old Datatype Package \<close>
|
57634
|
9 |
|
56454
|
10 |
theory Compat
|
|
11 |
imports Main
|
|
12 |
begin
|
|
13 |
|
58125
|
14 |
subsection \<open> Viewing and Registering New-Style Datatypes as Old-Style Ones \<close>
|
|
15 |
|
58124
|
16 |
ML \<open>
|
|
17 |
fun check_len n xs label =
|
|
18 |
length xs = n orelse error ("Expected length " ^ string_of_int (length xs) ^ " for " ^ label);
|
|
19 |
|
|
20 |
fun check_lens (n1, n2, n3) (xs1, xs2, xs3) =
|
|
21 |
check_len n1 xs1 "old" andalso check_len n2 xs2 "unfold" andalso check_len n3 xs3 "keep";
|
|
22 |
|
|
23 |
fun get_descrs thy lens T_name =
|
|
24 |
(these (Option.map #descr (Old_Datatype_Data.get_info thy T_name)),
|
|
25 |
these (Option.map #descr (BNF_LFP_Compat.get_info thy BNF_LFP_Compat.Unfold_Nesting T_name)),
|
|
26 |
these (Option.map #descr (BNF_LFP_Compat.get_info thy BNF_LFP_Compat.Keep_Nesting T_name)))
|
|
27 |
|> tap (check_lens lens);
|
|
28 |
\<close>
|
|
29 |
|
|
30 |
datatype 'a old_lst = Old_Nl | Old_Cns 'a "'a old_lst"
|
|
31 |
|
58125
|
32 |
ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name old_lst}; \<close>
|
58124
|
33 |
|
56454
|
34 |
datatype_new 'a lst = Nl | Cns 'a "'a lst"
|
58124
|
35 |
|
58125
|
36 |
ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name lst}; \<close>
|
58124
|
37 |
|
56454
|
38 |
datatype_compat lst
|
|
39 |
|
58125
|
40 |
ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name lst}; \<close>
|
58124
|
41 |
|
56456
|
42 |
datatype_new 'b w = W | W' "'b w \<times> 'b list"
|
58124
|
43 |
|
|
44 |
(* no support for sums of products:
|
56454
|
45 |
datatype_compat w
|
|
46 |
*)
|
|
47 |
|
58125
|
48 |
ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name w}; \<close>
|
58124
|
49 |
|
56454
|
50 |
datatype_new ('c, 'b) s = L 'c | R 'b
|
58124
|
51 |
|
58125
|
52 |
ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name s}; \<close>
|
58124
|
53 |
|
56454
|
54 |
datatype_new 'd x = X | X' "('d x lst, 'd list) s"
|
58124
|
55 |
|
58125
|
56 |
ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name x}; \<close>
|
58124
|
57 |
|
56454
|
58 |
datatype_compat s
|
58124
|
59 |
|
58125
|
60 |
ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name s}; \<close>
|
|
61 |
ML \<open> get_descrs @{theory} (0, 3, 1) @{type_name x}; \<close>
|
58124
|
62 |
|
56454
|
63 |
datatype_compat x
|
|
64 |
|
58125
|
65 |
ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name x}; \<close>
|
58124
|
66 |
|
56454
|
67 |
datatype_new 'a tttre = TTTre 'a "'a tttre lst lst lst"
|
58124
|
68 |
|
58125
|
69 |
ML \<open> get_descrs @{theory} (0, 4, 1) @{type_name tttre}; \<close>
|
58124
|
70 |
|
56454
|
71 |
datatype_compat tttre
|
|
72 |
|
58125
|
73 |
ML \<open> get_descrs @{theory} (4, 4, 1) @{type_name tttre}; \<close>
|
58124
|
74 |
|
56454
|
75 |
datatype_new 'a ftre = FEmp | FTre "'a \<Rightarrow> 'a ftre lst"
|
58124
|
76 |
|
58125
|
77 |
ML \<open> get_descrs @{theory} (0, 2, 1) @{type_name ftre}; \<close>
|
58124
|
78 |
|
56454
|
79 |
datatype_compat ftre
|
|
80 |
|
58125
|
81 |
ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name ftre}; \<close>
|
58124
|
82 |
|
56454
|
83 |
datatype_new 'a btre = BTre 'a "'a btre lst" "'a btre lst"
|
58124
|
84 |
|
58125
|
85 |
ML \<open> get_descrs @{theory} (0, 3, 1) @{type_name btre}; \<close>
|
58124
|
86 |
|
56454
|
87 |
datatype_compat btre
|
|
88 |
|
58125
|
89 |
ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name btre}; \<close>
|
58124
|
90 |
|
56454
|
91 |
datatype_new 'a foo = Foo | Foo' 'a "'a bar" and 'a bar = Bar | Bar' 'a "'a foo"
|
58124
|
92 |
|
58125
|
93 |
ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name foo}; \<close>
|
|
94 |
ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name bar}; \<close>
|
58124
|
95 |
|
56454
|
96 |
datatype_compat foo bar
|
|
97 |
|
58125
|
98 |
ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name foo}; \<close>
|
|
99 |
ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name bar}; \<close>
|
58124
|
100 |
|
56454
|
101 |
datatype_new 'a tre = Tre 'a "'a tre lst"
|
58124
|
102 |
|
58125
|
103 |
ML \<open> get_descrs @{theory} (0, 2, 1) @{type_name tre}; \<close>
|
58124
|
104 |
|
56454
|
105 |
datatype_compat tre
|
|
106 |
|
58125
|
107 |
ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name tre}; \<close>
|
58124
|
108 |
|
56454
|
109 |
fun f_tre and f_tres where
|
|
110 |
"f_tre (Tre a ts) = {a} \<union> f_tres ts"
|
|
111 |
| "f_tres Nl = {}"
|
|
112 |
| "f_tres (Cns t ts) = f_tres ts"
|
|
113 |
|
|
114 |
datatype_new 'a f = F 'a and 'a g = G 'a
|
58124
|
115 |
|
58125
|
116 |
ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name f}; \<close>
|
|
117 |
ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name g}; \<close>
|
58124
|
118 |
|
56454
|
119 |
datatype_new h = H "h f" | H'
|
58124
|
120 |
|
58125
|
121 |
ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name h}; \<close>
|
58124
|
122 |
|
56454
|
123 |
datatype_compat f g
|
58124
|
124 |
|
58125
|
125 |
ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name f}; \<close>
|
|
126 |
ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name g}; \<close>
|
|
127 |
ML \<open> get_descrs @{theory} (0, 3, 1) @{type_name h}; \<close>
|
58124
|
128 |
|
56454
|
129 |
datatype_compat h
|
|
130 |
|
58125
|
131 |
ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name h}; \<close>
|
58124
|
132 |
|
56454
|
133 |
datatype_new myunit = MyUnity
|
58124
|
134 |
|
58125
|
135 |
ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name myunit}; \<close>
|
58124
|
136 |
|
56454
|
137 |
datatype_compat myunit
|
|
138 |
|
58125
|
139 |
ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name myunit}; \<close>
|
58124
|
140 |
|
56454
|
141 |
datatype_new mylist = MyNil | MyCons nat mylist
|
58124
|
142 |
|
58125
|
143 |
ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name mylist}; \<close>
|
58124
|
144 |
|
56454
|
145 |
datatype_compat mylist
|
|
146 |
|
58125
|
147 |
ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name mylist}; \<close>
|
58124
|
148 |
|
56454
|
149 |
fun f_mylist where
|
|
150 |
"f_mylist MyNil = 0"
|
|
151 |
| "f_mylist (MyCons _ xs) = Suc (f_mylist xs)"
|
|
152 |
|
|
153 |
datatype_new foo' = FooNil | FooCons bar' foo' and bar' = Bar
|
58124
|
154 |
|
58125
|
155 |
ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name foo'}; \<close>
|
|
156 |
ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name bar'}; \<close>
|
58124
|
157 |
|
56454
|
158 |
datatype_compat bar' foo'
|
|
159 |
|
58125
|
160 |
ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name foo'}; \<close>
|
|
161 |
ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name bar'}; \<close>
|
58124
|
162 |
|
56454
|
163 |
fun f_foo and f_bar where
|
|
164 |
"f_foo FooNil = 0"
|
|
165 |
| "f_foo (FooCons bar foo) = Suc (f_foo foo) + f_bar bar"
|
|
166 |
| "f_bar Bar = Suc 0"
|
|
167 |
|
|
168 |
locale opt begin
|
|
169 |
|
|
170 |
datatype_new 'a opt = Non | Som 'a
|
58124
|
171 |
|
58125
|
172 |
ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name opt}; \<close>
|
58124
|
173 |
|
56454
|
174 |
datatype_compat opt
|
|
175 |
|
58125
|
176 |
ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name opt}; \<close>
|
58124
|
177 |
|
56454
|
178 |
end
|
|
179 |
|
|
180 |
datatype funky = Funky "funky tre" | Funky'
|
58124
|
181 |
|
58125
|
182 |
ML \<open> get_descrs @{theory} (3, 3, 3) @{type_name funky}; \<close>
|
58124
|
183 |
|
56454
|
184 |
datatype fnky = Fnky "nat tre"
|
|
185 |
|
58125
|
186 |
ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name fnky}; \<close>
|
58124
|
187 |
|
56454
|
188 |
datatype_new tree = Tree "tree foo"
|
58124
|
189 |
|
58125
|
190 |
ML \<open> get_descrs @{theory} (0, 3, 1) @{type_name tree}; \<close>
|
58124
|
191 |
|
56488
|
192 |
datatype_compat tree
|
56454
|
193 |
|
58125
|
194 |
ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name tree}; \<close>
|
|
195 |
|
|
196 |
|
|
197 |
subsection \<open> Creating a New-Style Datatype Using an Old-Style Interface \<close>
|
|
198 |
|
|
199 |
ML \<open>
|
|
200 |
val l_specs =
|
|
201 |
[((@{binding l}, [("'a", @{sort type})], NoSyn),
|
|
202 |
[(@{binding N}, [], NoSyn),
|
|
203 |
(@{binding C}, [@{typ 'a}, Type (Sign.full_name @{theory} @{binding l}, [@{typ 'a}])], NoSyn)])];
|
|
204 |
\<close>
|
|
205 |
|
|
206 |
setup \<open> snd o BNF_LFP_Compat.add_datatype BNF_LFP_Compat.Unfold_Nesting l_specs \<close>
|
|
207 |
|
|
208 |
ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name l}; \<close>
|
|
209 |
|
|
210 |
thm l.exhaust l.map l.induct l.rec l.size
|
|
211 |
|
|
212 |
ML \<open>
|
|
213 |
val t_specs =
|
|
214 |
[((@{binding t}, [("'b", @{sort type})], NoSyn),
|
|
215 |
[(@{binding T}, [@{typ 'b}, Type (@{type_name l},
|
|
216 |
[Type (Sign.full_name @{theory} @{binding t}, [@{typ 'b}])])], NoSyn)])];
|
|
217 |
\<close>
|
|
218 |
|
|
219 |
setup \<open> snd o BNF_LFP_Compat.add_datatype BNF_LFP_Compat.Unfold_Nesting t_specs \<close>
|
|
220 |
|
|
221 |
ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name t}; \<close>
|
|
222 |
|
|
223 |
thm t.exhaust t.map t.induct t.rec t.size
|
56454
|
224 |
|
|
225 |
end
|