CS 410/510 - Homework #5
Create a single Haskell file that contains solutions to the following problems.
Be sure it compiles without errors, and submit it via D2L by class time on
May 8th, 2014.
Be sure you put your name and e-mail in a comment
at the beginning of the file. Without this you will not get any feedback.
In this homework you will do two kinds of things.
- Write a few functions using the Arrow package to structure your code.
- Write a few functions that consume or produce data (co-data) constructed
Start with the file HW5Template.html.
It has all the boiler plate you will need.
- Write a function strToInteger with type [Char] -> Integer using arrows.
Use the following as a guide. Each step consists of a single Arrow, and all the steps
are glued together with arrow composition. Here is a visual representation of
the steps on a sample string "4385".
Hint: Comment out the type of strToInteger and write it one step at
a time. At each step test your code to see that it does what you expect. Then add
another step. Replace the type when your through to check that you have indeed
written a function with the right type.
Test your code.
- Write the function printFileInc with type Kleisli IO FilePath () using arrows.
It reads a string of digits from a file, and prints to the terminal
the successor of the value of that string of digits as an Integer.
Test your code.
- Write the function incFile with type FilePath -> FilePath -> IO () using arrows.
It reads a string of digits from a file, and prints
the successor of the value of that string of digits to another file.
Test your code.
- Functors. Define two functors TreeF and LangF. The functor TreeF will be used as an initial
algebra to represent binary search trees. Such trees have two types of nodes. Leaves
(we will call "Tip") that store no data, and internal nodes (we will call "Fork") that store an Int and two
sub trees. The functor LangF will be used as an initial algebra to represent the language
T5 of homework 3. We reproduce the data definition used in that
homework here for reference.
data T5 = Add5 T5 T5
| Sub5 T5 T5
| Mult5 T5 T5
| Int5 Int
| Var5 String
| Assign5 String T5
Use the same constructor names in your function LangF, that are used in T5 above.
- Initial Algebras. Use the functors defined above (TreeF and LangF) and those defined
in HW5Template.html (F1, NatF, ListF, StreamF) to define several
values of the types listed below.
- The natural number corresponding to 4, as an (Initial NatF).
- The list corresponding to [3,7,1,6] as an (Initial (ListF Int)).
- A binary search tree (maintaining the search tree invariant) that stores
the values 7,2,45, and 11, as an (Initial TreeF).
- The T5 program corresponding to (y + (4 - (x := 8))) as an (Initial LangF).
- Consuming Initial Algebras.
- Write a function toInt, written as a catamorphism over (Initial NatF) that produces an equivalent Int.
for example. toInt (Init (S (Init Z)) --> 1
- Make the types (Initial (ListF Int)) and
(Initial LangF) instances of the Show class, by writing the show functions
as a catamorphism.
- Write the addition function over natural numbers as a function over (Initial NatF)
as a catamorphism. Your catamorphism should consume an algebra with type
Algebra (NatF c -> c), where c = (Initial NatF -> Initial NatF). That is the carrier is
a function, not a piece of data.
- Finally, redo the function eval5a :: T5 -> Store Map Value from homework 3 as a catamorphism.
Your function will have type eval5a :: Iniitia LangF -> Store Map Value.
You will probably have to cut and paste some helper functions into your solution file from your
homework 3 solution. You probably also want to make (Store m v) a monad instance.
- Final Algebras. Create an infinite program infExp as a Final LangF that represents
the program (1 + (2 + (3 + (+ 4 ...)))). Hint. First write a function f :: Int -> LangF (Final LangF),
and then apply it to 1 to build your value.
- Finite Observation of a Final Algebra. Write a function prefix with type
Int -> Final LangF -> Initial LangF that observes an infinite Final LangF structure to
a fixed depth. For example prefix 6 infExp produces the expression
(1 + (2 + (3 + (4 + (5 + (0 + 0)))))). Note that my function returns (Init(Int5 0)) for all
infinite structures deeper than the prefix.