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.

October 18, 2006


Suppose you wanted to provide mutable reference cells in Clean. What operations will you provide and what are heir types?

The class was divided into two small groups, both groups came up with slightly different solutions. Both groups decided to place the uniqueness marker was placed on the Ref constructor, but had different types for the dereference operation. The first group came up with:

ref   :: a -> Ref. a
deref :: Ref. a -> (Ref. a, a)
set   :: Ref. a -> a ->. Ref. a

The second group had this type for deref:

deref :: Ref a -> a

remark: Using the first design, it is not possible to copy the reference to a value. In the second, a reference cell can be copied, but then never updated afterwards.

Suppose the Env a is the type of an environment carrying values of type a, and Splay a is a splay tree with items of type a, with these operations:

empty :: Env a
find  :: Env a -> String -> Maybe a
bind  :: String -> a -> Env a -> Env a

emptyT :: Splay a
splay  :: Splay a -> String -> Maybe (a,Splay a)
insert :: String -> a -> Splay a -> Splay a

Using Clean's uniqueness types, define Env in terms of Splay. Your implementation should use a mutable ref cells to contain the current splay tree.

type Env a = Ref (Splay a)
empty :: Env. a
empty = ref emptyT

empty' :: () -> Env. a
empty' () = ref emptyT

find :: Env. a ->. String -> (Env. a, Maybe a)
find e x = let (e',t) = deref e
               result = splay t x
           in case result of
                Just (y,t') -> (set e' t', Just y)
                Nothing     -> (e', Nothing)

bind :: String. -> a -> Env. a -> Env. a
bind k v e = let (e',t) = deref e
                     t' = insert k v t
             in ref t'

Using Haskell's IO monad, define Env in terms of Splay. Your implementation should use a mutable ref cells to contain the current splay tree.

type Env a = IORef (Splay a)
empty :: IO (Env a)
empty = newIORef emptyT

find :: Env a -> String -> IO (Maybe a)
find env k = do { t <- readIORef env
                ; let result = splay t x
                ; case result of
                    Just (y,t') -> do { writeIORef env t' ; return (Just y) }
                    Nothing     -> return Nothing
                }

bind :: String -> a -> Env a -> IO (Env a)
bind k v e = do { t <- readIORef e
                ; newIORef (insert k v t)
                }

BibTeX

@Unpublished{Edsko-de-Vries-and-Rinus-Plasmeijer-and-David-Abrahamson2006
  , title      = "Uniqueness Typing Redefined"
  , month      = "October"
  , url        = "http://clean.cs.ru.nl"
  , author     = "Edsko de Vries and Rinus Plasmeijer and David Abrahamson"
  , year       = 2006
  , note       = "Unpublished work presented at IFL 2006"
  }
@article{Rinus-Plasmeijer-and-Marko-van-Eekelen1999
  , number     = 6
  , author     = "Rinus Plasmeijer and Marko van Eekelen"
  , journal    = "SIGPLAN Notices"
  , issn       = "0362-1340"
  , publisher  = "ACM Press"
  , title      = "Keep it clean: a unique approach to functional programming."
  , volume     = 34
  , address    = "New York, NY, USA"
  , url        = "http://doi.acm.org/10.1145/606666.606670"
  , pages      = "23--31"
  , year       = 1999
  }
@InProceedings{Philip-Wadler1993
  , isbn       = "3-540-57182-5"
  , author     = "Philip Wadler"
  , publisher  = "Springer Verlag"
  , title      = "A Taste of Linear Logic"
  , address    = "London, UK"
  , year       = 1993
  , url        = "http://homepages.inf.ed.ac.uk/wadler/topics/linear-logic.html"
  , pages      = "185--210"
  , booktitle  = "Proceedings of the 18th International Symposium on Mathematical Foundations of Computer Science"
  }