348 |
348 |
349 fun incr_bv inc lev = Same.commit (incr_bv_same inc lev); |
349 fun incr_bv inc lev = Same.commit (incr_bv_same inc lev); |
350 |
350 |
351 fun incr_boundvars inc = incr_bv inc 0; |
351 fun incr_boundvars inc = incr_bv inc 0; |
352 |
352 |
353 fun subst_bound arg body = |
353 fun subst_term_bound_same arg = |
354 let |
354 let |
355 fun term lev (ZBound i) = |
355 fun term lev (ZBound i) = |
356 if i < lev then raise Same.SAME (*var is locally bound*) |
356 if i < lev then raise Same.SAME (*var is locally bound*) |
357 else if i = lev then incr_boundvars lev arg |
357 else if i = lev then incr_boundvars lev arg |
358 else ZBound (i - 1) (*loose: change it*) |
358 else ZBound (i - 1) (*loose: change it*) |
359 | term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t) |
359 | term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t) |
360 | term lev (ZApp (t, u)) = |
360 | term lev (ZApp (t, u)) = |
361 (ZApp (term lev t, Same.commit (term lev) u) |
361 (ZApp (term lev t, Same.commit (term lev) u) |
362 handle Same.SAME => ZApp (t, term lev u)) |
362 handle Same.SAME => ZApp (t, term lev u)) |
363 | term _ _ = raise Same.SAME; |
363 | term _ _ = raise Same.SAME; |
364 in Same.commit (term 0) body end; |
364 in term end; |
|
365 |
|
366 fun subst_term_bound arg body = |
|
367 Same.commit (subst_term_bound_same arg 0) body; |
365 |
368 |
366 |
369 |
367 (* substitution of free or schematic variables *) |
370 (* substitution of free or schematic variables *) |
368 |
371 |
369 fun subst_type_same tvar = |
372 fun subst_type_same tvar = |
532 | could_beta_contract (ZAbs (_, _, b)) = could_beta_contract b |
535 | could_beta_contract (ZAbs (_, _, b)) = could_beta_contract b |
533 | could_beta_contract _ = false; |
536 | could_beta_contract _ = false; |
534 |
537 |
535 val beta_norm_same = |
538 val beta_norm_same = |
536 let |
539 let |
537 fun norm (ZApp (ZAbs (_, _, body), t)) = subst_bound t body |
540 fun norm (ZApp (ZAbs (_, _, body), t)) = subst_term_bound t body |
538 | norm (ZApp (f, t)) = |
541 | norm (ZApp (f, t)) = |
539 ((case norm f of |
542 ((case norm f of |
540 ZAbs (_, _, body) => subst_bound t body |
543 ZAbs (_, _, body) => subst_term_bound t body |
541 | nf => ZApp (nf, Same.commit norm t)) |
544 | nf => ZApp (nf, Same.commit norm t)) |
542 handle Same.SAME => ZApp (f, norm t)) |
545 handle Same.SAME => ZApp (f, norm t)) |
543 | norm (ZAbs (a, T, t)) = ZAbs (a, T, Same.commit norm t) |
546 | norm (ZAbs (a, T, t)) = ZAbs (a, T, Same.commit norm t) |
544 | norm _ = raise Same.SAME; |
547 | norm _ = raise Same.SAME; |
545 in norm end; |
548 in norm end; |
579 | norm (ZConst1 (a, T)) = ZConst1 (a, normT T) |
582 | norm (ZConst1 (a, T)) = ZConst1 (a, normT T) |
580 | norm (ZConst (a, Ts)) = ZConst (a, Same.map normT Ts) |
583 | norm (ZConst (a, Ts)) = ZConst (a, Same.map normT Ts) |
581 | norm (ZAbs (a, T, t)) = |
584 | norm (ZAbs (a, T, t)) = |
582 (ZAbs (a, normT T, Same.commit norm t) |
585 (ZAbs (a, normT T, Same.commit norm t) |
583 handle Same.SAME => ZAbs (a, T, norm t)) |
586 handle Same.SAME => ZAbs (a, T, norm t)) |
584 | norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_bound t body) |
587 | norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body) |
585 | norm (ZApp (f, t)) = |
588 | norm (ZApp (f, t)) = |
586 ((case norm f of |
589 ((case norm f of |
587 ZAbs (_, _, body) => Same.commit norm (subst_bound t body) |
590 ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body) |
588 | nf => ZApp (nf, Same.commit norm t)) |
591 | nf => ZApp (nf, Same.commit norm t)) |
589 handle Same.SAME => ZApp (f, norm t)) |
592 handle Same.SAME => ZApp (f, norm t)) |
590 | norm _ = raise Same.SAME; |
593 | norm _ = raise Same.SAME; |
591 in fn t => if Envir.is_empty envir then beta_norm_same t else norm t end; |
594 in fn t => if Envir.is_empty envir then beta_norm_same t else norm t end; |
592 |
595 |