Skip to content

Instantly share code, notes, and snippets.

@yuga
Created June 27, 2014 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuga/cddccb44542e7b148e39 to your computer and use it in GitHub Desktop.
Save yuga/cddccb44542e7b148e39 to your computer and use it in GitHub Desktop.
typeclass0.hs の例でのコンパイルエラーを前に少し悩んだので。
class Producer p where
get :: Resource r => p -> r
class Resource a where
fire :: a -> Int
data A = A Int
data B = B Int
instance Producer A where
get (A x) = B x
instance Resource B where
fire (B x) = x
c = A 32
r = get c :: B
a = fire r
{-
typeclass0.hs|11 col 18 error| Could not deduce (r ~ B)
|| Could not deduce (r ~ B)
|| from the context (Resource r)
|| bound by the type signature for get :: Resource r => A -> r
|| at test.hs:11:6-20
-}
{-# LANGUAGE MultiParamTypeClasses #-}
class Producer p r where
get :: Resource r => p -> r
class Resource a where
fire :: a -> Int
data A = A Int
data B = B Int
instance Producer A B where
get (A x) = B x
instance Resource B where
fire (B x) = x
c = A 32
r = get c :: B
a = fire r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment