{- YOUR NAME: [Fill in here! ]
----------------------------------------------------------------------
CS457/557 Functional Languages, Spring 2018 Homework 6
----------------------------------------------------------------------
-}
module HW6 where
{-
----------------------------------------------------------------------
For Credit Exercises: Your solutions to the following questions are
due by email to cs457acc@pdx.edu at the start of class (2pm) on May 30.
Mailing instructions: Your mail should have the subject line "HW6".
The body of the mail message is unimportant. Your mail should have
the following attachments:
a copy of this Haskell file, with answers and missing
definitions filled in.
Some of the problems below will be worked during class; your
submission script can include answers for these, but they will not be
graded.
-}
{-
----------------------------------------------------------------------
Question 1
----------
Suppose you are given the following definitions
[] ++ ys = ys (++1)
(x:xs) ++ ys = x:(xs ++ ys) (++2)
map f [] = [] (map1)
map f (x:xs) = (f x):(map f xs) (map2)
(a) Prove that for any finite lists xs,ys, and zs,
(xs ++ ys) ++ zs = xs ++ (ys ++ zs) (++assoc)
(b) Prove that for any finite lists xs and ys and function f, that
map f (xs ++ ys) = map f xs ++ map f ys
Question 2
----------
Lists are declared as an instance of Monad in the Prelude.
(a) By experimentation, figure out the
definitions of return and >>= that the Prelude
uses for lists, and fill them in here:
instance Monad [] where
return x =
m >>= k =
(b) The list monad is useful for describing non-deterministic
computations: a value of type [a] can be thought of as
representing a set of possible values of type a.
(Consistent with that, the empty list [] can be thought of as
representing an empty set of values.)
Modify and extend the original interpreter from lecture10e.hs to
include a non-deterministic choice operator
Amb e1 e2
which yields the value of either e1 or e2.
Modify the definition of eval to use the list monad
rather than the Exn monad, i.e. it should have type
eval :: Exp -> [Int]
This eval function should return a list (possibly empty)
of ALL possible values of the overall expression.
Cases that originally produced an Error exception should now
instead produce an empty list.
For example,
example1 = eval (Plus (Amb (Const 1) (Const 2)) (Amb (Const 10) (Const 20)))
should return the list
[11,21,12,22]
Also, the example
example2 = eval (Amb (Div (Const 1) (Const 0)) (Const 42))
should return the list
[42].
----------------------------------------------------------------------
Question 3
----------
Recall the edit distance problem from Question 2 of Homework 3.
(Note that a sample solution to that homework is available on the web site.)
Write a new version of the transform function that uses lazy memoization
to solve this problem in polynomial time rather
than the exponential time required by the sample solution.
Hints:
Follow the basic idea shown on slide 16 of the lecture on Laziness,
which uses the Vector library.
You will need to find a numbering scheme to identify the sub-problems to
solve: a simple one is to use the two indices of the positions being examined
in each list.
To build a two-dimensional memo table, you can use a vector of vectors.
-}