Solutions in F# Tests for problem 4

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Solutions in F# Tests for problem 4

Post by answerhappygod »

Solutions in F#
Solutions In F Tests For Problem 4 1
Solutions In F Tests For Problem 4 1 (104.53 KiB) Viewed 48 times
Solutions In F Tests For Problem 4 2
Solutions In F Tests For Problem 4 2 (113.95 KiB) Viewed 48 times
Solutions In F Tests For Problem 4 3
Solutions In F Tests For Problem 4 3 (186.14 KiB) Viewed 48 times
Tests for problem 4
Solutions In F Tests For Problem 4 4
Solutions In F Tests For Problem 4 4 (98.21 KiB) Viewed 48 times
The file Assignment3.fs contains a partial implementation of a language with floats, vectors (lists) of floats, and (first-order) functions. The syntax includes: • constant floats (NumF) and vectors (Vect); • addition (Plus) of either two floats, yielding a float; or of two vectors of the same length, yielding a vector of that length; • taking the average (Average) of the elements of a vector, yielding a float; • multiplication (Scale) of a float and a vector, yielding a vector of the same length; • if expressions (IfPositive) of the form if e > 0 then el else e2, where both branches are required to have the same type, and e is a float; • variables (Var), function calls (Call) and non-recursive function declarations (LetFun). (Ignore LetFunNoGeneralize for the first few problems.) Look at the definition of eval for the exact behaviour of the expressions. Functions can only accept vectors as arguments, but they can be polymorphic in the length of the vector. For example, LetFun ("f", "x", Var "x", Var "f") is the expression let f x = x in f, which has type Vector (’m) -> Vector ('m) where 'm is a variable for the length of the vector. Vectors can only be added together when they have the same length, so

LetFun ("p", "X", Plus (Var "x", Vect [1.2; 3.4; 5.6]), Var "p") has type Vector (3) -> Vector (3), LetFun ("f", "x", LetFun ("g", "y", Plus (Var "x", Var "y"), Var "g"), Var "f") has type Vector ('m) -> (Vector ('m) -> Vector ('m)), and Plus (Vect [1.2], Vect [3.4; 5.6]) is ill-typed. The types of this language are: • Float: floating-point numbers; • Vector (1): vectors of length 1; • Fun (1, t): the function type Vector (1) -> t. where 1 is a length (either an integer or a length variable 'm, ’n, ...). 4. Implement a unification function unify : typ -> typ -> unit. This should ensure that lengths of vectors can be unified (you can use unifyLength to do this). 5. Complete the definition of type inference, by implementing the cases for Plus, Average and Scale in the function infer. In addition to unify, some functions in Assignment3.fs will be useful: ensureFloat, ensureVector and ensureFloatorVector ensure that a type is Float, Vector, or either of the two. 6. LetFunNoGeneralize is the same as LetFun, except that type inference does not generalize the length variables. Use LetFunNoGeneralize to define an expression no_generalize such that type inference fails on no_generalize, but would have succeeded (in the empty type environment) if you had used LetFun instead.

type expr = | Num of int | Var of string | Plus of expr * expr type stmt = | Assign of string * expr | Block of string * stmt list // Block (x, stmts) is a block that // declares the variable x | If of expr * stmt * stmt | While of expr * stmt | Print of expr type naivestore = Map<string, int> let emptystore : Map<string, int> = Map.empty // (getSto store x) gets the value of the variable x from the store. // If x is not in the store (for example, because it has not been Il declared yet), then getSto returns 0. let getSto (store : naivestore) x = if store.Contains Key x then store. Item x else 0 /l (setSto store (k, v)) returns a new store, in which the value of the // variable k is set to v let setSto (store : naivestore) (k, v) = store.Add(k, v) let rec eval e (store : naivestore) : int = match e with | Num i -> i | Var x -> getSto store x | Plus(e1, e2) -> eval ei store + eval e2 store let rec exec stmt (store : naivestore) : naivestore = match stmt with | Assign (x, e) -> setSto store (x, eval e store) | If (e1, stmti, stmt2) -> if eval e1 store <> then exec stmti store else exec stmt2 store | Block (x, stmts) -> let rec loop ss sto = match ss with | [] -> sto 51::sr -> loop sr (exec s1 sto) loop stmts store | While (e, stmt) -> let rec loop sto = if eval e sto = 0 then sto else loop (exec stmt sto) loop store | Print e -> printf "%d\n" (eval e store); store let run stmt = exec stmt emptystore > ignore

Il > run (Block ("x", [Assign ("x", Csti 1); Block ("x", [Print (Var "x")])]));i // 0 // val it: unit = 0) // > run (Block ("x", [Print (Var "x"); Assign ("x", CstI 1); Block ("y", [Print (Var "x")])]));; // 0 // 1 // val it: unit = 0). // > run (Block ("y", [Assign ("x", CstI 5); Assign ("y", Csti 6); Block ("y", [Print (Var "x"); Print (Var "y")])]));; // 5 1/ 0 // val it: unit = 0 // > run (Block ("x", [Assign ("x", CstI 1); Assign ("y", Csti 2); Print (Var "y"); Block ("y", [Print (Var "y")]); Print (Var "y")]));; 1/ 2 I/O // 2 // val it: unit = 0). // > run (Block ("x", [Assign ("x", Csti 10); Block ("x", [Assign ("x", CstI 20); Block ("x", [Assign ("x", CstI 30); Print (Var "x")]); Print (Var "x")]); Print (Var "x")])) ;; // 30 // 20 // 10 // val it: unit = 0). // > run (Block ("x", [Assign ("x", Csti 10); Block ("x", [Assign ("x", CstI 20); Block ("x", [Assign ("x", CstI 30); Assign ("y", Plus (Var "y", Var "x"))]); Assign ("y", Plus (Var "y", Var "x"))]); Assign ("y", Plus (Var "y", Var "x")); Print (Var "y")]));i. // 60 // val it: unit = 0)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply