blob: ddb911553ad7436ef593668083d300c1b55c5d94 (
plain)
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
27
28
29
|
-- 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
-- 'test' checks that g's type is polymophic enough
test :: Functor f => (f Int -> b) -> f (Y Maybe) -> b
test = g
|