PolyRec.hs 739 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
-- An example of RelaxedPolyRec in action which came up
-- on Haskell Cafe June 2010 (Job Vranish)

module Foo where

import Data.Maybe

-- The fixed point datatype
data Y f = Y (f (Y f))

-- Silly dummy function
maybeToInt :: Maybe a -> Int
maybeToInt = length . maybeToList

---------------------------
-- f and g are mutually recursive
-- Even though f has a totally monomorphic
-- signature, g has a very polymorphic one

f :: Y Maybe -> Int
f (Y x) = g maybeToInt x

-- With RelaxedPolyRec we can infer this type
-- g :: Functor f => (f Int -> b) -> f (Y Maybe) -> b
g h x = h $ fmap f x

Gabor Greif's avatar
Gabor Greif committed
27
-- 'test' checks that g's type is polymorphic enough
Simon Peyton Jones's avatar
Simon Peyton Jones committed
28 29
test :: Functor f => (f Int -> b) -> f (Y Maybe) -> b
test = g