Skip to content

Instantly share code, notes, and snippets.

View rodrigogiraoserrao's full-sized avatar
🐍

Rodrigo Girão Serrão rodrigogiraoserrao

🐍
View GitHub Profile
@rodrigogiraoserrao
rodrigogiraoserrao / tautologies.hs
Created June 10, 2019 16:20
Data type for propositions in propositional logic.
data Prop =
Const Bool
| Var String
| Neg Prop
| And Prop Prop
| Or Prop Prop
| Equiv Prop Prop
| Impl Prop Prop
deriving (Eq, Show)
@rodrigogiraoserrao
rodrigogiraoserrao / tautologies.hs
Last active June 10, 2019 16:49
Take a proposition and find all the variables it contains.
-- 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)
@rodrigogiraoserrao
rodrigogiraoserrao / tautologies.hs
Last active June 10, 2019 16:32
Generate all attributions for a set of variables
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
@rodrigogiraoserrao
rodrigogiraoserrao / tautologies.hs
Created June 10, 2019 16:36
Evaluate a proposition from a given attribution
-- 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
@rodrigogiraoserrao
rodrigogiraoserrao / tautologies.hs
Created June 10, 2019 16:38
Take a proposition and verify if it is a tautology!
-- 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"
@rodrigogiraoserrao
rodrigogiraoserrao / tautologies.hs
Last active June 10, 2019 16:43
Usage example of the tautology verifier
-- 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
@rodrigogiraoserrao
rodrigogiraoserrao / snippets.jelly
Last active February 25, 2020 13:46
Set of Jelly snippets
// 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
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:
: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)
}
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