514 end; |
514 end; |
515 |
515 |
516 (* a runtime-quick function which makes sure that every variable has a |
516 (* a runtime-quick function which makes sure that every variable has a |
517 unique name *) |
517 unique name *) |
518 fun unique_namify_aux (ntab,(Abs(s,ty,t))) = |
518 fun unique_namify_aux (ntab,(Abs(s,ty,t))) = |
519 (case Symtab.curried_lookup ntab s of |
519 (case Symtab.lookup ntab s of |
520 NONE => |
520 NONE => |
521 let |
521 let |
522 val (ntab2,t2) = unique_namify_aux (Symtab.curried_update (s, s) ntab, t) |
522 val (ntab2,t2) = unique_namify_aux (Symtab.update (s, s) ntab, t) |
523 in |
523 in |
524 (ntab2,Abs(s,ty,t2)) |
524 (ntab2,Abs(s,ty,t2)) |
525 end |
525 end |
526 | SOME s2 => |
526 | SOME s2 => |
527 let |
527 let |
528 val s_new = (Term.variant (Symtab.keys ntab) s) |
528 val s_new = (Term.variant (Symtab.keys ntab) s) |
529 val (ntab2,t2) = |
529 val (ntab2,t2) = |
530 unique_namify_aux (Symtab.curried_update (s_new, s_new) ntab, t) |
530 unique_namify_aux (Symtab.update (s_new, s_new) ntab, t) |
531 in |
531 in |
532 (ntab2,Abs(s_new,ty,t2)) |
532 (ntab2,Abs(s_new,ty,t2)) |
533 end) |
533 end) |
534 | unique_namify_aux (ntab,(a $ b)) = |
534 | unique_namify_aux (ntab,(a $ b)) = |
535 let |
535 let |
538 in |
538 in |
539 (ntab2, t1$t2) |
539 (ntab2, t1$t2) |
540 end |
540 end |
541 | unique_namify_aux (nt as (ntab,Const x)) = nt |
541 | unique_namify_aux (nt as (ntab,Const x)) = nt |
542 | unique_namify_aux (nt as (ntab,f as Free (s,ty))) = |
542 | unique_namify_aux (nt as (ntab,f as Free (s,ty))) = |
543 (case Symtab.curried_lookup ntab s of |
543 (case Symtab.lookup ntab s of |
544 NONE => (Symtab.curried_update (s, s) ntab, f) |
544 NONE => (Symtab.update (s, s) ntab, f) |
545 | SOME _ => nt) |
545 | SOME _ => nt) |
546 | unique_namify_aux (nt as (ntab,v as Var ((s,i),ty))) = |
546 | unique_namify_aux (nt as (ntab,v as Var ((s,i),ty))) = |
547 (case Symtab.curried_lookup ntab s of |
547 (case Symtab.lookup ntab s of |
548 NONE => (Symtab.curried_update (s, s) ntab, v) |
548 NONE => (Symtab.update (s, s) ntab, v) |
549 | SOME _ => nt) |
549 | SOME _ => nt) |
550 | unique_namify_aux (nt as (ntab, Bound i)) = nt; |
550 | unique_namify_aux (nt as (ntab, Bound i)) = nt; |
551 |
551 |
552 fun unique_namify t = |
552 fun unique_namify t = |
553 #2 (unique_namify_aux (Symtab.empty, t)); |
553 #2 (unique_namify_aux (Symtab.empty, t)); |
557 for embedding checks, Note that this is a pretty naughty term |
557 for embedding checks, Note that this is a pretty naughty term |
558 manipulation, which doesn't have necessary relation to the original |
558 manipulation, which doesn't have necessary relation to the original |
559 sematics of the term. This is really a trick for our embedding code. *) |
559 sematics of the term. This is really a trick for our embedding code. *) |
560 |
560 |
561 fun bounds_to_frees_aux T (ntab,(Abs(s,ty,t))) = |
561 fun bounds_to_frees_aux T (ntab,(Abs(s,ty,t))) = |
562 (case Symtab.curried_lookup ntab s of |
562 (case Symtab.lookup ntab s of |
563 NONE => |
563 NONE => |
564 let |
564 let |
565 val (ntab2,t2) = |
565 val (ntab2,t2) = |
566 bounds_to_frees_aux ((s,ty)::T) (Symtab.curried_update (s, s) ntab, t) |
566 bounds_to_frees_aux ((s,ty)::T) (Symtab.update (s, s) ntab, t) |
567 in |
567 in |
568 (ntab2,Abs(s,ty,t2)) |
568 (ntab2,Abs(s,ty,t2)) |
569 end |
569 end |
570 | SOME s2 => |
570 | SOME s2 => |
571 let |
571 let |
572 val s_new = (Term.variant (Symtab.keys ntab) s) |
572 val s_new = (Term.variant (Symtab.keys ntab) s) |
573 val (ntab2,t2) = |
573 val (ntab2,t2) = |
574 bounds_to_frees_aux ((s_new,ty)::T) |
574 bounds_to_frees_aux ((s_new,ty)::T) |
575 (Symtab.curried_update (s_new, s_new) ntab, t) |
575 (Symtab.update (s_new, s_new) ntab, t) |
576 in |
576 in |
577 (ntab2,Abs(s_new,ty,t2)) |
577 (ntab2,Abs(s_new,ty,t2)) |
578 end) |
578 end) |
579 | bounds_to_frees_aux T (ntab,(a $ b)) = |
579 | bounds_to_frees_aux T (ntab,(a $ b)) = |
580 let |
580 let |
583 in |
583 in |
584 (ntab2, t1$t2) |
584 (ntab2, t1$t2) |
585 end |
585 end |
586 | bounds_to_frees_aux T (nt as (ntab,Const x)) = nt |
586 | bounds_to_frees_aux T (nt as (ntab,Const x)) = nt |
587 | bounds_to_frees_aux T (nt as (ntab,f as Free (s,ty))) = |
587 | bounds_to_frees_aux T (nt as (ntab,f as Free (s,ty))) = |
588 (case Symtab.curried_lookup ntab s of |
588 (case Symtab.lookup ntab s of |
589 NONE => (Symtab.curried_update (s, s) ntab, f) |
589 NONE => (Symtab.update (s, s) ntab, f) |
590 | SOME _ => nt) |
590 | SOME _ => nt) |
591 | bounds_to_frees_aux T (nt as (ntab,v as Var ((s,i),ty))) = |
591 | bounds_to_frees_aux T (nt as (ntab,v as Var ((s,i),ty))) = |
592 (case Symtab.curried_lookup ntab s of |
592 (case Symtab.lookup ntab s of |
593 NONE => (Symtab.curried_update (s, s) ntab, v) |
593 NONE => (Symtab.update (s, s) ntab, v) |
594 | SOME _ => nt) |
594 | SOME _ => nt) |
595 | bounds_to_frees_aux T (nt as (ntab, Bound i)) = |
595 | bounds_to_frees_aux T (nt as (ntab, Bound i)) = |
596 let |
596 let |
597 val (s,ty) = List.nth (T,i) |
597 val (s,ty) = List.nth (T,i) |
598 in |
598 in |