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 Wednesday February 10th, 2016.

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.

Start with the file HW5Template.html. It has all the boiler plate you will need.

  1. 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".
        "4385"::Int -->
        ([4,3,8,5],[0,1,2,3])::([Int],[Integer]) -->
        ([4,3,8,5],[0,1,2,3])::([Integer],[Integer])  -->
        ([5,8,3,4],[1,10,100,1000])::([Integer],[Integer]) -->
        [5,80,300,4000]::[Integer] -->
        4385::Integer
    

    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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. Consuming Initial Algebras.

  7. 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.

  8. 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.