162 |
162 |
163 val allocate_nice: name_pool -> string * string -> string * name_pool |
163 val allocate_nice: name_pool -> string * string -> string * name_pool |
164 |
164 |
165 val rcomb_tms: tm list -> tm -> tm |
165 val rcomb_tms: tm list -> tm -> tm |
166 val abs_tms: tm list -> tm -> tm |
166 val abs_tms: tm list -> tm -> tm |
167 val beta_reduce_tm: tm -> tm |
|
168 val eta_expandN_tm: int -> tm -> tm |
167 val eta_expandN_tm: int -> tm -> tm |
169 val eta_expand_builtin_tm: tm -> tm |
168 val eta_expand_builtin_tm: tm -> tm |
170 |
169 |
171 val str_of_ty: ty -> string |
170 val str_of_ty: ty -> string |
172 val str_of_tm: tm -> string |
171 val str_of_tm: tm -> string |
527 (case rest of |
526 (case rest of |
528 NApp (NApp (oper', arg1), rest') => |
527 NApp (NApp (oper', arg1), rest') => |
529 if oper' = oper then arg0 :: dest_rassoc_args oper arg1 rest' else [arg0, rest] |
528 if oper' = oper then arg0 :: dest_rassoc_args oper arg1 rest' else [arg0, rest] |
530 | _ => [arg0, rest]); |
529 | _ => [arg0, rest]); |
531 |
530 |
532 fun replace_tm from to = |
|
533 let |
|
534 (* This code assumes all enclosing binders bind distinct variables and bound variables are |
|
535 distinct from any other variables. *) |
|
536 fun repl_br (id, vars, body) = (id, map repl vars, repl body) |
|
537 and repl (NApp (const, arg)) = NApp (repl const, repl arg) |
|
538 | repl (NAbs (var, body)) = NAbs (var, repl body) |
|
539 | repl (NMatch (obj, branches)) = NMatch (repl obj, map repl_br branches) |
|
540 | repl tm = if tm = from then to else tm; |
|
541 in |
|
542 repl |
|
543 end; |
|
544 |
|
545 val rcomb_tms = fold (fn arg => fn func => NApp (func, arg)); |
531 val rcomb_tms = fold (fn arg => fn func => NApp (func, arg)); |
546 val abs_tms = fold_rev (curry NAbs); |
532 val abs_tms = fold_rev (curry NAbs); |
547 |
533 |
548 fun fresh_var_names_wrt_tm n tm = |
534 fun fresh_var_names_wrt_tm n tm = |
549 let |
535 let |
558 val dummy_name = nun_var_of_str Name.uu; |
544 val dummy_name = nun_var_of_str Name.uu; |
559 val max_name = max_var tm dummy_name; |
545 val max_name = max_var tm dummy_name; |
560 in |
546 in |
561 map (index_name max_name) (1 upto n) |
547 map (index_name max_name) (1 upto n) |
562 end; |
548 end; |
563 |
|
564 fun beta_reduce_tm (NApp (NAbs (var, body), arg)) = beta_reduce_tm (replace_tm var arg body) |
|
565 | beta_reduce_tm (NApp (const, arg)) = |
|
566 (case beta_reduce_tm const of |
|
567 const' as NAbs _ => beta_reduce_tm (NApp (const', arg)) |
|
568 | const' => NApp (const', beta_reduce_tm arg)) |
|
569 | beta_reduce_tm (NAbs (var, body)) = NAbs (var, beta_reduce_tm body) |
|
570 | beta_reduce_tm (NMatch (obj, branches)) = |
|
571 NMatch (beta_reduce_tm obj, map (@{apply 3(3)} beta_reduce_tm) branches) |
|
572 | beta_reduce_tm tm = tm; |
|
573 |
549 |
574 fun eta_expandN_tm 0 tm = tm |
550 fun eta_expandN_tm 0 tm = tm |
575 | eta_expandN_tm n tm = |
551 | eta_expandN_tm n tm = |
576 let |
552 let |
577 val var_names = fresh_var_names_wrt_tm n tm; |
553 val var_names = fresh_var_names_wrt_tm n tm; |
654 (case ty_opt of |
630 (case ty_opt of |
655 SOME ty => str_of (SOME (domain_ty ty)) |
631 SOME ty => str_of (SOME (domain_ty ty)) |
656 | NONE => nun_parens o str_of_tmty) var ^ |
632 | NONE => nun_parens o str_of_tmty) var ^ |
657 nun_dot ^ " " ^ str_of (Option.map range_ty ty_opt) body |
633 nun_dot ^ " " ^ str_of (Option.map range_ty ty_opt) body |
658 | str_of ty_opt (NMatch (obj, branches)) = |
634 | str_of ty_opt (NMatch (obj, branches)) = |
659 nun_match ^ " " ^ str_of NONE obj ^ " " ^ nun_with ^ " " ^ |
635 nun_match ^ " " ^ str_of NONE obj ^ " " ^ nun_with ^ |
660 space_implode "" (map (str_of_br ty_opt) branches) ^ " " ^ nun_end |
636 space_implode "" (map (str_of_br ty_opt) branches) ^ " " ^ nun_end |
661 | str_of ty_opt (app as NApp (func, argN)) = |
637 | str_of ty_opt (app as NApp (func, argN)) = |
662 (case (func, argN) of |
638 (case (func, argN) of |
663 (NApp (oper as NConst (id, _, _), arg1), arg2) => |
639 (NApp (oper as NConst (id, _, _), arg1), arg2) => |
664 if id = nun_asserting then |
640 if id = nun_asserting then |