






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







(a) Using just fold, write a function max that finds the largest element in a list of numbers. Consider this to be 0 for an empty list.
(b) Say you are given the following definition of lists: type ‘a mylist = Empty | Cons of ‘a ∗ ‘a mylist Write a function myMap that takes a function and maps it over the elements of a mylist.
(c) Say you are given the following definition of trees, with an arbitrary number of children: type ‘a mytree = Leaf of ‘a | Node of ‘a mytree list Write a function fold mytree that takes a function for the node case, a function for the leaf case, an identity, and a mytree, and returns the result of folding the functions over the tree. Note, you may want to think about mutually recursive functions here.
(d) Using fold mytree, write a function sum mytree that will sum the values of the leaves.
(b) Show the complete type derivation for the following term:
let x = 5 in g x where g is assigned type g : int → string
(a) Give a most general unifier for the following unification problem. Lower case letters (f, g, h, d) are constants or term constructors: specifically, f and g are term constructors with arity 1, h is a term constructor with arity 2, and d is a constant term with arity
{f(α) = f(g(γ)); γ = d; α = g(β); h(α, γ) = h(α, β)}
(b) Using the unifier discovered above, apply the substitution and show the final terms, which should be equalities and which should have no variables. If unification got stuck above, just write “unification failed” below.
(c) Using the definition of plus for Church numerals, show the result of adding the Church numeral for 1 to itself – show all β and α reduction steps needed, and use a lazy evaluation order. 1 ≡ λf x.f x plus ≡ λn m f x.n f(m f x)
(d) Using the function to flip the elements of pairs defined above, illustrate the use of this function on the pair λx.x c d (here c and d could be any two terms, but we don’t need to look inside them so we can just abstract them away). You may use any evaluation order.
(a) Given the alphabet {x, y, z}, provide a regular expression that defines all strings ending in z that have at least one y
(b) Given the alphabet {a, b}, provide a regular expression that defines all strings of a’s and b’s that start and end with the same letter.
Rules for type derivations:
Constants
` n : int (assuming n is an int)
` true : bool
` false : bool
Variables
Γ ` x : τ if(x : τ ) ∈ Γ
Arithmetic Operators Γ e 1 : int Γ e 2 : int Γ ` e 1 ⊕ e 2 : int
(⊕ ∈ {+, −, ∗, /, · · ·})
Relational Operators Γ e 1 : int Γ e 2 : int Γ ` e 1 ∼ e 2 : bool
(∼ ∈ {<, >, ≤, ≥, =, 6 =, · · ·})
Booleans Γ e 1 : bool Γ e 2 : bool Γ e 1 && e 2 : bool Γ e 1 : bool Γ e 2 : bool Γ e 1 || e 2 : bool Γ e 1 : bool Γ! e 1 : bool If Γ e 1 : bool Γ e 2 : τ Γ e 3 : τ Γ if e 1 then e 2 else e 3 : τ Function Abstraction Γ ∪ [x : τ 1 ] e : τ 2 Γ fun x−>e : τ 1 → τ 2 Function Application Γ e 1 : τ 1 → τ 2 Γ e 2 : τ 1 Γ e 1 e 2 : τ 2 Let Γ e 1 : τ Γ ∪ [x : τ ] e 2 : τ ′ Γ let x = e 1 in e 2 : τ ′ Letrec Γ ∪ [x : τ ] e 1 : τ Γ ∪ [x : τ ] e 2 : τ ′ Γ ` let rec x = e 1 in e 2 : τ ′