Advertisement
uyuyuy99

CS 425 - Ass 7 (Part 2)

Jun 9th, 2025 (edited)
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. datatype result = RES_ERROR of string
  2.                 | RES_NUM of int
  3.                 | RES_TRUE
  4.                 | RES_FALSE
  5.                 | RES_SUCC
  6.                 | RES_PRED
  7.                 | RES_ISZERO
  8.                 | RES_FUN of (string * term)
  9.                 | RES_CLOSURE of (string * term * env)
  10.                 | RES_LAZY of term
  11. and env = Env of (string -> result);
  12.  
  13.  
  14.  
  15. (* Interpret function *)
  16. fun interp_lazy (Env env) (AST_ID i)      =
  17.   let
  18.     val res = env i
  19.   in
  20.     (case res of
  21.       RES_LAZY(term) => interp_lazy (Env env) term
  22.     | _ => res)
  23.   end
  24.   | interp_lazy env (AST_NUM n)           = RES_NUM n
  25.   | interp_lazy env AST_TRUE              = RES_TRUE
  26.   | interp_lazy env AST_FALSE             = RES_FALSE
  27.   | interp_lazy env AST_SUCC              = RES_SUCC
  28.   | interp_lazy env AST_PRED              = RES_PRED
  29.   | interp_lazy env AST_ISZERO            = RES_ISZERO
  30.   | interp_lazy env (AST_IF (b, e1, e2))  =
  31.   let
  32.     val res = interp_lazy env b
  33.   in
  34.     case res of
  35.       RES_TRUE => interp_lazy env e1
  36.     | RES_FALSE => interp_lazy env e2
  37.     | _ => RES_ERROR "b must be a bool"
  38.   end
  39.   | interp_lazy env (AST_FUN (i, b))      = RES_FUN (i, b)
  40.   | interp_lazy env (AST_APP (f, p))      =
  41.   let
  42.     val res_f = interp_lazy env f
  43.   in
  44.     case res_f of
  45.       RES_SUCC =>
  46.         (case (interp_lazy env p) of
  47.           RES_NUM n => RES_NUM (n + 1)
  48.         | _ => RES_ERROR "param must be a number")
  49.     | RES_PRED =>
  50.         (case (interp_lazy env p) of
  51.           RES_NUM n =>
  52.             if n = 0 then RES_NUM 0
  53.             else RES_NUM (n - 1)
  54.         | _ => RES_ERROR "param must be a number")
  55.     | RES_ISZERO =>
  56.         (case (interp_lazy env p) of
  57.           RES_NUM n =>
  58.             if n = 0 then RES_TRUE
  59.             else RES_FALSE
  60.         | _ => RES_ERROR "param must be a number")
  61.     | RES_FUN(param, body) =>
  62.         let
  63.           val res_lazy = RES_LAZY p
  64.           val env_new = Env (update env param res_lazy)
  65.         in
  66.           interp_lazy env_new body
  67.         end
  68.     | _ => RES_ERROR "f must be a function"
  69.   end
  70.   | interp_lazy env (AST_LET (x, e1, e2)) =
  71.   let
  72.     val v1 = interp_lazy env e1
  73.     val env_new = Env (update env x v1)
  74.   in
  75.     interp_lazy env_new e2
  76.   end
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. (* Static-scoped *)
  85. fun interp_lazy_static (Env env) (AST_ID i)      =
  86.   let
  87.     val res = env i
  88.   in
  89.     (case res of
  90.       RES_LAZY(term) => interp_lazy_static (Env env) term
  91.     | _ => res)
  92.   end
  93.   | interp_lazy_static env (AST_NUM n)           = RES_NUM n
  94.   | interp_lazy_static env AST_TRUE              = RES_TRUE
  95.   | interp_lazy_static env AST_FALSE             = RES_FALSE
  96.   | interp_lazy_static env AST_SUCC              = RES_SUCC
  97.   | interp_lazy_static env AST_PRED              = RES_PRED
  98.   | interp_lazy_static env AST_ISZERO            = RES_ISZERO
  99.   | interp_lazy_static env (AST_IF (b, e1, e2))  =
  100.   let
  101.     val res = interp_lazy_static env b
  102.   in
  103.     case res of
  104.       RES_TRUE => interp_lazy_static env e1
  105.     | RES_FALSE => interp_lazy_static env e2
  106.     | _ => RES_ERROR "b must be a bool"
  107.   end
  108.   | interp_lazy_static env (AST_FUN (i, b))      = RES_CLOSURE (i, b, env)
  109.   | interp_lazy_static env (AST_APP (f, p))      =
  110.   let
  111.     val res_f = interp_lazy_static env f
  112.   in
  113.     case res_f of
  114.       RES_SUCC =>
  115.         (case (interp_lazy_static env p) of
  116.           RES_NUM n => RES_NUM (n + 1)
  117.         | _ => RES_ERROR "param must be a number")
  118.     | RES_PRED =>
  119.         (case (interp_lazy_static env p) of
  120.           RES_NUM n =>
  121.             if n = 0 then RES_NUM 0
  122.             else RES_NUM (n - 1)
  123.         | _ => RES_ERROR "param must be a number")
  124.     | RES_ISZERO =>
  125.         (case (interp_lazy_static env p) of
  126.           RES_NUM n =>
  127.             if n = 0 then RES_TRUE
  128.             else RES_FALSE
  129.         | _ => RES_ERROR "param must be a number")
  130.     | RES_FUN(param, body) =>
  131.         let
  132.           val res_lazy = RES_LAZY p
  133.           val env_new = Env (update env param res_lazy)
  134.         in
  135.           interp_lazy_static env_new body
  136.         end
  137.     | RES_CLOSURE(param, body, env1) =>
  138.         let
  139.           val res_lazy = RES_LAZY p
  140.           val env_new = Env (update env1 param res_lazy)
  141.         in
  142.           interp_lazy_static env_new body
  143.         end
  144.     | _ => RES_ERROR "f must be a function"
  145.   end
  146.   | interp_lazy_static env (AST_LET (x, e1, e2)) =
  147.   let
  148.     val v1 = interp_lazy_static env e1
  149.     val env_new = Env (update env x v1)
  150.   in
  151.     interp_lazy_static env_new e2
  152.   end
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement