Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

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

Views: 6871
1
{-# LANGUAGE PatternGuards #-}
2
3
module AllVerbsRelativeClauses where
4
5
6
import Data.List
7
import Control.Monad
8
import ARC/Syntax2
9
import ARC/FrontEnd
10
import ARC/SyllogisticInference
11
import ARC/ExampleSentences
12
import ARC/ExampleRules
13
import ARC/ProofTreeNumbers
14
import ARC/Models
15
16
type Proof = [(Int,Sent,RuleName,[Int])]
17
type ProofChunk = [(Sent,RuleName,[Sent])]
18
type StopList = [Term]
19
20
termEval x = semanticsTerm (head $ tF $ words x)
21
22
-- allVerbsIn gamma = nub $ concatMap verbsIn (readSs gamma)
23
24
-- tryOneStep z = applyAllRules z [antiARC, barbaraARC, axiom]
25
26
syntaxCheck :: String -> IO()
27
syntaxCheck x =
28
case (inARC (readS x)) of
29
(Just True) -> putStrLn $ "Looks good!"
30
(Just False) -> putStrLn $ "Sorry, not in the fragment of this notebook"
31
Nothing -> putStrLn $ "Sorry, invalid input"
32
33
evaluate :: Model -> String -> Bool
34
evaluate m s = semanticsSent (readS s) m
35
{--
36
37
allTerms gamma n = nub $ concatMap subterms $ map get1 $ (infiniteSeq gamma (head gamma )) !! n
38
39
40
infinitelyManyModels gamma n =
41
showSentenceTruthValues m (readSs gamma)
42
where
43
r = infiniteSeq gamma (head gamma)
44
a = r!! 2
45
t = allTerms gamma n
46
m = modelBuildARC t a
47
48
--}
49
50
oneStepARC :: StopList
51
-> ProofChunk
52
-> ProofChunk
53
oneStepARC stopList gammaWithReasons =
54
fixDuplicates $ gammaWithReasons ++
55
filter (checkARC stopList . get1)
56
( concatMap
57
(applyARule $ dropReasons gammaWithReasons)
58
[ antiARC
59
, barbaraARC
60
, axiom
61
]
62
)
63
64
65
66
67
68
69
70
71
checkARC :: StopList
72
-> Sent
73
-> Bool
74
checkARC stopList (Sent _ t _) = t `elem` stopList
75
76
{--
77
oldFollowsInARC :: [String] -> String -> IO ()
78
oldFollowsInARC gamma phi =
79
either
80
-- either (\x -> Left ( modelBuildARC stopList x , stopList , map get1 x )) Right
81
( \x -> do
82
putStrLn $ unlines
83
[ "The given sentence is not provable from the assumptions in ARC."
84
, ""
85
, "Here is a counter-model:"
86
, ""
87
88
]
89
showCounterModel gamma' phi' x
90
putStrLn $ unlines
91
[ ""
92
, "The model is built using the sentences below, all of which do follow from the assumptions:"
93
, ""
94
]
95
mapM_ (print . get1) x
96
putStrLn ""
97
)
98
( \p -> do
99
putStrLn $ unlines
100
[ "The conclusion follows from the assumptions in ARC."
101
, ""
102
, "Here is a derivation:"
103
]
104
putStrLn $ showTableForProofs [ ColDesc center " " left (show . line)
105
, ColDesc center " " left (show. sentence)
106
, ColDesc center " " left rule
107
, ColDesc center " " right (intercalate ", " . map show . supports)
108
] $ map (\k -> ProofLine {line= get1Of4 k, sentence=get2Of4 k, rule = get3Of4 k, supports = get4Of4 k}) p
109
putStrLn $ unlines ["", "",""]
110
)
111
$ followsInARCUnderRepresentations gamma' phi'
112
where
113
gamma' = readSs gamma
114
phi' = readS phi
115
stopList = mkStopList gamma' phi'
116
117
-- last few lines above
118
--p -- NOTE THE LINE BELOW!
119
mapM_ print p
120
putStrLn ""
121
)
122
$ followsInARCUnderRepresentations gamma' phi'
123
where
124
gamma' = readSs gamma
125
phi' = readS phi
126
stopList = mkStopList gamma' phi'
127
--}
128
129
130
showCounterModel :: [Sent] -> Sent -> ProofChunk -> IO ()
131
showCounterModel gamma phi x =
132
showModelNounsVerbsPlusJustificationsARC
133
(modelBuildARC stopList x)
134
gamma
135
phi
136
stopList
137
where
138
stopList = mkStopList gamma phi
139
140
--trivialExtension :: [Sent] -> Sent -> [Sent]
141
--trivialExtension gamma phi = gamma ++ (dropReasons $ applyARule [phi] axiom)
142
143
144
145
146
mkProofChunk gamma = addReasonsToOriginal $ readSs gamma
147
mkStopLforInfinite gamma = mkStopL $ readSs gamma
148
149
followsInARCUnderRepresentations :: [Sent] -> Sent -> Either ProofChunk Proof
150
followsInARCUnderRepresentations gamma phi =
151
--findFixedPointOrSat (oneStepARC2 stopList (concatMap cnsIn $ readSs $ gamma ++ [phi] ) (concatMap verbsIn $ readSs $ gamma ++ [phi]))
152
findFixedPointOrSat (oneStepARC stopList)
153
( \x -> if phi `elem` map get1 x then Just $ numberProof phi x else Nothing
154
)
155
$ (addReasonsToOriginal $ gamma) ++ (applyARule [phi] axiom) --- this did not have the applyARule part!
156
where
157
stopList = mkStopList gamma phi
158
159
160
--- revised stuff below
161
162
oneStepARC2 stopList gammaWithReasons x y =
163
fixDuplicates $ gammaWithReasons ++
164
filter (checkARC stopList . get1) (applyAllRules2 gammaWithReasons [antiARC,barbaraARC,axiom] x y)
165
166
167
f gamma phi =
168
findFixedPointOrSat
169
(\ w -> oneStepARC2 stopList w b c)
170
( \x -> if p `elem` map get1 x then Just $ numberProof p x else Nothing)
171
y
172
where
173
a = readSs $ gamma ++ [phi]
174
b = concatMap cnsIn a
175
c = concatMap verbsIn a
176
y = mkProofChunk gamma
177
p = readS phi
178
stopList = mkStopL a
179
180
181
182
followsInARC gamma phi =
183
either
184
-- either (\x -> Left ( modelBuildARC stopList x , stopList , map get1 x )) Right
185
( \x -> do
186
putStrLn $ unlines
187
[ "The given sentence is not provable from the assumptions in ARC."
188
, ""
189
, "Here is a counter-model:"
190
, ""
191
]
192
showCounterModel gamma' phi' x
193
putStrLn $ unlines
194
[ ""
195
, "The model is built using the sentences below, all of which do follow from the assumptions:"
196
, ""
197
]
198
mapM_ (print . get1) x
199
putStrLn ""
200
)
201
( \p -> do
202
putStrLn $ unlines
203
[ "The conclusion follows from the assumptions in ARC."
204
, ""
205
, "Here is a derivation:"
206
]
207
putStrLn $ showTableForProofs [ ColDesc center " " left (show . line)
208
, ColDesc center " " left (show. sentence)
209
, ColDesc center " " left rule
210
, ColDesc center " " right (intercalate ", " . map show . supports)
211
] $ map (\k -> ProofLine {line= get1Of4 k, sentence=get2Of4 k, rule = get3Of4 k, supports = get4Of4 k}) p
212
putStrLn $ unlines ["", "",""]
213
)
214
$ (f gamma phi)
215
where
216
gamma' = readSs gamma
217
phi' = readS phi
218
stopList = mkStopList gamma' phi'
219
220
infiniteSeq gamma phi = x
221
where
222
a = readSs $ gamma ++ [phi]
223
b = concatMap cnsIn a
224
c = concatMap verbsIn a
225
y = mkProofChunk gamma
226
stopList = mkStopL a
227
x = y : map (\ w -> oneStepARC2 stopList w b c ) x
228
229
230
numberProof :: Sent -> ProofChunk -> Proof
231
numberProof phi = modify . full_reverse . near . tree_reverse . proofSearch phi
232
233
justCounterModelARC :: [Sent] -> Sent -> Model
234
justCounterModelARC gamma phi =
235
modelBuildARC stopList
236
$ findFixedPoint (oneStepARC stopList)
237
$ addReasonsToOriginal gamma
238
where
239
stopList = mkStopList gamma phi
240
241
findFixedPointOrSat :: Eq a => (a -> a) -> (a -> Maybe b) -> a -> Either a b
242
findFixedPointOrSat f p = loop
243
where
244
loop x
245
| Just y <- p x = Right y
246
| x == f x = Left x
247
| otherwise = loop $ f x
248
249
findFixedPoint :: Eq a => (a -> a) -> a -> a
250
findFixedPoint f = either id id . findFixedPointOrSat f (\_ -> Nothing)
251
252
mkStopL :: [Sent] -> StopList
253
mkStopL gamma =
254
nub $ concatMap subterms $ gamma
255
256
mkStopList :: [Sent] -> Sent -> StopList
257
mkStopList gamma phi = mkStopL $ gamma ++ [phi]
258
259
{-
260
countermod :: [Sent]
261
-> Sent
262
-> StopList
263
-> ProofChunk
264
-> IO ()
265
countermod gamma phi stpList chunk =
266
showModelNounsVerbsPlusJustificationsARC m gamma phi stpList -- !!!! PUT THIS BACK FOR the FULL MODELS
267
-- showModelNounsVerbsPlusJustificationsARC mShortened gamma phi stpList -- !!! PUT THIS BACK FOR SMALLER MODELS
268
where
269
m = modelBuildARC stpList chunk -- !!!! USE THIS AND THIS ONLY FOR the FULL MODELS
270
-- mShortened = iterativelyShorten m (gamma++[negation phi]) -- !!!! PUT THIS BACK FOR SMALLER MODELS
271
-}
272
273
modelBuildARC :: StopList
274
-> ProofChunk
275
-> Model
276
modelBuildARC stopList chunk = Model
277
{ universe = uni
278
, cnDict = map
279
( \p ->
280
M p $ cnInterpretationFn p
281
) relevantCNs
282
, verbDict = map
283
( \transverb ->
284
Vb transverb
285
$ tvInterpretationFn transverb
286
) relVerbs
287
}
288
where
289
uni = [ 0 .. length stopList - 1 ]
290
sList = map get1 chunk
291
relVerbs = nub $ concatMap verbsIn sList
292
relevantCNs = nub $ concatMap cnsIn sList
293
order = map pairOfTerms sList
294
cnInterpretationFn cn =
295
[ r
296
| r <- uni
297
, (stopList !! r , CNasTerm $ PCN Pos cn) `elem` order
298
]
299
tvInterpretationFn tv =
300
[ (r,s)
301
| r <- uni
302
, s <- uni
303
, (stopList !! r, TermMaker (PV Pos tv) (TermNP All (stopList !! s))) `elem` order
304
]
305
termInterpretationFn t =
306
( t
307
, [ r
308
| r <- uni
309
, (stopList !! r , t) `elem` order
310
]
311
)
312
313
pairOfTerms :: Sent -> (Term,Term)
314
pairOfTerms (Sent _ t u) = (t,u)
315
316
-- Here is a test of the main example in Chapter 2
317
318
tSkunks, tMammals, tChordates, tSkunks2 :: Term
319
tSkunks = TermMaker sees (TermNP All (CNasTerm skunks) )
320
tMammals = TermMaker sees (TermNP All (CNasTerm mammals))
321
tChordates = TermMaker sees (TermNP All (CNasTerm chordates))
322
tSkunks2 = TermMaker sees (TermNP All tSkunks )
323
324
testCh2gamma :: [Sent]
325
testCh2gamma =
326
[ Sent All (CNasTerm skunks) (CNasTerm chordates)
327
, Sent All tMammals tSkunks2
328
, Sent All tSkunks2 (CNasTerm mammals)
329
]
330
331
testCh2phi :: Sent
332
testCh2phi = Sent All tSkunks tChordates
333
334
--followsInARCUnderRepresentations testCh2phi testCh2gamma
335
336
tCh2gamma :: [String]
337
tCh2gamma =
338
[ "all skunks chordates"
339
, "all see all mammals see all see all skunks"
340
, "all see all see all skunks mammals"
341
, "all mammals see all chordates"
342
]
343
344
tCh2phi :: String
345
tCh2phi = "all see all skunks see all chordates"
346
347
tCh2_run :: IO ()
348
tCh2_run = followsInARC tCh2gamma tCh2phi
349
350
-- followsInARC tCh2phi tCh2gamma
351
352
-- The specific mapping to Chapter 2 is
353
-- skunks --> hawks
354
---chordates --> birds
355
-- mammals -> turtles
356
357
-- 0 hawks
358
-- 1 birds
359
-- 2 see all turtles
360
-- 3 turtles
361
-- 4 see all see all hawks
362
-- 5 see all hawks
363
-- 6 see all birds
364
365
366
367
-- here there
368
--- 0 1
369
--- 1 6
370
--- 2 5
371
--- 3
372
--- 4
373
--- 5
374
--- 6
375
376
test2 :: [M]
377
test2 =
378
[ M Chordates [3,4]
379
, M Birds [1,2,5]
380
, M Skunks [2]
381
]
382
383
vTest2 :: [Vb]
384
vTest2 =
385
[ 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)],
386
Vb Loves [(1,1),(1,2), (1,3), (1,5), (2,1), (3,3), (5,2), (5,5)] ,
387
Vb Hates [(2,1),(3,4), (5,1), (5,2), (5,3), (5,4)]
388
]
389
390
model2 :: Model
391
model2 = Model
392
{ universe = [1,2,3,4,5]
393
, cnDict = test2
394
, verbDict = vTest2
395
}
396
397
398
399
--- A GOOD TEST IS BELOW
400
--- 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"]
401
402
-- 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"]
403
404
-- 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"]
405
406
407
--}
408
409