Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Logic of "all" + verbs + relative clauses, for a class at Indiana University

Path: ARC / Grand.hs
Views: 6871
1
module Grand where
2
3
4
import Data.List
5
import Data.Maybe
6
import Control.Arrow
7
import Control.Monad (guard)
8
{-import Data.Maybe (listToMaybe,maybeToList)-}
9
import Data.Char (toLower)
10
11
12
class Syn a where
13
negation :: a -> a
14
subterms :: a -> [Term]
15
cnsIn :: a -> [CN]
16
verbsIn :: a -> [V]
17
buildTermSub :: a -> a -> Maybe [(Term,Term)]
18
buildPVSub :: a -> a -> Maybe [(PV,PV)]
19
spellOut :: a -> [(Term, Term)] -> [(PV, PV)] -> a
20
hasNegativeMarker :: a-> Bool
21
22
23
24
xAsDefaultTerm :: Maybe Term -> Term
25
xAsDefaultTerm = fromMaybe (CNasTerm $ PCN Pos X)
26
27
rAsDefaultPV :: Maybe PV -> PV
28
rAsDefaultPV = fromMaybe (PV Pos R)
29
30
plusStrict :: (Eq a) => Maybe [a] -> Maybe [a] -> Maybe [a]
31
plusStrict a b = a >>= \x -> b >>= \y -> return $ nub $ x ++ y
32
33
hunt :: (Eq a) => a -> Maybe [(a,a)] -> Maybe a
34
35
hunt t Nothing = Nothing
36
hunt t (Just []) = Nothing
37
hunt t (Just ((uu,vv):rest))
38
| (t==uu) = (Just vv)
39
| otherwise = hunt t (Just rest)
40
41
moveAhead :: (Eq a) => Maybe [(a,a)] -> Maybe [(a,a)] -> Bool
42
moveAhead Nothing xx = False
43
moveAhead xx Nothing = False
44
moveAhead (Just []) xx = True
45
moveAhead (Just ((uu,vv):rest)) xx
46
| value == (Just vv) = (moveAhead (Just rest) xx)
47
| value == Nothing = (moveAhead (Just rest) xx)
48
| otherwise = False
49
where value = hunt uu xx
50
51
combineStructures xx yy =
52
if moveAhead xx yy == True then (plusStrict xx yy) else Nothing
53
54
polarizedCNNegation (PCN Pos cn) = (PCN Neg cn)
55
polarizedCNNegation (PCN Neg cn) = (PCN Pos cn)
56
57
-- the definition of pCNsIn is needed in Sdagleq.hs, but not elsewhere
58
pCNsIn (CNasTerm (PCN Pos cn)) = (PCN Pos cn)
59
pCNsIn (CNasTerm (PCN Neg cn)) = (PCN Neg cn)
60
pCNsIn (TermMaker (PV Pos v) (TermNP All n)) = pCNsIn n
61
pCNsIn (TermMaker (PV Pos v) (TermNP Some n)) = pCNsIn n
62
pCNsIn (TermMaker (PV Neg v) (TermNP All n)) = pCNsIn n
63
pCNsIn (TermMaker (PV Neg v) (TermNP Some n)) = pCNsIn n
64
65
instance Syn PolCN where
66
negation (PCN Pos cn) = (PCN Neg cn)
67
negation (PCN Neg cn) = (PCN Pos cn)
68
subterms cn = []
69
cnsIn (PCN p cn) = [cn]
70
verbsIn (PCN p cn) = [ ]
71
buildTermSub (PCN p cn) (PCN p' cn') = Just []
72
buildPVSub (PCN p cn) (PCN p' cn') = Just []
73
spellOut (PCN p cn) k k' = (PCN p cn) --- THIS LINE IS QUESTIONABLE. IT'S NOT NEEDED
74
hasNegativeMarker (PCN Pos cn) = False
75
hasNegativeMarker (PCN Neg cn) = True
76
77
instance Syn Term where
78
negation (CNasTerm (PCN Pos cn)) = (CNasTerm (PCN Neg cn))
79
negation (CNasTerm (PCN Neg cn)) = (CNasTerm (PCN Pos cn))
80
negation (TermMaker (PV Pos v) (TermNP All n)) = TermMaker (PV Neg v) (TermNP Some n)
81
negation (TermMaker (PV Pos v) (TermNP Some n)) = TermMaker (PV Neg v) (TermNP All n)
82
negation (TermMaker (PV Neg v) (TermNP All n)) = TermMaker (PV Pos v) (TermNP Some n)
83
negation (TermMaker (PV Neg v) (TermNP Some n)) = TermMaker (PV Pos v) (TermNP All n)
84
subterms (CNasTerm ter) = [(CNasTerm ter)]
85
subterms (TermMaker pv (TermNP d ter)) = (TermMaker pv (TermNP d ter)):subterms ter
86
cnsIn Ter1 = []
87
cnsIn Ter2 = []
88
cnsIn Ter3 = []
89
cnsIn Ter1bar = []
90
cnsIn Ter2bar = []
91
cnsIn Ter3bar = []
92
cnsIn (CNasTerm (PCN p cn)) = [cn]
93
cnsIn (TermMaker t n) = (cnsIn n)
94
hasNegativeMarker (CNasTerm (PCN Pos cn)) = False
95
hasNegativeMarker (CNasTerm (PCN Neg cn)) = True
96
hasNegativeMarker (TermMaker (PV Pos v) (TermNP All n)) = hasNegativeMarker n
97
hasNegativeMarker (TermMaker (PV Pos v) (TermNP Some n)) = hasNegativeMarker n
98
hasNegativeMarker (TermMaker (PV Neg v) (TermNP All n)) = hasNegativeMarker n
99
hasNegativeMarker (TermMaker (PV Neg v) (TermNP Some n)) = hasNegativeMarker n
100
101
102
verbsIn (TermMaker (PV pol ver) np) = [ver] ++ (verbsIn np)
103
verbsIn _ = []
104
105
buildTermSub (TermMaker x y) (CNasTerm z) = Nothing
106
buildTermSub (CNasTerm (PCN Pos cn)) (CNasTerm dn) = Just [(CNasTerm (PCN Pos cn), CNasTerm dn)]
107
buildTermSub (CNasTerm (PCN Neg cn)) (CNasTerm (PCN Pos dn)) = Just [(CNasTerm (PCN Pos cn), CNasTerm (PCN Neg dn))]
108
buildTermSub (CNasTerm (PCN Neg cn)) (CNasTerm (PCN Neg dn)) = Just [(CNasTerm (PCN Pos cn), CNasTerm (PCN Pos dn))]
109
buildTermSub (CNasTerm zz)(TermMaker xx yy) = Nothing
110
buildTermSub (TermMaker v np1) (TermMaker w np2) = buildTermSub np1 np2
111
buildTermSub Ter1 t = Just [(Ter1, t)]
112
buildTermSub Ter2 t = Just [(Ter2, t)]
113
buildTermSub Ter3 t = Just [(Ter3, t)]
114
buildTermSub Ter1bar t = Just [(Ter1, negation t)]
115
buildTermSub Ter2bar t = Just [(Ter2, negation t)]
116
buildTermSub Ter3bar t = Just [(Ter3, negation t)]
117
buildPVSub (TermMaker xx yy) (CNasTerm zz) = Nothing
118
buildPVSub (CNasTerm xx) ter = Just []
119
buildPVSub (TermMaker vv (TermNP d t)) (TermMaker ww (TermNP e u)) =
120
if (d == e) then Just [(vv,ww)] else Nothing
121
buildPVSub Ter1 t = Just []
122
buildPVSub Ter2 t = Just []
123
buildPVSub Ter3 t = Just []
124
buildPVSub Ter1bar t = Just []
125
buildPVSub Ter2bar t = Just []
126
buildPVSub Ter3bar t = Just []
127
spellOut (CNasTerm (PCN Pos cn)) list1 list2 = xAsDefaultTerm $ lookup (CNasTerm (PCN Pos cn)) list1
128
spellOut (CNasTerm (PCN Neg cn)) list1 list2 = negation $ xAsDefaultTerm $ lookup (CNasTerm (PCN Pos cn)) list1
129
spellOut (TermMaker t n) list1 list2 = (TermMaker (rAsDefaultPV $ lookup t list2) (spellOut n list1 list2))
130
spellOut Ter1 list1 list2 = xAsDefaultTerm $ lookup Ter1 list1
131
spellOut Ter2 list1 list2 = xAsDefaultTerm $ lookup Ter2 list1
132
spellOut Ter3 list1 list2 = xAsDefaultTerm $ lookup Ter3 list1
133
spellOut Ter1bar list1 list2 = negation $ spellOut Ter1 list1 list2
134
spellOut Ter2bar list1 list2 = negation $ spellOut Ter2 list1 list2
135
spellOut Ter3bar list1 list2 = negation $ spellOut Ter3 list1 list2
136
137
instance Syn NP where
138
negation (TermNP All t) = TermNP Some (negation t)
139
negation (TermNP Some t) = TermNP All (negation t)
140
--negation (TermNP Atleast t) = TermNP More (negation t)
141
--negation (TermNP More t) = TermNP Atleast (negation t)
142
subterms (TermNP All t) = subterms t
143
cnsIn Everyone = []
144
cnsIn Someone = []
145
cnsIn (TermNP d t) = cnsIn t
146
verbsIn Everyone = []
147
verbsIn Someone = []
148
verbsIn (TermNP d t) = verbsIn t
149
buildTermSub (TermNP d t) (TermNP e u) =
150
if (d == e)
151
then (buildTermSub t u)
152
else Nothing
153
buildPVSub xx yy = Just []
154
spellOut (TermNP d t) list1 list2 = (TermNP d (spellOut t list1 list2))
155
hasNegativeMarker (TermNP All t) = hasNegativeMarker t
156
hasNegativeMarker (TermNP Some t) = hasNegativeMarker t
157
hasNegativeMarker (TermNP Atleast t) = hasNegativeMarker t
158
hasNegativeMarker (TermNP More t) = hasNegativeMarker t
159
160
instance Syn Sent where
161
negation (Sent All t u) = (Sent Some t (negation u))
162
negation (Sent Some t u) = (Sent All t (negation u))
163
negation (Sent Atleast t u) = (Sent More u t)
164
negation (Sent More t u) = (Sent Atleast u t)
165
subterms (Sent d t u) = (subterms t) ++ (subterms u) -- I dropped at the front [t,u] ++
166
subterms (Sent2 d t u t2 u2) = (subterms t) ++ (subterms u) ++ (subterms t2) ++ (subterms u2) -- I dropped [t,u,t2,u2] ++
167
cnsIn (Sent d t u) = (cnsIn t) ++ (cnsIn u)
168
cnsIn (Sent2 d t u t2 u2) = (cnsIn t) ++ (cnsIn u) ++ (cnsIn t2) ++ (cnsIn u2)
169
verbsIn (Sent d t u) = (verbsIn t) ++ (verbsIn u)
170
verbsIn (Sent2 d t u t2 u2) = (verbsIn t) ++ (verbsIn u) ++(verbsIn t2) ++ (verbsIn u2)
171
buildTermSub (Sent d tt uu) (Sent e vv ww) =
172
if (d == e) then (combineStructures (buildTermSub tt vv) (buildTermSub uu ww)) else Nothing
173
buildTermSub (Sent2 d tt uu tt2 uu2) (Sent2 e vv ww vv2 ww2) =
174
if (d == e)
175
then foldl1 combineStructures [(buildTermSub tt vv), (buildTermSub uu ww),(buildTermSub tt2 vv2), (buildTermSub uu2 ww2)]
176
else Nothing
177
buildPVSub (Sent d t uu) (Sent e vv ww) =
178
if (d == e) then (plusStrict (buildPVSub t vv) (buildPVSub uu ww)) else Nothing
179
buildPVSub (Sent2 d tt uu tt2 uu2) (Sent2 e vv ww vv2 ww2) =
180
if (d == e)
181
then foldl1 plusStrict [(buildPVSub tt vv), (buildPVSub uu ww),(buildPVSub tt2 vv2), (buildPVSub uu2 ww2)]
182
else Nothing
183
spellOut (Sent d t u) list1 list2 = (Sent d (spellOut t list1 list2) (spellOut u list1 list2))
184
spellOut (Sent2 d t u t2 u2) list1 list2 = (Sent2 d (spellOut t list1 list2) (spellOut u list1 list2) (spellOut t2 list1 list2) (spellOut u2 list1 list2))
185
hasNegativeMarker (Sent All t u) = and [hasNegativeMarker t, hasNegativeMarker u ]
186
hasNegativeMarker (Sent Some t u) = and [hasNegativeMarker t , hasNegativeMarker u ]
187
hasNegativeMarker (Sent Atleast t u) = and [hasNegativeMarker t, hasNegativeMarker u ]
188
hasNegativeMarker (Sent More t u) = and [hasNegativeMarker t, hasNegativeMarker u ]
189
190
data Sent = Sent Det Term Term | Sent2 Det Term Term Term Term
191
deriving (Ord,Eq)
192
193
instance Show Sent where
194
-- show (Sent d t u) = show (d) ++ " " ++ show(t) ++ " " ++ show(u)
195
show (Sent d (TermMaker x y) (TermMaker z w) ) = show (d) ++ " who " ++ show((TermMaker x y)) ++ " also " ++ show((TermMaker z w))
196
show (Sent d (TermMaker x y) (CNasTerm pcn) ) = show (d) ++ " who " ++ show((TermMaker x y)) ++ " are " ++ show((CNasTerm pcn))
197
show (Sent d (CNasTerm pcn) (TermMaker x y)) = show (d) ++ " " ++ show((CNasTerm pcn)) ++ " " ++ show((TermMaker x y))
198
show (Sent d (CNasTerm pcn) (CNasTerm pcn2)) = show (d) ++ " " ++ show((CNasTerm pcn)) ++ " are " ++ show((CNasTerm pcn2))
199
show (Sent2 d x y u v) = show (d) ++ " " ++ show(x) ++ " which are " ++ show(y) ++ " are also " ++ show(u) ++ " which are " ++ show(v)
200
201
principalDet (Sent d t t') = d
202
converse s@(Sent All t u) = (Sent All u t)
203
204
data NP = Everyone | Someone | TermNP Det Term | IntTermNP Det IntersectionTerm
205
deriving (Ord,Eq)
206
207
instance Show NP where
208
show (TermNP d t) = show(d) ++ " " ++ show(t)
209
show (IntTermNP d t) = show(d) ++ " " ++ show(t)
210
211
data Det = All | Some | No | Most | Atleast | More | Contradiction
212
deriving (Ord,Eq)
213
214
instance Show Det where
215
show All = "all"
216
show Some = "some"
217
show No = "no"
218
show Most = "most"
219
show Atleast = "at least"
220
show More = "more"
221
show Contradiction = "contradiction"
222
223
224
225
data CN = Girls
226
| Boys
227
| Dogs | Cats | Skunks | Sneetches | Mammals | Chordates | Animals | Birds | X | Y | Z | W | P | Q | N --Var CNvariable
228
deriving (Eq,Ord)
229
230
instance Show CN where
231
show Girls = "girls"
232
show Boys = "boys"
233
show Dogs = "dogs"
234
show Cats = "cats"
235
show Skunks = "skunks"
236
show Sneetches = "sneetches"
237
show Mammals = "mammals"
238
show Chordates = "chordates"
239
show Animals = "animals"
240
show Birds = "birds"
241
show X = "x"
242
show Y = "y"
243
show Z = "z"
244
show W = "w"
245
show P = "p"
246
show Q = "q"
247
show N = "n"
248
249
data Polarity = Pos | Neg
250
deriving (Ord, Eq)
251
252
instance Show Polarity where
253
show Pos = ""
254
show Neg = "non-"
255
256
data PolCN = PCN Polarity CN
257
deriving (Eq,Ord)
258
instance Show PolCN where
259
show (PCN a b) = (show a)++(show b)
260
261
cnvariables = [X, Y, Z, W, P, Q, N]
262
263
data Term = CNasTerm PolCN
264
| TermMaker PV NP
265
| Ter1 | Ter2 | Ter3 | Ter1bar | Ter2bar | Ter3bar
266
deriving (Ord,Eq)
267
instance Show Term where
268
show (CNasTerm px) = show px
269
show (TermMaker t (TermNP d (TermMaker pv np))) = show(t) ++ " " ++ show(d) ++ " who " ++ show(pv) ++ " " ++ show(np)
270
show (TermMaker t (TermNP d (CNasTerm px))) = show(t) ++ " " ++ show(d) ++ " " ++ show(px)
271
--show (TermMaker t n) = "(" ++ show(t) ++ " " ++ show(n)++")" -- this was it!!!
272
show Ter1 = "Ter1"
273
show Ter2 = "Ter2"
274
show Ter3 = "Ter3"
275
show Ter1bar = "Ter1-bar"
276
show Ter2bar = "Ter2-bar"
277
show Ter3bar = "Ter3-bar"
278
279
data PV = PV Polarity V
280
deriving (Eq,Ord)
281
instance Show PV where
282
show (PV a b) = (show a)++(show b)
283
284
data V = Loves | Admires | Helps | Sees | Hates | R | S
285
deriving (Eq,Ord)
286
287
instance Show V where
288
show Loves = "love"
289
show Admires = "admire"
290
show Helps = "help"
291
show Sees = "see"
292
show Hates = "hate"
293
show R = "r"
294
show S = "s"
295
296
r = PV Pos R
297
x = PCN Pos X
298
y = PCN Pos Y
299
z = PCN Pos Z
300
p = PCN Pos P
301
q = PCN Pos Q
302
n = PCN Pos N
303
w = PCN Pos W
304
non_x = PCN Neg X
305
non_y = PCN Neg Y
306
non_z = PCN Neg Z
307
non_w = PCN Neg W
308
non_p = PCN Neg P
309
non_q = PCN Neg Q
310
non_n = PCN Neg N
311
skunks = PCN Pos Skunks
312
mammals = PCN Pos Mammals
313
animals = PCN Pos Animals
314
sneetches = PCN Pos Sneetches
315
dogs = PCN Pos Dogs
316
birds = PCN Pos Birds
317
chordates = PCN Pos Chordates
318
boys = PCN Pos Boys
319
girls = PCN Pos Girls
320
cats = PCN Pos Cats
321
non_skunks = PCN Neg Skunks
322
non_mammals = PCN Neg Mammals
323
non_animals = PCN Neg Animals
324
non_sneetches = PCN Neg Sneetches
325
non_dogs = PCN Neg Dogs
326
non_birds = PCN Neg Birds
327
non_chordates = PCN Neg Chordates
328
non_boys = PCN Neg Boys
329
non_girls = PCN Neg Girls
330
non_cats = PCN Neg Cats
331
loves = PV Pos Loves
332
admires = PV Pos Admires
333
helps = PV Pos Helps
334
sees = PV Pos Sees
335
hates = PV Pos Hates
336
not_loves = PV Neg Loves
337
not_admires = PV Neg Admires
338
not_helps = PV Neg Helps
339
not_sees = PV Neg Sees
340
not_hates = PV Neg Hates
341
342
343
data IntersectionTerm = IntersectionTerm PolCN Term
344
| IntTer1 | IntTer2 | IntTer3 | IntTer1bar | IntTer2bar | IntTer3bar
345
deriving (Ord,Eq)
346
instance Show IntersectionTerm where
347
show (IntersectionTerm p t) = show(p) ++ " who " ++ show(t)
348
show IntTer1 = "IntTer1"
349
show IntTer2 = "IntTer2"
350
show IntTer3 = "IntTer3"
351
show IntTer1bar = "IntTer1-bar"
352
show IntTer2bar = "IntTer2-bar"
353
show IntTer3bar = "IntTer3-bar"
354
355
356
357
-------------THIS WHOLE FILE SHOULD BE REWORKED IN THE LIGHT OF THE FRONT END! ------------------
358
359
360
361
cnlist = [Girls,Boys, Dogs, Cats, Skunks, Mammals, Animals, Chordates, Birds, X, Y, Z, P, Q]
362
verbList = [Loves, Admires,Helps, Hates,Sees, R, S]
363
tvVarList = [R,S]
364
verblistNotVars = verbList \\ tvVarList
365
cnVarList = [X, Y, Z, P, Q]
366
cnlistNotVars sList = cnlist \\ cnVarList
367
polarizedCNListNotVars aList = [CNasTerm (PCN Pos w) | w <- cnlistNotVars aList] ++ [CNasTerm (PCN Neg w) | w <- cnlistNotVars aList]
368
polarizedTermListNotVars aList = [CNasTerm (PCN Pos w) | w <- cnlistNotVars aList] ++ [CNasTerm (PCN Neg w) | w <- cnlistNotVars aList]
369
pairOfCns (Sent d (CNasTerm (PCN Pos n1)) (CNasTerm (PCN Pos n2))) = (n1,n2)
370
371
372
373
s2 = Sent All (CNasTerm x) (CNasTerm y)
374
t1 = TermMaker sees (TermNP All (CNasTerm skunks) )
375
t2 = TermMaker sees (TermNP All (CNasTerm girls))
376
s = Sent All t3 t2
377
t3 = TermMaker sees (TermNP All t1)
378
t4 =TermMaker (PV Pos R) (TermNP All (CNasTerm x))
379
t9 = TermMaker sees (TermNP All (CNasTerm sneetches))
380
t6 = TermMaker (PV Pos R) (TermNP Some t4)
381
s3 = Sent All t1 t5
382
t5 = (CNasTerm animals)
383
t7 = (CNasTerm chordates)
384
t8 = TermMaker helps (TermNP All (CNasTerm skunks ))
385
t10 = (CNasTerm sneetches)
386
sAllXY = s2
387
sAllYZ = Sent All (CNasTerm y) (CNasTerm z)
388
sAllYX = Sent All (CNasTerm y) (CNasTerm x)
389
sAllXZ = Sent All (CNasTerm x) (CNasTerm z)
390
sAllXX = Sent All (CNasTerm x) (CNasTerm x)
391
sAntiXY = Sent All (CNasTerm non_x) (CNasTerm non_y)
392
sAntiYX = Sent All (CNasTerm non_y) (CNasTerm non_x)
393
sSomeXX = Sent Some (CNasTerm x) (CNasTerm x)
394
sSomeXY = Sent Some (CNasTerm x) (CNasTerm y)
395
sSomeYX = Sent Some (CNasTerm y) (CNasTerm x)
396
sSomeXZ = Sent Some (CNasTerm x) (CNasTerm z)
397
sZeroX = Sent All (CNasTerm x) (CNasTerm non_x)
398
sOneX = Sent All (CNasTerm non_x) (CNasTerm x)
399
sSome = Sent Some (CNasTerm y) (CNasTerm z)
400
s4 = Sent Some (CNasTerm dogs) (TermMaker sees (TermNP Some (CNasTerm skunks) ))
401
s6 = Sent Some (CNasTerm dogs) (TermMaker sees (TermNP Some (CNasTerm skunks) ))
402
s5 = Sent Some (CNasTerm sneetches) t2
403
s7 = Sent All t5 t9
404
s8 = Sent All (CNasTerm dogs) t9
405
s9 = Sent All t5 t7
406
s10 = Sent Some t10 t5
407
s11 = Sent Some (CNasTerm dogs) (TermMaker sees (TermNP Some (CNasTerm sneetches)))
408
s12 = Sent No t10 t10
409
sentList = [
410
(Sent All (CNasTerm skunks) (CNasTerm mammals)),
411
(Sent Some (CNasTerm skunks) (CNasTerm mammals)),
412
(Sent All (CNasTerm mammals) (CNasTerm chordates)),
413
s3, s4, s5, s6, s7, s8, s9, s10, s11, s12
414
]
415
416
417
anotherList =
418
[ (Sent Some (CNasTerm skunks) (CNasTerm mammals)), (Sent Some (CNasTerm non_chordates) (CNasTerm mammals)),
419
(Sent All (CNasTerm non_chordates) (CNasTerm dogs)),
420
(Sent Some (CNasTerm skunks) (CNasTerm boys)), (Sent All (CNasTerm girls) (CNasTerm non_mammals))]
421
422
fourseven = [(Sent All (CNasTerm non_y) (CNasTerm p)), (Sent All (CNasTerm p) (CNasTerm q)),
423
(Sent All (CNasTerm q) (CNasTerm y)), (Sent All (CNasTerm y) (CNasTerm p)), (Sent All (CNasTerm q) (CNasTerm z))
424
]
425
426
smallSentList = [(Sent All (CNasTerm skunks) (CNasTerm mammals)),
427
(Sent Some (CNasTerm skunks) (CNasTerm mammals)),
428
(Sent Some (CNasTerm non_skunks) (CNasTerm mammals)),
429
(Sent All (CNasTerm mammals) (CNasTerm non_chordates)), (Sent Some (CNasTerm boys) (CNasTerm non_chordates)),
430
(Sent All (CNasTerm girls) (CNasTerm non_girls))]
431
conc = (Sent All t13 t14)
432
433
tinySentList = [(Sent All (CNasTerm skunks) (CNasTerm mammals)),
434
(Sent Some (CNasTerm mammals) (CNasTerm skunks) ),
435
(Sent All (CNasTerm sneetches) (CNasTerm skunks))]
436
437
438
t11 = TermMaker sees (TermNP All (CNasTerm skunks) )
439
t12 = TermMaker sees (TermNP All (CNasTerm mammals))
440
t13 = TermMaker helps (TermNP All t11)
441
t14 = TermMaker helps (TermNP All t12)
442
443
444
445
446
447
448
449
450
type Parser a b = [a] -> [(b,[a])]
451
type PARSER a b = Parser a (ParseTree a b)
452
453
epsilonT :: PARSER a b
454
epsilonT = succeed Ep
455
456
symbolT :: Eq a => a -> PARSER a b
457
symbolT s = (\ x -> Leaf x) <$$> symbol s
458
459
symbol :: Eq a => a -> Parser a a
460
symbol c [] = []
461
symbol c (x:xs) | c == x = [(x,xs)]
462
| otherwise = []
463
464
infixl 6 <:>
465
466
(<:>) :: Parser a b -> Parser a [b] -> Parser a [b]
467
(p <:> q) xs = [ (r:rs,zs) | (r,ys) <- p xs,
468
(rs,zs) <- q ys ]
469
470
(<|>) :: Parser a b -> Parser a b -> Parser a b
471
(p1 <|> p2) xs = p1 xs ++ p2 xs
472
473
(<**>) :: Parser a [b] -> Parser a [b] -> Parser a [b]
474
(p <**> q) xs = [ (r1 ++ r2,zs) | (r1,ys) <- p xs,
475
(r2,zs) <- q ys ]
476
infixl 7 <$$>
477
478
(<$$>) :: (a -> b) -> Parser s a -> Parser s b
479
(f <$$> p) xs = [ (f x,ys) | (x,ys) <- p xs ]
480
481
data ParseTree a b = Ep | Leaf a | Branch b [ParseTree a b]
482
deriving Eq
483
484
instance (Show a, Show b) => Show (ParseTree a b) where
485
show Ep = "[]"
486
show (Leaf t) = show t
487
show (Branch l ts) = "[." ++ show l ++ " "
488
++ show ts ++ "]"
489
succeed :: b -> Parser a b
490
succeed r xs = [(r,xs)]
491
492
493
collect :: [Parser a b] -> Parser a [b]
494
collect [] = succeed []
495
collect (p:ps) = p <:> collect ps
496
497
parseAs :: b -> [PARSER a b] -> PARSER a b
498
parseAs label ps = (\ xs -> Branch label xs) <$$> collect ps
499
500
sent, tv, cn, det, t, int, intOrT, neg, np :: PARSER String String
501
cn = symbolT "skunks"
502
<|> symbolT "mammals"
503
<|> symbolT "chordates"
504
<|> symbolT "boys" <|> symbolT "girls" <|> symbolT "animals"
505
506
tv = symbolT "see" <|> symbolT "love" <|> symbolT "admire" <|> symbolT "hate"
507
508
det = symbolT "every"
509
<|> symbolT "some"
510
<|> symbolT "no"
511
<|> symbolT "all"
512
<|> symbolT "most"
513
514
t = parseAs "Term" [cn]
515
<|> parseAs "Term" [symbolT "non" ,cn]
516
<|> parseAs "Term" [tv, det, intOrT]
517
<|> parseAs "Term" [tv, det, symbolT "who", intOrT]
518
519
--pcn = parseAs "PCN" [pol, cn]
520
neg = symbolT "non"
521
522
np = parseAs "NP" [det,intOrT]
523
--pv = parseAs "PV" [pol, tv]
524
525
sent = parseAs "S" [det,intOrT,intOrT]
526
527
int = parseAs "Int" [cn,symbolT "who",t]
528
529
intOrT = int <|> t
530
531
move :: ParseTree String String -> Term
532
move (Branch "Term" [Leaf "skunks"]) = (CNasTerm skunks)
533
move (Branch "Term" [Leaf "mammals"]) = (CNasTerm mammals)
534
move (Branch "Term" [Leaf "animals"]) = (CNasTerm animals)
535
move (Branch "Term" [Leaf "chordates"]) = (CNasTerm chordates)
536
move (Branch "Term" [Leaf "sneetches"]) = (CNasTerm sneetches)
537
move (Branch "Term" [(Leaf "non"), (Leaf "skunks")]) = (CNasTerm non_skunks)
538
move (Branch "Term" [(Leaf "non"), Leaf "mammals"]) = (CNasTerm non_mammals)
539
move (Branch "Term" [(Leaf "non"), Leaf "animals"]) = (CNasTerm non_animals)
540
move (Branch "Term" [(Leaf "non"), Leaf "chordates"]) = (CNasTerm non_chordates)
541
move (Branch "Term" [(Leaf "non"), Leaf "sneetches"]) = (CNasTerm non_sneetches)
542
543
544
move (Branch "Term" [(Leaf "see"), (Leaf "all"), (Leaf "who"), subtree])
545
= (TermMaker sees (TermNP All (move subtree)))
546
move (Branch "Term" [(Leaf "see"), (Leaf "all"), subtree])
547
= (TermMaker sees (TermNP All (move subtree)))
548
move (Branch "Term" [(Leaf "love"), (Leaf "all"), (Leaf "who"), subtree])
549
= (TermMaker loves (TermNP All (move subtree)))
550
move (Branch "Term" [(Leaf "love"), (Leaf "all"), subtree])
551
= (TermMaker loves (TermNP All (move subtree)))
552
move (Branch "Term" [(Leaf "hate"), (Leaf "all"), (Leaf "who"), subtree])
553
= (TermMaker hates (TermNP All (move subtree)))
554
move (Branch "Term" [(Leaf "hate"), (Leaf "all"), subtree])
555
= (TermMaker hates (TermNP All (move subtree)))
556
move (Leaf "skunks") = (CNasTerm skunks)
557
--move (Leaf ["mammals"]) = (CNasTerm mammals)
558
--move (Leaf ["animals"]) = (CNasTerm animals)
559
560
moveS :: ParseTree String String -> Sent
561
moveS (Branch "S" [(Leaf "all"), ttree, ttree2]) = (Sent All (move ttree) (move ttree2))
562
563
readMe = moveS . fst . head . sent . words
564
565
sentParses :: String -> [Sent]
566
sentParses =
567
map (moveS . fst)
568
. filter (null . snd)
569
. sent
570
. words
571
572
-----USAGE: moveS $ (fst.head) $ sent $ words "all see all see all skunks love all mammals"
573
---- ALSO move $ (fst . head) $ t $ words "see all see all non skunks"
574
--- note how 'non' worls
575
576
split2 :: [t] -> [[[t]]] ---- split2 takes any list $\ell$ and gives the list of all ways to split $\ell$ into two sublists
577
--- whose concatenation is $\ell$ again.
578
split2 [] = [[[],[]]]
579
split2 (x:xs) = [[ [], x:xs]] ++ map (\z -> [x:(z!!0),z!!1]) (split2 xs)
580
581
split2' :: [t] -> [([t],[t])]
582
split2' = \case
583
xs@(x : xs') -> ([],xs) : map (first (x :)) (split2' xs')
584
585
{-
586
[] = [([],[])]
587
split2' (x:xs) = [[ [], x:xs]] ++ map (\z -> [x:(z!!0),z!!1]) (split2 xs)
588
-}
589
590
tF :: [String] -> [Term]
591
tF quoted -- this parses terms
592
| quoted == [] = []
593
| (quoted!! 0 == "who") = tF $ drop 1 quoted
594
| (quoted!! 0 == "are") = tF $ drop 1 quoted
595
| quoted == ["skunks"] = [CNasTerm skunks]
596
| quoted == ["mammals"] = [CNasTerm mammals]
597
| quoted == ["chordates"] = [CNasTerm chordates]
598
| quoted == ["boys"] = [CNasTerm boys]
599
| quoted == ["girls"] = [CNasTerm girls]
600
| quoted == ["dogs"] = [CNasTerm dogs]
601
| quoted == ["cats"] = [CNasTerm cats]
602
| quoted == ["birds"] = [CNasTerm birds]
603
| quoted == ["animals"] = [CNasTerm animals]
604
| quoted == ["sneetches"] = [CNasTerm sneetches]
605
| quoted == ["non-skunks"] = [CNasTerm non_skunks]
606
| quoted == ["non-mammals"] = [CNasTerm non_mammals]
607
| quoted == ["non-chordates"] = [CNasTerm non_chordates]
608
| quoted == ["non-boys"] = [CNasTerm non_boys]
609
| quoted == ["non-girls"] = [CNasTerm non_girls]
610
| quoted == ["non-dogs"] = [CNasTerm non_dogs]
611
| quoted == ["non-cats"] = [CNasTerm non_cats]
612
| quoted == ["non-birds"] = [CNasTerm non_birds]
613
| quoted == ["non-animals"] = [CNasTerm non_animals]
614
| quoted == ["non-sneetches"] = [CNasTerm non_sneetches]
615
| quoted == ["x"] = [CNasTerm x]
616
| quoted == ["y"] = [CNasTerm y]
617
| quoted == ["z"] = [CNasTerm z]
618
| quoted == ["p"] = [CNasTerm p]
619
| quoted == ["q"] = [CNasTerm q]
620
| quoted == ["non-x"] = [CNasTerm non_x]
621
| quoted == ["non-y"] = [CNasTerm non_y]
622
| quoted == ["non-z"] = [CNasTerm non_z]
623
| quoted == ["non-p"] = [CNasTerm non_p]
624
| quoted == ["non-q"] = [CNasTerm non_q]
625
| (quoted!! 0 == "see") = [(TermMaker sees r) | r <- (npF (drop 1 quoted))]
626
| (quoted!! 0 == "sees") = [(TermMaker sees r) | r <- (npF (drop 1 quoted))]
627
| (quoted!! 0 == "admires") = [(TermMaker admires r) | r <- (npF (drop 1 quoted))]
628
| (quoted!! 0 == "admire") = [(TermMaker admires r) | r <- (npF (drop 1 quoted))]
629
| (quoted!! 0 == "loves") = [(TermMaker loves r) | r <- (npF (drop 1 quoted))]
630
| (quoted!! 0 == "love") = [(TermMaker loves r) | r <- (npF (drop 1 quoted))]
631
| (quoted!! 0 == "helps") = [(TermMaker helps r) | r <- (npF (drop 1 quoted))]
632
| (quoted!! 0 == "help") = [(TermMaker helps r) | r <- (npF (drop 1 quoted))]
633
| (quoted!! 0 == "hates") = [(TermMaker hates r) | r <- (npF (drop 1 quoted))]
634
| (quoted!! 0 == "hate") = [(TermMaker hates r) | r <- (npF (drop 1 quoted))]
635
| (quoted!! 0 == "doesn't-see") = [(TermMaker not_sees r) | r <- (npF (drop 1 quoted))]
636
| (quoted!! 0 == "doesn't-admire") = [(TermMaker not_admires r) | r <- (npF (drop 1 quoted))]
637
| (quoted!! 0 == "doesn't-love") = [(TermMaker not_loves r) | r <- (npF (drop 1 quoted))]
638
| (quoted!! 0 == "doesn't-help") = [(TermMaker not_helps r) | r <- (npF (drop 1 quoted))]
639
| (quoted!! 0 == "doesn't-hate") = [(TermMaker not_hates r) | r <- (npF (drop 1 quoted))]
640
| (quoted!! 0 == "don't-see") = [(TermMaker not_sees r) | r <- (npF (drop 1 quoted))]
641
| (quoted!! 0 == "don't-admire") = [(TermMaker not_admires r) | r <- (npF (drop 1 quoted))]
642
| (quoted!! 0 == "don't-love") = [(TermMaker not_loves r) | r <- (npF (drop 1 quoted))]
643
| (quoted!! 0 == "don't-help") = [(TermMaker not_helps r) | r <- (npF (drop 1 quoted))]
644
| (quoted!! 0 == "don't-hate") = [(TermMaker not_hates r) | r <- (npF (drop 1 quoted))]
645
| (quoted!! 0 == "r") = [(TermMaker r blurt) | blurt <- (npF (drop 1 quoted))]
646
| otherwise = []
647
648
{--
649
intTF quoted
650
| (quoted !! 0 == ["skunks"]) = [(IntersectionTerm (PCN Pos Skunks) x) | x <- (map tF (drop 1 quoted))]
651
| quoted !! 0 == ["mammals"] = [CNasTerm mammals]
652
| quoted !! 0== ["chordates"] = [CNasTerm chordates]
653
| quoted !! 0== ["boys"] = [CNasTerm boys]
654
| quoted !! 0 == ["girls"] = [CNasTerm girls]
655
| quoted !! 0 == ["dogs"] = [CNasTerm dogs]
656
| quoted !! 0 == ["cats"] = [CNasTerm cats]
657
| quoted !! 0 == ["birds"] = [CNasTerm birds]
658
| quoted !! 0 == ["animals"] = [CNasTerm animals]
659
| quoted !! 0 == ["sneetches"] = [CNasTerm sneetches]
660
| otherwise = []
661
--}
662
663
npF x --- this parses noun phrases
664
| x == [] = []
665
| (x!! 0 == "all") = [(TermNP All w) | w <- tF (drop 1 x)]
666
| (x!! 0 == "some") = [(TermNP Some w) | w <- tF (drop 1 x)]
667
| (x!! 0 == "no") = [(TermNP No w) | w <- tF (drop 1 x)]
668
| (x!! 0 == "most") = [(TermNP Most w) | w <- tF (drop 1 x)]
669
| otherwise = []
670
671
readS input = --- this parses sentences
672
let
673
w = words input
674
firstWord = head w
675
y = tail w
676
sp = split2 y
677
tr = [ x | x <- (map (map tF) sp), (x!!0) /= [], (x!!1) /= []]
678
a = head $ head $ head tr
679
b = head $ head $ tail $ head tr
680
output
681
| firstWord == "all" = Sent All a b
682
| firstWord == "some" = Sent Some a b
683
| firstWord == "most" = Sent Most a b
684
| firstWord == "atleast" = Sent Atleast a b
685
| firstWord == "more" = Sent More a b
686
| firstWord == "no" = Sent No a b
687
| firstWord == "All" = Sent All a b
688
| firstWord == "Some" = Sent Some a b
689
| firstWord == "Most" = Sent Most a b
690
| firstWord == "No" = Sent No a b
691
in output
692
693
readSs = map readS
694
695
toMaybe :: Foldable t => t a -> Maybe a
696
toMaybe = foldl (maybe Just (const . Just)) Nothing
697
698
699
readS' :: String -> Maybe Sent
700
readS' input = case words input of
701
w0:ws -> do
702
q <- lookup (toLower <$> w0)
703
[ ( "all" , All )
704
, ( "some" , Some )
705
, ( "most" , Most )
706
, ( "atleast" , Atleast )
707
, ( "more" , More )
708
, ( "no" , No )
709
]
710
(a,b) <- listToMaybe
711
$ split2' ws >>= uncurry zip . (tF *** tF)
712
return $ Sent q a b
713
_ -> Nothing
714
715
-- return either the strings which failed to be parsed,
716
-- or the full set of parsed sentences.
717
readSs' :: [String] -> Either [String] [Sent]
718
readSs' = foldr rS $ Right []
719
where
720
rS :: String -> Either [String] [Sent] -> Either [String] [Sent]
721
rS s es = case readS' s of
722
Just s' -> case es of
723
Left bad -> Left bad
724
Right good -> Right $ s' : good
725
Nothing -> case es of
726
Left bad -> Left $ s : bad
727
Right good -> Left [s]
728
729
730
type RuleName = String
731
data Rule = Rule {rulename :: RuleName,
732
premises :: [Sent],
733
conclusion :: Sent}
734
deriving (Show, Eq)
735
type RuleList = [Rule]
736
737
junk = Rule {rulename = "junk", premises = readSs ["all x x", "all y y"] , conclusion = readS "all x y"}
738
739
anti = Rule {rulename = "anti", premises = readSs ["all x y"] , conclusion = readS "all non-y non-x"}
740
barbara = Rule {rulename = "barbara", premises = readSs ["all x y", "all y z"] , conclusion = readS "all x z"}
741
some1 = Rule {rulename = "some1", premises = readSs ["some x y"], conclusion = readS "some x x"}
742
some2 = Rule {rulename = "some2", premises = readSs ["some x y"], conclusion = readS "some y x"}
743
darii = Rule {rulename = "darii", premises = readSs ["all y z", "some x y"], conclusion = readS "some x z"}
744
zero = Rule {rulename = "zero", premises = readSs ["all x non-x"], conclusion =readS "all x y"}
745
one = Rule {rulename = "one", premises = readSs ["all non-x x"], conclusion =readS "all y x"}
746
axiom = Rule {rulename = "axiom", premises = [], conclusion =readS "all x x"}
747
exFalso = Rule {rulename = "X", premises = readSs ["some x y", "all x non-y"],
748
conclusion = Sent Contradiction (CNasTerm x) (CNasTerm y) }
749
sdagger = [anti,barbara,some1, some2, darii,zero,axiom,exFalso]
750
751
antiARC = Rule {rulename = "anti", premises = [Sent All Ter1 Ter2], conclusion = (Sent All (TermMaker r (TermNP All Ter2)) (TermMaker r (TermNP All Ter1)))}
752
barbaraARC = Rule {rulename = "barbara", premises = [Sent All Ter1 Ter2, Sent All Ter2 Ter3], conclusion =(Sent All Ter1 Ter3)}
753
allARC = Rule {rulename = "down", premises = [Sent All Ter1 Ter2, Sent All Ter3 (TermMaker r (TermNP All Ter2)) ], conclusion = (Sent All Ter3 (TermMaker r (TermNP All Ter1)))}
754
755
756
757
758
759
data PTree a = T a [PTree a]
760
deriving (Show, Eq)
761
762
lineNumberHelp :: [(a, [Int], Int)] -> [(a, [Int], Int)] -> [(a, [Int], Int)]
763
lineNumberHelp firstseq secondseq =
764
let
765
n = if null firstseq then 0 else (get3 $ head firstseq)
766
modify w = map ( \ (x, listofInts, k) -> (x, (map (\ i -> 1 + n+i) listofInts), k)) w
767
in (firstseq ++ modify secondseq)
768
769
------ near of a PTree lists the nodes in depth-first order, along with an extra list for each node.
770
------ for the node n in the tree it lists the addresses of the children of n in the same tree, again in the
771
------ depth-first order of the tree overall. Getting this right was probably the hardest part of this whole exercise.
772
------ Note also that what we want in the end is not the depth-first listing of the PTree but rather the 'bottom-up' listing,
773
------ and these are related by
774
------
775
------ bottom_up t = reverse (depth_first ( tree_reverse t))
776
777
near :: (PTree a) -> [(a, [Int], Int)]
778
near (T x l) = [(x,s, k+1 )] ++ extrastuff
779
where
780
k = sum r
781
--extrastuff :: [(a, [Int], Int)]
782
extrastuff = foldl lineNumberHelp [] (map near l)
783
--q :: [(a, [Int], Int)]
784
q = map head $ map near l
785
r :: [Int]
786
r = map get3 q
787
s = init( scanl (+) 2 r)
788
789
tree_reverse (T n t) = T n (map tree_reverse (reverse t))
790
791
full_reverse :: [(a, [Int], Int)] -> [(Int, a, [Int])]
792
full_reverse h =
793
let
794
n = length h
795
p = [(i, reverse( map (\ x -> n + 1 - x) j )) | (i,j,k) <- h]
796
q = reverse p
797
w = [1..n]
798
mergeMe :: [Int] -> [(a,[Int])] -> [(Int,a,[Int])]
799
mergeMe [] [] = []
800
mergeMe (i:wMore) (pair:pMore) = (i, (fst pair), (snd pair)) : (mergeMe wMore pMore)
801
in mergeMe w q
802
803
804
805
806
dropList :: [Int] -> Int -> [Int]
807
dropList list x = [i | i <- list, i /= x]
808
dropM :: M -> Int -> M
809
dropM m x = M { noun = (noun m), items = (dropList (items m) x) }
810
dropVb :: Vb -> Int -> Vb
811
dropVb v x = Vb { verb = (verb v), verb_items = [(i,j) | (i,j) <- verb_items v, i/= x, j/= x]}
812
dropModel :: Model -> Int -> Model
813
dropModel m x = Model { universe = (dropList (universe m) x), cnDict = map (\ n -> (dropM n x)) (cnDict m) , verbDict = map (\ v -> (dropVb v x)) (verbDict m) }
814
815
consecutiveDropList :: [Int] -> Int -> [Int]
816
helpForConsecutiveDrop :: Int -> Int -> Int
817
helpForConsecutiveDrop k l = if k < l then k else k -1
818
consecutiveDropList list x = [helpForConsecutiveDrop i x | i <- list, i /= x]
819
consecutiveDropM m x = M { noun = (noun m), items = (consecutiveDropList (items m) x) }
820
consecutiveDropVb :: Vb -> Int -> Vb
821
consecutiveDropVb v x = Vb { verb = (verb v), verb_items = [(helpForConsecutiveDrop i x, helpForConsecutiveDrop j x) | (i,j) <- verb_items v, i/= x, j/= x]}
822
consecutiveDropModel :: Model -> Int -> Model
823
consecutiveDropModel m x = Model { universe = (consecutiveDropList (universe m) x), cnDict = map (\ n -> (consecutiveDropM n x)) (cnDict m) , verbDict = map (\ v -> (consecutiveDropVb v x)) (verbDict m) }
824
825
maybeDrop model x gamma =
826
let
827
mNew = consecutiveDropModel model x --- was dropModel, not consecutiveDropModel
828
tvs = map (\ s -> (semanticsSent s mNew)) gamma
829
in
830
if (and tvs) then mNew else model
831
832
shorten :: Model -> [Sent] -> Model
833
shorten model gamma = foldl (\ m -> (\ x -> maybeDrop m x gamma)) model (universe model)
834
835
iterativelyShorten model gamma = if (model == shorten model gamma) then model else (iterativelyShorten (shorten model gamma) gamma)
836
837
{--dropALot m gamma = foldl (\acc x -> listU m) (listU m)
838
--}
839
data SentencesInModel = SentencesInModel { sentFromModels :: String, truthvalueOfSent :: [Bool]}
840
deriving Show
841
842
data M = M { noun :: CN, items :: [Int] }
843
deriving (Eq, Show)
844
845
data Vb = Vb { verb :: V, verb_items :: [(Int,Int)] }
846
deriving (Eq,Show)
847
848
type Universe = [Int]
849
data Model = Model {universe::Universe, cnDict :: [M], verbDict :: [Vb]}
850
deriving (Eq,Show)
851
852
semanticsTerm :: Term -> Model -> [Int]
853
semanticsTerm (CNasTerm (PCN Pos cn)) m = helper (CNasTerm (PCN Pos cn)) (cnDict m)
854
semanticsTerm (CNasTerm (PCN Neg cn)) m = (universe m) \\ semanticsTerm (CNasTerm (PCN Pos cn)) m
855
semanticsTerm (TermMaker (PV Pos tv) (TermNP All t)) m =
856
let
857
u = universe m
858
tt = semanticsTerm t m
859
vv = verbHelper tv (verbDict m)
860
in
861
[ x | x <- u, and (map (\ y -> implies (y `elem` tt) ((x,y) `elem` vv)) u)]
862
863
semanticsTerm (TermMaker (PV Pos tv) (TermNP Some t)) m =
864
let
865
u = universe m
866
tt = semanticsTerm t m
867
vv = verbHelper tv (verbDict m)
868
in
869
[ x | x <- u, or (map (\ y -> (y `elem` tt) && ((x,y) `elem` vv)) u)]
870
871
semanticsTerm (TermMaker (PV Neg tv) (TermNP All t)) m =
872
let
873
u = universe m
874
tt = semanticsTerm t m
875
vv = verbHelper tv (verbDict m)
876
in
877
[ x | x <- u, and (map (\ y -> implies (y `elem` tt) (not ((x,y) `elem` vv)) ) u)]
878
879
semanticsTerm (TermMaker (PV Neg tv) (TermNP Some t)) m =
880
let
881
u = universe m
882
tt = semanticsTerm t m
883
vv = verbHelper tv (verbDict m)
884
in
885
[ x | x <- u, or (map (\ y -> (y `elem` tt) && (not ((x,y) `elem` vv)) ) u)]
886
887
helper (CNasTerm (PCN Pos cn)) partialList
888
| null partialList = []
889
| cn == (noun $ head $ partialList) = items $ head partialList
890
| otherwise = helper (CNasTerm (PCN Pos cn)) (tail partialList)
891
892
implies b c = (not b) || c
893
894
verbHelper v vList
895
| null vList = []
896
| v == (verb $ head $ vList) = verb_items $ head vList
897
| otherwise = verbHelper v (tail vList)
898
899
semanticsSent :: Sent -> Model -> Bool
900
semanticsSent (Sent All t1 t2) m = and ( map (\ x -> x `elem` s2) s1)
901
where
902
s1 = semanticsTerm t1 m
903
s2 = semanticsTerm t2 m
904
905
semanticsSent (Sent Some t1 t2) m = or (map (\ x -> x `elem` s2) s1)
906
where
907
s1 = semanticsTerm t1 m
908
s2 = semanticsTerm t2 m
909
910
semanticsSent (Sent No t1 t2) m = and ( map (\ x -> not (x `elem` s2)) s1)
911
where
912
s1 = semanticsTerm t1 m
913
s2 = semanticsTerm t2 m
914
915
semanticsSent (Sent Most t1 t2) m =
916
let
917
s1 = semanticsTerm t1 m
918
s2 = semanticsTerm t2 m
919
intersection = s1 `intersect` s2
920
n = length s1
921
k = length intersection
922
in
923
n < 2 * k
924
925
semanticsSent (Sent Atleast t1 t2) m =
926
let
927
s1 = semanticsTerm t1 m
928
s2 = semanticsTerm t2 m
929
in
930
s2 <= s1
931
932
semanticsSent (Sent More t1 t2) m =
933
let
934
s1 = semanticsTerm t1 m
935
s2 = semanticsTerm t2 m
936
in
937
s2 < s1
938
939
--------- pretty printing of models below
940
941
data U = U {name::String, itemsU :: [Int] }
942
deriving Show
943
944
transMtoU t = (U (show(noun t)) (items t))
945
946
data Vextra = Vextra { verbextra :: String, verb_items_extra :: [(Int,Int)] }
947
deriving Show
948
949
transVtoVextra t = (Vextra (show(verb t)) (verb_items t))
950
951
-- a type for fill functions
952
type Filler = Int -> String -> String
953
954
-- a type for describing table columns
955
data ColDesc t = ColDesc
956
{ colTitleFill :: Filler
957
, colTitle :: String
958
, colValueFill :: Filler
959
, colValue :: t -> String
960
}
961
962
-- test data
963
test =
964
[ M Cats [1,2,3],
965
M Dogs [4,5,6],
966
M Skunks [1,3,5,6],
967
M Chordates [ ]
968
]
969
970
vTest = [Vb Sees [(1,1),(1,4),(3,4),(2,5)]]
971
972
model1 = Model {universe = [1,2,3,4,5,6], cnDict = test, verbDict = vTest}
973
974
-- functions that fill a string (s) to a given width (n) by adding pad
975
-- character (c) to align left, right, or center
976
fillLeft c n s = s ++ replicate (n - length s) c
977
fillRight c n s = replicate (n - length s) c ++ s
978
fillCenter c n s = replicate l c ++ s ++ replicate r c
979
where x = n - length s
980
l = x `div` 2
981
r = x - l
982
983
-- functions that fill with spaces
984
newleft = fillLeft ' '
985
right = fillRight ' '
986
center = fillCenter ' '
987
--showTable :: [ColDesc t] -> [t] -> String
988
989
990
showTable cs ts =
991
let header = map colTitle cs
992
rows = [[colValue c t | c <- cs] | t <- ts]
993
widths = [maximum $ map length col | col <- transpose $ header : rows]
994
separator = intercalate "-+-" [replicate width '-' | width <- widths]
995
fillCols fill cols = intercalate " | " [fill c width col | (c, width, col) <- zip3 cs widths cols]
996
in
997
unlines $ fillCols colTitleFill header : separator : map (fillCols colValueFill) rows
998
999
showModelNounsPlusJustifications m gamma phi = do
1000
putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")
1001
putStrLn " "
1002
putStrLn "The nouns are interpreted as follows:"
1003
putStrLn " "
1004
showNouns m
1005
putStrLn " "
1006
putStrLn "Here is how the assumptions and purported conclusion fare in this model:"
1007
putStrLn " "
1008
showSentenceTruthValues m $ gamma++[phi]
1009
1010
showModelNounsVerbsPlusJustifications m gamma phi = do
1011
putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")
1012
putStrLn " "
1013
putStrLn "The nouns and verbs are interpreted as follows:"
1014
putStrLn " "
1015
showNouns m
1016
putStrLn " "
1017
showVerbs m
1018
putStrLn "Here is how the assumptions and purported conclusion fare in this model:"
1019
putStrLn " "
1020
showSentenceTruthValues m $ gamma++[phi]
1021
1022
showModelNounsOnly m = do
1023
putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")
1024
putStrLn " "
1025
putStrLn "The nouns are interpreted as follows:"
1026
putStrLn " "
1027
showNouns m
1028
1029
showModel m = do
1030
putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")
1031
putStrLn " "
1032
putStrLn "The nouns are interpreted as follows:"
1033
putStrLn " "
1034
showNouns m
1035
putStrLn "The transitive verbs are interpreted as follows:"
1036
putStrLn " "
1037
showVerbs m
1038
1039
showNouns m = putStrLn $
1040
showTable
1041
[ ColDesc center "Noun" newleft name
1042
, ColDesc center "Interpretation" newleft (intercalate ", " . map show . itemsU)
1043
]
1044
$ map transMtoU (cnDict m)
1045
1046
showVerbs m = putStrLn $
1047
showTable
1048
[ ColDesc center "Verb" newleft verbextra
1049
, ColDesc center "Interpretation" newleft (intercalate ", " . map show . verb_items_extra)
1050
]
1051
$ map transVtoVextra (verbDict m)
1052
1053
--- showGeneric below is used in showModelPlus
1054
showGeneric m nameLabel contentLabel recordList = putStrLn $
1055
showTable
1056
[ ColDesc center "Term" newleft nameLabel
1057
, ColDesc center "Interpretation" newleft (intercalate ", " . map show . contentLabel)
1058
] recordList
1059
1060
makeSentenceEntry s m = SentencesInModel (show s) [semanticsSent s m]
1061
1062
showSentenceTruthValues m gamma = putStrLn $
1063
showTable
1064
[ ColDesc center "Sentence" newleft sentFromModels
1065
, ColDesc center "Truth Value" newleft (intercalate ", " . map show . truthvalueOfSent )
1066
] (map (\ x -> makeSentenceEntry x m) (gamma))
1067
1068
showModelPlus mod tList = do
1069
showModel mod
1070
putStrLn " "
1071
putStrLn "Extra information on the relevant terms:"
1072
putStrLn " "
1073
let rrrList = map (\ z -> (U { name = (show z) , itemsU = (semanticsTerm z mod) }) ) tList
1074
showGeneric mod name itemsU rrrList
1075
1076
showModelNounsVerbsPlusJustificationsARC :: Model -> [Sent] -> Sent -> [Term] -> IO ()
1077
showModelNounsVerbsPlusJustificationsARC m gamma phi tList = do
1078
putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")
1079
putStrLn " "
1080
putStrLn "The nouns and verbs are interpreted as follows:"
1081
putStrLn " "
1082
showNouns m
1083
putStrLn " "
1084
showVerbs m
1085
putStrLn "Extra information on the relevant terms:"
1086
putStrLn " "
1087
let rrrList = map (\ z -> (U { name = (show z) , itemsU = (semanticsTerm z m) }) ) tList
1088
showGeneric m name itemsU rrrList
1089
putStrLn "Here is how the assumptions and purported conclusion fare in this model:"
1090
putStrLn " "
1091
showSentenceTruthValues m (gamma++[phi])
1092
1093
{-
1094
m gamma phi tList
1095
-----------------
1096
show (universe m)
1097
showNouns n:
1098
showTable
1099
[ ColDesc center "Noun" newleft name
1100
, ColDesc center "Interpretation" newleft (intercalate ", " . map show . itemsU)
1101
]
1102
$ map transMtoU (cnDict m)
1103
showVerbs m:
1104
showTable
1105
[ ColDesc center "Verb" newleft verbextra
1106
, ColDesc center "Interpretation" newleft (intercalate ", " . map show . verb_items_extra)
1107
]
1108
$ map transVtoVextra (verbDict m)
1109
showGeneric m name itemsU (...):
1110
showTable
1111
[ ColDesc center "Term" newleft name
1112
, ColDesc center "Interpretation" newleft (intercalate ", " . map show . itemsU)
1113
] $ map (\ z -> U { name = show z, itemsU = semanticsTerm z m }) tList
1114
showSentenceTruthValues m $ gamma ++ [phi]:
1115
showTable
1116
[ ColDesc center "Sentence" newleft sentFromModels
1117
, ColDesc center "Truth Value" newleft (intercalate ", " . map show . truthvalueOfSent )
1118
]
1119
$ map (\ x -> makeSentenceEntry x m) $ gamma ++ [phi]
1120
-}
1121
1122
1123
1124
{-# LANGUAGE PatternGuards #-}
1125
1126
1127
get1 (a,b,c) = a
1128
get2 (a,b,c) = b
1129
get3 (a,b,c) = c
1130
1131
get1Of4 (a,b,c,d) = a
1132
get2Of4 (a,b,c,d) = b
1133
get3Of4(a,b,c,d) = c
1134
get4Of4(a,b,c,d) = d
1135
1136
--firstHelp :: a -> [a] -> [[a]]
1137
firstHelp x [] = []
1138
firstHelp x (y:ytail) = (x,y) : firstHelp x ytail
1139
1140
--secondHelp :: [a] -> [a] -> [[[a]]]
1141
secondHelp list ys = map (\ x -> firstHelp x ys) list
1142
1143
allFns list1 list2 = sequence $ (secondHelp list1 list2)
1144
1145
lookfor key ((a,b):abs)
1146
| key == a = b
1147
1148
emptyAsDefault :: Maybe [a] -> [a]
1149
emptyAsDefault mx = case mx of
1150
Nothing -> []
1151
Just xy -> xy
1152
zeroAsDefault :: Maybe Int -> Int
1153
zeroAsDefault mx = case mx of
1154
Nothing -> 0
1155
Just xy -> xy
1156
1157
findD (Sent d t1 t2) = d
1158
g1 (Sent d t1 t2) = t1
1159
g2 (Sent d t1 t2) = t2
1160
1161
addAssumptionAsReason :: Sent -> (Sent, String, [Sent])
1162
addAssumptionAsReason s = (s, "A", [])
1163
addReasonsToOriginal :: [Sent] -> [(Sent,String,[Sent])]
1164
addReasonsToOriginal = map addAssumptionAsReason
1165
1166
varsInConclusion :: Rule -> [Term]
1167
varsInConclusion r = nub [g1 $ conclusion r, g2 $ conclusion r]
1168
1169
premiseExtractPairs :: Rule -> [(Term,Term)]
1170
premiseExtractPairs r =
1171
let f (Sent d t1 t2) = (t1, t2)
1172
in map f $ premises r
1173
1174
varsInPremises :: Rule -> [Term]
1175
varsInPremises r = concat [[a,b] | (a,b) <- premiseExtractPairs r]
1176
1177
1178
extras :: Rule -> [Term]
1179
extras r = (varsInConclusion r) \\ (varsInPremises r)
1180
1181
-- I am not sure if 'extras' just above is used. But the variants below are used.
1182
1183
extracnsInRule r = nub $ (cnsIn (conclusion r) ) \\ (concatMap cnsIn (premises r))
1184
extraVerbsInRule r = nub $ (verbsIn (conclusion r) ) \\ (concatMap verbsIn (premises r))
1185
1186
fixDuplicates xs = nubBy conclusionsMatch xs
1187
where conclusionsMatch ys zs =
1188
get1 ys == get1 zs
1189
1190
--------------------------- here is where the main part of the code starts
1191
1192
buildPairOfSubs :: Sent -> [Sent] -> [(Sent, Maybe [(Term, Term)], Maybe [(PV, PV)])]
1193
buildPairOfSubs sent sList = [(s, buildTermSub sent s,buildPVSub sent s) | s <- sList, Nothing /= buildTermSub sent s, Nothing /= buildPVSub sent s ]
1194
1195
ruleToPairOfSubs :: Rule -> [Sent] -> [[(Sent, Maybe [(Term, Term)], Maybe [(PV, PV)])]]
1196
ruleToPairOfSubs rule sList = map (\ x -> buildPairOfSubs x sList) (premises rule)
1197
1198
applicableInstances :: Rule -> [Sent] -> [([Sent], [(Term, Term)], [(PV, PV)])]
1199
applicableInstances rule sList =
1200
let
1201
jj = sequence $ (ruleToPairOfSubs rule sList)
1202
checkerUnary x = foldl combineStructures (Just []) (map get2 x)
1203
checkerBinary x = foldl combineStructures (Just []) (map get3 x)
1204
in
1205
[((map get1 x), (emptyAsDefault $ checkerUnary x), (emptyAsDefault $ checkerBinary x)) | x <- jj, Nothing /= checkerUnary x, Nothing /= checkerBinary x]
1206
1207
1208
1209
extraReconciliationVerbs rule sList = if extraVerbsInRule rule == [] then (applicableInstances rule sList) else
1210
let
1211
above = [[((PV Pos x),(PV Pos y))] | x <- (extraVerbsInRule rule), y<- verblistNotVars]
1212
in
1213
concatMap (\ y -> (map (\ x -> (get1 x, get2 x , (get3 x ++ y))) (applicableInstances rule sList ))) above
1214
1215
1216
extraReconciliationCNs rule sList = if extracnsInRule rule == [] then (extraReconciliationVerbs rule sList) else
1217
let
1218
useThese = nub $ concatMap cnsIn sList
1219
firstSet = [CNasTerm (PCN Pos cn1 ) | cn1 <- (extracnsInRule rule)]
1220
secondSet = [CNasTerm (PCN Pos cn2 ) | cn2 <- useThese]
1221
firstSetNeg = [CNasTerm (PCN Neg cn1 ) | cn1 <- (extracnsInRule rule)]
1222
secondSetNeg = [CNasTerm (PCN Neg cn2 ) | cn2 <- useThese]
1223
tv = or $ map (hasNegativeMarker . pCNsIn) $ concatMap subterms sList
1224
extras = if tv then allFns firstSet secondSetNeg else [ ]
1225
subs = (allFns firstSet secondSet) ++ extras
1226
in
1227
concatMap (\ y -> (map (\ x -> (get1 x, (get2 x ++ y) , get3 x)) (extraReconciliationVerbs rule sList ))) subs
1228
1229
1230
1231
render rule item =
1232
let t = conclusion rule
1233
u = spellOut t (get2 item) (get3 item)
1234
in (u, (rulename rule), get1 item)
1235
1236
dropReasons = map get1
1237
1238
1239
applyARule sList r = nub $ map (\ x -> render r x) (extraReconciliationCNs r sList)
1240
1241
applyAllRules sListWithReasons rl =
1242
let
1243
z = dropReasons sListWithReasons
1244
a = map (applyARule z) rl
1245
b = concat a
1246
in
1247
fixDuplicates $ sListWithReasons ++ b
1248
1249
1250
type SentRule = (Sent,String)
1251
--data PTree = T (Sent,String) [PTree] ---- for development purposes, this declaration was moved to ProofTreeNumbers.hs
1252
-- deriving (Show, Eq)
1253
1254
1255
1256
ll phi stumpset =
1257
if (get1 $ (stumpset !! 0)) == phi then (stumpset !! 0) else (ll phi (tail stumpset))
1258
1259
proofSearch phi stumpset =
1260
T ((get1 a), (get2 a)) (map (\ x -> (proofSearch x stumpset)) (get3 a))
1261
where a = ll phi stumpset
1262
1263
1264
firstRepeat (x:y:rest) = if x == y then x else firstRepeat (y:rest)
1265
1266
allDerived :: [Sent] -> [Rule] -> [(Sent, RuleName, [Sent])]
1267
allDerived noReasons rl = allDerivedUnderRepresentations noReasons rl
1268
1269
1270
allDerivedUnderRepresentations noReasons rl= firstRepeat $ fixedPoint addReasons rl
1271
where addReasons = addReasonsToOriginal noReasons
1272
1273
fixedPoint withReasons rl = withReasons : map (\ x -> applyAllRules x rl) (fixedPoint withReasons rl)
1274
1275
fullStory noReasonList ruleList = fullStoryUnderRepresentations (readSs noReasonList) ruleList
1276
--- e.g. fullStory ["all skunks mammals", "some mammals non-chordates"] sdagger
1277
1278
--fullStoryUnderRepresentations :: AssumptionList -> [Rule] -> IO ()
1279
fullStoryUnderRepresentations noReasonList ruleList = mapM_ print $ map get1 $ allDerivedUnderRepresentations noReasonList ruleList
1280
1281
modify outputList = map (\ x -> (get1 x, fst (get2 x), snd(get2 x), get3 x)) outputList
1282
1283
-- let stumpset = allDerived [s8,s12] sList
1284
-- let phi = get1 $ stumpset !! 13
1285
findProofByNumber n ch = mapM_ print $ modify . full_reverse . near . tree_reverse $ proofSearch (get1 (ch !! n)) ch
1286
1287
inconsistency stumpset =
1288
let
1289
q = map get1 stumpset
1290
--qq = map principalDet q
1291
in
1292
dropWhile (\ s -> Contradiction /= principalDet s)q
1293
1294
-- relevantChunk is NOT USED!
1295
1296
relevantChunk phi gamma ruleList =
1297
let
1298
addReasons = addReasonsToOriginal gamma
1299
bingo = fixedPoint addReasons ruleList
1300
firstRepeatOrFind (x:y:rest)
1301
| phi `elem` (map get1 x) = x
1302
| (inconsistency x) /= [] = x
1303
| x == y = x
1304
| otherwise = firstRepeatOrFind (y:rest)
1305
in firstRepeatOrFind bingo
1306
1307
1308
follows phi gamma ruleList = followsUnderRepresentation (readS phi) (readSs gamma) ruleList
1309
1310
followsUnderRepresentation phi gamma ruleList =
1311
let
1312
addReasons = addReasonsToOriginal gamma
1313
bingo = fixedPoint addReasons ruleList
1314
firstRepeatOrFind (x:y:rest)
1315
| phi `elem` (map get1 x) = do
1316
putStrLn " "
1317
putStrLn "The sentence follows, and here is a derivation in the given logic from the assumptions:"
1318
putStrLn " "
1319
mapM_ print $ modify . full_reverse $ near $ tree_reverse $ proofSearch phi x
1320
putStrLn " "
1321
| (inconsistency x) /= [] = do
1322
putStrLn " "
1323
putStrLn "As shown below, the list of assumptions is inconsistent, so every sentence follows."
1324
putStrLn " "
1325
mapM_ print $ modify . full_reverse $ near $ tree_reverse $ proofSearch (head $ (inconsistency x)) x
1326
| x == y = do
1327
putStrLn " "
1328
putStrLn "The given sentence is not provable from the assumptions in the logic."
1329
putStrLn " "
1330
| otherwise = firstRepeatOrFind (y:rest)
1331
in firstRepeatOrFind bingo
1332
1333
--- :set +s for timing
1334
1335
1336
1337
---- EXTRA STUFF TO TRY TO PRETTY-PRINT THE PROOFS IN 3 NICE COLUMNS
1338
---- SO FAR, NO LUCK!
1339
1340
--import Data.List (transpose, intercalate)
1341
1342
1343
-- a type for records
1344
data K = K { make :: String
1345
, model :: String
1346
, modell :: String
1347
, years :: [Int] }
1348
deriving Show
1349
1350
1351
1352
1353
1354
1355
data SToPrint = SToPrint { lineToPrint :: Int
1356
, sentToPrint :: Sent
1357
, ruleToPrint :: RuleName
1358
, reasonsToPrint :: [Int] }
1359
deriving Show
1360
1361
1362
1363
1364
-- functions that fill with spaces
1365
1366
type Proof = [(Int,Sent,RuleName,[Int])]
1367
type ProofChunk = [(Sent,RuleName,[Sent])]
1368
type StopList = [Term]
1369
1370
oneStepARC :: StopList
1371
-> ProofChunk
1372
-> ProofChunk
1373
oneStepARC stopList gammaWithReasons =
1374
fixDuplicates $ gammaWithReasons ++
1375
filter (checkARC stopList . get1)
1376
( concatMap
1377
(applyARule $ dropReasons gammaWithReasons)
1378
[ antiARC
1379
, barbaraARC
1380
, axiom
1381
]
1382
)
1383
1384
checkARC :: StopList
1385
-> Sent
1386
-> Bool
1387
checkARC stopList (Sent _ t _) = t `elem` stopList
1388
1389
followsInARC :: [String] -> String -> IO ()
1390
followsInARC gamma phi =
1391
either
1392
-- either (\x -> Left ( modelBuildARC stopList x , stopList , map get1 x )) Right
1393
( \x -> do
1394
putStrLn $ unlines
1395
[ "The given sentence is not provable from the assumptions in ARC."
1396
, ""
1397
, "Here is a counter-model:"
1398
, ""
1399
]
1400
showCounterModel gamma' phi' x
1401
putStrLn $ unlines
1402
[ ""
1403
, "And here is the list of sentences which do follow, and with restrictions:"
1404
, ""
1405
]
1406
mapM_ (print . get1) x
1407
putStrLn ""
1408
)
1409
( \p -> do
1410
putStrLn $ unlines
1411
[ "The sentence follows from the assumptions in ARC."
1412
, ""
1413
, "Here is a derivation:"
1414
, ""
1415
]
1416
mapM_ print p
1417
putStrLn ""
1418
)
1419
$ followsInARCUnderRepresentations gamma' phi'
1420
where
1421
gamma' = readSs gamma
1422
phi' = readS phi
1423
stopList = mkStopList gamma' phi'
1424
1425
showCounterModel :: [Sent] -> Sent -> ProofChunk -> IO ()
1426
showCounterModel gamma phi x =
1427
showModelNounsVerbsPlusJustificationsARC
1428
(modelBuildARC stopList x)
1429
gamma
1430
phi
1431
stopList
1432
where
1433
stopList = mkStopList gamma phi
1434
1435
followsInARCUnderRepresentations :: [Sent] -> Sent -> Either ProofChunk Proof
1436
followsInARCUnderRepresentations gamma phi =
1437
findFixedPointOrSat (oneStepARC stopList)
1438
( \x -> if phi `elem` map get1 x then Just $ numberProof phi x else Nothing
1439
)
1440
$ addReasonsToOriginal gamma
1441
where
1442
stopList = mkStopList gamma phi
1443
1444
numberProof :: Sent -> ProofChunk -> Proof
1445
numberProof phi = modify . full_reverse . near . tree_reverse . proofSearch phi
1446
1447
justCounterModelARC :: [Sent] -> Sent -> Model
1448
justCounterModelARC gamma phi =
1449
modelBuildARC stopList
1450
$ findFixedPoint (oneStepARC stopList)
1451
$ addReasonsToOriginal gamma
1452
where
1453
stopList = mkStopList gamma phi
1454
1455
findFixedPointOrSat :: Eq a => (a -> a) -> (a -> Maybe b) -> a -> Either a b
1456
findFixedPointOrSat f p = loop
1457
where
1458
loop x
1459
| Just y <- p x = Right y
1460
| x == f x = Left x
1461
| otherwise = loop $ f x
1462
1463
findFixedPoint :: Eq a => (a -> a) -> a -> a
1464
findFixedPoint f = either id id . findFixedPointOrSat f (\_ -> Nothing)
1465
1466
mkStopList :: [Sent] -> Sent -> StopList
1467
mkStopList gamma phi = nub $ concatMap subterms $ gamma ++ [phi]
1468
1469
{-
1470
countermod :: [Sent]
1471
-> Sent
1472
-> StopList
1473
-> ProofChunk
1474
-> IO ()
1475
countermod gamma phi stpList chunk =
1476
showModelNounsVerbsPlusJustificationsARC m gamma phi stpList -- !!!! PUT THIS BACK FOR the FULL MODELS
1477
-- showModelNounsVerbsPlusJustificationsARC mShortened gamma phi stpList -- !!! PUT THIS BACK FOR SMALLER MODELS
1478
where
1479
m = modelBuildARC stpList chunk -- !!!! USE THIS AND THIS ONLY FOR the FULL MODELS
1480
-- mShortened = iterativelyShorten m (gamma++[negation phi]) -- !!!! PUT THIS BACK FOR SMALLER MODELS
1481
-}
1482
1483
modelBuildARC :: StopList
1484
-> ProofChunk
1485
-> Model
1486
modelBuildARC stopList chunk = Model
1487
{ universe = uni
1488
, cnDict = map
1489
( \p ->
1490
M p $ cnInterpretationFn p
1491
) relevantCNs
1492
, verbDict = map
1493
( \transverb ->
1494
Vb transverb
1495
$ tvInterpretationFn transverb
1496
) relVerbs
1497
}
1498
where
1499
uni = [ 0 .. length stopList - 1 ]
1500
sList = map get1 chunk
1501
relVerbs = nub $ concatMap verbsIn sList
1502
relevantCNs = nub $ concatMap cnsIn sList
1503
order = map pairOfTerms sList
1504
cnInterpretationFn cn =
1505
[ r
1506
| r <- uni
1507
, (stopList !! r , CNasTerm $ PCN Pos cn) `elem` order
1508
]
1509
tvInterpretationFn tv =
1510
[ (r,s)
1511
| r <- uni
1512
, s <- uni
1513
, (stopList !! r, TermMaker (PV Pos tv) (TermNP All (stopList !! s))) `elem` order
1514
]
1515
termInterpretationFn t =
1516
( t
1517
, [ r
1518
| r <- uni
1519
, (stopList !! r , t) `elem` order
1520
]
1521
)
1522
1523
pairOfTerms :: Sent -> (Term,Term)
1524
pairOfTerms (Sent _ t u) = (t,u)
1525
1526
-- Here is a test of the main example in Chapter 2
1527
1528
tSkunks, tMammals, tChordates, tSkunks2 :: Term
1529
tSkunks = TermMaker sees (TermNP All (CNasTerm skunks) )
1530
tMammals = TermMaker sees (TermNP All (CNasTerm mammals))
1531
tChordates = TermMaker sees (TermNP All (CNasTerm chordates))
1532
tSkunks2 = TermMaker sees (TermNP All tSkunks )
1533
1534
testCh2gamma :: [Sent]
1535
testCh2gamma =
1536
[ Sent All (CNasTerm skunks) (CNasTerm chordates)
1537
, Sent All tMammals tSkunks2
1538
, Sent All tSkunks2 (CNasTerm mammals)
1539
]
1540
1541
testCh2phi :: Sent
1542
testCh2phi = Sent All tSkunks tChordates
1543
1544
--followsInARCUnderRepresenatations testCh2phi testCh2gamma
1545
1546
tCh2gamma :: [String]
1547
tCh2gamma =
1548
[ "all skunks chordates"
1549
, "all see all mammals see all see all skunks"
1550
, "all see all see all skunks mammals"
1551
, "all mammals see all chordates"
1552
]
1553
1554
tCh2phi :: String
1555
tCh2phi = "all see all skunks see all chordates"
1556
1557
tCh2_run :: IO ()
1558
tCh2_run = followsInARC tCh2gamma tCh2phi
1559
1560
-- followsInARC tCh2phi tCh2gamma
1561
1562
-- The specific mapping to Chapter 2 is
1563
-- skunks --> hawks
1564
---chordates --> birds
1565
-- mammals -> turtles
1566
1567
-- 0 hawks
1568
-- 1 birds
1569
-- 2 see all turtles
1570
-- 3 turtles
1571
-- 4 see all see all hawks
1572
-- 5 see all hawks
1573
-- 6 see all birds
1574
1575
1576
1577
-- here there
1578
--- 0 1
1579
--- 1 6
1580
--- 2 5
1581
--- 3
1582
--- 4
1583
--- 5
1584
--- 6
1585
1586
test2 :: [M]
1587
test2 =
1588
[ M Chordates [3,4]
1589
, M Birds [1,2,5]
1590
, M Skunks [2]
1591
]
1592
1593
vTest2 :: [Vb]
1594
vTest2 =
1595
[ Vb Sees [(1,2),(1,3), (2,1), (2,5), (3,1), (3,3), (3,4), (3,5), (4,3), (4,4), (5,2), (5,3)],
1596
Vb Loves [(1,1),(1,2), (1,3), (1,5), (2,1), (3,3), (5,2), (5,5)] ,
1597
Vb Hates [(2,1),(3,4), (5,1), (5,2), (5,3), (5,4)]
1598
]
1599
1600
model2 :: Model
1601
model2 = Model
1602
{ universe = [1,2,3,4,5]
1603
, cnDict = test2
1604
, verbDict = vTest2
1605
}
1606
1607
--- A GOOD TEST IS BELOW
1608
--- followsInARC "all who see all who hate all skunks see all who love all mammals" ["all mammals hate all skunks", "all skunks see all skunks", "all who love all mammals are skunks", "all boys see all who love all skunks", "all who see all boys are mammals"]
1609
1610
-- followsInARC "all who see all who hate all skunks see all who love all mammals" ["all mammals see all skunks", "all skunks see all skunks", "all who love all mammals are skunks", "all boys see all who love all skunks", "all who see all boys are mammals"]
1611
1612
-- followsInARC "all who see all who hate all skunks see all who love all mammals" ["all mammals hate all skunks", "all skunks see all skunks", "all who love all mammals are skunks", "all boys see all who love all skunks", "all who see all boys are mammals", "all who see all boys see all skunks", "all who see all skunks love all skunks"]
1613
1614
1615
1616
1617
1618