175
|
1 |
(* Title: HOL/datatype.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Max Breitling, Carsten Clasohm, Tobias Nipkow, Norbert Voelker
|
211
|
4 |
Copyright 1995 TU Muenchen
|
129
|
5 |
*)
|
|
6 |
|
|
7 |
|
|
8 |
(*used for constructor parameters*)
|
|
9 |
datatype dt_type = dtVar of string |
|
|
10 |
dtTyp of dt_type list * string |
|
|
11 |
dtRek of dt_type list * string;
|
|
12 |
|
|
13 |
local
|
|
14 |
|
|
15 |
val mysort = sort;
|
186
|
16 |
open ThyParse HOLogic;
|
129
|
17 |
exception Impossible;
|
|
18 |
exception RecError of string;
|
|
19 |
|
|
20 |
val is_dtRek = (fn dtRek _ => true | _ => false);
|
|
21 |
fun opt_parens s = if s = "" then "" else enclose "(" ")" s;
|
|
22 |
|
|
23 |
(* ----------------------------------------------------------------------- *)
|
|
24 |
(* Derivation of the primrec combinator application from the equations *)
|
|
25 |
|
211
|
26 |
(* substitute fname(ls,xk,rs) by yk(ls,rs) in t for (xk,yk) in pairs *)
|
129
|
27 |
|
|
28 |
fun subst_apps (_,_) [] t = t
|
211
|
29 |
| subst_apps (fname,rpos) pairs t =
|
129
|
30 |
let
|
|
31 |
fun subst (Abs(a,T,t)) = Abs(a,T,subst t)
|
|
32 |
| subst (funct $ body) =
|
|
33 |
let val (f,b) = strip_comb (funct$body)
|
|
34 |
in
|
|
35 |
if is_Const f andalso fst(dest_Const f) = fname
|
|
36 |
then
|
211
|
37 |
let val (ls,rest) = (take(rpos,b), drop(rpos,b));
|
129
|
38 |
val (xk,rs) = (hd rest,tl rest)
|
|
39 |
handle LIST _ => raise RecError "not enough arguments \
|
|
40 |
\ in recursive application on rhs"
|
|
41 |
in
|
|
42 |
(case assoc (pairs,xk) of
|
|
43 |
None => raise RecError
|
|
44 |
("illegal occurence of " ^ fname ^ " on rhs")
|
211
|
45 |
| Some(U) => list_comb(U,map subst (ls @ rs)))
|
129
|
46 |
end
|
|
47 |
else list_comb(f, map subst b)
|
|
48 |
end
|
|
49 |
| subst(t) = t
|
|
50 |
in subst t end;
|
|
51 |
|
|
52 |
(* abstract rhs *)
|
|
53 |
|
211
|
54 |
fun abst_rec (fname,rpos,tc,ls,cargs,rs,rhs) =
|
129
|
55 |
let val rargs = (map fst o
|
|
56 |
(filter (fn (a,T) => is_dtRek T))) (cargs ~~ tc);
|
|
57 |
val subs = map (fn (s,T) => (s,dummyT))
|
|
58 |
(rev(rename_wrt_term rhs rargs));
|
211
|
59 |
val subst_rhs = subst_apps (fname,rpos)
|
129
|
60 |
(map Free rargs ~~ map Free subs) rhs;
|
211
|
61 |
in
|
|
62 |
list_abs_free (cargs @ subs @ ls @ rs, subst_rhs)
|
129
|
63 |
end;
|
|
64 |
|
|
65 |
(* parsing the prim rec equations *)
|
|
66 |
|
|
67 |
fun dest_eq ( Const("Trueprop",_) $ (Const ("op =",_) $ lhs $ rhs))
|
|
68 |
= (lhs, rhs)
|
|
69 |
| dest_eq _ = raise RecError "not a proper equation";
|
|
70 |
|
|
71 |
fun dest_rec eq =
|
|
72 |
let val (lhs,rhs) = dest_eq eq;
|
|
73 |
val (name,args) = strip_comb lhs;
|
|
74 |
val (ls',rest) = take_prefix is_Free args;
|
|
75 |
val (middle,rs') = take_suffix is_Free rest;
|
211
|
76 |
val rpos = length ls';
|
129
|
77 |
val (c,cargs') = strip_comb (hd middle)
|
|
78 |
handle LIST "hd" => raise RecError "constructor missing";
|
|
79 |
val (ls,cargs,rs) = (map dest_Free ls', map dest_Free cargs'
|
|
80 |
, map dest_Free rs')
|
|
81 |
handle TERM ("dest_Free",_) =>
|
|
82 |
raise RecError "constructor has illegal argument in pattern";
|
|
83 |
in
|
|
84 |
if length middle > 1 then
|
|
85 |
raise RecError "more than one non-variable in pattern"
|
|
86 |
else if not(null(findrep (map fst (ls @ rs @ cargs)))) then
|
|
87 |
raise RecError "repeated variable name in pattern"
|
|
88 |
else (fst(dest_Const name) handle TERM _ =>
|
|
89 |
raise RecError "function is not declared as constant in theory"
|
211
|
90 |
,rpos,ls,fst( dest_Const c),cargs,rs,rhs)
|
129
|
91 |
end;
|
|
92 |
|
|
93 |
(* check function specified for all constructors and sort function terms *)
|
|
94 |
|
|
95 |
fun check_and_sort (n,its) =
|
|
96 |
if length its = n
|
|
97 |
then map snd (mysort (fn ((i : int,_),(j,_)) => i<j) its)
|
|
98 |
else raise error "Primrec definition error:\n\
|
|
99 |
\Please give an equation for every constructor";
|
|
100 |
|
|
101 |
(* translate rec equations into function arguments suitable for rec comb *)
|
|
102 |
(* theory parameter needed for printing error messages *)
|
|
103 |
|
|
104 |
fun trans_recs _ _ [] = error("No primrec equations.")
|
|
105 |
| trans_recs thy cs' (eq1::eqs) =
|
211
|
106 |
let val (name1,rpos1,ls1,_,_,_,_) = dest_rec eq1
|
129
|
107 |
handle RecError s =>
|
|
108 |
error("Primrec definition error: " ^ s ^ ":\n"
|
|
109 |
^ " " ^ Sign.string_of_term (sign_of thy) eq1);
|
|
110 |
val tcs = map (fn (_,c,T,_) => (c,T)) cs';
|
|
111 |
val cs = map fst tcs;
|
|
112 |
fun trans_recs' _ [] = []
|
|
113 |
| trans_recs' cis (eq::eqs) =
|
211
|
114 |
let val (name,rpos,ls,c,cargs,rs,rhs) = dest_rec eq;
|
129
|
115 |
val tc = assoc(tcs,c);
|
|
116 |
val i = (1 + find (c,cs)) handle LIST "find" => 0;
|
|
117 |
in
|
|
118 |
if name <> name1 then
|
|
119 |
raise RecError "function names inconsistent"
|
211
|
120 |
else if rpos <> rpos1 then
|
129
|
121 |
raise RecError "position of rec. argument inconsistent"
|
|
122 |
else if i = 0 then
|
|
123 |
raise RecError "illegal argument in pattern"
|
|
124 |
else if i mem cis then
|
|
125 |
raise RecError "constructor already occured as pattern "
|
211
|
126 |
else (i,abst_rec (name,rpos,the tc,ls,cargs,rs,rhs))
|
129
|
127 |
:: trans_recs' (i::cis) eqs
|
|
128 |
end
|
|
129 |
handle RecError s =>
|
|
130 |
error("Primrec definition error\n" ^ s ^ "\n"
|
|
131 |
^ " " ^ Sign.string_of_term (sign_of thy) eq);
|
|
132 |
in ( name1, ls1
|
|
133 |
, check_and_sort (length cs, trans_recs' [] (eq1::eqs)))
|
|
134 |
end ;
|
|
135 |
|
|
136 |
in
|
|
137 |
fun add_datatype (typevars, tname, cons_list') thy =
|
|
138 |
let (*search for free type variables and convert recursive *)
|
|
139 |
fun analyse_types (cons, typlist, syn) =
|
|
140 |
let fun analyse(t as dtVar v) =
|
|
141 |
if t mem typevars then t
|
|
142 |
else error ("Free type variable " ^ v ^ " on rhs.")
|
|
143 |
| analyse(dtTyp(typl,s)) =
|
|
144 |
if tname <> s then dtTyp(analyses typl, s)
|
|
145 |
else if typevars = typl then dtRek(typl, s)
|
|
146 |
else error (s ^ " used in different ways")
|
|
147 |
| analyse(dtRek _) = raise Impossible
|
|
148 |
and analyses ts = map analyse ts;
|
|
149 |
in (cons, Syntax.const_name cons syn, analyses typlist, syn)
|
|
150 |
end;
|
|
151 |
|
|
152 |
(*test if all elements are recursive, i.e. if the type is empty*)
|
|
153 |
|
|
154 |
fun non_empty (cs : ('a * 'b * dt_type list * 'c) list) =
|
|
155 |
not(forall (exists is_dtRek o #3) cs) orelse
|
|
156 |
error("Empty datatype not allowed!");
|
|
157 |
|
|
158 |
val cons_list = map analyse_types cons_list';
|
|
159 |
val dummy = non_empty cons_list;
|
|
160 |
val num_of_cons = length cons_list;
|
|
161 |
|
|
162 |
(* Auxiliary functions to construct argument and equation lists *)
|
|
163 |
|
|
164 |
(*generate 'var_n, ..., var_m'*)
|
|
165 |
fun Args(var, delim, n, m) =
|
|
166 |
space_implode delim (map (fn n => var^string_of_int(n)) (n upto m));
|
|
167 |
|
|
168 |
(*generate 'name_1', ..., 'name_n'*)
|
|
169 |
fun C_exp(name, n, var) =
|
|
170 |
if n > 0 then name ^ parens(Args(var, ",", 1, n)) else name;
|
|
171 |
|
|
172 |
(*generate 'x_n = y_n, ..., x_m = y_m'*)
|
|
173 |
fun Arg_eql(n,m) =
|
|
174 |
if n=m then "x" ^ string_of_int(n) ^ "=y" ^ string_of_int(n)
|
|
175 |
else "x" ^ string_of_int(n) ^ "=y" ^ string_of_int(n) ^ " & " ^
|
|
176 |
Arg_eql(n+1, m);
|
|
177 |
|
|
178 |
(*Pretty printers for type lists;
|
|
179 |
pp_typlist1: parentheses, pp_typlist2: brackets*)
|
|
180 |
fun pp_typ (dtVar s) = s
|
|
181 |
| pp_typ (dtTyp (typvars, id)) =
|
|
182 |
if null typvars then id else (pp_typlist1 typvars) ^ id
|
|
183 |
| pp_typ (dtRek (typvars, id)) = (pp_typlist1 typvars) ^ id
|
|
184 |
and
|
|
185 |
pp_typlist' ts = commas (map pp_typ ts)
|
|
186 |
and
|
|
187 |
pp_typlist1 ts = if null ts then "" else parens (pp_typlist' ts);
|
|
188 |
|
|
189 |
fun pp_typlist2 ts = if null ts then "" else brackets (pp_typlist' ts);
|
|
190 |
|
|
191 |
(* Generate syntax translation for case rules *)
|
|
192 |
fun calc_xrules c_nr y_nr ((_, name, typlist, _) :: cs) =
|
|
193 |
let val arity = length typlist;
|
|
194 |
val body = "z" ^ string_of_int(c_nr);
|
|
195 |
val args1 = if arity=0 then ""
|
|
196 |
else parens (Args ("y", ",", y_nr, y_nr+arity-1));
|
|
197 |
val args2 = if arity=0 then ""
|
|
198 |
else "% " ^ Args ("y", " ", y_nr, y_nr+arity-1)
|
|
199 |
^ ". ";
|
|
200 |
val (rest1,rest2) =
|
|
201 |
if null cs then ("","")
|
|
202 |
else let val (h1, h2) = calc_xrules (c_nr+1) (y_nr+arity) cs
|
|
203 |
in (" | " ^ h1, ", " ^ h2) end;
|
|
204 |
in (name ^ args1 ^ " => " ^ body ^ rest1, args2 ^ body ^ rest2) end
|
|
205 |
| calc_xrules _ _ [] = raise Impossible;
|
|
206 |
|
|
207 |
val xrules =
|
|
208 |
let val (first_part, scnd_part) = calc_xrules 1 1 cons_list
|
|
209 |
in [("logic", "case x of " ^ first_part) <->
|
|
210 |
("logic", tname ^ "_case(" ^ scnd_part ^ ", x)" )]
|
|
211 |
end;
|
|
212 |
|
|
213 |
(*type declarations for constructors*)
|
|
214 |
fun const_type (id, _, typlist, syn) =
|
|
215 |
(id,
|
|
216 |
(if null typlist then "" else pp_typlist2 typlist ^ " => ") ^
|
|
217 |
pp_typlist1 typevars ^ tname, syn);
|
|
218 |
|
|
219 |
|
|
220 |
fun assumpt (dtRek _ :: ts, v :: vs ,found) =
|
|
221 |
let val h = if found then ";P(" ^ v ^ ")" else "[| P(" ^ v ^ ")"
|
|
222 |
in h ^ (assumpt (ts, vs, true)) end
|
|
223 |
| assumpt (t :: ts, v :: vs, found) = assumpt (ts, vs, found)
|
|
224 |
| assumpt ([], [], found) = if found then "|] ==>" else ""
|
|
225 |
| assumpt _ = raise Impossible;
|
|
226 |
|
|
227 |
(*insert type with suggested name 'varname' into table*)
|
|
228 |
fun insert typ varname ((tri as (t, s, n)) :: xs) =
|
|
229 |
if typ = t then (t, s, n+1) :: xs
|
|
230 |
else tri :: (if varname = s then insert typ (varname ^ "'") xs
|
|
231 |
else insert typ varname xs)
|
|
232 |
| insert typ varname [] = [(typ, varname, 1)];
|
|
233 |
|
|
234 |
fun typid(dtRek(_,id)) = id
|
|
235 |
| typid(dtVar s) = implode (tl (explode s))
|
|
236 |
| typid(dtTyp(_,id)) = id;
|
|
237 |
|
|
238 |
val insert_types = foldl (fn (tab,typ) => insert typ (typid typ) tab);
|
|
239 |
|
|
240 |
fun update(dtRek _, s, v :: vs, (dtRek _) :: ts) = s :: vs
|
|
241 |
| update(t, s, v :: vs, t1 :: ts) =
|
|
242 |
if t=t1 then s :: vs else v :: (update (t, s, vs, ts))
|
|
243 |
| update _ = raise Impossible;
|
|
244 |
|
|
245 |
fun update_n (dtRek r1, s, v :: vs, (dtRek r2) :: ts, n) =
|
|
246 |
if r1 = r2 then (s ^ string_of_int n) ::
|
|
247 |
(update_n (dtRek r1, s, vs, ts, n+1))
|
|
248 |
else v :: (update_n (dtRek r1, s, vs, ts, n))
|
|
249 |
| update_n (t, s, v :: vs, t1 :: ts, n) =
|
|
250 |
if t = t1 then (s ^ string_of_int n) ::
|
|
251 |
(update_n (t, s, vs, ts, n+1))
|
|
252 |
else v :: (update_n (t, s, vs, ts, n))
|
|
253 |
| update_n (_,_,[],[],_) = []
|
|
254 |
| update_n _ = raise Impossible;
|
|
255 |
|
|
256 |
(*insert type variables into table*)
|
|
257 |
fun convert typs =
|
|
258 |
let fun conv(vars, (t, s, n)) =
|
|
259 |
if n=1 then update (t, s, vars, typs)
|
|
260 |
else update_n (t, s, vars, typs, 1)
|
|
261 |
in foldl conv
|
|
262 |
end;
|
|
263 |
|
|
264 |
fun empty_list n = replicate n "";
|
|
265 |
|
|
266 |
fun t_inducting ((_, name, typl, _) :: cs) =
|
|
267 |
let val tab = insert_types([],typl);
|
|
268 |
val arity = length typl;
|
|
269 |
val var_list = convert typl (empty_list arity,tab);
|
|
270 |
val h = if arity = 0 then " P(" ^ name ^ ")"
|
|
271 |
else " !!" ^ (space_implode " " var_list) ^ "." ^
|
|
272 |
(assumpt (typl, var_list, false)) ^ "P(" ^
|
|
273 |
name ^ "(" ^ (commas var_list) ^ "))";
|
|
274 |
val rest = t_inducting cs;
|
|
275 |
in if rest = "" then h else h ^ "; " ^ rest end
|
|
276 |
| t_inducting [] = "";
|
|
277 |
|
|
278 |
fun t_induct cl typ_name =
|
|
279 |
"[|" ^ t_inducting cl ^ "|] ==> P(" ^ typ_name ^ ")";
|
|
280 |
|
|
281 |
fun gen_typlist typevar f ((_, _, ts, _) :: cs) =
|
|
282 |
let val h = if (length ts) > 0
|
|
283 |
then pp_typlist2(f ts) ^ "=>"
|
|
284 |
else ""
|
|
285 |
in h ^ typevar ^ "," ^ (gen_typlist typevar f cs) end
|
|
286 |
| gen_typlist _ _ [] = "";
|
|
287 |
|
|
288 |
|
|
289 |
(* -------------------------------------------------------------------- *)
|
|
290 |
(* The case constant and rules *)
|
|
291 |
|
|
292 |
val t_case = tname ^ "_case";
|
|
293 |
|
|
294 |
fun case_rule n (id, name, ts, _) =
|
|
295 |
let val args = opt_parens(Args("x", ",", 1, length ts))
|
|
296 |
in (t_case ^ "_" ^ id,
|
|
297 |
t_case ^ "(" ^ Args("f", ",", 1, num_of_cons)
|
|
298 |
^ "," ^ name ^ args
|
|
299 |
^ ") = f" ^ string_of_int(n) ^ args)
|
|
300 |
end
|
|
301 |
|
|
302 |
fun case_rules n (c :: cs) = case_rule n c :: case_rules(n+1) cs
|
|
303 |
| case_rules _ [] = [];
|
|
304 |
|
|
305 |
val datatype_arity = length typevars;
|
|
306 |
|
|
307 |
val types = [(tname, datatype_arity, NoSyn)];
|
|
308 |
|
|
309 |
val arities =
|
186
|
310 |
let val term_list = replicate datatype_arity termS;
|
|
311 |
in [(tname, term_list, termS)]
|
129
|
312 |
end;
|
|
313 |
|
|
314 |
val datatype_name = pp_typlist1 typevars ^ tname;
|
|
315 |
|
|
316 |
val new_tvar_name = variant (map (fn dtVar s => s) typevars) "'z";
|
|
317 |
|
|
318 |
val case_const =
|
|
319 |
(t_case,
|
|
320 |
"[" ^ gen_typlist new_tvar_name I cons_list
|
|
321 |
^ pp_typlist1 typevars ^ tname ^ "] =>" ^ new_tvar_name,
|
|
322 |
NoSyn);
|
|
323 |
|
|
324 |
val rules_case = case_rules 1 cons_list;
|
|
325 |
|
|
326 |
(* -------------------------------------------------------------------- *)
|
|
327 |
(* The prim-rec combinator *)
|
|
328 |
|
|
329 |
val t_rec = tname ^ "_rec"
|
|
330 |
|
|
331 |
(* adding type variables for dtRek types to end of list of dt_types *)
|
|
332 |
|
|
333 |
fun add_reks ts =
|
|
334 |
ts @ map (fn _ => dtVar new_tvar_name) (filter is_dtRek ts);
|
|
335 |
|
|
336 |
(* positions of the dtRek types in a list of dt_types, starting from 1 *)
|
|
337 |
|
|
338 |
fun rek_pos ts =
|
|
339 |
map snd (filter (is_dtRek o fst) (ts ~~ (1 upto length ts)))
|
|
340 |
|
|
341 |
fun rec_rule n (id,name,ts,_) =
|
|
342 |
let val args = Args("x",",",1,length ts)
|
|
343 |
val fargs = Args("f",",",1,num_of_cons)
|
|
344 |
fun rarg i = "," ^ t_rec ^ parens(fargs ^ "," ^ "x" ^
|
|
345 |
string_of_int(i))
|
|
346 |
val rargs = implode (map rarg (rek_pos ts))
|
|
347 |
in
|
|
348 |
( t_rec ^ "_" ^ id
|
|
349 |
, t_rec ^ parens(fargs ^ "," ^ name ^ (opt_parens args)) ^ " = f"
|
|
350 |
^ string_of_int(n) ^ opt_parens (args ^ rargs))
|
|
351 |
end
|
|
352 |
|
|
353 |
fun rec_rules n (c::cs) = rec_rule n c :: rec_rules (n+1) cs
|
|
354 |
| rec_rules _ [] = [];
|
|
355 |
|
|
356 |
val rec_const =
|
|
357 |
(t_rec,
|
|
358 |
"[" ^ (gen_typlist new_tvar_name add_reks cons_list)
|
|
359 |
^ (pp_typlist1 typevars) ^ tname ^ "] =>" ^ new_tvar_name,
|
|
360 |
NoSyn);
|
|
361 |
|
|
362 |
val rules_rec = rec_rules 1 cons_list
|
|
363 |
|
|
364 |
(* -------------------------------------------------------------------- *)
|
|
365 |
val consts =
|
|
366 |
map const_type cons_list
|
|
367 |
@ (if num_of_cons < dtK then []
|
|
368 |
else [(tname ^ "_ord", datatype_name ^ "=>nat", NoSyn)])
|
|
369 |
@ [case_const,rec_const];
|
|
370 |
|
|
371 |
|
|
372 |
fun Ci_ing ((id, name, typlist, _) :: cs) =
|
|
373 |
let val arity = length typlist;
|
|
374 |
in if arity = 0 then Ci_ing cs
|
|
375 |
else ("inject_" ^ id,
|
|
376 |
"(" ^ C_exp(name,arity,"x") ^ "=" ^ C_exp(name,arity,"y")
|
|
377 |
^ ") = (" ^ Arg_eql (1, arity) ^ ")") :: (Ci_ing cs)
|
|
378 |
end
|
|
379 |
| Ci_ing [] = [];
|
|
380 |
|
|
381 |
fun Ci_negOne (id1, name1, tl1, _) (id2, name2, tl2, _) =
|
|
382 |
let val ax = C_exp(name1, length tl1, "x") ^ "~=" ^
|
|
383 |
C_exp(name2, length tl2, "y")
|
|
384 |
in (id1 ^ "_not_" ^ id2, ax)
|
|
385 |
end;
|
|
386 |
|
|
387 |
fun Ci_neg1 [] = []
|
|
388 |
| Ci_neg1 (c1::cs) = (map (Ci_negOne c1) cs) @ Ci_neg1 cs;
|
|
389 |
|
|
390 |
fun suc_expr n =
|
|
391 |
if n=0 then "0" else "Suc(" ^ suc_expr(n-1) ^ ")";
|
|
392 |
|
|
393 |
fun Ci_neg2() =
|
|
394 |
let val ord_t = tname ^ "_ord";
|
|
395 |
val cis = cons_list ~~ (0 upto (num_of_cons - 1))
|
|
396 |
fun Ci_neg2equals ((id, name, typlist, _), n) =
|
|
397 |
let val ax = ord_t ^ "(" ^ (C_exp(name, length typlist, "x"))
|
|
398 |
^ ") = " ^ (suc_expr n)
|
|
399 |
in (ord_t ^ "_" ^ id, ax) end
|
|
400 |
in (ord_t ^ "_distinct", ord_t^"(x) ~= "^ord_t^"(y) ==> x ~= y") ::
|
|
401 |
(map Ci_neg2equals cis)
|
|
402 |
end;
|
|
403 |
|
|
404 |
val rules_distinct = if num_of_cons < dtK then Ci_neg1 cons_list
|
|
405 |
else Ci_neg2();
|
|
406 |
|
|
407 |
val rules_inject = Ci_ing cons_list;
|
|
408 |
|
|
409 |
val rule_induct = (tname ^ "_induct", t_induct cons_list tname);
|
|
410 |
|
|
411 |
val rules = rule_induct ::
|
|
412 |
(rules_inject @ rules_distinct @ rules_case @ rules_rec);
|
|
413 |
|
|
414 |
fun add_primrec eqns thy =
|
|
415 |
let val rec_comb = Const(t_rec,dummyT)
|
143
|
416 |
val teqns = map (fn neq => snd(read_axm (sign_of thy) neq)) eqns
|
129
|
417 |
val (fname,ls,fns) = trans_recs thy cons_list teqns
|
|
418 |
val rhs =
|
|
419 |
list_abs_free
|
|
420 |
(ls @ [(tname,dummyT)]
|
|
421 |
,list_comb(rec_comb
|
|
422 |
, fns @ map Bound (0 ::(length ls downto 1))));
|
|
423 |
val sg = sign_of thy;
|
147
|
424 |
val defpair = mk_defpair (Const(fname,dummyT),rhs)
|
|
425 |
val defpairT as (_, _ $ Const(_,T) $ _ ) = inferT_axm sg defpair;
|
129
|
426 |
val varT = Type.varifyT T;
|
186
|
427 |
val ftyp = the (Sign.const_type sg fname);
|
129
|
428 |
in
|
|
429 |
if Type.typ_instance (#tsig(Sign.rep_sg sg), ftyp, varT)
|
147
|
430 |
then add_defs_i [defpairT] thy
|
129
|
431 |
else error("Primrec definition error: \ntype of " ^ fname
|
|
432 |
^ " is not instance of type deduced from equations")
|
|
433 |
end;
|
|
434 |
|
|
435 |
in
|
|
436 |
(thy
|
|
437 |
|> add_types types
|
|
438 |
|> add_arities arities
|
|
439 |
|> add_consts consts
|
|
440 |
|> add_trrules xrules
|
|
441 |
|> add_axioms rules,add_primrec)
|
|
442 |
end
|
|
443 |
end
|
211
|
444 |
|
|
445 |
(*
|
|
446 |
Informal description of functions used in datatype.ML for the Isabelle/HOL
|
|
447 |
implementation of prim. rec. function definitions. (N. Voelker, Feb. 1995)
|
|
448 |
|
|
449 |
* subst_apps (fname,rpos) pairs t:
|
|
450 |
substitute the term
|
|
451 |
fname(ls,xk,rs)
|
|
452 |
by
|
|
453 |
yk(ls,rs)
|
|
454 |
in t for (xk,yk) in pairs, where rpos = length ls.
|
|
455 |
Applied with :
|
|
456 |
fname = function name
|
|
457 |
rpos = position of recursive argument
|
|
458 |
pairs = list of pairs (xk,yk), where
|
|
459 |
xk are the rec. arguments of the constructor in the pattern,
|
|
460 |
yk is a variable with name derived from xk
|
|
461 |
t = rhs of equation
|
|
462 |
|
|
463 |
* abst_rec (fname,rpos,tc,ls,cargs,rs,rhs)
|
|
464 |
- filter recursive arguments from constructor arguments cargs,
|
|
465 |
- perform substitutions on rhs,
|
|
466 |
- derive list subs of new variable names yk for use in subst_apps,
|
|
467 |
- abstract rhs with respect to cargs, subs, ls and rs.
|
|
468 |
|
|
469 |
* dest_eq t
|
|
470 |
destruct a term denoting an equation into lhs and rhs.
|
|
471 |
|
|
472 |
* dest_req eq
|
|
473 |
destruct an equation of the form
|
|
474 |
name (vl1..vlrpos, Ci(vi1..vin), vr1..vrn) = rhs
|
|
475 |
into
|
|
476 |
- function name (name)
|
|
477 |
- position of the first non-variable parameter (rpos)
|
|
478 |
- the list of first rpos parameters (ls = [vl1..vlrpos])
|
|
479 |
- the constructor (fst( dest_Const c) = Ci)
|
|
480 |
- the arguments of the constructor (cargs = [vi1..vin])
|
|
481 |
- the rest of the variables in the pattern (rs = [vr1..vrn])
|
|
482 |
- the right hand side of the equation (rhs).
|
|
483 |
|
|
484 |
* check_and_sort (n,its)
|
|
485 |
check that n = length its holds, and sort elements of its by
|
|
486 |
first component.
|
|
487 |
|
|
488 |
* trans_recs thy cs' (eq1::eqs)
|
|
489 |
destruct eq1 into name1, rpos1, ls1, etc..
|
|
490 |
get constructor list with and without type (tcs resp. cs) from cs',
|
|
491 |
for every equation:
|
|
492 |
destruct it into (name,rpos,ls,c,cargs,rs,rhs)
|
|
493 |
get typed constructor tc from c and tcs
|
|
494 |
determine the index i of the constructor
|
|
495 |
check function name and position of rec. argument by comparison
|
|
496 |
with first equation
|
|
497 |
check for repeated variable names in pattern
|
|
498 |
derive function term f_i which is used as argument of the rec. combinator
|
|
499 |
sort the terms f_i according to i and return them together
|
|
500 |
with the function name and the parameter of the definition (ls).
|
|
501 |
|
|
502 |
* Application:
|
|
503 |
|
|
504 |
The rec. combinator is applied to the function terms resulting from
|
|
505 |
trans_rec. This results in a function which takes the recursive arg.
|
|
506 |
as first parameter and then the arguments corresponding to ls. The
|
|
507 |
order of parameters is corrected by setting the rhs equal to
|
|
508 |
|
|
509 |
list_abs_free
|
|
510 |
(ls @ [(tname,dummyT)]
|
|
511 |
,list_comb(rec_comb
|
|
512 |
, fns @ map Bound (0 ::(length ls downto 1))));
|
|
513 |
|
|
514 |
Note the de-Bruijn indices counting the number of lambdas between the
|
|
515 |
variable and its binding.
|
|
516 |
*)
|