This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data Prop = | |
| Const Bool | |
| | Var String | |
| | Neg Prop | |
| | And Prop Prop | |
| | Or Prop Prop | |
| | Equiv Prop Prop | |
| | Impl Prop Prop | |
| deriving (Eq, Show) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- given a proposition, return a list with all the variables that show up | |
| collectVars :: Prop -> [String] | |
| collectVars (Const _ ) = [] | |
| collectVars (Var s ) = [s] | |
| collectVars (Neg prop) = nub $ collectVars prop | |
| -- these four patterns are rather annoying | |
| collectVars (And p1 p2) = nub $ (collectVars p1) ++ (collectVars p2) | |
| collectVars (Or p1 p2) = nub $ (collectVars p1) ++ (collectVars p2) | |
| collectVars (Equiv p1 p2) = nub $ (collectVars p1) ++ (collectVars p2) | |
| collectVars (Impl p1 p2) = nub $ (collectVars p1) ++ (collectVars p2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Attribution = [(String, Bool)] | |
| -- given a list of variables, returns a list with all possible attributions | |
| varAttributions :: [String] -> [Attribution] | |
| --varAttributions vars = map (zipWith (,) vars) tables | |
| varAttributions vars = map (zip vars) tables | |
| where | |
| tables = allTruthTables $ length vars | |
| -- generates all 2^n truth tables for n variables |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- given a proposition and an attribution, return its truth value | |
| -- (TODO) could be nice to implement the binary operators with short-circuiting logic | |
| truthValue :: Prop -> Attribution -> Maybe Bool | |
| truthValue (Const bool) _ = Just bool | |
| truthValue (Var v ) attrs = lookup v attrs | |
| truthValue (Neg p ) attrs = not <$> (truthValue p attrs) | |
| truthValue (And p1 p2) attrs = (&&) <$> (truthValue p1 attrs) <*> (truthValue p2 attrs) | |
| truthValue (Or p1 p2) attrs = (||) <$> (truthValue p1 attrs) <*> (truthValue p2 attrs) | |
| truthValue (Equiv p1 p2) attrs = (==) <$> (truthValue p1 attrs) <*> (truthValue p2 attrs) | |
| -- maybe it is cheating, but b1 <= b2 as "less than or equal to" evaluates to the same thing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- given a proposition, evaluate it at all of its attributions | |
| extensiveEvaluation :: Prop -> [Bool] | |
| extensiveEvaluation prop = unwrap values | |
| where | |
| vars = collectVars prop | |
| attributions = varAttributions vars | |
| -- use sequence to make this [Maybe Bool] a Maybe [Bool] | |
| values = sequence $ map (truthValue prop) attributions | |
| unwrap (Just j) = j | |
| -- if "attributions" is calculated with my varAttributions then "truthValue" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- P v Q <=> (~P => Q) | |
| prop1 = Equiv | |
| (Or (Var "P") (Var "Q")) | |
| (Impl (Neg (Var "P")) (Var "Q")) | |
| -- P ^ Q <=> ~(P => ~Q) | |
| prop2 = Equiv | |
| (And (Var "P") (Var "Q")) | |
| (Neg (Impl (Var "P") (Neg (Var "Q")))) | |
| -- (P <=> Q) <=> ~((P => Q) => ~(Q => P)) | |
| prop3 = Equiv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Get the next prime number by successively adding 1 to the argument | |
| +1µẒ¬µ¿ | |
| // Can also use, | |
| ‘Ẓ¬$¿ | |
| // where ‘ increments one, | |
| // Ẓ checks for primality and ¬ negates and | |
| // the $ quick refers to the previous two links as a monad | |
| // Implement the prime counting function https://tio.run/##y0rNyan8/z/o4a5Jwf8Ptz9qWvP/f7ShjpGOsY6JjqmOmY65joWOZSwA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def choose(set, n): | |
| """Takes a list of unique elements and returns all the possible | |
| ways of choosing n elements from it, as well as the | |
| remainder of the set that was not chosen.""" | |
| if n == 0 or n > len(set): | |
| return [] | |
| # recursive base case, there is only one way to do it | |
| elif n == 1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :Namespace GameOf24 | |
| ⍝ Generalized solver for the "game of 24". | |
| Solve ← { | |
| ⍝ Dyadic function to find ways of building ⍺ with the numbers in ⍵ | |
| (reprs values) ← Combine⊂⍵ | |
| mask ← ⍺=∊values | |
| (mask/reprs) (mask/values) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add a 0 = a | |
| add a b = succ (add a (pred b)) | |
| dup a = add a a | |
| half a | |
| | even a = findMeAHalf a 0 | |
| | odd a = error "Odd numbers have no whole halfs" | |
| where findMeAHalf a b | |
| | a == 2*b = b |
OlderNewer