266 |
266 |
267 |
267 |
268 (*Accumulate all 'loose' bound vars referring to level 'lev' or beyond. |
268 (*Accumulate all 'loose' bound vars referring to level 'lev' or beyond. |
269 (Bound 0) is loose at level 0 *) |
269 (Bound 0) is loose at level 0 *) |
270 fun add_loose_bnos (Bound i, lev, js) = |
270 fun add_loose_bnos (Bound i, lev, js) = |
271 if i<lev then js else (i-lev) ins js |
271 if i<lev then js else (i-lev) ins_int js |
272 | add_loose_bnos (Abs (_,_,t), lev, js) = add_loose_bnos (t, lev+1, js) |
272 | add_loose_bnos (Abs (_,_,t), lev, js) = add_loose_bnos (t, lev+1, js) |
273 | add_loose_bnos (f$t, lev, js) = |
273 | add_loose_bnos (f$t, lev, js) = |
274 add_loose_bnos (f, lev, add_loose_bnos (t, lev, js)) |
274 add_loose_bnos (f, lev, add_loose_bnos (t, lev, js)) |
275 | add_loose_bnos (_, _, js) = js; |
275 | add_loose_bnos (_, _, js) = js; |
276 |
276 |
315 | (Bound i) aconv (Bound j) = i=j |
315 | (Bound i) aconv (Bound j) = i=j |
316 | (Abs(_,T,t)) aconv (Abs(_,U,u)) = t aconv u andalso T=U |
316 | (Abs(_,T,t)) aconv (Abs(_,U,u)) = t aconv u andalso T=U |
317 | (f$t) aconv (g$u) = (f aconv g) andalso (t aconv u) |
317 | (f$t) aconv (g$u) = (f aconv g) andalso (t aconv u) |
318 | _ aconv _ = false; |
318 | _ aconv _ = false; |
319 |
319 |
|
320 (** Membership, insertion, union for terms **) |
|
321 |
|
322 fun mem_term (_, []) = false |
|
323 | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts); |
|
324 |
|
325 fun ins_term(t,ts) = if mem_term(t,ts) then ts else t :: ts; |
|
326 |
|
327 fun union_term (xs, []) = xs |
|
328 | union_term ([], ys) = ys |
|
329 | union_term ((x :: xs), ys) = union_term (xs, ins_term (x, ys)); |
|
330 |
|
331 (** Equality, membership and insertion of indexnames (string*ints) **) |
|
332 |
|
333 (*optimized equality test for indexnames. Yields a huge gain under Poly/ML*) |
|
334 fun eq_ix ((a:string, i:int), (a',i')) = (a=a') andalso i=i'; |
|
335 |
|
336 (*membership in a list, optimized version for indexnames*) |
|
337 fun mem_ix (x:string*int, []) = false |
|
338 | mem_ix (x, y :: ys) = eq_ix(x,y) orelse mem_ix (x, ys); |
|
339 |
|
340 (*insertion into list, optimized version for indexnames*) |
|
341 fun ins_ix (x,xs) = if mem_ix (x, xs) then xs else x :: xs; |
|
342 |
|
343 (** Equality, membership and insertion of sorts (string lists) **) |
|
344 |
|
345 fun eq_sort ([]:sort, []) = true |
|
346 | eq_sort ((s::ss), (t::ts)) = s=t andalso eq_sort(ss,ts) |
|
347 | eq_sort (_, _) = false; |
|
348 |
|
349 fun mem_sort (_:sort, []) = false |
|
350 | mem_sort (S, S'::Ss) = eq_sort (S, S') orelse mem_sort(S,Ss); |
|
351 |
|
352 fun ins_sort(S,Ss) = if mem_sort(S,Ss) then Ss else S :: Ss; |
|
353 |
|
354 fun union_sort (xs, []) = xs |
|
355 | union_sort ([], ys) = ys |
|
356 | union_sort ((x :: xs), ys) = union_sort (xs, ins_sort (x, ys)); |
|
357 |
320 (*are two term lists alpha-convertible in corresponding elements?*) |
358 (*are two term lists alpha-convertible in corresponding elements?*) |
321 fun aconvs ([],[]) = true |
359 fun aconvs ([],[]) = true |
322 | aconvs (t::ts, u::us) = t aconv u andalso aconvs(ts,us) |
360 | aconvs (t::ts, u::us) = t aconv u andalso aconvs(ts,us) |
323 | aconvs _ = false; |
361 | aconvs _ = false; |
324 |
362 |
488 (*maps (bs,v) to v'::bs this reverses the identifiers bs*) |
526 (*maps (bs,v) to v'::bs this reverses the identifiers bs*) |
489 fun add_new_id (bs, c) : string list = variant bs c :: bs; |
527 fun add_new_id (bs, c) : string list = variant bs c :: bs; |
490 |
528 |
491 (*Accumulates the names in the term, suppressing duplicates. |
529 (*Accumulates the names in the term, suppressing duplicates. |
492 Includes Frees and Consts. For choosing unambiguous bound var names.*) |
530 Includes Frees and Consts. For choosing unambiguous bound var names.*) |
493 fun add_term_names (Const(a,_), bs) = a ins bs |
531 fun add_term_names (Const(a,_), bs) = a ins_string bs |
494 | add_term_names (Free(a,_), bs) = a ins bs |
532 | add_term_names (Free(a,_), bs) = a ins_string bs |
495 | add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs)) |
533 | add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs)) |
496 | add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs) |
534 | add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs) |
497 | add_term_names (_, bs) = bs; |
535 | add_term_names (_, bs) = bs; |
498 |
536 |
499 (*Accumulates the TVars in a type, suppressing duplicates. *) |
537 (*Accumulates the TVars in a type, suppressing duplicates. *) |
501 | add_typ_tvars(TFree(_),vs) = vs |
539 | add_typ_tvars(TFree(_),vs) = vs |
502 | add_typ_tvars(TVar(v),vs) = v ins vs; |
540 | add_typ_tvars(TVar(v),vs) = v ins vs; |
503 |
541 |
504 (*Accumulates the TFrees in a type, suppressing duplicates. *) |
542 (*Accumulates the TFrees in a type, suppressing duplicates. *) |
505 fun add_typ_tfree_names(Type(_,Ts),fs) = foldr add_typ_tfree_names (Ts,fs) |
543 fun add_typ_tfree_names(Type(_,Ts),fs) = foldr add_typ_tfree_names (Ts,fs) |
506 | add_typ_tfree_names(TFree(f,_),fs) = f ins fs |
544 | add_typ_tfree_names(TFree(f,_),fs) = f ins_string fs |
507 | add_typ_tfree_names(TVar(_),fs) = fs; |
545 | add_typ_tfree_names(TVar(_),fs) = fs; |
508 |
546 |
509 fun add_typ_tfrees(Type(_,Ts),fs) = foldr add_typ_tfrees (Ts,fs) |
547 fun add_typ_tfrees(Type(_,Ts),fs) = foldr add_typ_tfrees (Ts,fs) |
510 | add_typ_tfrees(TFree(f),fs) = f ins fs |
548 | add_typ_tfrees(TFree(f),fs) = f ins fs |
511 | add_typ_tfrees(TVar(_),fs) = fs; |
549 | add_typ_tfrees(TVar(_),fs) = fs; |
512 |
550 |
513 fun add_typ_varnames(Type(_,Ts),nms) = foldr add_typ_varnames (Ts,nms) |
551 fun add_typ_varnames(Type(_,Ts),nms) = foldr add_typ_varnames (Ts,nms) |
514 | add_typ_varnames(TFree(nm,_),nms) = nm ins nms |
552 | add_typ_varnames(TFree(nm,_),nms) = nm ins_string nms |
515 | add_typ_varnames(TVar((nm,_),_),nms) = nm ins nms; |
553 | add_typ_varnames(TVar((nm,_),_),nms) = nm ins_string nms; |
516 |
554 |
517 (*Accumulates the TVars in a term, suppressing duplicates. *) |
555 (*Accumulates the TVars in a term, suppressing duplicates. *) |
518 val add_term_tvars = it_term_types add_typ_tvars; |
556 val add_term_tvars = it_term_types add_typ_tvars; |
519 |
557 |
520 (*Accumulates the TFrees in a term, suppressing duplicates. *) |
558 (*Accumulates the TFrees in a term, suppressing duplicates. *) |
530 fun term_tvars t = add_term_tvars(t,[]); |
568 fun term_tvars t = add_term_tvars(t,[]); |
531 |
569 |
532 (*special code to enforce left-to-right collection of TVar-indexnames*) |
570 (*special code to enforce left-to-right collection of TVar-indexnames*) |
533 |
571 |
534 fun add_typ_ixns(ixns,Type(_,Ts)) = foldl add_typ_ixns (ixns,Ts) |
572 fun add_typ_ixns(ixns,Type(_,Ts)) = foldl add_typ_ixns (ixns,Ts) |
535 | add_typ_ixns(ixns,TVar(ixn,_)) = if ixn mem ixns then ixns else ixns@[ixn] |
573 | add_typ_ixns(ixns,TVar(ixn,_)) = if mem_ix (ixn, ixns) then ixns |
|
574 else ixns@[ixn] |
536 | add_typ_ixns(ixns,TFree(_)) = ixns; |
575 | add_typ_ixns(ixns,TFree(_)) = ixns; |
537 |
576 |
538 fun add_term_tvar_ixns(Const(_,T),ixns) = add_typ_ixns(ixns,T) |
577 fun add_term_tvar_ixns(Const(_,T),ixns) = add_typ_ixns(ixns,T) |
539 | add_term_tvar_ixns(Free(_,T),ixns) = add_typ_ixns(ixns,T) |
578 | add_term_tvar_ixns(Free(_,T),ixns) = add_typ_ixns(ixns,T) |
540 | add_term_tvar_ixns(Var(_,T),ixns) = add_typ_ixns(ixns,T) |
579 | add_term_tvar_ixns(Var(_,T),ixns) = add_typ_ixns(ixns,T) |