| Download
Logic of "all" + verbs + relative clauses, for a class at Indiana University
Project: moss notebooks
Views: 6871module Syntax2 where1import Data.List2import Data.Maybe34class Syn a where5negation :: a -> a6subterms :: a -> [Term]7cnsIn :: a -> [CN]8verbsIn :: a -> [V]9buildTermSub :: a -> a -> Maybe [(Term,Term)]10buildPVSub :: a -> a -> Maybe [(PV,PV)]11spellOut :: a -> [(Term, Term)] -> [(PV, PV)] -> a12hasNegativeMarker :: a-> Bool13inARC :: a -> Maybe Bool1415-- I DON'T THINK WE NEED hasNegativeMarker1617xAsDefaultTerm :: Maybe Term -> Term18xAsDefaultTerm = fromMaybe (CNasTerm $ PCN Pos X)1920rAsDefaultPV :: Maybe PV -> PV21rAsDefaultPV = fromMaybe (PV Pos R)2223plusStrict :: (Eq a) => Maybe [a] -> Maybe [a] -> Maybe [a]24plusStrict a b = a >>= \x -> b >>= \y -> return $ nub $ x ++ y2526hunt :: (Eq a) => a -> Maybe [(a,a)] -> Maybe a2728hunt t Nothing = Nothing29hunt t (Just []) = Nothing30hunt t (Just ((uu,vv):rest))31| (t==uu) = (Just vv)32| otherwise = hunt t (Just rest)3334moveAhead :: (Eq a) => Maybe [(a,a)] -> Maybe [(a,a)] -> Bool35moveAhead Nothing xx = False36moveAhead xx Nothing = False37moveAhead (Just []) xx = True38moveAhead (Just ((uu,vv):rest)) xx39| value == (Just vv) = (moveAhead (Just rest) xx)40| value == Nothing = (moveAhead (Just rest) xx)41| otherwise = False42where value = hunt uu xx4344combineStructures xx yy =45if moveAhead xx yy == True then (plusStrict xx yy) else Nothing4647-- I THINK WE SHOULD DROP THESE48--polarizedCNNegation (PCN Pos cn) = (PCN Neg cn)49--polarizedCNNegation (PCN Neg cn) = (PCN Pos cn)5051-- the definition of pCNsIn is needed in Sdagleq.hs, but not elsewhere52pCNsIn (CNasTerm (PCN Pos cn)) = (PCN Pos cn)53pCNsIn (CNasTerm (PCN Neg cn)) = (PCN Neg cn)54pCNsIn (TermMaker (PV Pos v) (TermNP All n)) = pCNsIn n55pCNsIn (TermMaker (PV Pos v) (TermNP Some n)) = pCNsIn n56pCNsIn (TermMaker (PV Neg v) (TermNP All n)) = pCNsIn n57pCNsIn (TermMaker (PV Neg v) (TermNP Some n)) = pCNsIn n5859{-60instance Syn [Char] where61cnsIn x = cnsIn (readS x)62-}6364instance Syn PolCN where65negation (PCN Pos cn) = (PCN Neg cn)66negation (PCN Neg cn) = (PCN Pos cn)67subterms cn = []68cnsIn (PCN p cn) = [cn]69verbsIn (PCN p cn) = [ ]70buildTermSub (PCN p cn) (PCN p' cn') = Just []71buildPVSub (PCN p cn) (PCN p' cn') = Just []72spellOut (PCN p cn) k k' = (PCN p cn) --- THIS LINE IS QUESTIONABLE. IT'S NOT NEEDED73hasNegativeMarker (PCN Pos cn) = False74hasNegativeMarker (PCN Neg cn) = True75inARC (PCN Pos cn) = Just True76inARC (PCN Neg cn) = Just False7778instance Syn Term where79negation (CNasTerm (PCN Pos cn)) = (CNasTerm (PCN Neg cn))80negation (CNasTerm (PCN Neg cn)) = (CNasTerm (PCN Pos cn))81negation (TermMaker (PV Pos v) (TermNP All n)) = TermMaker (PV Neg v) (TermNP Some n)82negation (TermMaker (PV Pos v) (TermNP Some n)) = TermMaker (PV Neg v) (TermNP All n)83negation (TermMaker (PV Neg v) (TermNP All n)) = TermMaker (PV Pos v) (TermNP Some n)84negation (TermMaker (PV Neg v) (TermNP Some n)) = TermMaker (PV Pos v) (TermNP All n)85subterms (CNasTerm ter) = [(CNasTerm ter)]86subterms (TermMaker pv (TermNP d ter)) = (TermMaker pv (TermNP d ter)):subterms ter87cnsIn Ter1 = []88cnsIn Ter2 = []89cnsIn Ter3 = []90cnsIn Ter1bar = []91cnsIn Ter2bar = []92cnsIn Ter3bar = []93cnsIn (CNasTerm (PCN p cn)) = [cn]94cnsIn (TermMaker t n) = (cnsIn n)95hasNegativeMarker (CNasTerm (PCN Pos cn)) = False96hasNegativeMarker (CNasTerm (PCN Neg cn)) = True97hasNegativeMarker (TermMaker (PV Pos v) (TermNP All n)) = hasNegativeMarker n98hasNegativeMarker (TermMaker (PV Pos v) (TermNP Some n)) = hasNegativeMarker n99hasNegativeMarker (TermMaker (PV Neg v) (TermNP All n)) = hasNegativeMarker n100hasNegativeMarker (TermMaker (PV Neg v) (TermNP Some n)) = hasNegativeMarker n101inARC (CNasTerm (PCN Pos cn)) = Just True102inARC (CNasTerm (PCN Neg cn)) = Just False103inARC (TermMaker (PV Pos v) (TermNP All n)) = Just $ not $ hasNegativeMarker n104inARC (TermMaker (PV Pos v) (TermNP Some n)) = Just False105inARC (TermMaker (PV Neg v) (TermNP All n)) = Just False106inARC (TermMaker (PV Neg v) (TermNP Some n)) = Just False107inARC x = Nothing108verbsIn (TermMaker (PV pol ver) np) = [ver] ++ (verbsIn np)109verbsIn _ = []110buildTermSub (TermMaker x y) (CNasTerm z) = Nothing111buildTermSub (CNasTerm (PCN Pos cn)) (CNasTerm dn) = Just [(CNasTerm (PCN Pos cn), CNasTerm dn)]112buildTermSub (CNasTerm (PCN Neg cn)) (CNasTerm (PCN Pos dn)) = Just [(CNasTerm (PCN Pos cn), CNasTerm (PCN Neg dn))]113buildTermSub (CNasTerm (PCN Neg cn)) (CNasTerm (PCN Neg dn)) = Just [(CNasTerm (PCN Pos cn), CNasTerm (PCN Pos dn))]114buildTermSub (CNasTerm zz)(TermMaker xx yy) = Nothing115buildTermSub (TermMaker v np1) (TermMaker w np2) = buildTermSub np1 np2116buildTermSub Ter1 t = Just [(Ter1, t)]117buildTermSub Ter2 t = Just [(Ter2, t)]118buildTermSub Ter3 t = Just [(Ter3, t)]119buildTermSub Ter1bar t = Just [(Ter1, negation t)]120buildTermSub Ter2bar t = Just [(Ter2, negation t)]121buildTermSub Ter3bar t = Just [(Ter3, negation t)]122buildPVSub (TermMaker xx yy) (CNasTerm zz) = Nothing123buildPVSub (CNasTerm xx) ter = Just []124buildPVSub (TermMaker vv (TermNP d t)) (TermMaker ww (TermNP e u)) =125if (d == e) then Just [(vv,ww)] else Nothing126buildPVSub Ter1 t = Just []127buildPVSub Ter2 t = Just []128buildPVSub Ter3 t = Just []129buildPVSub Ter1bar t = Just []130buildPVSub Ter2bar t = Just []131buildPVSub Ter3bar t = Just []132spellOut (CNasTerm (PCN Pos cn)) list1 list2 = xAsDefaultTerm $ lookup (CNasTerm (PCN Pos cn)) list1133spellOut (CNasTerm (PCN Neg cn)) list1 list2 = negation $ xAsDefaultTerm $ lookup (CNasTerm (PCN Pos cn)) list1134spellOut (TermMaker t n) list1 list2 = (TermMaker (rAsDefaultPV $ lookup t list2) (spellOut n list1 list2))135spellOut Ter1 list1 list2 = xAsDefaultTerm $ lookup Ter1 list1136spellOut Ter2 list1 list2 = xAsDefaultTerm $ lookup Ter2 list1137spellOut Ter3 list1 list2 = xAsDefaultTerm $ lookup Ter3 list1138spellOut Ter1bar list1 list2 = negation $ spellOut Ter1 list1 list2139spellOut Ter2bar list1 list2 = negation $ spellOut Ter2 list1 list2140spellOut Ter3bar list1 list2 = negation $ spellOut Ter3 list1 list2141142instance Syn NP where143negation (TermNP All t) = TermNP Some (negation t)144negation (TermNP Some t) = TermNP All (negation t)145--negation (TermNP Atleast t) = TermNP More (negation t)146--negation (TermNP More t) = TermNP Atleast (negation t)147subterms (TermNP All t) = subterms t148cnsIn Everyone = []149cnsIn Someone = []150cnsIn (TermNP d t) = cnsIn t151verbsIn Everyone = []152verbsIn Someone = []153verbsIn (TermNP d t) = verbsIn t154buildTermSub (TermNP d t) (TermNP e u) =155if (d == e)156then (buildTermSub t u)157else Nothing158buildPVSub xx yy = Just []159spellOut (TermNP d t) list1 list2 = (TermNP d (spellOut t list1 list2))160hasNegativeMarker (TermNP All t) = hasNegativeMarker t161hasNegativeMarker (TermNP Some t) = hasNegativeMarker t162hasNegativeMarker (TermNP Atleast t) = hasNegativeMarker t163hasNegativeMarker (TermNP More t) = hasNegativeMarker t164inARC (TermNP All t) = inARC t165inARC (TermNP Some t) = Just False166inARC (TermNP Atleast t) = Just False167inARC (TermNP More t) = Just False168inARC x = Nothing169170instance Syn Sent where171negation (Sent All t u) = (Sent Some t (negation u))172negation (Sent Some t u) = (Sent All t (negation u))173negation (Sent Atleast t u) = (Sent More u t)174negation (Sent More t u) = (Sent Atleast u t)175subterms (Sent d t u) = (subterms t) ++ (subterms u) -- I dropped at the front [t,u] ++176subterms (Sent2 d t u t2 u2) = (subterms t) ++ (subterms u) ++ (subterms t2) ++ (subterms u2) -- I dropped [t,u,t2,u2] ++177cnsIn (Sent d t u) = (cnsIn t) ++ (cnsIn u)178cnsIn (Sent2 d t u t2 u2) = (cnsIn t) ++ (cnsIn u) ++ (cnsIn t2) ++ (cnsIn u2)179verbsIn (Sent d t u) = (verbsIn t) ++ (verbsIn u)180verbsIn (Sent2 d t u t2 u2) = (verbsIn t) ++ (verbsIn u) ++(verbsIn t2) ++ (verbsIn u2)181buildTermSub (Sent d tt uu) (Sent e vv ww) =182if (d == e) then (combineStructures (buildTermSub tt vv) (buildTermSub uu ww)) else Nothing183buildTermSub (Sent2 d tt uu tt2 uu2) (Sent2 e vv ww vv2 ww2) =184if (d == e)185then foldl1 combineStructures [(buildTermSub tt vv), (buildTermSub uu ww),(buildTermSub tt2 vv2), (buildTermSub uu2 ww2)]186else Nothing187buildPVSub (Sent d t uu) (Sent e vv ww) =188if (d == e) then (plusStrict (buildPVSub t vv) (buildPVSub uu ww)) else Nothing189buildPVSub (Sent2 d tt uu tt2 uu2) (Sent2 e vv ww vv2 ww2) =190if (d == e)191then foldl1 plusStrict [(buildPVSub tt vv), (buildPVSub uu ww),(buildPVSub tt2 vv2), (buildPVSub uu2 ww2)]192else Nothing193spellOut (Sent d t u) list1 list2 = (Sent d (spellOut t list1 list2) (spellOut u list1 list2))194spellOut (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))195hasNegativeMarker (Sent All t u) = or [hasNegativeMarker t, hasNegativeMarker u ]196hasNegativeMarker (Sent Some t u) = or [hasNegativeMarker t , hasNegativeMarker u ]197hasNegativeMarker (Sent Atleast t u) = or [hasNegativeMarker t, hasNegativeMarker u ]198hasNegativeMarker (Sent More t u) = or [hasNegativeMarker t, hasNegativeMarker u ]199inARC (Sent All t u) =200case (inARC t) of201Just True -> (inARC u)202Just False -> case (inARC u) of203Nothing -> Nothing204_ -> Just False205Nothing -> Nothing206inARC (Sent Some t u) = Just False207inARC (Sent Atleast t u) = Just False208inARC (Sent More t u) = Just False209inARC x = Nothing210data Sent = None | Sent Det Term Term | Sent2 Det Term Term Term Term211deriving (Ord,Eq)212213instance Show Sent where214-- show (Sent d t u) = show (d) ++ " " ++ show(t) ++ " " ++ show(u)215show (Sent d (TermMaker x y) (TermMaker z w) ) = show (d) ++ " who " ++ show((TermMaker x y)) ++ " also " ++ show((TermMaker z w))216show (Sent d (TermMaker x y) (CNasTerm pcn) ) = show (d) ++ " who " ++ show((TermMaker x y)) ++ " are " ++ show((CNasTerm pcn))217show (Sent d (CNasTerm pcn) (TermMaker x y)) = show (d) ++ " " ++ show((CNasTerm pcn)) ++ " " ++ show((TermMaker x y))218show (Sent d (CNasTerm pcn) (CNasTerm pcn2)) = show (d) ++ " " ++ show((CNasTerm pcn)) ++ " are " ++ show((CNasTerm pcn2))219show (Sent2 d x y u v) = show (d) ++ " " ++ show(x) ++ " which are " ++ show(y) ++ " are also " ++ show(u) ++ " which are " ++ show(v)220221principalDet (Sent d t t') = d222converse s@(Sent All t u) = (Sent All u t)223224data NP = Everyone | Someone | TermNP Det Term | IntTermNP Det IntersectionTerm225deriving (Ord,Eq)226227instance Show NP where228show (TermNP d t) = show(d) ++ " " ++ show(t)229show (IntTermNP d t) = show(d) ++ " " ++ show(t)230231data Det = All | Some | No | Most | Atleast | More | Contradiction232deriving (Ord,Eq)233234instance Show Det where235show All = "all"236show Some = "some"237show No = "no"238show Most = "most"239show Atleast = "at least"240show More = "more"241show Contradiction = "contradiction"242243244245data CN = Girls246| Boys247| Dogs | Cats | Skunks | Sneetches | Mammals | Chordates | Animals | Birds | X | Y | Z | W | P | Q | N --Var CNvariable248deriving (Eq,Ord)249250instance Show CN where251show Girls = "girls"252show Boys = "boys"253show Dogs = "dogs"254show Cats = "cats"255show Skunks = "skunks"256show Sneetches = "sneetches"257show Mammals = "mammals"258show Chordates = "chordates"259show Animals = "animals"260show Birds = "birds"261show X = "x"262show Y = "y"263show Z = "z"264show W = "w"265show P = "p"266show Q = "q"267show N = "n"268269data Polarity = Pos | Neg270deriving (Ord, Eq)271272instance Show Polarity where273show Pos = ""274show Neg = "non-"275276data PolCN = PCN Polarity CN277deriving (Eq,Ord)278instance Show PolCN where279show (PCN a b) = (show a)++(show b)280281cnvariables = [X, Y, Z, W, P, Q, N]282283data Term = CNasTerm PolCN284| TermMaker PV NP285| Ter1 | Ter2 | Ter3 | Ter1bar | Ter2bar | Ter3bar286deriving (Ord,Eq)287instance Show Term where288show (CNasTerm px) = show px289show (TermMaker t (TermNP d (TermMaker pv np))) = show(t) ++ " " ++ show(d) ++ " who " ++ show(TermMaker pv np)290-- show (TermMaker t (TermNP d (TermMaker pv np))) = show(t) ++ " " ++ show(d) ++ " who " ++ show(pv) ++ " " ++ show(np)291show (TermMaker t (TermNP d (CNasTerm px))) = show(t) ++ " " ++ show(d) ++ " " ++ show(px)292--show (TermMaker t n) = "(" ++ show(t) ++ " " ++ show(n)++")" -- this was it!!!293show Ter1 = "Ter1"294show Ter2 = "Ter2"295show Ter3 = "Ter3"296show Ter1bar = "Ter1-bar"297show Ter2bar = "Ter2-bar"298show Ter3bar = "Ter3-bar"299300data PV = PV Polarity V301deriving (Eq,Ord)302instance Show PV where303show (PV a b) = (show a)++(show b)304305data V = Loves | Admires | Helps | Sees | Hates | R | S306deriving (Eq,Ord)307308instance Show V where309show Loves = "love"310show Admires = "admire"311show Helps = "help"312show Sees = "see"313show Hates = "hate"314show R = "r"315show S = "s"316317r = PV Pos R318x = PCN Pos X319y = PCN Pos Y320z = PCN Pos Z321p = PCN Pos P322q = PCN Pos Q323n = PCN Pos N324w = PCN Pos W325non_x = PCN Neg X326non_y = PCN Neg Y327non_z = PCN Neg Z328non_w = PCN Neg W329non_p = PCN Neg P330non_q = PCN Neg Q331non_n = PCN Neg N332skunks = PCN Pos Skunks333mammals = PCN Pos Mammals334animals = PCN Pos Animals335sneetches = PCN Pos Sneetches336dogs = PCN Pos Dogs337birds = PCN Pos Birds338chordates = PCN Pos Chordates339boys = PCN Pos Boys340girls = PCN Pos Girls341cats = PCN Pos Cats342non_skunks = PCN Neg Skunks343non_mammals = PCN Neg Mammals344non_animals = PCN Neg Animals345non_sneetches = PCN Neg Sneetches346non_dogs = PCN Neg Dogs347non_birds = PCN Neg Birds348non_chordates = PCN Neg Chordates349non_boys = PCN Neg Boys350non_girls = PCN Neg Girls351non_cats = PCN Neg Cats352loves = PV Pos Loves353admires = PV Pos Admires354helps = PV Pos Helps355sees = PV Pos Sees356hates = PV Pos Hates357not_loves = PV Neg Loves358not_admires = PV Neg Admires359not_helps = PV Neg Helps360not_sees = PV Neg Sees361not_hates = PV Neg Hates362363364data IntersectionTerm = IntersectionTerm PolCN Term365| IntTer1 | IntTer2 | IntTer3 | IntTer1bar | IntTer2bar | IntTer3bar366deriving (Ord,Eq)367instance Show IntersectionTerm where368show (IntersectionTerm p t) = show(p) ++ " who " ++ show(t)369show IntTer1 = "IntTer1"370show IntTer2 = "IntTer2"371show IntTer3 = "IntTer3"372show IntTer1bar = "IntTer1-bar"373show IntTer2bar = "IntTer2-bar"374show IntTer3bar = "IntTer3-bar"375376377378