-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInterExample.tex.lhs
267 lines (185 loc) · 6.19 KB
/
InterExample.tex.lhs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
\documentclass{article}
% \usepackage[pdftex,bookmarks,unicode]{hyperref}
%include polycode.fmt
%if style /= newcode
% Use sans-serif font for constructors
%subst conid a = "\mathsf{" a "}"
%format <*> = "\varoast"
%format &&& = "\land"
%format ||| = "\lor"
%format ==> = "\Rightarrow"
%format <=> = "\Leftrightarrow"
%format === = "="
%format =/= = "\neq"
%format thereIs = "\exists"
%format forAll = "\forall"
%format neg = "\lnot"
%format I = "\mathcal{I}"
%endif
\title{Delimited Continuations, Applicative Functors and Natural Language Semantics}
\author{
Bj\"{o}rn Bringert \\
Department of Computer Science and Engineering \\
Chalmers University of Technology and University of Gothenburg\\
\texttt{bringert{\char64}chalmers.se}}
\begin{document}
\maketitle
\begin{abstract}
\end{abstract}
% \section{Introduction}
%if style == newcode
> import FOL
> import Data.List (nub)
> import Text.PrettyPrint.HughesPJ hiding (char)
%endif
\section{Abstract syntax}
\textit{``Every man loves a woman''} \\
|PredVP (DetCN Every (UseN Man)) (ComplV2 Love (DetCN A (UseN Woman)))|
> data S = PredVP NP VP
%if style == newcode
> deriving Show
%endif
> data RS = RelVP VP
%if style == newcode
> deriving Show
%endif
> data VP = ComplV2 V2 NP | UseV V
%if style == newcode
> deriving Show
%endif
> data NP = DetCN Det CN | Everyone | Someone | John
%if style == newcode
> deriving Show
%endif
> data Det = Every | A
%if style == newcode
> deriving Show
%endif
> data CN = UseN N
> | ComplN2 N2 NP
> | RelCN CN RS
%if style == newcode
> deriving Show
%endif
> data V = Sleep
%if style == newcode
> deriving Show
%endif
> data V2 = Love
%if style == newcode
> deriving Show
%endif
> data N = Man | Woman | Shake
%if style == newcode
> deriving Show
%endif
> data N2 = Owner
%if style == newcode
> deriving Show
%endif
\section{Plumbing}
\subsection{Applicative Functors}
> infixl 4 <*>
> class Functor f => Applicative f where
> pure :: a -> f a
> (<*>) :: f (a -> b) -> f a -> f b
\subsection{Applicative Continuation Functor}
> data Cont a = Pure a | Cont ((a -> Prop) -> Prop)
> runCont :: Cont a -> ((a -> Prop) -> Prop)
> runCont (Pure x) = \c -> c x
> runCont (Cont r) = r
> instance Functor Cont where
> fmap f x = pure f <*> x
We have a special case for |Pure f <*> Pure x| to avoid redundant copies
in the implementation of |<*>| for |I|.
> instance Applicative Cont where
> pure a = Pure a
> Pure f <*> Pure x = Pure (f x)
> x <*> y = Cont $ \c -> runCont x $ \f -> runCont y $ \a -> c (f a)
> newtype I a = I [Cont a]
> instance Functor I where
> fmap f x = pure f <*> x
> instance Applicative I where
> pure a = I [pure a]
> I xs <*> I ys = I [z | x <- xs, y <- ys, z <- app x y]
> where
> app :: Cont (a -> b) -> Cont a -> [Cont b]
> app x@(Cont _) y@(Cont _) = [x <*> y, pure (\x' y' -> y' x') <*> y <*> x]
> app x y = [x <*> y]
> cont :: ((a -> Prop) -> Prop) -> I a
> cont f = I [Cont f]
> reset :: Run a => I a -> I a
> reset x = I [pure y | y <- retrieve x]
> retrieve :: Run a => I a -> [a]
> retrieve (I xs) = map run xs
> class Run a where
> run :: Cont a -> a
> instance Run Prop where
> run x = runCont x id
> instance Run b => Run (a -> b) where
> run x = \e -> run (fmap (\f -> f e) x)
\section{FOL}
\begin{spec}
data Prop = Pred String [Ind]
| Prop &&& Prop
| Prop ||| Prop
| Prop ==> Prop
| Prop <=> Prop
| neg Prop
| Prop = Prop
| forAll (Ind -> Prop)
| thereIs (Ind -> Prop)
| true
| false
data Ind = Const String | Var String
\end{spec}
\section{Semantics}
> iS :: S -> I Prop
> iS (PredVP np vp) = iNP np <*> iVP vp
Relative clauses are scope islands, and thus use |reset|.
> iRS :: RS -> I (Ind -> Prop)
> iRS (RelVP vp) = reset (iVP vp)
> iVP :: VP -> I (Ind -> Prop)
> iVP (ComplV2 v2 np) = iV2 v2 <*> iNP np
> iVP (UseV v) = iV v
> iNP :: NP -> I ((Ind -> Prop) -> Prop)
> iNP (DetCN det cn) = iDet det <*> iCN cn
> iNP Everyone = cont (\c -> forAll (\x -> c (\u -> u x)))
> iNP Someone = cont (\c -> thereIs (\x -> c (\u -> u x)))
> iNP John = pure (\f -> f (Const "john"))
> iDet :: Det -> I ((Ind -> Prop) -> (Ind -> Prop) -> Prop)
> iDet Every = cont (\c -> forAll (\x -> c (\u v -> u x ==> v x)))
> iDet A = cont (\c -> thereIs (\x -> c (\u v -> u x &&& v x)))
> iCN :: CN -> I (Ind -> Prop)
> iCN (UseN n) = iN n
Is this a scope island?
> iCN (ComplN2 n2 np) = iN2 n2 <*> iNP np
> iCN (RelCN cn rs) = pure (\ci ri x -> ci x &&& ri x) <*> iCN cn <*> iRS rs
> iV :: V -> I (Ind -> Prop)
> iV Sleep = pure (\x -> Pred "sleep" [x])
> iV2 :: V2 -> I (((Ind -> Prop) -> Prop) -> Ind -> Prop)
> iV2 Love = pure (\u x -> u (\y -> Pred "love" [x,y]))
> iN :: N -> I (Ind -> Prop)
> iN Man = pure (\x -> Pred "man" [x])
> iN Woman = pure (\x -> Pred "woman" [x])
> iN Shake = pure (\x -> Pred "shake" [x])
> iN2 :: N2 -> I (((Ind -> Prop) -> Prop) -> (Ind -> Prop))
> iN2 Owner = pure (\o x -> o (\y -> Pred "owner" [x,y]))
% \section{Testing}
%if style == newcode
> utt0 = PredVP (DetCN Every (UseN Man)) (UseV Sleep)
> utt1 = PredVP (DetCN Every (UseN Man)) (ComplV2 Love (DetCN A (UseN Woman)))
> utt2 = PredVP (DetCN A (RelCN (UseN Man) (RelVP (ComplV2 Love (DetCN A (UseN Shake)))))) (UseV Sleep)
> utt3 = PredVP John (ComplV2 Love (DetCN Every (ComplN2 Owner (DetCN A (UseN Shake)))))
> utt4 = PredVP (DetCN Every (RelCN (UseN Man) (RelVP (ComplV2 Love (DetCN A (UseN Woman)))))) (UseV Sleep)
> utt5 = PredVP (DetCN A (RelCN (UseN Man) (RelVP (ComplV2 Love (DetCN A (UseN Shake))))))
> (ComplV2 Love
> (DetCN A (RelCN (UseN Woman) (RelVP (ComplV2 Love (DetCN A (UseN Shake)))))))
> utt6 = PredVP (DetCN Every (ComplN2 Owner (DetCN A (UseN Shake)))) (UseV Sleep)
> utt7 = PredVP (DetCN Every (ComplN2 Owner (DetCN A (UseN Shake))))
> (ComplV2 Love (DetCN A (UseN Woman)))
> test = mapM_ print . {- nub . -} retrieve . iS
%endif
\bibliographystyle{plainnat}
\bibliography{bringert-bibliography.bib}
\end{document}