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: 7246{-# LANGUAGE PatternGuards #-}12module AllVerbsRelativeClausesPrime where34import Data.List5import Control.Monad6import Syntax27import FrontEnd8import SyllogisticInference9import ExampleSentences10import ExampleRules11import ProofTreeNumbers12import Models1314type Proof = [(Int,Sent,RuleName,[Int])]15type ProofChunk = [(Sent,RuleName,[Sent])]16type StopList = [Term]1718oneStepARC :: StopList19-> ProofChunk20-> ProofChunk21oneStepARC stopList gammaWithReasons =22fixDuplicates $ gammaWithReasons ++23filter (checkARC stopList . get1)24( concatMap25(applyARule $ dropReasons gammaWithReasons)26[ downARC27, barbaraARC28, axiom29]30)3132checkARC :: StopList33-> Sent34-> Bool35checkARC stopList (Sent _ t _) = t `elem` stopList3637followsInARC :: [String] -> String -> IO ()38followsInARC gamma phi =39either40-- either (\x -> Left ( modelBuildARC stopList x , stopList , map get1 x )) Right41( \x -> do42putStrLn $ unlines43[ "The given sentence is not provable from the assumptions in ARC."44, ""45, "Here is a counter-model:"46, ""47]48showCounterModel gamma' phi' x49putStrLn $ unlines50[ ""51, "And here is the list of sentences which do follow, and with restrictions:"52, ""53]54mapM_ (print . get1) x55putStrLn ""56)57( \p -> do58putStrLn $ unlines59[ "The sentence follows from the assumptions in ARC."60, ""61, "Here is a derivation:"62, ""63]64mapM_ print p65putStrLn ""66)67$ followsInARCUnderRepresentations gamma' phi'68where69gamma' = readSs gamma70phi' = readS phi71stopList = mkStopList gamma' phi'7273showCounterModel :: [Sent] -> Sent -> ProofChunk -> IO ()74showCounterModel gamma phi x =75showModelNounsVerbsPlusJustificationsARC76(modelBuildARC stopList x)77gamma78phi79stopList80where81stopList = mkStopList gamma phi8283followsInARCUnderRepresentations :: [Sent] -> Sent -> Either ProofChunk Proof84followsInARCUnderRepresentations gamma phi =85findFixedPointOrSat (oneStepARC stopList)86( \x -> if phi `elem` map get1 x then Just $ numberProof phi x else Nothing87)88$ addReasonsToOriginal $ gamma89where90stopList = mkStopList gamma phi9192numberProof :: Sent -> ProofChunk -> Proof93numberProof phi = modify . full_reverse . near . tree_reverse . proofSearch phi9495justCounterModelARC :: [Sent] -> Sent -> Model96justCounterModelARC gamma phi =97modelBuildARC stopList98$ findFixedPoint (oneStepARC stopList)99$ addReasonsToOriginal gamma100where101stopList = mkStopList gamma phi102103findFixedPointOrSat :: Eq a => (a -> a) -> (a -> Maybe b) -> a -> Either a b104findFixedPointOrSat f p = loop105where106loop x107| Just y <- p x = Right y108| x == f x = Left x109| otherwise = loop $ f x110111findFixedPoint :: Eq a => (a -> a) -> a -> a112findFixedPoint f = either id id . findFixedPointOrSat f (\_ -> Nothing)113114mkStopList :: [Sent] -> Sent -> StopList115mkStopList gamma phi = nub $ concatMap subterms $ gamma ++ [phi]116117{-118countermod :: [Sent]119-> Sent120-> StopList121-> ProofChunk122-> IO ()123countermod gamma phi stpList chunk =124showModelNounsVerbsPlusJustificationsARC m gamma phi stpList -- !!!! PUT THIS BACK FOR the FULL MODELS125-- showModelNounsVerbsPlusJustificationsARC mShortened gamma phi stpList -- !!! PUT THIS BACK FOR SMALLER MODELS126where127m = modelBuildARC stpList chunk -- !!!! USE THIS AND THIS ONLY FOR the FULL MODELS128-- mShortened = iterativelyShorten m (gamma++[negation phi]) -- !!!! PUT THIS BACK FOR SMALLER MODELS129-}130131modelBuildARC :: StopList132-> ProofChunk133-> Model134modelBuildARC stopList chunk = Model135{ universe = uni136, cnDict = map137( \p ->138M p $ cnInterpretationFn p139) relevantCNs140, verbDict = map141( \transverb ->142Vb transverb143$ tvInterpretationFn transverb144) relVerbs145}146where147uni = [ 0 .. length stopList - 1 ]148sList = map get1 chunk149relVerbs = nub $ concatMap verbsIn sList150relevantCNs = nub $ concatMap cnsIn sList151order = map pairOfTerms sList152cnInterpretationFn cn =153[ r154| r <- uni155, (stopList !! r , CNasTerm $ PCN Pos cn) `elem` order156]157tvInterpretationFn tv =158[ (r,s)159| r <- uni160, s <- uni161, (stopList !! r, TermMaker (PV Pos tv) (TermNP All (stopList !! s))) `elem` order162]163termInterpretationFn t =164( t165, [ r166| r <- uni167, (stopList !! r , t) `elem` order168]169)170171pairOfTerms :: Sent -> (Term,Term)172pairOfTerms (Sent _ t u) = (t,u)173174-- Here is a test of the main example in Chapter 2175176tSkunks, tMammals, tChordates, tSkunks2 :: Term177tSkunks = TermMaker sees (TermNP All (CNasTerm skunks) )178tMammals = TermMaker sees (TermNP All (CNasTerm mammals))179tChordates = TermMaker sees (TermNP All (CNasTerm chordates))180tSkunks2 = TermMaker sees (TermNP All tSkunks )181182testCh2gamma :: [Sent]183testCh2gamma =184[ Sent All (CNasTerm skunks) (CNasTerm chordates)185, Sent All tMammals tSkunks2186, Sent All tSkunks2 (CNasTerm mammals)187]188189testCh2phi :: Sent190testCh2phi = Sent All tSkunks tChordates191192--followsInARCUnderRepresentations testCh2phi testCh2gamma193194tCh2gamma :: [String]195tCh2gamma =196[ "all skunks chordates"197, "all see all mammals see all see all skunks"198, "all see all see all skunks mammals"199, "all mammals see all chordates"200]201202tCh2phi :: String203tCh2phi = "all see all skunks see all chordates"204205tCh2_run :: IO ()206tCh2_run = followsInARC tCh2gamma tCh2phi207208-- followsInARC tCh2phi tCh2gamma209210-- The specific mapping to Chapter 2 is211-- skunks --> hawks212---chordates --> birds213-- mammals -> turtles214215-- 0 hawks216-- 1 birds217-- 2 see all turtles218-- 3 turtles219-- 4 see all see all hawks220-- 5 see all hawks221-- 6 see all birds222223224225-- here there226--- 0 1227--- 1 6228--- 2 5229--- 3230--- 4231--- 5232--- 6233234test2 :: [M]235test2 =236[ M Chordates [3,4]237, M Birds [1,2,5]238, M Skunks [2]239]240241vTest2 :: [Vb]242vTest2 =243[ 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)],244Vb Loves [(1,1),(1,2), (1,3), (1,5), (2,1), (3,3), (5,2), (5,5)] ,245Vb Hates [(2,1),(3,4), (5,1), (5,2), (5,3), (5,4)]246]247248model2 :: Model249model2 = Model250{ universe = [1,2,3,4,5]251, cnDict = test2252, verbDict = vTest2253}254255--- A GOOD TEST IS BELOW256--- 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"]257258-- 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"]259260-- 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"]261262263264265