Quick actions

cmd+k|ctrl+k

Navigation

Languages

Functions to Polymorphic Types Answer

Snippet info

Language

Haskell

Visibility

public

Author

homam

Created

2016-08-25T13:46:26Z

Updated

2016-08-25T13:46:26Z

{-# LANGUAGE GADTs, ScopedTypeVariables #-}

import Data.Typeable

data Foo where
  Foo :: (Typeable a, Show a) => a -> Foo

instance Show Foo where
  show (Foo a) = show a

fiveFoo :: Foo
fiveFoo = Foo (5 :: Int) -- (Foo 5) doesn't work because of ambiguity

falseFoo :: Foo
falseFoo = Foo False

getFoo :: String -> Foo
getFoo "five" = fiveFoo
getFoo "false" = falseFoo

main = do
  print $ getFoo "five" -- prints '5'
  print $ getFoo "false" -- prints 'False'

INFO