Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
| Download
Logic of "all" + verbs + relative clauses, for a class at Indiana University
Project: moss notebooks
Views: 7247{-# LANGUAGE PatternGuards #-}12module AllVerbsRelativeClauses where345import Data.List6import Control.Monad7import ARC/Syntax28import ARC/FrontEnd9import ARC/SyllogisticInference10import ARC/ExampleSentences11import ARC/ExampleRules12import ARC/ProofTreeNumbers13import ARC/Models1415type Proof = [(Int,Sent,RuleName,[Int])]16type ProofChunk = [(Sent,RuleName,[Sent])]17type StopList = [Term]1819termEval x = semanticsTerm (head $ tF $ words x)2021-- allVerbsIn gamma = nub $ concatMap verbsIn (readSs gamma)2223-- tryOneStep z = applyAllRules z [antiARC, barbaraARC, axiom]2425syntaxCheck :: String -> IO()26syntaxCheck x =27case (inARC (readS x)) of28(Just True) -> putStrLn $ "Looks good!"29(Just False) -> putStrLn $ "Sorry, not in the fragment of this notebook"30Nothing -> putStrLn $ "Sorry, invalid input"3132evaluate :: Model -> String -> Bool33evaluate m s = semanticsSent (readS s) m34{--3536allTerms gamma n = nub $ concatMap subterms $ map get1 $ (infiniteSeq gamma (head gamma )) !! n373839infinitelyManyModels gamma n =40showSentenceTruthValues m (readSs gamma)41where42r = infiniteSeq gamma (head gamma)43a = r!! 244t = allTerms gamma n45m = modelBuildARC t a4647--}4849oneStepARC :: StopList50-> ProofChunk51-> ProofChunk52oneStepARC stopList gammaWithReasons =53fixDuplicates $ gammaWithReasons ++54filter (checkARC stopList . get1)55( concatMap56(applyARule $ dropReasons gammaWithReasons)57[ antiARC58, barbaraARC59, axiom60]61)626364656667686970checkARC :: StopList71-> Sent72-> Bool73checkARC stopList (Sent _ t _) = t `elem` stopList7475{--76oldFollowsInARC :: [String] -> String -> IO ()77oldFollowsInARC gamma phi =78either79-- either (\x -> Left ( modelBuildARC stopList x , stopList , map get1 x )) Right80( \x -> do81putStrLn $ unlines82[ "The given sentence is not provable from the assumptions in ARC."83, ""84, "Here is a counter-model:"85, ""8687]88showCounterModel gamma' phi' x89putStrLn $ unlines90[ ""91, "The model is built using the sentences below, all of which do follow from the assumptions:"92, ""93]94mapM_ (print . get1) x95putStrLn ""96)97( \p -> do98putStrLn $ unlines99[ "The conclusion follows from the assumptions in ARC."100, ""101, "Here is a derivation:"102]103putStrLn $ showTableForProofs [ ColDesc center " " left (show . line)104, ColDesc center " " left (show. sentence)105, ColDesc center " " left rule106, ColDesc center " " right (intercalate ", " . map show . supports)107] $ map (\k -> ProofLine {line= get1Of4 k, sentence=get2Of4 k, rule = get3Of4 k, supports = get4Of4 k}) p108putStrLn $ unlines ["", "",""]109)110$ followsInARCUnderRepresentations gamma' phi'111where112gamma' = readSs gamma113phi' = readS phi114stopList = mkStopList gamma' phi'115116-- last few lines above117--p -- NOTE THE LINE BELOW!118mapM_ print p119putStrLn ""120)121$ followsInARCUnderRepresentations gamma' phi'122where123gamma' = readSs gamma124phi' = readS phi125stopList = mkStopList gamma' phi'126--}127128129showCounterModel :: [Sent] -> Sent -> ProofChunk -> IO ()130showCounterModel gamma phi x =131showModelNounsVerbsPlusJustificationsARC132(modelBuildARC stopList x)133gamma134phi135stopList136where137stopList = mkStopList gamma phi138139--trivialExtension :: [Sent] -> Sent -> [Sent]140--trivialExtension gamma phi = gamma ++ (dropReasons $ applyARule [phi] axiom)141142143144145mkProofChunk gamma = addReasonsToOriginal $ readSs gamma146mkStopLforInfinite gamma = mkStopL $ readSs gamma147148followsInARCUnderRepresentations :: [Sent] -> Sent -> Either ProofChunk Proof149followsInARCUnderRepresentations gamma phi =150--findFixedPointOrSat (oneStepARC2 stopList (concatMap cnsIn $ readSs $ gamma ++ [phi] ) (concatMap verbsIn $ readSs $ gamma ++ [phi]))151findFixedPointOrSat (oneStepARC stopList)152( \x -> if phi `elem` map get1 x then Just $ numberProof phi x else Nothing153)154$ (addReasonsToOriginal $ gamma) ++ (applyARule [phi] axiom) --- this did not have the applyARule part!155where156stopList = mkStopList gamma phi157158159--- revised stuff below160161oneStepARC2 stopList gammaWithReasons x y =162fixDuplicates $ gammaWithReasons ++163filter (checkARC stopList . get1) (applyAllRules2 gammaWithReasons [antiARC,barbaraARC,axiom] x y)164165166f gamma phi =167findFixedPointOrSat168(\ w -> oneStepARC2 stopList w b c)169( \x -> if p `elem` map get1 x then Just $ numberProof p x else Nothing)170y171where172a = readSs $ gamma ++ [phi]173b = concatMap cnsIn a174c = concatMap verbsIn a175y = mkProofChunk gamma176p = readS phi177stopList = mkStopL a178179180181followsInARC gamma phi =182either183-- either (\x -> Left ( modelBuildARC stopList x , stopList , map get1 x )) Right184( \x -> do185putStrLn $ unlines186[ "The given sentence is not provable from the assumptions in ARC."187, ""188, "Here is a counter-model:"189, ""190]191showCounterModel gamma' phi' x192putStrLn $ unlines193[ ""194, "The model is built using the sentences below, all of which do follow from the assumptions:"195, ""196]197mapM_ (print . get1) x198putStrLn ""199)200( \p -> do201putStrLn $ unlines202[ "The conclusion follows from the assumptions in ARC."203, ""204, "Here is a derivation:"205]206putStrLn $ showTableForProofs [ ColDesc center " " left (show . line)207, ColDesc center " " left (show. sentence)208, ColDesc center " " left rule209, ColDesc center " " right (intercalate ", " . map show . supports)210] $ map (\k -> ProofLine {line= get1Of4 k, sentence=get2Of4 k, rule = get3Of4 k, supports = get4Of4 k}) p211putStrLn $ unlines ["", "",""]212)213$ (f gamma phi)214where215gamma' = readSs gamma216phi' = readS phi217stopList = mkStopList gamma' phi'218219infiniteSeq gamma phi = x220where221a = readSs $ gamma ++ [phi]222b = concatMap cnsIn a223c = concatMap verbsIn a224y = mkProofChunk gamma225stopList = mkStopL a226x = y : map (\ w -> oneStepARC2 stopList w b c ) x227228229numberProof :: Sent -> ProofChunk -> Proof230numberProof phi = modify . full_reverse . near . tree_reverse . proofSearch phi231232justCounterModelARC :: [Sent] -> Sent -> Model233justCounterModelARC gamma phi =234modelBuildARC stopList235$ findFixedPoint (oneStepARC stopList)236$ addReasonsToOriginal gamma237where238stopList = mkStopList gamma phi239240findFixedPointOrSat :: Eq a => (a -> a) -> (a -> Maybe b) -> a -> Either a b241findFixedPointOrSat f p = loop242where243loop x244| Just y <- p x = Right y245| x == f x = Left x246| otherwise = loop $ f x247248findFixedPoint :: Eq a => (a -> a) -> a -> a249findFixedPoint f = either id id . findFixedPointOrSat f (\_ -> Nothing)250251mkStopL :: [Sent] -> StopList252mkStopL gamma =253nub $ concatMap subterms $ gamma254255mkStopList :: [Sent] -> Sent -> StopList256mkStopList gamma phi = mkStopL $ gamma ++ [phi]257258{-259countermod :: [Sent]260-> Sent261-> StopList262-> ProofChunk263-> IO ()264countermod gamma phi stpList chunk =265showModelNounsVerbsPlusJustificationsARC m gamma phi stpList -- !!!! PUT THIS BACK FOR the FULL MODELS266-- showModelNounsVerbsPlusJustificationsARC mShortened gamma phi stpList -- !!! PUT THIS BACK FOR SMALLER MODELS267where268m = modelBuildARC stpList chunk -- !!!! USE THIS AND THIS ONLY FOR the FULL MODELS269-- mShortened = iterativelyShorten m (gamma++[negation phi]) -- !!!! PUT THIS BACK FOR SMALLER MODELS270-}271272modelBuildARC :: StopList273-> ProofChunk274-> Model275modelBuildARC stopList chunk = Model276{ universe = uni277, cnDict = map278( \p ->279M p $ cnInterpretationFn p280) relevantCNs281, verbDict = map282( \transverb ->283Vb transverb284$ tvInterpretationFn transverb285) relVerbs286}287where288uni = [ 0 .. length stopList - 1 ]289sList = map get1 chunk290relVerbs = nub $ concatMap verbsIn sList291relevantCNs = nub $ concatMap cnsIn sList292order = map pairOfTerms sList293cnInterpretationFn cn =294[ r295| r <- uni296, (stopList !! r , CNasTerm $ PCN Pos cn) `elem` order297]298tvInterpretationFn tv =299[ (r,s)300| r <- uni301, s <- uni302, (stopList !! r, TermMaker (PV Pos tv) (TermNP All (stopList !! s))) `elem` order303]304termInterpretationFn t =305( t306, [ r307| r <- uni308, (stopList !! r , t) `elem` order309]310)311312pairOfTerms :: Sent -> (Term,Term)313pairOfTerms (Sent _ t u) = (t,u)314315-- Here is a test of the main example in Chapter 2316317tSkunks, tMammals, tChordates, tSkunks2 :: Term318tSkunks = TermMaker sees (TermNP All (CNasTerm skunks) )319tMammals = TermMaker sees (TermNP All (CNasTerm mammals))320tChordates = TermMaker sees (TermNP All (CNasTerm chordates))321tSkunks2 = TermMaker sees (TermNP All tSkunks )322323testCh2gamma :: [Sent]324testCh2gamma =325[ Sent All (CNasTerm skunks) (CNasTerm chordates)326, Sent All tMammals tSkunks2327, Sent All tSkunks2 (CNasTerm mammals)328]329330testCh2phi :: Sent331testCh2phi = Sent All tSkunks tChordates332333--followsInARCUnderRepresentations testCh2phi testCh2gamma334335tCh2gamma :: [String]336tCh2gamma =337[ "all skunks chordates"338, "all see all mammals see all see all skunks"339, "all see all see all skunks mammals"340, "all mammals see all chordates"341]342343tCh2phi :: String344tCh2phi = "all see all skunks see all chordates"345346tCh2_run :: IO ()347tCh2_run = followsInARC tCh2gamma tCh2phi348349-- followsInARC tCh2phi tCh2gamma350351-- The specific mapping to Chapter 2 is352-- skunks --> hawks353---chordates --> birds354-- mammals -> turtles355356-- 0 hawks357-- 1 birds358-- 2 see all turtles359-- 3 turtles360-- 4 see all see all hawks361-- 5 see all hawks362-- 6 see all birds363364365366-- here there367--- 0 1368--- 1 6369--- 2 5370--- 3371--- 4372--- 5373--- 6374375test2 :: [M]376test2 =377[ M Chordates [3,4]378, M Birds [1,2,5]379, M Skunks [2]380]381382vTest2 :: [Vb]383vTest2 =384[ 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)],385Vb Loves [(1,1),(1,2), (1,3), (1,5), (2,1), (3,3), (5,2), (5,5)] ,386Vb Hates [(2,1),(3,4), (5,1), (5,2), (5,3), (5,4)]387]388389model2 :: Model390model2 = Model391{ universe = [1,2,3,4,5]392, cnDict = test2393, verbDict = vTest2394}395396397398--- A GOOD TEST IS BELOW399--- 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"]400401-- 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"]402403-- 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"]404405406--}407408409