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: 7219-- There are "2" versions of a bunch of commands1-- Right now, those are the only ones that are needed.2-- The rest should be dropped at some point.34module SyllogisticInference where56import Data.List7import Control.Monad8import ARC/Syntax29import ARC/ProofTreeNumbers10import ARC/ExampleSentences11import ARC/ExampleRules12import ARC/FrontEnd13import Data.List (transpose, intercalate)1415get1 (a,b,c) = a16get2 (a,b,c) = b17get3 (a,b,c) = c1819get1Of4 (a,b,c,d) = a20get2Of4 (a,b,c,d) = b21get3Of4(a,b,c,d) = c22get4Of4(a,b,c,d) = d2324--firstHelp :: a -> [a] -> [[a]]25firstHelp x [] = []26firstHelp x (y:ytail) = (x,y) : firstHelp x ytail2728--secondHelp :: [a] -> [a] -> [[[a]]]29secondHelp list ys = map (\ x -> firstHelp x ys) list3031allFns list1 list2 = sequence $ (secondHelp list1 list2)3233{- not needed, I think34lookfor key ((a,b):abs)35| key == a = b36-}3738emptyAsDefault :: Maybe [a] -> [a]39emptyAsDefault mx = case mx of40Nothing -> []41Just xy -> xy42zeroAsDefault :: Maybe Int -> Int43zeroAsDefault mx = case mx of44Nothing -> 045Just xy -> xy464748findD (Sent d t1 t2) = d49g1 (Sent d t1 t2) = t150g2 (Sent d t1 t2) = t2515253addAssumptionAsReason :: Sent -> (Sent, String, [Sent])54addAssumptionAsReason s = (s, "assumption", [])55addReasonsToOriginal :: [Sent] -> [(Sent,String,[Sent])]56addReasonsToOriginal = map addAssumptionAsReason5758varsInConclusion :: Rule -> [Term]59varsInConclusion r = nub [g1 $ conclusion r, g2 $ conclusion r]6061premiseExtractPairs :: Rule -> [(Term,Term)]62premiseExtractPairs r =63let f (Sent d t1 t2) = (t1, t2)64in map f $ premises r6566varsInPremises :: Rule -> [Term]67varsInPremises r = concat [[a,b] | (a,b) <- premiseExtractPairs r]6869extras :: Rule -> [Term]70extras r = (varsInConclusion r) \\ (varsInPremises r)7172-- I am not sure if 'extras' just above is used. But the variants below are used.7374extracnsInRule r = nub $ (cnsIn (conclusion r) ) \\ (concatMap cnsIn (premises r))75extraVerbsInRule r = nub $ (verbsIn (conclusion r) ) \\ (concatMap verbsIn (premises r))7677fixDuplicates xs = nubBy conclusionsMatch xs78where conclusionsMatch ys zs =79get1 ys == get1 zs8081--------------------------- here is where the main part of the code starts8283buildPairOfSubs :: Sent -> [Sent] -> [(Sent, Maybe [(Term, Term)], Maybe [(PV, PV)])]84buildPairOfSubs sent sList = [(s, buildTermSub sent s,buildPVSub sent s) | s <- sList, Nothing /= buildTermSub sent s, Nothing /= buildPVSub sent s ]8586ruleToPairOfSubs :: Rule -> [Sent] -> [[(Sent, Maybe [(Term, Term)], Maybe [(PV, PV)])]]87ruleToPairOfSubs rule sList = map (\ x -> buildPairOfSubs x sList) (premises rule)8889applicableInstances :: [Sent] -> Rule -> [([Sent], [(Term, Term)], [(PV, PV)])]90applicableInstances sList rule =91let92jj = sequence $ (ruleToPairOfSubs rule sList)93checkerUnary x = foldl combineStructures (Just []) (map get2 x)94checkerBinary x = foldl combineStructures (Just []) (map get3 x)95in96[((map get1 x), (emptyAsDefault $ checkerUnary x), (emptyAsDefault $ checkerBinary x)) | x <- jj, Nothing /= checkerUnary x, Nothing /= checkerBinary x]979899extraReconciliationVerbs rule sList = if extraVerbsInRule rule == [] then (applicableInstances sList rule ) else100let101above = [[((PV Pos x),(PV Pos y))] | x <- (extraVerbsInRule rule), y<- verblistNotVars]102in103concatMap (\ y -> (map (\ x -> (get1 x, get2 x , (get3 x ++ y))) (applicableInstances sList rule))) above104-- the 'above' above had y <- verblistNotVars rather than w105106107eVerbs rule sList vList = if extraVerbsInRule rule == [] then (applicableInstances sList rule ) else108let109above = [[((PV Pos x),(PV Pos y))] | x <- (extraVerbsInRule rule), y<- vList ]110in111concatMap (\ y -> (map (\ x -> (get1 x, get2 x , (get3 x ++ y))) (applicableInstances sList rule))) above112113114115extraReconciliationCNs rule sList = if extracnsInRule rule == [] then (extraReconciliationVerbs rule sList) else116let117useThese = nub $ concatMap cnsIn sList118firstSet = [CNasTerm (PCN Pos cn1 ) | cn1 <- extracnsInRule rule]119secondSet = [CNasTerm (PCN Pos cn2 ) | cn2 <- useThese]120firstSetNeg = [CNasTerm (PCN Neg cn1 ) | cn1 <- extracnsInRule rule]121secondSetNeg = [CNasTerm (PCN Neg cn2 ) | cn2 <- useThese]122tv = or $ map (hasNegativeMarker . pCNsIn) $ concatMap subterms sList123extras = if tv then allFns firstSet secondSetNeg else [ ]124subs = (allFns firstSet secondSet) ++ extras125in126concatMap (\ y -> (map (\ x -> (get1 x, (get2 x ++ y) , get3 x)) (extraReconciliationVerbs rule sList ))) subs127128129extraCNs rule sList cnsToInclude verbsToInclude = if extracnsInRule rule == [] then (eVerbs rule sList verbsToInclude) else130let131useThese = cnsToInclude132firstSet = [CNasTerm (PCN Pos cn1 ) | cn1 <- extracnsInRule rule]133secondSet = [CNasTerm (PCN Pos cn2 ) | cn2 <- useThese]134firstSetNeg = [CNasTerm (PCN Neg cn1 ) | cn1 <- extracnsInRule rule]135secondSetNeg = [CNasTerm (PCN Neg cn2 ) | cn2 <- useThese]136tv = or $ map (hasNegativeMarker . pCNsIn) $ concatMap subterms sList137extras = if tv then allFns firstSet secondSetNeg else [ ]138subs = (allFns firstSet secondSet) ++ extras139in140concatMap (\ y -> (map (\ x -> (get1 x, (get2 x ++ y) , get3 x)) (extraReconciliationVerbs rule sList ))) subs141142143render rule item =144let t = conclusion rule145u = spellOut t (get2 item) (get3 item)146in (u, (rulename rule), get1 item)147148dropReasons = map get1149150applyARule sList r = nub $ map (\ x -> render r x) (extraReconciliationCNs r sList)151152153applyARule2 sList r exCNs exVerbs = nub $ map (\ x -> render r x) (extraCNs r sList exCNs exVerbs)154155156applyAllRules2 sListWithReasons rl exCNs exVerbs=157let158z = dropReasons sListWithReasons159a = map (\ x -> applyARule2 z x exCNs exVerbs) rl160b = concat a161in162fixDuplicates $ sListWithReasons ++ b163164165applyAllRules sListWithReasons rl =166let167z = dropReasons sListWithReasons168a = map (applyARule z) rl169b = concat a170in171fixDuplicates $ sListWithReasons ++ b172173174type SentRule = (Sent,String)175--data PTree = T (Sent,String) [PTree] ---- for development purposes, this declaration was moved to ProofTreeNumbers.hs176-- deriving (Show, Eq)177178179180ll phi stumpset =181if (get1 $ (stumpset !! 0)) == phi then (stumpset !! 0) else (ll phi (tail stumpset))182183proofSearch phi stumpset =184T ((get1 a), (get2 a)) (map (\ x -> (proofSearch x stumpset)) (get3 a))185where a = ll phi stumpset186187188firstRepeat (x:y:rest) = if x == y then x else firstRepeat (y:rest)189190allDerived :: [Sent] -> [Rule] -> [(Sent, RuleName, [Sent])]191allDerived noReasons rl = allDerivedUnderRepresentations noReasons rl192193--allDerived2 :: [Sent] -> [Rule] -> [(Sent, RuleName, [Sent])]194allDerived2 noReasons rl exCNs exVerbs = allDerivedUnderRepresentations2 noReasons rl exCNs exVerbs195196197allDerivedUnderRepresentations noReasons rl= firstRepeat $ fixedPoint addReasons rl198where addReasons = addReasonsToOriginal noReasons199200allDerivedUnderRepresentations2 noReasons rl exCNs exVerbs = firstRepeat $ fixedPoint2 addReasons rl exCNs exVerbs201where addReasons = addReasonsToOriginal noReasons202203fixedPoint withReasons rl = withReasons : map (\ x -> applyAllRules x rl) (fixedPoint withReasons rl)204205fixedPoint2 withReasons rl exCNs exVerbs =206withReasons : map (\ x -> applyAllRules2 x rl exCNs exVerbs) (fixedPoint2 withReasons rl exCNs exVerbs)207208209fullStory noReasonList ruleList = fullStoryUnderRepresentations (readSs noReasonList) ruleList210--- e.g. fullStory ["all skunks mammals", "some mammals non-chordates"] sdagger211212--fullStoryUnderRepresentations :: AssumptionList -> [Rule] -> IO ()213fullStoryUnderRepresentations noReasonList ruleList = mapM_ print $ map get1 $ allDerivedUnderRepresentations noReasonList ruleList214215modify outputList = map (\ x -> (get1 x, fst (get2 x), snd(get2 x), get3 x)) outputList216217-- let stumpset = allDerived [s8,s12] sList218-- let phi = get1 $ stumpset !! 13219findProofByNumber n ch = mapM_ print $ modify . full_reverse . near . tree_reverse $ proofSearch (get1 (ch !! n)) ch220221inconsistency stumpset =222let223q = map get1 stumpset224--qq = map principalDet q225in226dropWhile (\ s -> Contradiction /= principalDet s)q227228-- relevantChunk is NOT USED!229230relevantChunk phi gamma ruleList =231let232addReasons = addReasonsToOriginal gamma233bingo = fixedPoint addReasons ruleList234firstRepeatOrFind (x:y:rest)235| phi `elem` (map get1 x) = x236| (inconsistency x) /= [] = x237| x == y = x238| otherwise = firstRepeatOrFind (y:rest)239in firstRepeatOrFind bingo240241followsUnderRepresentation phi gamma ruleList =242let243addReasons = addReasonsToOriginal gamma244bingo = fixedPoint2 addReasons ruleList (cnsIn phi) (concatMap verbsIn (gamma ++ [phi]))245firstRepeatOrFind (x:y:rest)246| phi `elem` (map get1 x) = do247putStrLn " "248putStrLn "The sentence follows, and here is a derivation in the given logic from the assumptions:"249putStrLn " "250mapM_ print $ modify . full_reverse $ near $ tree_reverse $ proofSearch phi x251putStrLn " "252| (inconsistency x) /= [] = do253putStrLn " "254putStrLn "As shown below, the list of assumptions is inconsistent, so every sentence follows."255putStrLn " "256mapM_ print $ modify . full_reverse $ near $ tree_reverse $ proofSearch (head $ (inconsistency x)) x257| x == y = do258putStrLn " "259putStrLn "The given sentence is not provable from the assumptions in the logic."260putStrLn " "261| otherwise = firstRepeatOrFind (y:rest)262in firstRepeatOrFind bingo263264follows phi gamma ruleList = followsUnderRepresentation (readS phi) (readSs gamma) ruleList265266findAProof :: String -> [String] -> [Rule] -> [(Int, Sent, RuleName, [Int])]267findAProof p g ruleList =268let269phi = readS p270gamma = readSs g271addReasons = addReasonsToOriginal gamma272bingo = fixedPoint addReasons ruleList273firstRepeatOrFind (x:y:rest)274| phi `elem` (map get1 x) = modify . full_reverse $ near $ tree_reverse $ proofSearch phi x275| (inconsistency x) /= [] = []276| x == y = []277| otherwise = firstRepeatOrFind (y:rest)278in firstRepeatOrFind bingo279280281-- based on https://stackoverflow.com/questions/5929377/format-list-output-in-haskell282-- a type for records283data ProofLine = ProofLine { line :: Int284, sentence :: Sent285, rule :: RuleName286, supports :: [Int] }287deriving Show288289290-- test data291{- test =292[ ProofLine 1 (readS "all skunks mammals") "assumption" []293, ProofLine 2 (readS "all mammals chordates") "assumption" [1]294, ProofLine 3 (readS "all skunks chordates") "barbara" [1,2]295]296-}297298299displayAProof p g ruleList =300putStrLn $ showTable [ ColDesc center " " left (show . line)301, ColDesc center " " left (show. sentence)302, ColDesc center " " left rule303, ColDesc center " " right (intercalate ", " . map show . supports)304] $ getReadyToDisplay p g ruleList305306getReadyToDisplay p g ruleList = map (\k -> ProofLine {line= get1Of4 k, sentence=get2Of4 k, rule = get3Of4 k, supports = get4Of4 k}) $ findAProof p g ruleList307308309310--- :set +s for timing311312313314---- EXTRA STUFF TO TRY TO PRETTY-PRINT THE PROOFS IN 3 NICE COLUMNS315---- SO FAR, NO LUCK!316317--import Data.List (transpose, intercalate)318319320321showT cs ts =322let header = map colTitle cs323rows = [[colValue c t | c <- cs] | t <- ts]324widths = [maximum $ map length col | col <- transpose $ header : rows]325separator = intercalate "-+-" [replicate width '-' | width <- widths]326fillCols fill cols = intercalate " | " [fill c width col | (c, width, col) <- zip3 cs widths cols]327in328unlines $ fillCols colTitleFill header : separator : map (fillCols colValueFill) rows329330331332