-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMS.hs
1883 lines (1431 loc) · 50.2 KB
/
MS.hs
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE GADTs #-}
module MS where
import Prelude hiding (pred,Ord,Num)
import Control.Monad.State hiding (ap)
import Logic hiding (Pol)
import LogicB ()
import qualified Logic
import Data.List (intersect,nub,partition,nubBy,sortBy,find)
import Control.Monad.Logic hiding (ap)
import Control.Applicative
import Control.Applicative.Alternative
import Data.Function (on)
import Control.Arrow (first)
type Object = Exp
type Prop = Exp
--------------------------------
-- Operators
protected :: Dynamic a -> Dynamic a
protected a = do
s <- get
x <- a
put s
return x
imply :: Monad m => (t1 -> t -> b) -> m t1 -> m t -> m b
imply implication a b = do
a' <- a
b' <- b
return (implication a' b')
(==>) :: Effect -> Effect -> Effect
(==>) = imply (-->)
data Gender where
Male :: Gender
Female :: Gender
Neutral :: Gender
deriving (Eq,Show)
data Role where
Subject :: Role
Other :: Role
deriving (Eq,Show)
-- first :: (t2 -> t1) -> (t2, t) -> (t1, t)
-- first f (x,y) = (f x,y)
second :: forall t t1 t2. (t2 -> t1) -> (t, t2) -> (t, t1)
second f (x,y) = (x,f y)
data Descriptor = Descriptor {dPluralizable :: Bool
,dGender :: [Gender]
,dNum :: Num
,dRole :: Role} deriving Show
type ObjQuery = Descriptor -> Bool
type ObjEnv = [(Descriptor,NP)]
type NounEnv = [CN]
clearRole :: Env -> Env
clearRole Env{..} = Env{objEnv = map cr objEnv,..}
where cr (d,np) = (d {dRole = Other},np)
-- | After a sentence is closed, we may need to allow to refer certain objects by a plural.
-- See fracas 131.
pluralize :: Env -> Env
pluralize Env{..} = Env{objEnv = map (first pl) objEnv,..}
where pl Descriptor{..} = Descriptor{dNum = if dPluralizable then Unspecified else dNum,..}
-- FIXME this should be done only on things that are introduced inside the sentence!
withClause :: MonadState Env m => m b -> m b
withClause e = do
pl <- gets envPluralizingQuantifier
x <- e
modify clearRole -- Once the sentence is complete, accusative pronouns can refer to the direct arguments.
modify pluralize
modify $ \Env{..} -> Env{envPluralizingQuantifier = pl,..}
return x
type VPEnv = [VP]
data Env = Env {vpEnv :: VPEnv
,vp2Env :: V2
,apEnv :: AP
,cn2Env :: CN2
,objEnv :: ObjEnv
,cnEnv :: NounEnv
,sEnv :: S
,quantityEnv :: [(Var,CN')] -- map from CN' to "default" quantity.
,envDefinites :: [(Exp,Object)] -- map from CN to pure objects
,envMissing :: [(Exp,Var)] -- definites that we could not find. A map from CN to missing variables
,envSloppyFeatures :: Bool
,envPluralizingQuantifier :: Bool
,freshVars :: [String]}
-- deriving Show
------------------------------
-- Gets
overlaps :: Eq a => [a] -> [a] -> Bool
overlaps a b = case intersect a b of
[] -> False
_ -> True
isNeutral, isMale, isFemale :: Descriptor -> Bool
isMale Descriptor{..} = dGender `overlaps` [Male]
isFemale Descriptor{..} = dGender `overlaps` [Female]
isNeutral Descriptor{..} = dGender `overlaps` [Neutral]
isPerson :: Descriptor -> Bool
isPerson = const True -- FIXME
isSingular :: Descriptor -> Bool
isSingular Descriptor {..} = dNum `elem` [Singular, Cardinal 1, Unspecified]
isPlural :: Descriptor -> Bool
isPlural Descriptor {..} = dNum `elem` [Plural, Unspecified] -- FIXME: many more cases
isNotSubject :: Descriptor -> Bool
isNotSubject = (/= Subject) . dRole
isCoArgument :: Descriptor -> Bool
isCoArgument = (== Subject) . dRole
getCN :: Env -> [CN]
getCN Env{cnEnv=cn:cns} = cn:cns
getCN _ = [return assumedCN]
getCN2 :: Env -> CN2
getCN2 Env{cn2Env=cn} = cn
getNP' :: ObjQuery -> Env -> [NP]
getNP' q Env{objEnv=os,..} = case filter (q . fst) os of
[] -> [return $ MkNP [] assumedNum (ObjectQuant (constant "assumedNP")) assumedCN]
xs -> map snd xs
getNP :: ObjQuery -> Dynamic [NP]
getNP q = gets (getNP' q)
getDefinite :: CN' -> Dynamic Object
getDefinite (cn',_gender) = do
things <- gets envDefinites
let pred = lam (\x -> (noExtraObjs (cn' x)))
case find (eqExp' pred . fst) things of
Just (_,y) -> return y
Nothing -> do
y <- getFresh
modify $ \Env {..} -> Env{envDefinites=(pred,Var y):envDefinites
,envMissing=(pred,y):envMissing,..}
return (Var y)
getQuantity :: Dynamic (Nat,CN')
getQuantity = do
qs <- gets quantityEnv
case qs of
((q,cn'):_) -> return (Nat (Var q),cn')
-------------------------------
-- Pushes
pushQuantity :: Var -> CN' -> Env -> Env
pushQuantity v cn Env{..} = Env{quantityEnv=(v,cn):quantityEnv,..}
pushDefinite :: (Object -> S') -> Var -> Env -> Env
pushDefinite source target Env{..} =
Env{envDefinites = (lam (\x' -> noExtraObjs (source x')),Var target):envDefinites,..}
pushNP :: Descriptor -> NP -> Env -> Env
pushNP d o1 Env{..} = Env{objEnv = (d,o1):objEnv,..}
pushCN :: CN -> Env -> Env
pushCN cn Env{..} = Env{cnEnv=cn:cnEnv,..}
pushVP :: VP -> Env -> Env
pushVP vp Env{..} = Env{vpEnv=vp:vpEnv,..}
pushV2 :: V2 -> Env -> Env
pushV2 vp2 Env{..} = Env{vp2Env=vp2,..}
pushAP :: AP -> Env -> Env
pushAP a Env{..} = Env{apEnv=a,..}
pushCN2 :: CN2 -> Env -> Env
pushCN2 cn2 Env{..} = Env{cn2Env=cn2,..}
pushS :: S -> Env -> Env
pushS s Env{..} = Env{sEnv=s,..}
----------------------------------
-- Effects/Dynamic
allVars :: [String]
allVars = map (:[]) ['a'..'z'] ++ cycle (map (:[]) ['α'..'ω'])
quantifyMany :: [(Exp,Var)] -> Exp -> Exp
quantifyMany [] e = e
quantifyMany ((dom,x):xs) e = Forall x (dom `app` (Var x)) (quantifyMany xs e)
evalDynamic :: Dynamic Exp -> [Exp]
evalDynamic (Dynamic x) = do
(formula,Env {..}) <- observeAll (runStateT x assumed)
return (quantifyMany [(Lam (\_ -> Con "Nat"),v) | (v,_cn) <- quantityEnv] (quantifyMany envMissing formula))
newtype Dynamic a = Dynamic {fromDynamic :: StateT Env Logic a} deriving (Monad,Applicative,Functor,MonadState Env,Alternative,MonadPlus,MonadLogic)
instance Show (Dynamic a) where show _ = "<DYNAMIC>"
-- newtype Dynamic a = Dynamic {fromDynamic :: LogicT (State Env) a} deriving (Monad,Applicative,Functor,MonadState Env,Alternative,MonadPlus,MonadLogic)
type Effect = Dynamic Prop
filterKey :: Eq a => a -> [(a, b)] -> [(a, b)]
filterKey k = filter ((/= k) . fst)
appArgs :: String -> [Object] -> ExtraArgs -> Prop
appArgs nm objs@(_:_) (filterKey "compClass" -> prepositions0,adverbs) = adverbs (app (pAdverbs prep'd)) directObject
where prep'd = Con (nm ++ concatMap fst prepositions) `apps` (map snd prepositions ++ indirectObjects)
indirectObjects = init objs
directObject = last objs
cleanedPrepositions = sortBy (compare `on` fst) $ nubBy ((==) `on` fst) prepositions0
(adverbialPrepositions,prepositions) = partition ((== "before") . fst) cleanedPrepositions
pAdverbs x = foldr app x [Con (p ++ "_PREP") `app` arg | (p,arg) <- adverbialPrepositions]
appAdjArgs :: String -> [Object] -> ExtraArgs -> Prop
appAdjArgs nm [cn,obj] (prepositions0,adverbs) = adverbs (\x -> apps prep'd [cn,x]) obj
where prep'd = Con "appA" `app` (Con (nm ++ concatMap fst prepositions) `apps` ((map snd prepositions)))
prepositions = nubBy ((==) `on` fst) prepositions0
mkPred :: String -> Object -> S'
mkPred p x extraObjs = appArgs p [x] extraObjs
mkRel2 :: String -> Object -> Object -> S'
mkRel2 p x y extraObjs = appArgs p [x,y] extraObjs
mkRel3 :: String -> Object -> Object -> Object -> S'
mkRel3 p x y z extraObjs = appArgs p [x,y,z] extraObjs
constant :: String -> Exp
constant x = Con x
pureObj :: Exp -> Num -> CN' -> NP
pureObj x number cn = return $ MkNP [] number (ObjectQuant x) cn
pureVar :: Var -> Num -> CN' -> NP
pureVar x = pureObj (Var x)
getFresh :: Dynamic String
getFresh = do
(x:_) <- gets freshVars
modify (\Env{freshVars=_:freshVars,..} -> Env{..})
return x
----------------------------------
-- Assumed
assumedPred :: String -> Dynamic (Object -> S')
assumedPred predName = do
return $ \x -> (mkPred predName x)
assumedCN :: CN'
assumedCN = (mkPred "assumedCN", [Male,Female,Neutral])
assumedNum :: Num
assumedNum = Singular
assumed :: Env
assumed = Env {vp2Env = return $ \x y -> (mkRel2 "assumedV2" x y)
, vpEnv = []
-- ,apEnv = (pureIntersectiveAP (mkPred "assumedAP"))
-- ,cn2Env = pureCN2 (mkPred "assumedCN2") Neutral Singular
,objEnv = []
,sEnv = return (\_ -> constant "assumedS")
,quantityEnv = []
,cnEnv = []
,envDefinites = []
,envMissing = []
,envSloppyFeatures = False
,envPluralizingQuantifier = False
,freshVars = allVars}
onS' :: (Prop -> Prop) -> S' -> S'
onS' f p eos = f (p eos)
type ExtraArgs = ([(Var,Object)] -- prepositions
,(Object -> Prop) -> Object -> Prop) -- adverbs
type S' = ExtraArgs -> Prop
type S = Dynamic S'
type V2 = Dynamic (Object -> Object -> S') -- Object first, subject second.
type V3 = Dynamic (Object -> Object -> Object -> S')
type CN' = (Object -> S',[Gender])
type CN = Dynamic CN'
type CN2 = Dynamic CN2'
type CN2' = (Object -> Object -> S',[Gender])
type NP' = (Object -> S') -> S'
type NP = Dynamic NPData
type V = Dynamic (Object -> S')
type V2S = Dynamic (Object -> S' -> Object -> S')
type V2V = Dynamic (Object -> VP' -> Object -> S')
type VV = Dynamic (VP' -> Object -> S')
type SC = Dynamic VP'
type VS = Dynamic (S' -> VP')
type Cl = Dynamic S'
type Temp = Prop -> Prop
type ClSlash = Dynamic VP'
type RCl = Dynamic RCl'
type RCl' = Object -> Prop
type RS = Dynamic RCl'
data Num where
Unspecified :: Num
Singular :: Num
Plural :: Num
AFew :: Num
MoreThan :: Num -> Num
Cardinal :: Nat -> Num
deriving (Show,Eq)
numSg,numPl :: Num
numSg = Singular
numPl = Plural
data NPData where
MkNP :: [Predet] -> Num -> Quant -> CN' -> NPData
type N = CN
type PN = (String,[Gender],Num)
all' :: [a -> Bool] -> a -> Bool
all' fs x = all ($ x) fs
any' :: [a -> Bool] -> a -> Bool
any' fs x = any ($ x) fs
-------------------
-- "PureX"
genderedN :: String -> [Gender] -> CN
genderedN s gender =
do modify (pushCN (genderedN s gender))
return (mkPred s,gender)
genderedN2 :: String -> [Gender] -> CN2
genderedN2 s gender =
do modify (pushCN2 (genderedN2 s gender))
return (mkRel2 s,gender)
pureV2 :: (Object -> Object -> S') -> V2
pureV2 v2 = do
modify (pushV2 (pureV2 v2))
return (\x y -> (v2 x y))
pureV3 :: (Object -> Object -> Object -> S') -> V3
pureV3 v3 = do
-- modify (pushV2 (pureV2 v2)) -- no v3 yet in the env
return v3
-----------------
-- Lexemes
meta :: Exp
meta = Con "META"
lexemeV :: String -> V
lexemeV x = return $ mkPred x
lexemeVP :: String -> VP
lexemeVP "elliptic_VP" = elliptic_VP
lexemeVP x = return $ mkPred x
class IsAdj a where
fromAdj :: String -> a
instance IsAdj A' where
fromAdj a = (\cn obj -> appAdjArgs a [lam cn,obj])
newtype GradableA = GradableA String
instance IsAdj GradableA where
fromAdj = GradableA
lexemeA :: IsAdj a => String -> Dynamic a
lexemeA a = return $ fromAdj a
lexemeV3 :: String -> V3
lexemeV3 x = return $ mkRel3 x
lexemeV2 :: String -> V2
lexemeV2 x = pureV2 (mkRel2 x)
lexemeV2S :: String -> V2S
lexemeV2S v2s = return $ \x s y -> mkRel3 v2s x (noExtraObjs s) y
lexemeVS :: String -> VS
lexemeVS vs = return $ \s x -> mkRel2 vs (noExtraObjs s) x
lexemeV2V :: String -> V2V
lexemeV2V v2v = return $ \x vp y -> appArgs v2v [x,lam (\z -> noExtraObjs (vp z)),y]
pnTable :: [(String,([Gender],Num))]
pnTable = [("smith_PN" , ([Male,Female],Singular)) -- smith is female in 123 but male in 182 and following
,("john_PN" , ([Male],Singular))
,("itel_PN" , ([Neutral],Plural))
,("gfi_PN" , ([Neutral],Singular))
,("bt_PN" , ([Neutral],Plural))
,("mary_PN" , ([Female],Singular))]
lexemePN :: String -> PN
lexemePN x = case lookup x pnTable of
Just (g,n) -> (x,g,n)
Nothing -> (x,[Male,Female,Neutral],Unspecified)
type Prep = Dynamic (Object -> S' -> S')
lexemePrep :: String -> Prep
lexemePrep "by8agent_Prep" = return (modifyingPrep "by")
lexemePrep prep = return (modifyingPrep (takeWhile (/= '_') prep))
modifyingPrep :: String -> Object -> S' -> S'
modifyingPrep aname x s (preps,adv) = s (preps++[(aname,x)],adv)
type RP = ()
lexemeRP :: String -> RP
lexemeRP _ = ()
idRP :: RP
idRP = ()
implicitRP :: RP
implicitRP = ()
---------------------
-- Unimplemented categories
conditional,future,pastPerfect,past,present,presentPerfect :: Temp
past = id
present = id
presentPerfect = id
pastPerfect = id
future = id
conditional = id
------------------
-- Pol
type Pol = Prop -> Prop
pPos :: Pol
pPos = id
pNeg :: Pol
pNeg = not'
uncNeg :: Pol
uncNeg = pNeg
-----------------
-- Card
type Card = Num
adNum :: AdN -> Card -> Card
adNum = id
numNumeral :: Numeral -> Card
numNumeral = Cardinal
--------------------
-- AdN
type AdN = Card -> Card
more_than_AdN :: AdN
more_than_AdN = MoreThan
-- less_than_AdN :: AdN
-- less_than_AdN = LessThan
-----------------
-- Nums
numCard :: Card -> Num
numCard = id
type Numeral = Nat
n_two :: Nat
n_two = 2
n_10 :: Nat
n_10 = 10
n_100 :: Nat
n_100 = 100
n_13 :: Nat
n_13 = 13
n_14 :: Nat
n_14 = 14
n_15 :: Nat
n_15 = 15
n_150 :: Nat
n_150 = 150
n_2 :: Nat
n_2 = 2
n_2500 :: Nat
n_2500 = 2500
n_3000 :: Nat
n_3000 = 3000
n_4 :: Nat
n_4 = 4
n_500 :: Nat
n_500 = 500
n_5500 :: Nat
n_5500 = 5500
n_8 :: Nat
n_8 = 8
n_99 :: Nat
n_99 = 99
n_eight :: Nat
n_eight = 8
n_eleven :: Nat
n_eleven = 11
n_five :: Nat
n_five = 5
n_fortyfive :: Nat
n_fortyfive = 45
n_four :: Nat
n_four = 4
n_one :: Nat
n_one = 1
n_six :: Nat
n_six = 6
n_sixteen :: Nat
n_sixteen = 16
n_ten :: Nat
n_ten = 10
n_three :: Nat
n_three = 3
n_twenty :: Nat
n_twenty = 20
-------------------
-- N2
lexemeN2 :: String -> N2
lexemeN2 x = genderedN2 x [Male,Female,Neutral]
--------------------
-- N
lexemeN :: String -> N
lexemeN "one_N" = one_N
lexemeN x@"line_N" = genderedN x [Neutral]
lexemeN x@"department_N" = genderedN x [Neutral]
lexemeN x@"committee_N" = genderedN x [Neutral]
lexemeN x@"customer_N" = genderedN x [Male,Female]
lexemeN x@"executive_N" = genderedN x [Male,Female]
lexemeN x@"sales_department_N" = genderedN x [Neutral]
lexemeN x@"invoice_N" = genderedN x [Neutral]
lexemeN x@"meeting_N" = genderedN x [Neutral]
lexemeN x@"report_N" = genderedN x [Neutral]
lexemeN x@"laptop_computer_N" = genderedN x [Neutral]
lexemeN x@"car_N" = genderedN x [Neutral]
lexemeN x@"company_N" = genderedN x [Neutral]
lexemeN x@"proposal_N" = genderedN x [Neutral]
lexemeN x@"chairman_N" = genderedN x [Male]
lexemeN x = genderedN x [Male,Female,Neutral]
one_N :: N
one_N = elliptic_CN
--------------------
-- A
type A = Dynamic A'
type A' = (Object -> Prop) -> Object -> S'
positA :: A -> A
positA = id
--------------
-- A2
type A2' = Object -> (Object -> Prop) -> Object -> Prop
type A2 = Dynamic A2'
lexemeA2 :: String -> A2
lexemeA2 a = return $ \x cn y -> Con a `apps` [x,lam cn,y]
--------------------
-- AP
type AP = Dynamic A'
advAP :: AP -> Adv -> AP -- basically wrong syntax in the input. (Instead of AP we should have CN)
advAP ap adv = do
adv' <- adv
ap' <- ap
return (\cn x -> adv' (ap' cn x))
sentAP :: AP -> SC -> AP
sentAP ap cl = do
ap' <- ap
cl' <- cl
return $ \cn x -> ap' (\y -> noExtraObjs (cl' y) ∧ cn y) x
complA2 :: A2 -> NP -> AP
complA2 a2 np = do
a2' <- a2
np' <- interpNP np Other
return $ \cn x -> (np' (\y _extraObjs -> a2' x cn y))
adAP :: AdA -> AP -> AP
adAP ada a = do
ada' <- ada
a' <- a
return $ ada' a'
comparA :: Dynamic GradableA -> NP -> AP
comparA a np = do
GradableA a' <- a
np' <- interpNP np Other
return $ \cn' x -> np' (\ y _extraObjs -> Con "compareGradableMore" `apps` [Con a',lam cn',x,y])
comparAsAs :: Dynamic GradableA -> NP -> AP
comparAsAs a np = do
GradableA a' <- a
np' <- interpNP np Other
return $ \cn' x -> np' (\ y _extraObjs -> Con "compareGradableEqual" `apps` [Con a',lam cn',x,y])
-- FIXME: very questionable that this should even be in the syntax.
useComparA_prefix :: A -> AP
useComparA_prefix a = do
a' <- a
return $ \cn' x -> a' cn' x
--------------------
-- Subjs
type Subj = Dynamic (S' -> S' -> S')
lexemeSubj :: String -> Subj
lexemeSubj s = return $ \s1 s2 extraObjs -> Con s `apps` [s1 extraObjs, s2 extraObjs]
--------------------
-- AdA
type AdA = Dynamic (A' -> A')
lexemeAdA :: String -> AdA
lexemeAdA ada = return $ \a cn x extraObjs -> Con ada `apps` [lam $ \cn2 -> lam $ \x2 -> a (app cn2) x2 extraObjs,lam cn,x]
--------------------
-- Adv
type ADV' = S' -> S'
type ADV = Dynamic ADV'
type Adv = ADV
type AdvV = ADV
type AdV = ADV
lexemeAdv :: String -> Adv
lexemeAdv "too_Adv" = uninformativeAdv -- TODO: in coq
lexemeAdv "also_AdV" = uninformativeAdv -- TODO: in coq
lexemeAdv adv = return $ sentenceApplyAdv (appAdverb adv)
sentenceApplyAdv :: ((Object -> Prop) -> Object -> Prop) -> S' -> S'
sentenceApplyAdv adv = \s' (preps,adv') -> s' (preps,adv . adv')
appAdverb :: String -> (Object -> Prop) -> (Object -> Prop)
appAdverb adv vp obj = apps (Con "appAdv") [Con adv,lam vp, obj]
positAdvAdj :: A -> Adv
positAdvAdj a = do
a' <- a
return $ sentenceApplyAdv (\p x -> noExtraObjs (a' p x) )
uninformativeAdv :: Adv
uninformativeAdv = return $ \vp x -> vp x -- ALT: Coq/HOL
lexemeAdV :: String -> AdV
lexemeAdV = lexemeAdv
prepNP :: Prep -> NP -> AdV
prepNP prep np = do
prep' <- prep
np' <- interpNP np Other
return (\s' -> np' $ \x -> prep' x s')
subjS :: Subj -> S -> Adv
subjS subj s = do
subj' <- subj
s' <- s
return $ subj' s'
--------------------
-- CN
useN :: N -> CN
useN = id
combineGenders :: [Gender] -> [Gender] -> [Gender]
combineGenders g1 g2 = case intersect g1 g2 of
[] -> [Neutral]
i -> i
conjCN2 :: Conj -> CN -> CN -> CN
conjCN2 _c cn1 cn2 = do
(c1,g1) <- cn1
(c2,g2) <- cn2
-- NOTE: the only meaningful conjuctions to use between CNs are 'and' or 'or'.
-- However, both of them the same thing. ("Or"). See FraCas 87 -- 89.
return (\x -> apConj2 or_Conj (c1 x) (c2 x),combineGenders g1 g2)
relCN :: CN->RS->CN
relCN cn rs = do
(cn',gender) <- cn
rs' <- rs
return $ (\x eos -> cn' x eos ∧ rs' x, gender)
-- GF FIXME: Relative clauses should apply to NPs. See 013, 027, 044
advCN :: CN -> Adv -> CN
advCN cn adv = do
(cn',gender) <- cn
adv' <- adv
return (\x eos -> adv' (cn' x) eos ,gender) -- FIXME: lift cn
adjCN :: A -> CN -> CN
adjCN a cn = do
a' <- a
(cn',gendr) <- cn
modify (pushCN (adjCN a cn))
return $ (\x eos -> noExtraObjs (a' (flip cn' eos) x),gendr)
elliptic_CN :: CN
elliptic_CN = do
cns <- gets getCN
cn <- afromList cns
cn
type N2 = CN2
complN2 :: N2 -> NP -> CN
complN2 n2 np = do
(n2',gender) <- n2
np' <- interpNP np Other
return (\y -> np' $ \x -> n2' y x, gender)
sentCN :: CN -> SC -> CN
sentCN cn sc = do
(cn',gender) <- cn
sc' <- sc
return $ (\x extraObjs -> apps (Con "SentCN") [lam (flip cn' extraObjs),lam (nos sc'),x],gender)
--------------------
-- SC
embedVP :: VP -> SC
embedVP = id
embedPresPart :: VP -> SC
embedPresPart = id
embedS :: S -> SC
embedS s = do
s' <- s
return $ \_x -> s' -- this is only used with an impersonal subject. so we can ignore it safely.
------------------------
-- NP
oneToo :: NP
oneToo = do
cn' <- elliptic_CN
return $ MkNP [] Singular indefArt cn'
interpNP :: NP -> Role -> Dynamic NP'
interpNP np role = do
np' <- np
evalNPData np' role
evalNPData :: NPData -> Role -> Dynamic NP'
evalNPData (MkNP pre n q c) = evalQuant pre q n c
elliptic_NP_Sg :: NP
elliptic_NP_Sg = qPron $ all' [isSingular]
usePN :: PN -> NP
usePN (o,g,n) = pureNP False (app (Con (parens ("PN2Class " ++ o)))) (Con (parens ("PN2object " ++ o))) g n Subject -- FIXME: role
pureNP :: Bool -> (Object -> Prop) -> Object -> [Gender] -> Num -> Role -> NP
pureNP dPluralizable cn o dGender dNum dRole = do
modify (pushNP (Descriptor{..}) (pureNP dPluralizable cn o dGender dNum dRole))
return $ MkNP [] dNum (ObjectQuant o) (\x _extraObjs -> cn x,dGender)
massNP :: CN -> NP
massNP cn = do
cn' <- cn
return $ MkNP [] Singular some_Quant cn'
detCN :: Det -> CN -> NP
detCN (num,quant) cn = do
cn' <- cn
return (MkNP [] num quant cn')
usePron :: Pron -> NP
usePron = id
advNP :: NP -> Adv -> NP
advNP np adv = do
MkNP pre num1 q1 (cn1,gender1) <- np
adv' <- adv
return $ MkNP pre num1
(ObliviousQuant $ \num' (cn',gender) role -> do
p1 <- evalQuant pre q1 num' (adv' . cn',gender) role
return $ \vp -> p1 vp)
(cn1,gender1)
predetNP :: Predet -> NP -> NP
predetNP f np = do
MkNP pre n q cn <- np
return $ MkNP (f:pre) n q cn
-- FIXME: WTF?
detNP :: Det -> NP
detNP (number,quant) =
return (MkNP [] number quant (const (const TRUE) {- fixme -},[Male,Female,Neutral]))
pPartNP :: NP -> V2 -> NP -- Word of warning: in FraCas partitives always behave like intersection, which is probably not true in general
pPartNP np v2 = do
MkNP pre num q (cn,gender) <- np
v2' <- v2
subject <- getFresh
return $ MkNP pre num q ((\x eos -> (cn x eos) ∧ Exists subject true (noExtraObjs (v2' x (Var subject)))),gender)
relNPa :: NP -> RS -> NP
relNPa np rs = do
MkNP pre num q (cn,gender) <- np
rs' <- rs
return $ MkNP pre num q (\x eos -> cn x eos ∧ rs' x, gender)
conjNP2 :: Conj -> NP -> NP -> NP
conjNP2 c np1 np2 = do
MkNP pre1 num1 q1 (cn1,gender1) <- np1
MkNP pre2 num2 q2 (cn2,gender2) <- np2
modify (pushNP (Descriptor False (nub (gender1 ++ gender2)) Plural Other) (conjNP2 c np1 np2))
return $ MkNP [] (num1) {- FIXME add numbers? min? max? -}
(ObliviousQuant $ \_num _cn role -> do
p1 <- evalQuant pre1 q1 num1 (cn1,gender1) role
p2 <- evalQuant pre2 q2 num2 (cn2,gender2) role
return $ \vp -> apConj2 c (p1 vp) (p2 vp))
(\x eos -> cn1 x eos ∨ cn2 x eos, gender1) -- FIXME: problem 128, etc.
conjNP3 :: Conj -> NP -> NP -> NP -> NP
conjNP3 c np1 np2 np3 = do
MkNP pre1 num1 q1 (cn1,gender1) <- np1
MkNP pre2 num2 q2 (cn2,gender2) <- np2
MkNP pre3 num3 q3 (cn3,gender3) <- np3
return $ MkNP [] (num1) {- FIXME add numbers? min? max? -}
(ObliviousQuant $ \_num _cn role -> do
p1 <- evalQuant pre1 q1 num1 (cn1,gender1) role
p2 <- evalQuant pre2 q2 num2 (cn2,gender2) role
p3 <- evalQuant pre3 q3 num3 (cn3,gender3) role
return $ \vp -> apConj3 c (p1 vp) (p2 vp) (p3 vp))
(\x eos -> cn1 x eos ∨ cn2 x eos ∨ cn3 x eos, gender1)
----------------------
-- Pron
type Pron = NP
qPron :: ObjQuery -> Pron
qPron q = do
(isSloppy :: Bool) <- gets envSloppyFeatures
nps <- getNP (\x -> isSloppy || q x)
np <- afromList nps
protected np
sheRefl_Pron :: Pron
sheRefl_Pron = qPron $ all' [isFemale, isSingular, isCoArgument]
theyRefl_Pron :: Pron
theyRefl_Pron = qPron $ all' [isPlural, isCoArgument]
heRefl_Pron :: Pron
heRefl_Pron = qPron $ all' [isMale, isSingular, isCoArgument]
he_Pron, she_Pron :: Pron
he_Pron = qPron $ all' [isMale, isSingular]
she_Pron = qPron $ all' [isFemale, isSingular]
it_Pron :: Pron
it_Pron = qPron $ all' [isNeutral, isSingular]
itRefl_Pron :: Pron
itRefl_Pron = qPron $ all' [isNeutral, isSingular, isCoArgument]
they_Pron :: Pron
they_Pron = qPron $ all' [isPlural
, not . isCoArgument -- this form excludes "themselves"
]
someone_Pron :: Pron
someone_Pron = return $ MkNP [] Singular indefArt (mkPred "person_N",[Male,Female])
maximallySloppyPron :: Pron
maximallySloppyPron = qPron $ const True
everyone_Pron :: Pron
everyone_Pron = return $ MkNP [] Unspecified every_Quant (mkPred "person_N",[Male,Female])
no_one_Pron :: Pron
no_one_Pron = return $ MkNP [] Unspecified none_Quant (mkPred "person_N",[Male,Female])
nobody_Pron :: Pron
nobody_Pron = no_one_Pron
----------------------------
-- Ord
type Ord = A' -- FIXME
ordSuperl :: A -> Ord
ordSuperl _ = return (\x _ -> x) -- FIXME
ordNumeral :: Numeral -> Ord
ordNumeral _ = return (\x _ -> x) -- FIXME
---------------------------
-- Det
type Det = (Num,Quant)
one_or_more_Det :: Det
one_or_more_Det = (Unspecified,BoundQuant Pos 1)
another_Det :: Det
another_Det = (Cardinal 1, ObliviousQuant anotherQuant')
detQuant :: Quant -> Num -> Det
detQuant q n = (n,q)
detQuantOrd :: Quant->Num->Ord->Det
detQuantOrd q n o = (n,q) -- FIXME: do not ignore the ord
every_Det :: Det
every_Det = (Unspecified,every_Quant)
each_Det :: Det
each_Det = (Unspecified,every_Quant)
somePl_Det :: Det
somePl_Det = (Plural,ExistQuant)
someSg_Det :: Det
someSg_Det = (Singular,ExistQuant)
several_Det :: Det
several_Det = (Plural,several_Quant)
many_Det :: Det
many_Det = (Plural,ETypeQuant (Quant Many Pos))
few_Det :: Det
few_Det = (AFew,ETypeQuant (Quant Few Neg))
a_few_Det :: Det
a_few_Det = (AFew,ETypeQuant (Quant Few Pos))
a_lot_of_Det :: Det
a_lot_of_Det = (Plural,ETypeQuant (Quant Lots Pos))
both_Det :: Det
both_Det = (Cardinal 2, ObliviousQuant bothQuant')
neither_Det :: Det
neither_Det = (Cardinal 2, ObliviousQuant neitherQuant')