Created
February 2, 2011 16:47
-
-
Save maoe/807969 to your computer and use it in GitHub Desktop.
UHCのドキュメントにあるexistentialsの例をGHCでやってみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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