Skip to content

Instantly share code, notes, and snippets.

@munro
Created April 24, 2014 04:49
Show Gist options
  • Save munro/11241890 to your computer and use it in GitHub Desktop.
Save munro/11241890 to your computer and use it in GitHub Desktop.
{-# LANGUAGE FlexibleInstances #-}
import GHC.Float
data LangVariable = LangString String | LangNumber Double | LangNull deriving Show
class LangCoercion a where
toLangVariable :: a -> LangVariable
instance LangCoercion String where
toLangVariable a = LangString a
instance LangCoercion Double where
toLangVariable a = LangNumber a
instance LangCoercion Float where
toLangVariable a = LangNumber $ float2Double a
instance LangCoercion (Maybe a) where
toLangVariable (Just n) = toLangVariable n
-- ^ Error
-- No instance for (LangCoercion a)
-- arising from a use of `toLangVariable'
-- Possible fix:
-- add (LangCoercion a) to the context of the instance declaration
-- In the expression: toLangVariable n
-- In an equation for `toLangVariable':
-- toLangVariable (Just n) = toLangVariable n
-- In the instance declaration for `LangCoercion (Maybe a)'
toLangVariable (Nothing) = LangNull
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment