Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Created September 20, 2020 03:15
Show Gist options
  • Save abiodun0/667f125277228fc3c84eeaaca0535fc1 to your computer and use it in GitHub Desktop.
Save abiodun0/667f125277228fc3c84eeaaca0535fc1 to your computer and use it in GitHub Desktop.
Fixed point algebras (To be Updated)
-- Algebra gives the ability to perform calculaitons with number and symbols
-- algebra treat symbols as elements of a vector space, multiplied by scalar quantity and added to each other
type Algebra f a = f a -> a
type SimpleA = Algebra ExprF Int
data ExprF a = Cons Int
| Add a a
| Mul a a
newtype Fix f = In (f (Fix f))
type Expr = Fix ExprF
val :: Expr
val = In (Cons 12)
testExpr = In $ (In $ (In $ Cons 2) `Add`
(In $ Cons 3)) `Mul` (In $ Cons 4)
instance Functor ExprF where
fmap f (Cons i) = Cons i
fmap f (Add a b) = Add (f a) (f b)
fmap f (Mul a b) = Mul (f a) (f b)
alg :: ExprF Int -> Int
alg (Cons i) = i
alg (Add a b) = a + b
alg (Mul a b) = a * b
algf :: SimpleA
algf (Cons i) = i
algf (Add a b) = a + b
algf (Mul a b) = a * b
-- Samples
-- algf $ fmap (+1) $ Add 7 5
-- alg $ Add 7 5
-- alg $ fmap (*2) $ Add 7 5
-- data Expr = Cons Int
-- | Add Expr Expr
-- | Mul Expr Expr
-- type Name = [Char]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment