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 AllVerbsRelativeClausesPrime where
4
5
import Data.List
6
import Control.Monad
7
import Syntax2
8
import FrontEnd
9
import SyllogisticInference
10
import ExampleSentences
11
import ExampleRules
12
import ProofTreeNumbers
13
import Models
14
15
type Proof = [(Int,Sent,RuleName,[Int])]
16
type ProofChunk = [(Sent,RuleName,[Sent])]
17
type StopList = [Term]
18
19
oneStepARC :: StopList
20
-> ProofChunk
21
-> ProofChunk
22
oneStepARC stopList gammaWithReasons =
23
fixDuplicates $ gammaWithReasons ++
24
filter (checkARC stopList . get1)
25
( concatMap
26
(applyARule $ dropReasons gammaWithReasons)
27
[ downARC
28
, barbaraARC
29
, axiom
30
]
31
)
32
33
checkARC :: StopList
34
-> Sent
35
-> Bool
36
checkARC stopList (Sent _ t _) = t `elem` stopList
37
38
followsInARC :: [String] -> String -> IO ()
39
followsInARC gamma phi =
40
either
41
-- either (\x -> Left ( modelBuildARC stopList x , stopList , map get1 x )) Right
42
( \x -> do
43
putStrLn $ unlines
44
[ "The given sentence is not provable from the assumptions in ARC."
45
, ""
46
, "Here is a counter-model:"
47
, ""
48
]
49
showCounterModel gamma' phi' x
50
putStrLn $ unlines
51
[ ""
52
, "And here is the list of sentences which do follow, and with restrictions:"
53
, ""
54
]
55
mapM_ (print . get1) x
56
putStrLn ""
57
)
58
( \p -> do
59
putStrLn $ unlines
60
[ "The sentence follows from the assumptions in ARC."
61
, ""
62
, "Here is a derivation:"
63
, ""
64
]
65
mapM_ print p
66
putStrLn ""
67
)
68
$ followsInARCUnderRepresentations gamma' phi'
69
where
70
gamma' = readSs gamma
71
phi' = readS phi
72
stopList = mkStopList gamma' phi'
73
74
showCounterModel :: [Sent] -> Sent -> ProofChunk -> IO ()
75
showCounterModel gamma phi x =
76
showModelNounsVerbsPlusJustificationsARC
77
(modelBuildARC stopList x)
78
gamma
79
phi
80
stopList
81
where
82
stopList = mkStopList gamma phi
83
84
followsInARCUnderRepresentations :: [Sent] -> Sent -> Either ProofChunk Proof
85
followsInARCUnderRepresentations gamma phi =
86
findFixedPointOrSat (oneStepARC stopList)
87
( \x -> if phi `elem` map get1 x then Just $ numberProof phi x else Nothing
88
)
89
$ addReasonsToOriginal $ gamma
90
where
91
stopList = mkStopList gamma phi
92
93
numberProof :: Sent -> ProofChunk -> Proof
94
numberProof phi = modify . full_reverse . near . tree_reverse . proofSearch phi
95
96
justCounterModelARC :: [Sent] -> Sent -> Model
97
justCounterModelARC gamma phi =
98
modelBuildARC stopList
99
$ findFixedPoint (oneStepARC stopList)
100
$ addReasonsToOriginal gamma
101
where
102
stopList = mkStopList gamma phi
103
104
findFixedPointOrSat :: Eq a => (a -> a) -> (a -> Maybe b) -> a -> Either a b
105
findFixedPointOrSat f p = loop
106
where
107
loop x
108
| Just y <- p x = Right y
109
| x == f x = Left x
110
| otherwise = loop $ f x
111
112
findFixedPoint :: Eq a => (a -> a) -> a -> a
113
findFixedPoint f = either id id . findFixedPointOrSat f (\_ -> Nothing)
114
115
mkStopList :: [Sent] -> Sent -> StopList
116
mkStopList gamma phi = nub $ concatMap subterms $ gamma ++ [phi]
117
118
{-
119
countermod :: [Sent]
120
-> Sent
121
-> StopList
122
-> ProofChunk
123
-> IO ()
124
countermod gamma phi stpList chunk =
125
showModelNounsVerbsPlusJustificationsARC m gamma phi stpList -- !!!! PUT THIS BACK FOR the FULL MODELS
126
-- showModelNounsVerbsPlusJustificationsARC mShortened gamma phi stpList -- !!! PUT THIS BACK FOR SMALLER MODELS
127
where
128
m = modelBuildARC stpList chunk -- !!!! USE THIS AND THIS ONLY FOR the FULL MODELS
129
-- mShortened = iterativelyShorten m (gamma++[negation phi]) -- !!!! PUT THIS BACK FOR SMALLER MODELS
130
-}
131
132
modelBuildARC :: StopList
133
-> ProofChunk
134
-> Model
135
modelBuildARC stopList chunk = Model
136
{ universe = uni
137
, cnDict = map
138
( \p ->
139
M p $ cnInterpretationFn p
140
) relevantCNs
141
, verbDict = map
142
( \transverb ->
143
Vb transverb
144
$ tvInterpretationFn transverb
145
) relVerbs
146
}
147
where
148
uni = [ 0 .. length stopList - 1 ]
149
sList = map get1 chunk
150
relVerbs = nub $ concatMap verbsIn sList
151
relevantCNs = nub $ concatMap cnsIn sList
152
order = map pairOfTerms sList
153
cnInterpretationFn cn =
154
[ r
155
| r <- uni
156
, (stopList !! r , CNasTerm $ PCN Pos cn) `elem` order
157
]
158
tvInterpretationFn tv =
159
[ (r,s)
160
| r <- uni
161
, s <- uni
162
, (stopList !! r, TermMaker (PV Pos tv) (TermNP All (stopList !! s))) `elem` order
163
]
164
termInterpretationFn t =
165
( t
166
, [ r
167
| r <- uni
168
, (stopList !! r , t) `elem` order
169
]
170
)
171
172
pairOfTerms :: Sent -> (Term,Term)
173
pairOfTerms (Sent _ t u) = (t,u)
174
175
-- Here is a test of the main example in Chapter 2
176
177
tSkunks, tMammals, tChordates, tSkunks2 :: Term
178
tSkunks = TermMaker sees (TermNP All (CNasTerm skunks) )
179
tMammals = TermMaker sees (TermNP All (CNasTerm mammals))
180
tChordates = TermMaker sees (TermNP All (CNasTerm chordates))
181
tSkunks2 = TermMaker sees (TermNP All tSkunks )
182
183
testCh2gamma :: [Sent]
184
testCh2gamma =
185
[ Sent All (CNasTerm skunks) (CNasTerm chordates)
186
, Sent All tMammals tSkunks2
187
, Sent All tSkunks2 (CNasTerm mammals)
188
]
189
190
testCh2phi :: Sent
191
testCh2phi = Sent All tSkunks tChordates
192
193
--followsInARCUnderRepresentations testCh2phi testCh2gamma
194
195
tCh2gamma :: [String]
196
tCh2gamma =
197
[ "all skunks chordates"
198
, "all see all mammals see all see all skunks"
199
, "all see all see all skunks mammals"
200
, "all mammals see all chordates"
201
]
202
203
tCh2phi :: String
204
tCh2phi = "all see all skunks see all chordates"
205
206
tCh2_run :: IO ()
207
tCh2_run = followsInARC tCh2gamma tCh2phi
208
209
-- followsInARC tCh2phi tCh2gamma
210
211
-- The specific mapping to Chapter 2 is
212
-- skunks --> hawks
213
---chordates --> birds
214
-- mammals -> turtles
215
216
-- 0 hawks
217
-- 1 birds
218
-- 2 see all turtles
219
-- 3 turtles
220
-- 4 see all see all hawks
221
-- 5 see all hawks
222
-- 6 see all birds
223
224
225
226
-- here there
227
--- 0 1
228
--- 1 6
229
--- 2 5
230
--- 3
231
--- 4
232
--- 5
233
--- 6
234
235
test2 :: [M]
236
test2 =
237
[ M Chordates [3,4]
238
, M Birds [1,2,5]
239
, M Skunks [2]
240
]
241
242
vTest2 :: [Vb]
243
vTest2 =
244
[ 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)],
245
Vb Loves [(1,1),(1,2), (1,3), (1,5), (2,1), (3,3), (5,2), (5,5)] ,
246
Vb Hates [(2,1),(3,4), (5,1), (5,2), (5,3), (5,4)]
247
]
248
249
model2 :: Model
250
model2 = Model
251
{ universe = [1,2,3,4,5]
252
, cnDict = test2
253
, verbDict = vTest2
254
}
255
256
--- A GOOD TEST IS BELOW
257
--- 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"]
258
259
-- 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"]
260
261
-- 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"]
262
263
264
265