-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexistentialTypes.hs
More file actions
130 lines (70 loc) · 1.54 KB
/
Copy pathexistentialTypes.hs
File metadata and controls
130 lines (70 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-- ghci> :set -XRankNTypes
-- this needs to be set to use the forall
-- REMEMBER TO DO THIS
:set -XRankNTypes
:set -XExistentialQuantification
:{
let map :: forall a b. (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs
:}
a :: Int
data ShowBox = forall s. Show s => SB s
:t SB
:{
let heteroList :: [ShowBox]
heteroList = [SB (), SB 5, SB True]
:}
instance Show ShowBox where show (SB s) = show s
-- (*) see the comment in the text below
:{
let f :: [ShowBox] -> IO ()
f xs = mapM_ print xs
:}
:t print
:t True
let main = f heteroList
main
data T = forall a. MkT a
data T' = forall a. Show a => MkT' a
let heteroList' = [MkT' 5, MkT' (), MkT' True, MkT' "Sartre"]
let main = mapM_ (\(MkT' x) -> print x) heteroList'
main
{-# LANGUAGE ExistentialQuantification, RankNTypes #-}
newtype Pair a b = Pair (forall c. (a -> b -> c) -> c)
newtype Pair a b = Pair {runPair :: forall c. (a -> b -> c) -> c}
:{
let makePair :: a -> b -> Pair a b
makePair a b = Pair $ \f -> f a b
:}
let asdf = makePair 3 4
let wasdf = makePair 3 'a'
:t asdf
:t wasdf
runPair asdf (\x y -> x)
:t MkT
:t Cons
:t Cons
Cons 3 l2
:{
foob :: forall a b. (b -> b) -> b -> (a -> b) -> Maybe a -> b
foob postProcess onNothin onJust mval =
postProcess val
where
val :: b
val = maybe onNothin onJust mval
:}
data L x = Empty | Cons x (L x)
-- so Cons :: x -> L x -> L x
:t Empty
:{
let l1 :: L Int
l1 = Cons 3 Empty
:}
let l2 = Cons 4 l1
:t l2
3
:t (==)
data B = T | F
eqB :: a -> b -> B
eqB