blob: 1adc350084212cce37095f937f448e5a87a8a955 (
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
|
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
-- !!! Functional dependencies
module Main where
class Foo a b | a -> b where
foo :: a -> b
instance Foo [a] (Maybe a) where
foo [] = Nothing
foo (x:_) = Just x
instance Foo (Maybe a) [a] where
foo Nothing = []
foo (Just x) = [x]
test3:: [a] -> [a]
test3 = foo . foo
-- First foo must use the first instance,
-- second must use the second. So we should
-- get in effect: test3 (x:xs) = [x]
main:: IO ()
main = print (test3 "foo")
|