Advertisement
Guest User

Untitled

a guest
Feb 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.43 KB | None | 0 0
  1.  
  2. open System.Collections
  3.  
  4. (* Exercise 3.4 *)
  5. //    type state =
  6. //    | State of Map 'a 'b
  7.  
  8.     type aExp =          (* arithmetical expressions *)
  9.     | N of int           (* numbers *)
  10.     | V of string        (* variables *)
  11.     | Add of aExp * aExp (* addition *)
  12.     | Mul of aExp * aExp (* multiplication *)
  13.     | Sub of aExp * aExp (* subtraction *)
  14.  
  15.     let unop  f a s   = f (a s)
  16.     let binop f a b s = f (a s) (b s)
  17.  
  18.     let rec A =
  19.         function
  20.         | N n          -> fun _ -> n
  21.         | V x          -> Map.find x
  22.         | Add (a1, a2) -> binop ( + ) (A a1) (A a2)
  23.         | Mul (a1, a2) -> binop ( * ) (A a1) (A a2)
  24.         | Sub (a1, a2) -> binop ( - ) (A a1) (A a2)
  25.  
  26.     type bExp =          (* boolean expressions *)
  27.     | TT                 (* true *)
  28.     | FF                 (* false *)
  29.     | Eq of aExp * aExp  (* numeric equality *)
  30.     | Lt of aExp * aExp  (* numeric less than *)
  31.     | Neg of bExp        (* boolean not *)
  32.     | Con of bExp * bExp (* boolean conjunction *)
  33.  
  34.     (* TODO: Write a function B : bExp -> state -> bool
  35.              to evaluate boolean expressions (note that you will need to refer to A) *)
  36.     let rec B b state =
  37.         match b with
  38.         | TT -> true
  39.         | FF -> false
  40.         | Eq(x,y) -> (A x state) = (A y state)
  41.         | Lt(x,y) -> (A x state) < (A y state)
  42.         | Neg(x) -> not (B x state)
  43.         | Con(x,y) -> (B(x) state && B(y) state)
  44.  
  45.     type stm =                (* statements *)
  46.     | Ass of string * aExp    (* variable assignment *)
  47.     | Skip                    (* nop *)
  48.     | Seq of stm * stm        (* sequential osition *)
  49.     | IT of bExp * stm
  50.     | ITE of bExp * stm * stm (* if-then-else statement *)
  51.     | While of bExp * stm     (* while statement *)
  52.     | Repeat of bExp * stm
  53.  
  54.     let update = Map.add
  55.  
  56.     (* TODO: Write I : stm -> state -> state.
  57.              You can rewrite the skeleton if you want, but the signature must be the same *)
  58.     let rec I stm s =
  59.       match stm with
  60.       | Ass (x,a)         -> update x (A a s) s
  61.       | Skip              -> s
  62.       | Seq (stm1, stm2)  -> I stm2 (I stm1 s)
  63.       | IT (b, stm)       -> if (B b s) then I stm s else s
  64.       | ITE (b,stm1,stm2) -> if (B b s) then I stm1 s else I stm2 s
  65.       | While (b, stm)    -> if (B b s) then I (Seq(stm,While(b, stm))) s else s
  66.       | Repeat (b, stm)  -> if not (B b s) then I stm s else I (Seq(stm,Repeat (b, stm))) s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement