Skip to content

Instantly share code, notes, and snippets.

@maoe
Created February 2, 2011 16:47
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 maoe/807969 to your computer and use it in GitHub Desktop.
Save maoe/807969 to your computer and use it in GitHub Desktop.
UHCのドキュメントにあるexistentialsの例をGHCでやってみた
{-# LANGUAGE ExistentialQuantification #-}
module Main where
import Data.Char (ord)
data Exists r = forall a. Exists a (a -> r)
x2 :: Exists Int
x2 = Exists (3 :: Int) id
xapp :: Exists a -> a
xapp (Exists v f) = f v
x2app :: Int
x2app = xapp x2
mkx :: Bool -> Exists Int
mkx b = if b then x2 else Exists 'a' ord
y1, y2 :: Exists Int
y1 = mkx True
y2 = mkx False
{-
-- type error
mixy = case y1 of
Exists v1 f1 ->
case y2 of
Exists v2 f2 -> f1 v2
-}
main :: IO ()
main = do putStrLn $ show $ xapp y1
putStrLn $ show $ xapp y2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment