cs252r : Advanced Functional Programming - Fall 2006

These pages are a record of the in-class discussions for the graduate class "Advanced Functional Programming" given at Harvard University in the Fall of 2006.

November 29, 2006


Data types with open recursion

In Section 2.3, note that List, ListF, Tree, TreeF and so on are not recursive data types --- the only recursive type is Fix. The types ListF, TreeF, and BtreeF use a form of what is called open recursion.

I claim that (Fix s a) is a solution to a recursion equation of the form t=... Fill in the right-hand side.

 t = s a t 

Write the definitions of List, Tree, and Btree as you ordinarily would in Haskell using recursive datatype definitions.

data List  a = Nil | Cons a (List a)
data Tree  a = Empty | Node a (Tree a) (Tree a)
data Btree a = Tip a | Bin (Btree a) (Btree a)

Given your definition of List, could a compiler or other program-analysis tool automatically recover ListF? What might be the potential benefit in doing so?

s' a             --> s a b
K ... (s' a) ... --> K .... b ....

This will allow us to use generic functions on all of our data types.

Rewrite the definition of the datatype describing Core Scheme from Monday's paper using open recursion.

data M' a = C
          | Var name
          | Lam Name a
          | Let Name a a
          | If0 a a a
          | App a a
type M = Fix' M

data Fix' s = In (s (Fix' s))

As discussed in class Monday, here is a language of first-order, A-normalized, Core ML terms.

data V   = Con Int | Var Name
data ANF = Val v
         | Let Name V ANF
         | If0 V ANF ANF
         | Tailapp V V
         | App Name V V ANF

Rewrite ANF using open recursion.

data ANF' v b = Val V        -- note big V
              | Let Name v b
              | If0 v b b
              | Tailapp v v
              | App Name v v b

Explicitly "tie the knot" to recover a definition of terms in A-normal form.

data ANF = Tie (ANF' V ANF)

Can you find another way to tie the knot so that you can express all the terms that are exressible in type M in question 4?

 data M  = Tie (ANF' M M) | L Name M 

BibTeX

@InProceedings{Jeremy-Gibbons2006
  , isbn       = "1-59593-492-6"
  , author     = "Jeremy Gibbons"
  , year       = 2006
  , publisher  = "ACM Press"
  , title      = "Design patterns as higher-order datatype-generic programs"
  , address    = "New York, NY, USA"
  , location   = "Portland, Oregon, USA"
  , url        = "http://doi.acm.org/10.1145/1159861.1159863"
  , pages      = "1--12"
  , booktitle  = "WGP '06: Proceedings of the 2006 ACM SIGPLAN workshop on Generic programming"
  }