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: 7216module Models where1import ARC/ExampleRules2import Data.List3import ARC/Syntax24import ARC/FrontEnd56dropList :: [Int] -> Int -> [Int]7dropList list x = [i | i <- list, i /= x]8dropM :: M -> Int -> M9dropM m x = M { noun = (noun m), items = (dropList (items m) x) }10dropVb :: Vb -> Int -> Vb11dropVb v x = Vb { verb = (verb v), verb_items = [(i,j) | (i,j) <- verb_items v, i/= x, j/= x]}12dropModel :: Model -> Int -> Model13dropModel m x = Model { universe = (dropList (universe m) x), cnDict = map (\ n -> (dropM n x)) (cnDict m) , verbDict = map (\ v -> (dropVb v x)) (verbDict m) }1415consecutiveDropList :: [Int] -> Int -> [Int]16helpForConsecutiveDrop :: Int -> Int -> Int17helpForConsecutiveDrop k l = if k < l then k else k -118consecutiveDropList list x = [helpForConsecutiveDrop i x | i <- list, i /= x]19consecutiveDropM m x = M { noun = (noun m), items = (consecutiveDropList (items m) x) }20consecutiveDropVb :: Vb -> Int -> Vb21consecutiveDropVb v x = Vb { verb = (verb v), verb_items = [(helpForConsecutiveDrop i x, helpForConsecutiveDrop j x) | (i,j) <- verb_items v, i/= x, j/= x]}22consecutiveDropModel :: Model -> Int -> Model23consecutiveDropModel m x = Model { universe = (consecutiveDropList (universe m) x), cnDict = map (\ n -> (consecutiveDropM n x)) (cnDict m) , verbDict = map (\ v -> (consecutiveDropVb v x)) (verbDict m) }2425maybeDrop model x gamma =26let27mNew = consecutiveDropModel model x --- was dropModel, not consecutiveDropModel28tvs = map (\ s -> (semanticsSent s mNew)) gamma29in30if (and tvs) then mNew else model3132shorten :: Model -> [Sent] -> Model33shorten model gamma = foldl (\ m -> (\ x -> maybeDrop m x gamma)) model (universe model)3435iterativelyShorten model gamma = if (model == shorten model gamma) then model else (iterativelyShorten (shorten model gamma) gamma)3637{--dropALot m gamma = foldl (\acc x -> listU m) (listU m)38--}39data SentencesInModel = SentencesInModel { sentFromModels :: String, truthvalueOfSent :: [Bool]}40deriving Show4142data M = M { noun :: CN, items :: [Int] }43deriving (Eq, Show)4445data Vb = Vb { verb :: V, verb_items :: [(Int,Int)] }46deriving (Eq,Show)4748type Universe = [Int]49data Model = Model {universe::Universe, cnDict :: [M], verbDict :: [Vb]}50deriving (Eq,Show)5152semanticsTerm :: Term -> Model -> [Int]53semanticsTerm (CNasTerm (PCN Pos cn)) m = helper (CNasTerm (PCN Pos cn)) (cnDict m)54semanticsTerm (CNasTerm (PCN Neg cn)) m = (universe m) \\ semanticsTerm (CNasTerm (PCN Pos cn)) m55semanticsTerm (TermMaker (PV Pos tv) (TermNP All t)) m =56let57u = universe m58tt = semanticsTerm t m59vv = verbHelper tv (verbDict m)60in61[ x | x <- u, and (map (\ y -> implies (y `elem` tt) ((x,y) `elem` vv)) u)]6263semanticsTerm (TermMaker (PV Pos tv) (TermNP Some t)) m =64let65u = universe m66tt = semanticsTerm t m67vv = verbHelper tv (verbDict m)68in69[ x | x <- u, or (map (\ y -> (y `elem` tt) && ((x,y) `elem` vv)) u)]7071semanticsTerm (TermMaker (PV Neg tv) (TermNP All t)) m =72let73u = universe m74tt = semanticsTerm t m75vv = verbHelper tv (verbDict m)76in77[ x | x <- u, and (map (\ y -> implies (y `elem` tt) (not ((x,y) `elem` vv)) ) u)]7879semanticsTerm (TermMaker (PV Neg tv) (TermNP Some t)) m =80let81u = universe m82tt = semanticsTerm t m83vv = verbHelper tv (verbDict m)84in85[ x | x <- u, or (map (\ y -> (y `elem` tt) && (not ((x,y) `elem` vv)) ) u)]8687helper (CNasTerm (PCN Pos cn)) partialList88| null partialList = []89| cn == (noun $ head $ partialList) = items $ head partialList90| otherwise = helper (CNasTerm (PCN Pos cn)) (tail partialList)9192implies b c = (not b) || c9394verbHelper v vList95| null vList = []96| v == (verb $ head $ vList) = verb_items $ head vList97| otherwise = verbHelper v (tail vList)9899semanticsSent :: Sent -> Model -> Bool100semanticsSent (Sent All t1 t2) m = and ( map (\ x -> x `elem` s2) s1)101where102s1 = semanticsTerm t1 m103s2 = semanticsTerm t2 m104105semanticsSent (Sent Some t1 t2) m = or (map (\ x -> x `elem` s2) s1)106where107s1 = semanticsTerm t1 m108s2 = semanticsTerm t2 m109110semanticsSent (Sent No t1 t2) m = and ( map (\ x -> not (x `elem` s2)) s1)111where112s1 = semanticsTerm t1 m113s2 = semanticsTerm t2 m114115semanticsSent (Sent Most t1 t2) m =116let117s1 = semanticsTerm t1 m118s2 = semanticsTerm t2 m119intersection = s1 `intersect` s2120n = length s1121k = length intersection122in123n < 2 * k124125semanticsSent (Sent Atleast t1 t2) m =126let127s1 = semanticsTerm t1 m128s2 = semanticsTerm t2 m129in130s2 <= s1131132semanticsSent (Sent More t1 t2) m =133let134s1 = semanticsTerm t1 m135s2 = semanticsTerm t2 m136in137s2 < s1138139--------- pretty printing of models below140141data U = U {name::String, itemsU :: [Int] }142deriving Show143144transMtoU t = (U (show(noun t)) (items t))145146data Vextra = Vextra { verbextra :: String, verb_items_extra :: [(Int,Int)] }147deriving Show148149transVtoVextra t = (Vextra (show(verb t)) (verb_items t))150151{-152-- a type for fill functions153type Filler = Int -> String -> String154155-- a type for describing table columns156data ColDesc t = ColDesc157{ colTitleFill :: Filler158, colTitle :: String159, colValueFill :: Filler160, colValue :: t -> String161}162-}163164-- test data165test =166[ M Cats [1,2,3],167M Dogs [4,5,6],168M Skunks [1,3,5,6],169M Chordates [ ]170]171172vTest = [Vb Sees [(1,1),(1,4),(3,4),(2,5)]]173174model1 = Model {universe = [1,2,3,4,5,6], cnDict = test, verbDict = vTest}175176{-177-- functions that fill a string (s) to a given width (n) by adding pad178-- character (c) to align left, right, or center179fillLeft c n s = s ++ replicate (n - length s) c180fillRight c n s = replicate (n - length s) c ++ s181fillCenter c n s = replicate l c ++ s ++ replicate r c182where x = n - length s183l = x `div` 2184r = x - l185186-- functions that fill with spaces187left = fillLeft ' '188right = fillRight ' '189center = fillCenter ' '190-}191192{-193--showTable :: [ColDesc t] -> [t] -> String194showTable cs ts =195let header = map colTitle cs196rows = [[colValue c t | c <- cs] | t <- ts]197widths = [maximum $ map length col | col <- transpose $ header : rows]198separator = intercalate "-+-" [replicate width '-' | width <- widths]199fillCols fill cols = intercalate " | " [fill c width col | (c, width, col) <- zip3 cs widths cols]200in201unlines $ fillCols colTitleFill header : separator : map (fillCols colValueFill) rows202-}203204showModelNounsPlusJustifications m gamma phi = do205putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")206putStrLn " "207putStrLn "The nouns are interpreted as follows:"208putStrLn " "209showNouns m210putStrLn " "211putStrLn "Here is how the assumptions and purported conclusion fare in this model:"212putStrLn " "213showSentenceTruthValues m $ gamma++[phi]214215showModelNounsVerbsPlusJustifications m gamma phi = do216putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")217putStrLn " "218putStrLn "The nouns and verbs are interpreted as follows:"219putStrLn " "220showNouns m221putStrLn " "222showVerbs m223putStrLn "Here is how the assumptions and purported conclusion fare in this model:"224putStrLn " "225showSentenceTruthValues m $ gamma++[phi]226227showModelNounsOnly m = do228putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")229putStrLn " "230putStrLn "The nouns are interpreted as follows:"231putStrLn " "232showNouns m233234showModel m = do235putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")236putStrLn " "237putStrLn "The nouns are interpreted as follows:"238putStrLn " "239showNouns m240putStrLn "The transitive verbs are interpreted as follows:"241putStrLn " "242showVerbs m243244showNouns m = putStrLn $245showTable246[ ColDesc center "Noun" left name247, ColDesc center "Interpretation" left (intercalate ", " . map show . itemsU)248]249$ map transMtoU (cnDict m)250251showVerbs m = putStrLn $252showTable253[ ColDesc center "Verb" left verbextra254, ColDesc center "Interpretation" left (intercalate ", " . map show . verb_items_extra)255]256$ map transVtoVextra (verbDict m)257258--- showGeneric below is used in showModelPlus259showGeneric m nameLabel contentLabel recordList = putStrLn $260showTable261[ ColDesc center "Term" left nameLabel262, ColDesc center "Interpretation" left (intercalate ", " . map show . contentLabel)263] recordList264265makeSentenceEntry s m = SentencesInModel (show s) [semanticsSent s m]266267showSentenceTruthValues m gamma = putStrLn $268showTable269[ ColDesc center "Sentence" left sentFromModels270, ColDesc center "Truth Value" left (intercalate ", " . map show . truthvalueOfSent )271] (map (\ x -> makeSentenceEntry x m) (gamma))272273showModelPlus mod tList = do274showModel mod275putStrLn " "276putStrLn "Extra information on the relevant terms:"277putStrLn " "278let rrrList = map (\ z -> (U { name = (show z) , itemsU = (semanticsTerm z mod) }) ) tList279showGeneric mod name itemsU rrrList280281showModelNounsVerbsPlusJustificationsARC :: Model -> [Sent] -> Sent -> [Term] -> IO ()282showModelNounsVerbsPlusJustificationsARC m gamma phi tList = do283putStrLn ("The universe is the set of numbers in " ++ show(universe m)++".")284putStrLn " "285putStrLn "The nouns and verbs are interpreted as follows:"286putStrLn " "287showNouns m288putStrLn " "289showVerbs m290putStrLn "Extra information on the relevant terms:"291putStrLn " "292let rrrList = map (\ z -> (U { name = (show z) , itemsU = (semanticsTerm z m) }) ) tList293showGeneric m name itemsU rrrList294putStrLn "Here is how the assumptions and purported conclusion fare in this model:"295putStrLn " "296showSentenceTruthValues m (gamma++[phi])297298{-299m gamma phi tList300-----------------301show (universe m)302showNouns n:303showTable304[ ColDesc center "Noun" left name305, ColDesc center "Interpretation" left (intercalate ", " . map show . itemsU)306]307$ map transMtoU (cnDict m)308showVerbs m:309showTable310[ ColDesc center "Verb" left verbextra311, ColDesc center "Interpretation" left (intercalate ", " . map show . verb_items_extra)312]313$ map transVtoVextra (verbDict m)314showGeneric m name itemsU (...):315showTable316[ ColDesc center "Term" left name317, ColDesc center "Interpretation" left (intercalate ", " . map show . itemsU)318] $ map (\ z -> U { name = show z, itemsU = semanticsTerm z m }) tList319showSentenceTruthValues m $ gamma ++ [phi]:320showTable321[ ColDesc center "Sentence" left sentFromModels322, ColDesc center "Truth Value" left (intercalate ", " . map show . truthvalueOfSent )323]324$ map (\ x -> makeSentenceEntry x m) $ gamma ++ [phi]325-}326327328329