Docsity

Solutions to Lecture 29: OCaml Programming, Abstract Syntax, Parsing, and More - Prof. Sam, Study notes of Computer Science

Toggle sidebar
bg1
Solutionstoproblemsinlecture29
OCamlprogramming
1.funfij=i+j
fung(i,j)=i+j
funh[i;j]=i+j
2.letrecsums_leftx=matchxwith
[]‐>[]
|[a]‐>[a]
|y::ys‐>letz::zs=sums_leftys
iny+z::z::zs;;
letsums_rightx=
letrecsums'nx=matchxwith
[]‐>[]
|y::ys‐>(y+n)::sums'(y+n)ys
insums'0x;;
3.(a)[5;6;7;8]
(b)44
Abstractsyntax
4.letrecppns=matchswith
Assign(s,e)‐>s^"="^(pp_expre)^";"
|If(e,s,s')‐>"if("^(pp_expre)^")\n"^
(indentn)^"then"^(pp(n+3)s)^
(matchs'with
Block[]‐>""
|_‐>"\n"^(indentn)^"else"^(pp(n+3)s'))^"\n"
|While(e,s)‐>"while("^(pp_expre)^")\n"^
(indent(n+3))^(pp(n+3)s)^"\n"
|Blocksl‐>"{\n"^
(fold_right(^)
(map(funs‐>(indent(n+3))^pp(n+3)s^"\n")sl)
"")^
(indentn)^"}\n";;
(Note:Wewillnotaskaquestionthisdifficultonthefinal.)
pf3
pf4

Partial preview of the text

Download Solutions to Lecture 29: OCaml Programming, Abstract Syntax, Parsing, and More - Prof. Sam and more Study notes Computer Science in PDF only on Docsity!

Solutions to problems in lecture 29

OCaml programming

  1. fun f i j = i+j fun g (i,j) = i+j fun h [i;j] = i+j
  2. let rec sums_left x = match x with [] ‐> [] | [a] ‐> [a] | y::ys ‐> let z::zs = sums_left ys in y + z :: z :: zs;; let sums_right x = let rec sums' n x = match x with [] ‐> [] | y::ys ‐> (y+n) :: sums' (y+n) ys in sums' 0 x;;
  3. (a) [5;6;7;8] (b) 44 Abstract syntax
  4. let rec pp n s = match s with Assign(s,e) ‐> s ^ " = " ^ (pp_expr e) ^ ";" | If(e,s,s') ‐> "if (" ^ (pp_expr e) ^ ")\n" ^ (indent n) ^ "then " ^ (pp (n+3) s) ^ (match s' with Block [] ‐> "" | _ ‐> "\n" ^ (indent n) ^ "else " ^ (pp (n+3) s')) ^ "\n" | While(e,s) ‐> "while (" ^ (pp_expr e) ^ ")\n" ^ (indent (n+3)) ^ (pp (n+3) s) ^ "\n" | Block sl ‐> "{\n" ^ (fold_right (^) (map (fun s ‐> (indent (n+3)) ^ pp (n+3) s ^ "\n") sl) "") ^ (indent n) ^ "}\n";; (Note: We will not ask a question this difficult on the final.)

Lexical analysis

Parsing

  1. (a) Right‐hand sides of productions from I begin the same. (b) E → ident S S → ε | [I] I → E J J → ε | , I

Action Stack Input Sh x[y,z] Sh x [y,z] Sh x[ y,z] R I→id x[y ,z] Sh x[I ,z] Sh x[I, z] R I→I,id x[I,z ] Sh x[I ] R S→I] x[I] eof R E→id [ S x[S eof Accept E eof

Type‐checking

  1. (a) sp : ('a * 'b) list ‐> 'a list * 'b list (b) ex : 'a ‐> 'a list ‐> bool (c) ex2 : ('a ‐> bool) ‐> 'a list ‐> bool Proving properties of recursive functions
  2. Define size (Node(_,tl)) = 1 + size’ tl and size’ tl = fold_right (+) (map size tl) 0 That is, size is the total number of nodes in a tree, and size’ is the total number of nodes in all the trees in a tree list. Now define t « t’ iff size t < size t’. Correctness of imperative programs
  3. The invariant for this loop can be variously stated as: s = Σx0 ‐ Σx, or ∃y. x0 = y@x ∧ s = Σy, or s = Σ|x0|‐|x|^ x where Σz = the sum of all the elements in z, and Σi^ z = the sum of the first i elements of z.