175
|
1 |
(* Title: HOL/datatype.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Max Breitling, Carsten Clasohm, Tobias Nipkow, Norbert Voelker
|
129
|
4 |
Copyright 1994 TU Muenchen
|
|
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 |
|
|
26 |
(* subst. applications fname(ls,xk,rs) by yk(ls,rs) for xk in rargs *)
|
|
27 |
|
|
28 |
fun subst_apps (_,_) [] t = t
|
|
29 |
| subst_apps (fname,cpos) pairs t =
|
|
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
|
|
37 |
let val (ls,rest) = (take(cpos,b), drop (cpos,b));
|
|
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")
|
|
45 |
| Some(U) => list_comb(U,ls @ rs))
|
|
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 |
|
|
54 |
fun abst_rec (fname,cpos,tc,ls,cargs,rs,rhs) =
|
|
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));
|
|
59 |
val subst_rhs = subst_apps (fname,cpos)
|
|
60 |
(map Free rargs ~~ map Free subs) rhs;
|
|
61 |
val res = list_abs_free (cargs @ subs @ ls @ rs, subst_rhs);
|
|
62 |
in
|
|
63 |
if fname mem add_term_names (res,[])
|
|
64 |
then raise RecError ("illegal occurence of " ^ fname ^ " on rhs")
|
|
65 |
else res
|
|
66 |
end;
|
|
67 |
|
|
68 |
(* parsing the prim rec equations *)
|
|
69 |
|
|
70 |
fun dest_eq ( Const("Trueprop",_) $ (Const ("op =",_) $ lhs $ rhs))
|
|
71 |
= (lhs, rhs)
|
|
72 |
| dest_eq _ = raise RecError "not a proper equation";
|
|
73 |
|
|
74 |
fun dest_rec eq =
|
|
75 |
let val (lhs,rhs) = dest_eq eq;
|
|
76 |
val (name,args) = strip_comb lhs;
|
|
77 |
val (ls',rest) = take_prefix is_Free args;
|
|
78 |
val (middle,rs') = take_suffix is_Free rest;
|
|
79 |
val cpos = length ls';
|
|
80 |
val (c,cargs') = strip_comb (hd middle)
|
|
81 |
handle LIST "hd" => raise RecError "constructor missing";
|
|
82 |
val (ls,cargs,rs) = (map dest_Free ls', map dest_Free cargs'
|
|
83 |
, map dest_Free rs')
|
|
84 |
handle TERM ("dest_Free",_) =>
|
|
85 |
raise RecError "constructor has illegal argument in pattern";
|
|
86 |
in
|
|
87 |
if length middle > 1 then
|
|
88 |
raise RecError "more than one non-variable in pattern"
|
|
89 |
else if not(null(findrep (map fst (ls @ rs @ cargs)))) then
|
|
90 |
raise RecError "repeated variable name in pattern"
|
|
91 |
else (fst(dest_Const name) handle TERM _ =>
|
|
92 |
raise RecError "function is not declared as constant in theory"
|
|
93 |
,cpos,ls,fst( dest_Const c),cargs,rs,rhs)
|
|
94 |
end;
|
|
95 |
|
|
96 |
(* check function specified for all constructors and sort function terms *)
|
|
97 |
|
|
98 |
fun check_and_sort (n,its) =
|
|
99 |
if length its = n
|
|
100 |
then map snd (mysort (fn ((i : int,_),(j,_)) => i<j) its)
|
|
101 |
else raise error "Primrec definition error:\n\
|
|
102 |
\Please give an equation for every constructor";
|
|
103 |
|
|
104 |
(* translate rec equations into function arguments suitable for rec comb *)
|
|
105 |
(* theory parameter needed for printing error messages *)
|
|
106 |
|
|
107 |
fun trans_recs _ _ [] = error("No primrec equations.")
|
|
108 |
| trans_recs thy cs' (eq1::eqs) =
|
|
109 |
let val (name1,cpos1,ls1,_,_,_,_) = dest_rec eq1
|
|
110 |
handle RecError s =>
|
|
111 |
error("Primrec definition error: " ^ s ^ ":\n"
|
|
112 |
^ " " ^ Sign.string_of_term (sign_of thy) eq1);
|
|
113 |
val tcs = map (fn (_,c,T,_) => (c,T)) cs';
|
|
114 |
val cs = map fst tcs;
|
|
115 |
fun trans_recs' _ [] = []
|
|
116 |
| trans_recs' cis (eq::eqs) =
|
|
117 |
let val (name,cpos,ls,c,cargs,rs,rhs) = dest_rec eq;
|
|
118 |
val tc = assoc(tcs,c);
|
|
119 |
val i = (1 + find (c,cs)) handle LIST "find" => 0;
|
|
120 |
in
|
|
121 |
if name <> name1 then
|
|
122 |
raise RecError "function names inconsistent"
|
|
123 |
else if cpos <> cpos1 then
|
|
124 |
raise RecError "position of rec. argument inconsistent"
|
|
125 |
else if i = 0 then
|
|
126 |
raise RecError "illegal argument in pattern"
|
|
127 |
else if i mem cis then
|
|
128 |
raise RecError "constructor already occured as pattern "
|
|
129 |
else (i,abst_rec (name,cpos,the tc,ls,cargs,rs,rhs))
|
|
130 |
:: trans_recs' (i::cis) eqs
|
|
131 |
end
|
|
132 |
handle RecError s =>
|
|
133 |
error("Primrec definition error\n" ^ s ^ "\n"
|
|
134 |
^ " " ^ Sign.string_of_term (sign_of thy) eq);
|
|
135 |
in ( name1, ls1
|
|
136 |
, check_and_sort (length cs, trans_recs' [] (eq1::eqs)))
|
|
137 |
end ;
|
|
138 |
|
|
139 |
in
|
|
140 |
fun add_datatype (typevars, tname, cons_list') thy =
|
|
141 |
let (*search for free type variables and convert recursive *)
|
|
142 |
fun analyse_types (cons, typlist, syn) =
|
|
143 |
let fun analyse(t as dtVar v) =
|
|
144 |
if t mem typevars then t
|
|
145 |
else error ("Free type variable " ^ v ^ " on rhs.")
|
|
146 |
| analyse(dtTyp(typl,s)) =
|
|
147 |
if tname <> s then dtTyp(analyses typl, s)
|
|
148 |
else if typevars = typl then dtRek(typl, s)
|
|
149 |
else error (s ^ " used in different ways")
|
|
150 |
| analyse(dtRek _) = raise Impossible
|
|
151 |
and analyses ts = map analyse ts;
|
|
152 |
in (cons, Syntax.const_name cons syn, analyses typlist, syn)
|
|
153 |
end;
|
|
154 |
|
|
155 |
(*test if all elements are recursive, i.e. if the type is empty*)
|
|
156 |
|
|
157 |
fun non_empty (cs : ('a * 'b * dt_type list * 'c) list) =
|
|
158 |
not(forall (exists is_dtRek o #3) cs) orelse
|
|
159 |
error("Empty datatype not allowed!");
|
|
160 |
|
|
161 |
val cons_list = map analyse_types cons_list';
|
|
162 |
val dummy = non_empty cons_list;
|
|
163 |
val num_of_cons = length cons_list;
|
|
164 |
|
|
165 |
(* Auxiliary functions to construct argument and equation lists *)
|
|
166 |
|
|
167 |
(*generate 'var_n, ..., var_m'*)
|
|
168 |
fun Args(var, delim, n, m) =
|
|
169 |
space_implode delim (map (fn n => var^string_of_int(n)) (n upto m));
|
|
170 |
|
|
171 |
(*generate 'name_1', ..., 'name_n'*)
|
|
172 |
fun C_exp(name, n, var) =
|
|
173 |
if n > 0 then name ^ parens(Args(var, ",", 1, n)) else name;
|
|
174 |
|
|
175 |
(*generate 'x_n = y_n, ..., x_m = y_m'*)
|
|
176 |
fun Arg_eql(n,m) =
|
|
177 |
if n=m then "x" ^ string_of_int(n) ^ "=y" ^ string_of_int(n)
|
|
178 |
else "x" ^ string_of_int(n) ^ "=y" ^ string_of_int(n) ^ " & " ^
|
|
179 |
Arg_eql(n+1, m);
|
|
180 |
|
|
181 |
(*Pretty printers for type lists;
|
|
182 |
pp_typlist1: parentheses, pp_typlist2: brackets*)
|
|
183 |
fun pp_typ (dtVar s) = s
|
|
184 |
| pp_typ (dtTyp (typvars, id)) =
|
|
185 |
if null typvars then id else (pp_typlist1 typvars) ^ id
|
|
186 |
| pp_typ (dtRek (typvars, id)) = (pp_typlist1 typvars) ^ id
|
|
187 |
and
|
|
188 |
pp_typlist' ts = commas (map pp_typ ts)
|
|
189 |
and
|
|
190 |
pp_typlist1 ts = if null ts then "" else parens (pp_typlist' ts);
|
|
191 |
|
|
192 |
fun pp_typlist2 ts = if null ts then "" else brackets (pp_typlist' ts);
|
|
193 |
|
|
194 |
(* Generate syntax translation for case rules *)
|
|
195 |
fun calc_xrules c_nr y_nr ((_, name, typlist, _) :: cs) =
|
|
196 |
let val arity = length typlist;
|
|
197 |
val body = "z" ^ string_of_int(c_nr);
|
|
198 |
val args1 = if arity=0 then ""
|
|
199 |
else parens (Args ("y", ",", y_nr, y_nr+arity-1));
|
|
200 |
val args2 = if arity=0 then ""
|
|
201 |
else "% " ^ Args ("y", " ", y_nr, y_nr+arity-1)
|
|
202 |
^ ". ";
|
|
203 |
val (rest1,rest2) =
|
|
204 |
if null cs then ("","")
|
|
205 |
else let val (h1, h2) = calc_xrules (c_nr+1) (y_nr+arity) cs
|
|
206 |
in (" | " ^ h1, ", " ^ h2) end;
|
|
207 |
in (name ^ args1 ^ " => " ^ body ^ rest1, args2 ^ body ^ rest2) end
|
|
208 |
| calc_xrules _ _ [] = raise Impossible;
|
|
209 |
|
|
210 |
val xrules =
|
|
211 |
let val (first_part, scnd_part) = calc_xrules 1 1 cons_list
|
|
212 |
in [("logic", "case x of " ^ first_part) <->
|
|
213 |
("logic", tname ^ "_case(" ^ scnd_part ^ ", x)" )]
|
|
214 |
end;
|
|
215 |
|
|
216 |
(*type declarations for constructors*)
|
|
217 |
fun const_type (id, _, typlist, syn) =
|
|
218 |
(id,
|
|
219 |
(if null typlist then "" else pp_typlist2 typlist ^ " => ") ^
|
|
220 |
pp_typlist1 typevars ^ tname, syn);
|
|
221 |
|
|
222 |
|
|
223 |
fun assumpt (dtRek _ :: ts, v :: vs ,found) =
|
|
224 |
let val h = if found then ";P(" ^ v ^ ")" else "[| P(" ^ v ^ ")"
|
|
225 |
in h ^ (assumpt (ts, vs, true)) end
|
|
226 |
| assumpt (t :: ts, v :: vs, found) = assumpt (ts, vs, found)
|
|
227 |
| assumpt ([], [], found) = if found then "|] ==>" else ""
|
|
228 |
| assumpt _ = raise Impossible;
|
|
229 |
|
|
230 |
(*insert type with suggested name 'varname' into table*)
|
|
231 |
fun insert typ varname ((tri as (t, s, n)) :: xs) =
|
|
232 |
if typ = t then (t, s, n+1) :: xs
|
|
233 |
else tri :: (if varname = s then insert typ (varname ^ "'") xs
|
|
234 |
else insert typ varname xs)
|
|
235 |
| insert typ varname [] = [(typ, varname, 1)];
|
|
236 |
|
|
237 |
fun typid(dtRek(_,id)) = id
|
|
238 |
| typid(dtVar s) = implode (tl (explode s))
|
|
239 |
| typid(dtTyp(_,id)) = id;
|
|
240 |
|
|
241 |
val insert_types = foldl (fn (tab,typ) => insert typ (typid typ) tab);
|
|
242 |
|
|
243 |
fun update(dtRek _, s, v :: vs, (dtRek _) :: ts) = s :: vs
|
|
244 |
| update(t, s, v :: vs, t1 :: ts) =
|
|
245 |
if t=t1 then s :: vs else v :: (update (t, s, vs, ts))
|
|
246 |
| update _ = raise Impossible;
|
|
247 |
|
|
248 |
fun update_n (dtRek r1, s, v :: vs, (dtRek r2) :: ts, n) =
|
|
249 |
if r1 = r2 then (s ^ string_of_int n) ::
|
|
250 |
(update_n (dtRek r1, s, vs, ts, n+1))
|
|
251 |
else v :: (update_n (dtRek r1, s, vs, ts, n))
|
|
252 |
| update_n (t, s, v :: vs, t1 :: ts, n) =
|
|
253 |
if t = t1 then (s ^ string_of_int n) ::
|
|
254 |
(update_n (t, s, vs, ts, n+1))
|
|
255 |
else v :: (update_n (t, s, vs, ts, n))
|
|
256 |
| update_n (_,_,[],[],_) = []
|
|
257 |
| update_n _ = raise Impossible;
|
|
258 |
|
|
259 |
(*insert type variables into table*)
|
|
260 |
fun convert typs =
|
|
261 |
let fun conv(vars, (t, s, n)) =
|
|
262 |
if n=1 then update (t, s, vars, typs)
|
|
263 |
else update_n (t, s, vars, typs, 1)
|
|
264 |
in foldl conv
|
|
265 |
end;
|
|
266 |
|
|
267 |
fun empty_list n = replicate n "";
|
|
268 |
|
|
269 |
fun t_inducting ((_, name, typl, _) :: cs) =
|
|
270 |
let val tab = insert_types([],typl);
|
|
271 |
val arity = length typl;
|
|
272 |
val var_list = convert typl (empty_list arity,tab);
|
|
273 |
val h = if arity = 0 then " P(" ^ name ^ ")"
|
|
274 |
else " !!" ^ (space_implode " " var_list) ^ "." ^
|
|
275 |
(assumpt (typl, var_list, false)) ^ "P(" ^
|
|
276 |
name ^ "(" ^ (commas var_list) ^ "))";
|
|
277 |
val rest = t_inducting cs;
|
|
278 |
in if rest = "" then h else h ^ "; " ^ rest end
|
|
279 |
| t_inducting [] = "";
|
|
280 |
|
|
281 |
fun t_induct cl typ_name =
|
|
282 |
"[|" ^ t_inducting cl ^ "|] ==> P(" ^ typ_name ^ ")";
|
|
283 |
|
|
284 |
fun gen_typlist typevar f ((_, _, ts, _) :: cs) =
|
|
285 |
let val h = if (length ts) > 0
|
|
286 |
then pp_typlist2(f ts) ^ "=>"
|
|
287 |
else ""
|
|
288 |
in h ^ typevar ^ "," ^ (gen_typlist typevar f cs) end
|
|
289 |
| gen_typlist _ _ [] = "";
|
|
290 |
|
|
291 |
|
|
292 |
(* -------------------------------------------------------------------- *)
|
|
293 |
(* The case constant and rules *)
|
|
294 |
|
|
295 |
val t_case = tname ^ "_case";
|
|
296 |
|
|
297 |
fun case_rule n (id, name, ts, _) =
|
|
298 |
let val args = opt_parens(Args("x", ",", 1, length ts))
|
|
299 |
in (t_case ^ "_" ^ id,
|
|
300 |
t_case ^ "(" ^ Args("f", ",", 1, num_of_cons)
|
|
301 |
^ "," ^ name ^ args
|
|
302 |
^ ") = f" ^ string_of_int(n) ^ args)
|
|
303 |
end
|
|
304 |
|
|
305 |
fun case_rules n (c :: cs) = case_rule n c :: case_rules(n+1) cs
|
|
306 |
| case_rules _ [] = [];
|
|
307 |
|
|
308 |
val datatype_arity = length typevars;
|
|
309 |
|
|
310 |
val types = [(tname, datatype_arity, NoSyn)];
|
|
311 |
|
|
312 |
val arities =
|
186
|
313 |
let val term_list = replicate datatype_arity termS;
|
|
314 |
in [(tname, term_list, termS)]
|
129
|
315 |
end;
|
|
316 |
|
|
317 |
val datatype_name = pp_typlist1 typevars ^ tname;
|
|
318 |
|
|
319 |
val new_tvar_name = variant (map (fn dtVar s => s) typevars) "'z";
|
|
320 |
|
|
321 |
val case_const =
|
|
322 |
(t_case,
|
|
323 |
"[" ^ gen_typlist new_tvar_name I cons_list
|
|
324 |
^ pp_typlist1 typevars ^ tname ^ "] =>" ^ new_tvar_name,
|
|
325 |
NoSyn);
|
|
326 |
|
|
327 |
val rules_case = case_rules 1 cons_list;
|
|
328 |
|
|
329 |
(* -------------------------------------------------------------------- *)
|
|
330 |
(* The prim-rec combinator *)
|
|
331 |
|
|
332 |
val t_rec = tname ^ "_rec"
|
|
333 |
|
|
334 |
(* adding type variables for dtRek types to end of list of dt_types *)
|
|
335 |
|
|
336 |
fun add_reks ts =
|
|
337 |
ts @ map (fn _ => dtVar new_tvar_name) (filter is_dtRek ts);
|
|
338 |
|
|
339 |
(* positions of the dtRek types in a list of dt_types, starting from 1 *)
|
|
340 |
|
|
341 |
fun rek_pos ts =
|
|
342 |
map snd (filter (is_dtRek o fst) (ts ~~ (1 upto length ts)))
|
|
343 |
|
|
344 |
fun rec_rule n (id,name,ts,_) =
|
|
345 |
let val args = Args("x",",",1,length ts)
|
|
346 |
val fargs = Args("f",",",1,num_of_cons)
|
|
347 |
fun rarg i = "," ^ t_rec ^ parens(fargs ^ "," ^ "x" ^
|
|
348 |
string_of_int(i))
|
|
349 |
val rargs = implode (map rarg (rek_pos ts))
|
|
350 |
in
|
|
351 |
( t_rec ^ "_" ^ id
|
|
352 |
, t_rec ^ parens(fargs ^ "," ^ name ^ (opt_parens args)) ^ " = f"
|
|
353 |
^ string_of_int(n) ^ opt_parens (args ^ rargs))
|
|
354 |
end
|
|
355 |
|
|
356 |
fun rec_rules n (c::cs) = rec_rule n c :: rec_rules (n+1) cs
|
|
357 |
| rec_rules _ [] = [];
|
|
358 |
|
|
359 |
val rec_const =
|
|
360 |
(t_rec,
|
|
361 |
"[" ^ (gen_typlist new_tvar_name add_reks cons_list)
|
|
362 |
^ (pp_typlist1 typevars) ^ tname ^ "] =>" ^ new_tvar_name,
|
|
363 |
NoSyn);
|
|
364 |
|
|
365 |
val rules_rec = rec_rules 1 cons_list
|
|
366 |
|
|
367 |
(* -------------------------------------------------------------------- *)
|
|
368 |
val consts =
|
|
369 |
map const_type cons_list
|
|
370 |
@ (if num_of_cons < dtK then []
|
|
371 |
else [(tname ^ "_ord", datatype_name ^ "=>nat", NoSyn)])
|
|
372 |
@ [case_const,rec_const];
|
|
373 |
|
|
374 |
|
|
375 |
fun Ci_ing ((id, name, typlist, _) :: cs) =
|
|
376 |
let val arity = length typlist;
|
|
377 |
in if arity = 0 then Ci_ing cs
|
|
378 |
else ("inject_" ^ id,
|
|
379 |
"(" ^ C_exp(name,arity,"x") ^ "=" ^ C_exp(name,arity,"y")
|
|
380 |
^ ") = (" ^ Arg_eql (1, arity) ^ ")") :: (Ci_ing cs)
|
|
381 |
end
|
|
382 |
| Ci_ing [] = [];
|
|
383 |
|
|
384 |
fun Ci_negOne (id1, name1, tl1, _) (id2, name2, tl2, _) =
|
|
385 |
let val ax = C_exp(name1, length tl1, "x") ^ "~=" ^
|
|
386 |
C_exp(name2, length tl2, "y")
|
|
387 |
in (id1 ^ "_not_" ^ id2, ax)
|
|
388 |
end;
|
|
389 |
|
|
390 |
fun Ci_neg1 [] = []
|
|
391 |
| Ci_neg1 (c1::cs) = (map (Ci_negOne c1) cs) @ Ci_neg1 cs;
|
|
392 |
|
|
393 |
fun suc_expr n =
|
|
394 |
if n=0 then "0" else "Suc(" ^ suc_expr(n-1) ^ ")";
|
|
395 |
|
|
396 |
fun Ci_neg2() =
|
|
397 |
let val ord_t = tname ^ "_ord";
|
|
398 |
val cis = cons_list ~~ (0 upto (num_of_cons - 1))
|
|
399 |
fun Ci_neg2equals ((id, name, typlist, _), n) =
|
|
400 |
let val ax = ord_t ^ "(" ^ (C_exp(name, length typlist, "x"))
|
|
401 |
^ ") = " ^ (suc_expr n)
|
|
402 |
in (ord_t ^ "_" ^ id, ax) end
|
|
403 |
in (ord_t ^ "_distinct", ord_t^"(x) ~= "^ord_t^"(y) ==> x ~= y") ::
|
|
404 |
(map Ci_neg2equals cis)
|
|
405 |
end;
|
|
406 |
|
|
407 |
val rules_distinct = if num_of_cons < dtK then Ci_neg1 cons_list
|
|
408 |
else Ci_neg2();
|
|
409 |
|
|
410 |
val rules_inject = Ci_ing cons_list;
|
|
411 |
|
|
412 |
val rule_induct = (tname ^ "_induct", t_induct cons_list tname);
|
|
413 |
|
|
414 |
val rules = rule_induct ::
|
|
415 |
(rules_inject @ rules_distinct @ rules_case @ rules_rec);
|
|
416 |
|
|
417 |
fun add_primrec eqns thy =
|
|
418 |
let val rec_comb = Const(t_rec,dummyT)
|
143
|
419 |
val teqns = map (fn neq => snd(read_axm (sign_of thy) neq)) eqns
|
129
|
420 |
val (fname,ls,fns) = trans_recs thy cons_list teqns
|
|
421 |
val rhs =
|
|
422 |
list_abs_free
|
|
423 |
(ls @ [(tname,dummyT)]
|
|
424 |
,list_comb(rec_comb
|
|
425 |
, fns @ map Bound (0 ::(length ls downto 1))));
|
|
426 |
val sg = sign_of thy;
|
147
|
427 |
val defpair = mk_defpair (Const(fname,dummyT),rhs)
|
|
428 |
val defpairT as (_, _ $ Const(_,T) $ _ ) = inferT_axm sg defpair;
|
129
|
429 |
val varT = Type.varifyT T;
|
186
|
430 |
val ftyp = the (Sign.const_type sg fname);
|
129
|
431 |
in
|
|
432 |
if Type.typ_instance (#tsig(Sign.rep_sg sg), ftyp, varT)
|
147
|
433 |
then add_defs_i [defpairT] thy
|
129
|
434 |
else error("Primrec definition error: \ntype of " ^ fname
|
|
435 |
^ " is not instance of type deduced from equations")
|
|
436 |
end;
|
|
437 |
|
|
438 |
in
|
|
439 |
(thy
|
|
440 |
|> add_types types
|
|
441 |
|> add_arities arities
|
|
442 |
|> add_consts consts
|
|
443 |
|> add_trrules xrules
|
|
444 |
|> add_axioms rules,add_primrec)
|
|
445 |
end
|
|
446 |
end
|