module FunDep(main) whereimport Data.Charimport Data.Functor.Constclass C a b | a -> b where f :: a -> binstance C Char Int where f = ordclass Plus a b c | a b -> c where plus :: a -> b -> cinstance Plus Int Int Int where plus = (+)instance Plus Double Double Double where plus = (+)instance Plus Int Double Double where plus x y = fromIntegral x + yinstance Plus Double Int Double where plus x y = x + fromIntegral yclass Iso a b | a -> b, b -> a where isoL :: a -> b isoR :: b -> ainstance Iso Char Int where isoL = ord isoR = chrnewtype T a = T ainstance C a b => C (T a) b where f (T a) = f a------type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f ttype Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f ttype Traversal' s a = Traversal s s a atype Getting r s a = (a -> Const r a) -> s -> Const r sclass Cons s t a b | s -> a, t -> b, s b -> t, t a -> s where _Cons :: Traversal s t (a,s) (b,t)instance Cons [a] [b] a b where _Cons f (a:as) = uncurry (:) <$> f (a, as) _Cons _ [] = pure []class Field2 s t a b | s -> a, t -> b, s b -> t, t a -> s where _2 :: Lens s t a binstance Field2 (a,b) (a,b') b b' where _2 k ~(a,b) = (\b' -> (a,b')) <$> k b_tail :: Cons s s a a => Traversal' s s_tail = _Cons._2(^.) :: s -> Getting a s a -> as ^. l = getConst (l Const s){-class D a where d :: a -> Intinstance D Char where d _ = 999class E a-- This currently not allowed (unbound b), but it could be.class C a b => E a where e :: a -> a e = idinstance E Int-}class C3 aa bb cc | aa -> bb, bb -> cc where c3 :: aa -> bb -> ccinstance C3 Int Int Int where c3 = (+)main :: IO ()main = do print $ f 'a' + 1 print $ plus (1::Int) (2::Int) print $ plus (1::Int) (2::Double) print $ plus (1::Double) (2::Int) print $ plus (1::Double) (2::Double) print $ isoL 'a' print $ isoR (100::Int) print $ f (T 'b') print $ ("abcd"::String)^._tail._tail-- print $ d (f (e (0::Int))) print $ c3 (1::Int) 2