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: 7236module AllVerbsRelativeClausesLM where12import Data.List3import Control.Monad4import Syntax25import FrontEnd6import SyllogisticInference7import ExampleSentences8import ExampleRules9import ProofTreeNumbers10import Models111213termEval x = semanticsTerm (head $ tF $ words x)1415allVerbsIn gamma = nub $ concatMap verbsIn (readSs gamma)1617tryOneStep z = applyAllRules z [antiARC, barbaraARC, axiom]1819oneStepARC gammaWithReasons stopList =20let21z = dropReasons gammaWithReasons22a = map (applyARule z) [antiARC, barbaraARC,axiom] -- was antiARC23b = concat a24in25fixDuplicates $ gammaWithReasons ++ [ s | s <- b, checkARC stopList (get1 s)]262728filterAfterApply stopList x = [ s | s <- x, checkARC stopList s]29checkARC stopList (Sent d t u) = (t `elem` stopList)3031fixedPointARC gammaWithReasons stopList = gammaWithReasons : map (\ x -> oneStepARC x stopList )(fixedPointARC gammaWithReasons stopList)3233followsInARC phi gamma = followsInARCUnderRepresenatations (readS phi) (readSs gamma)34followsInARCUnderRepresenatations phi gamma =35let36ruleList = [axiom,barbaraARC,antiARC]37stopList = nub $ concatMap subterms (gamma ++ [phi])38withReasons = addReasonsToOriginal gamma -- this was the original line; don't lose it!39h = applyARule (gamma ++ [phi]) axiom -- new40withReason = withReasons ++ h41oneStepARC setWithReasons =42let43z = dropReasons setWithReasons44a = map (applyARule z) ruleList45b = concat a46in47fixDuplicates $ setWithReasons ++ [ s | s <- b, checkARC stopList (get1 s)]48fixedPointARC = withReason : map oneStepARC fixedPointARC --- withReason had been withReasons !! note49firstRepeatOrFind (x:y:rest)50| phi `elem` (map get1 x) = do51putStrLn " "52putStrLn "The sentence follows, and here is a derivation in ARC from the assumptions:"53putStrLn " "54mapM_ print $ modify . full_reverse $ near $ tree_reverse $ proofSearch phi x55putStrLn " "56| x == y = do57{58putStrLn "The given sentence is not provable from the assumptions in ARC.";59putStrLn " ";60-- mapM_ print $ x ; -- !!61putStrLn "Here is a counter-model:";62countermod gamma phi stopList x;63}64| otherwise = firstRepeatOrFind (y:rest)65in firstRepeatOrFind fixedPointARC666768justCounterModelARC p g =69let70phi = readS p71gamma = readSs g72ruleList = [axiom,barbaraARC,antiARC]73stopList = nub $ concatMap subterms (gamma ++ [phi])74withReasons = addReasonsToOriginal gamma75oneStepARC setWithReasons =76let77z = dropReasons setWithReasons78a = map (applyARule z) ruleList79b = concat a80in81fixDuplicates $ setWithReasons ++ [ s | s <- b, checkARC stopList (get1 s)]82fixedPointARC = withReasons : map oneStepARC fixedPointARC83firstRepeatOrFind (x:y:rest)84| x == y = modelBuildARC stopList x -- countermod gamma phi stopList x85| otherwise = firstRepeatOrFind (y:rest)86in firstRepeatOrFind fixedPointARC87888990fullStoryARC phi gamma = fullStoryARCUnderRepresenatations (readS phi) (readSs gamma)91fullStoryARCUnderRepresenatations phi gamma =92let93ruleList = [axiom,barbaraARC,antiARC]94stopList = nub $ concatMap subterms (gamma ++ [phi])95withReasons = addReasonsToOriginal gamma96oneStepARC setWithReasons =97let98z = dropReasons setWithReasons99a = map (applyARule z) ruleList100b = concat a101in102fixDuplicates $ setWithReasons ++ [ s | s <- b, checkARC stopList (get1 s)]103fixedPointARC = withReasons : map oneStepARC fixedPointARC104firstRepeatOrFind (x:y:rest)105| phi `elem` (map get1 x) = do106putStrLn " "107putStrLn "The sentence follows, and here is a derivation in ARC from the assumptions:"108putStrLn " "109mapM_ print $ modify . full_reverse $ near $ tree_reverse $ proofSearch phi x110putStrLn " "111| x == y = do112{113putStrLn "The given sentence is not provable from the assumptions in ARC.";114putStrLn " ";115putStrLn "Here is the list of sentences which do follow, and with restrictions:";116mapM_ print $ map get1 x117}118| otherwise = firstRepeatOrFind (y:rest)119in firstRepeatOrFind fixedPointARC120121122123countermod gamma phi stpList chunk =124let125m = modelBuildARC stpList chunk -- !!!! USE THIS AND THIS ONLY FOR the FULL MODELS126-- mShortened = iterativelyShorten m (gamma++[negation phi]) -- !!!! PUT THIS BACK FOR SMALLER MODELS127128in129showModelNounsVerbsPlusJustificationsARC m gamma phi stpList -- !!!! PUT THIS BACK FOR the FULL MODELS130-- showModelNounsVerbsPlusJustificationsARC mShortened gamma phi stpList -- !!! PUT THIS BACK FOR SMALLER MODELS131132modelBuildARC stpList chunk =133let134k:: Int135k = length stpList136uni = [0..k-1]137sList = map get1 chunk138relVerbs = nub $ concatMap verbsIn sList139relevantCNs = nub $ concatMap cnsIn sList140withNumbers = zip stpList uni141order = map pairOfTerms sList142cnInterpretationFn cn = [ r | r<- uni, ((stpList !! r), (CNasTerm (PCN Pos cn) )) `elem` order]143tvInterpretationFn tv = [(r,s) | r<-uni, s<-uni, ((stpList!! r), (TermMaker (PV Pos tv) (TermNP All (stpList!! s)))) `elem` order]144termInterpretationFn t = (t, [r | r<-uni, ((stpList !! r),t) `elem` order])145m = Model {universe = uni, cnDict = [ (M p (cnInterpretationFn p)) | p <- relevantCNs],146verbDict = [ (Vb transverb (tvInterpretationFn transverb)) | transverb <- relVerbs] }147in148m -- showModelPlus m stpList149150151152153154pairOfTerms (Sent d t u) = (t,u)155156syntaxCheck :: String -> IO()157syntaxCheck x =158case (inARC (readS x)) of159(Just True) -> putStrLn $ "Looks good!"160(Just False) -> putStrLn $ "Sorry, not in the fragment of this notebook"161Nothing -> putStrLn $ "Sorry, invalid input"162163164-- Here is a test of the main example in Chapter 2165166tSkunks = TermMaker sees (TermNP All (CNasTerm skunks) )167tMammals = TermMaker sees (TermNP All (CNasTerm mammals))168tChordates = TermMaker sees (TermNP All (CNasTerm chordates))169tSkunks2 = TermMaker sees (TermNP All tSkunks )170171testCh2gamma= [172(Sent All (CNasTerm skunks) (CNasTerm chordates)),173(Sent All tMammals tSkunks2),174(Sent All tSkunks2 (CNasTerm mammals))]175testCh2phi = (Sent All tSkunks tChordates)176177--followsInARCUnderRepresenatations testCh2phi testCh2gamma178179tCh2gamma = [ "all skunks chordates",180"all see all mammals see all see all skunks",181"all see all see all skunks mammals",182"all mammals see all chordates" ]183tCh2phi = "all see all skunks see all chordates"184185-- followsInARC tCh2phi tCh2gamma186187-- The specific mapping to Chapter 2 is188-- skunks --> hawks189---chordates --> birds190-- mammals -> turtles191192-- 0 hawks193-- 1 birds194-- 2 see all turtles195-- 3 turtles196-- 4 see all see all hawks197-- 5 see all hawks198-- 6 see all birds199200201202-- here there203--- 0 1204--- 1 6205--- 2 5206--- 3207--- 4208--- 5209--- 6210211test2 =212[ M Chordates [3,4],213M Birds [1,2,5],214M Skunks [2]215]216vTest2= [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)],217Vb Loves [(1,1),(1,2), (1,3), (1,5), (2,1), (3,3), (5,2), (5,5)] ,218Vb Hates [(2,1),(3,4), (5,1), (5,2), (5,3), (5,4)]219]220221222model2 = Model {universe = [1,2,3,4,5], cnDict = test2, verbDict = vTest2}223224--- A GOOD TEST IS BELOW225--- 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"]226227-- 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"]228229-- 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"]230231232conclusion1 = "all who see all who hate all skunks see all who love all mammals"233assumptions1 = ["all mammals hate all skunks",234"all skunks see all skunks",235"all who love all mammals are skunks",236"all birds see all who love all skunks",237"all who see all birds are mammals"]238239240assumptions2 = ["all mammals hate all skunks",241"all skunks see all skunks",242"all who love all mammals are skunks",243"all birds see all who love all skunks",244"all who see all birds are mammals",245"all who see all birds see all skunks",246"all who see all skunks love all skunks"]247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278