Rewriting OCaml definitions to an equivalent form (parts 1a - 1c) 1. Rewrite eac
ID: 3733422 • Letter: R
Question
Rewriting OCaml definitions to an equivalent form (parts 1a - 1c)
1. Rewrite each of the following OCaml definitions to an equivalent form by adding trailing integers to each identifier, using as many distinct integers as possible. Use the integers in order 0, 1, 2, .... For example, for 'fun a -> fun b -> (fun a -> a) (a + b)' you would write “fun ao -> fun b1 -> (fun a2 -> 2) (a + b1)'. Or, if it's not possible to rewrite as requested, explain why not. la (2 minutes). lb (3 minutes). let rec f x = f x type ('nonterminal, 'terminal) symbol = | N of 'nonterminal | T of 'terminal let a a a = function |,a,_) -> a lc (4 minutes). 2. Convert each of the definitions (la), (b), (1c) into a simple form with no shorthand. In the simple form, every 'let' should be of the form 'let ID = EXPR', every lambda expression should be of the form 'fun ID -> EXPR', where ID stands for a single identifier and EXPR for a single expression. Or, if it's not possible to rewrite a (Problem values are the definition as requested, explain why not. same as for problem 1.) 3. For each of the definitions (la), (1b), (lc), list the types of (An identifier is "top-level" if it is every top-level identifier. Or, if it's not visible to later definitions in the same program. ) possible to list a top-level identifier's type, explain why not. (Problem values are the same as for problem 1.).Explanation / Answer
1(a): let rec f x = f x rewriting this to ocaml definition we can write it as
val f : 'a1 -> 'b1 = <fun>.
1(b):the ocaml definition of this is not changed from its original. the reason is that because of the keyword type tells you the type of every value. So in this nonterminal is referesd as N and terminal is referred as T. So next time if we use nonterminal it we will be N and terminal will be T.
1(c): let a a a = fun(_,a,_) -> a can e rewritten in ocaml as
val a1 : 'a1 -> 'b1 -> 'c1 * 'd1 * 'e1 -> 'd1 = <fun>.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.