perga/lib/Expr.hs

138 lines
4.3 KiB
Haskell
Raw Normal View History

2024-10-05 13:31:09 -07:00
module Expr where
import Data.Function (on)
2024-11-14 22:02:04 -08:00
import Data.Text (Text)
import qualified Data.Text as T
2024-10-05 13:31:09 -07:00
data Expr where
2024-11-14 22:02:04 -08:00
Var :: Integer -> Text -> Expr
2024-10-06 14:02:35 -07:00
Star :: Expr
Square :: Expr
App :: Expr -> Expr -> Expr
2024-11-14 22:02:04 -08:00
Abs :: Text -> Expr -> Expr -> Expr
Pi :: Text -> Expr -> Expr -> Expr
deriving (Show)
instance Eq Expr where
(Var n _) == (Var m _) = n == m
Star == Star = True
Square == Square = True
(App e1 e2) == (App f1 f2) = e1 == f1 && e2 == f2
(Abs _ t1 b1) == (Abs _ t2 b2) = t1 == t2 && b1 == b2
(Pi _ t1 b1) == (Pi _ t2 b2) = t1 == t2 && b1 == b2
_ == _ = False
2024-10-06 14:02:35 -07:00
2024-11-11 13:43:28 -08:00
occursFree :: Integer -> Expr -> Bool
occursFree n (Var k _) = n == k
2024-11-11 13:43:28 -08:00
occursFree _ Star = False
occursFree _ Square = False
2024-11-11 13:52:50 -08:00
occursFree n (App a b) = on (||) (occursFree n) a b
occursFree n (Abs _ a b) = occursFree n a || occursFree (n + 1) b
occursFree n (Pi _ a b) = occursFree n a || occursFree (n + 1) b
2024-11-11 13:43:28 -08:00
2024-10-06 14:02:35 -07:00
{- --------------------- PRETTY PRINTING ----------------------------- -}
2024-11-14 22:02:04 -08:00
parenthesize :: Text -> Text
parenthesize s = T.concat ["(", s, ")"]
2024-11-11 14:34:55 -08:00
2024-11-14 22:02:04 -08:00
collectLambdas :: Expr -> ([(Text, Expr)], Expr)
2024-11-11 16:38:46 -08:00
collectLambdas (Abs x ty body) = ((x, ty) : params, final)
where
(params, final) = collectLambdas body
collectLambdas e = ([], e)
2024-11-14 22:02:04 -08:00
collectPis :: Expr -> ([(Text, Expr)], Expr)
2024-11-11 16:38:46 -08:00
collectPis p@(Pi "" _ _) = ([], p)
collectPis (Pi x ty body) = ((x, ty) : params, final)
where
(params, final) = collectPis body
collectPis e = ([], e)
2024-11-14 22:02:04 -08:00
groupParams :: [(Text, Expr)] -> [([Text], Expr)]
2024-11-11 16:38:46 -08:00
groupParams = foldr addParam []
where
2024-11-14 22:02:04 -08:00
addParam :: (Text, Expr) -> [([Text], Expr)] -> [([Text], Expr)]
2024-11-11 16:38:46 -08:00
addParam (x, t) [] = [([x], t)]
addParam (x, t) l@((xs, s) : rest)
| incIndices t == s = (x : xs, t) : rest
2024-11-11 16:38:46 -08:00
| otherwise = ([x], t) : l
2024-11-14 22:02:04 -08:00
showParamGroup :: ([Text], Expr) -> Text
showParamGroup (ids, ty) = parenthesize $ T.unwords ids <> " : " <> pretty ty
2024-11-11 16:38:46 -08:00
2024-11-14 22:02:04 -08:00
helper :: Integer -> Expr -> Text
2024-11-11 14:34:55 -08:00
helper _ (Var _ s) = s
helper _ Star = "*"
helper _ Square = ""
2024-11-11 16:38:46 -08:00
helper k (App e1 e2) = if k > 3 then parenthesize res else res
2024-11-11 14:34:55 -08:00
where
2024-11-14 22:02:04 -08:00
res = helper 3 e1 <> " " <> helper 4 e2
2024-11-11 16:38:46 -08:00
helper k (Pi "" t1 t2) = if k > 2 then parenthesize res else res
where
2024-11-14 22:02:04 -08:00
res = helper 3 t1 <> " -> " <> helper 2 t2
2024-11-11 16:38:46 -08:00
helper k e@(Pi{}) = if k > 2 then parenthesize res else res
2024-11-11 14:34:55 -08:00
where
2024-11-11 16:38:46 -08:00
(params, body) = collectPis e
grouped = showParamGroup <$> groupParams params
2024-11-14 22:02:04 -08:00
res = "" <> T.unwords grouped <> " . " <> pretty body
2024-11-11 16:38:46 -08:00
helper k e@(Abs{}) = if k >= 1 then parenthesize res else res
2024-11-11 14:34:55 -08:00
where
2024-11-11 16:38:46 -08:00
(params, body) = collectLambdas e
grouped = showParamGroup <$> groupParams params
2024-11-14 22:02:04 -08:00
res = "λ " <> T.unwords grouped <> " . " <> pretty body
2024-11-11 14:34:55 -08:00
2024-11-14 22:02:04 -08:00
pretty :: Expr -> Text
2024-11-11 14:34:55 -08:00
pretty = helper 0
2024-10-06 14:02:35 -07:00
2024-11-14 22:02:04 -08:00
prettyS :: Expr -> String
prettyS = T.unpack . pretty
2024-10-06 14:02:35 -07:00
{- --------------- ACTUAL MATH STUFF ---------------- -}
2024-10-05 13:31:09 -07:00
isSort :: Expr -> Bool
isSort Star = True
isSort Square = True
isSort _ = False
shiftIndices :: Integer -> Integer -> Expr -> Expr
shiftIndices d c (Var k x)
| k >= c = Var (k + d) x
| otherwise = Var k x
shiftIndices _ _ Star = Star
shiftIndices _ _ Square = Square
shiftIndices d c (App m n) = App (shiftIndices d c m) (shiftIndices d c n)
shiftIndices d c (Abs x m n) = Abs x (shiftIndices d c m) (shiftIndices d (c + 1) n)
shiftIndices d c (Pi x m n) = Pi x (shiftIndices d c m) (shiftIndices d (c + 1) n)
2024-10-05 13:31:09 -07:00
incIndices :: Expr -> Expr
incIndices = shiftIndices 1 0
-- substitute s for k *AND* decrement indices; only use after reducing a redex.
subst :: Integer -> Expr -> Expr -> Expr
subst k s (Var n x)
| k == n = s
| n > k = Var (n - 1) x
| otherwise = Var n x
subst _ _ Star = Star
subst _ _ Square = Square
subst k s (App m n) = App (subst k s m) (subst k s n)
subst k s (Abs x m n) = Abs x (subst k s m) (subst (k + 1) (incIndices s) n)
subst k s (Pi x m n) = Pi x (subst k s m) (subst (k + 1) (incIndices s) n)
2024-10-05 13:31:09 -07:00
betaReduce :: Expr -> Expr
betaReduce (Var k s) = Var k s
2024-10-05 13:31:09 -07:00
betaReduce Star = Star
betaReduce Square = Square
betaReduce (App (Abs _ _ v) n) = subst 0 n v
2024-10-05 13:31:09 -07:00
betaReduce (App m n) = App (betaReduce m) (betaReduce n)
betaReduce (Abs x t v) = Abs x (betaReduce t) (betaReduce v)
betaReduce (Pi x t v) = Pi x (betaReduce t) (betaReduce v)
2024-10-05 13:31:09 -07:00
betaNF :: Expr -> Expr
betaNF e = if e == e' then e else betaNF e'
where
e' = betaReduce e
betaEquiv :: Expr -> Expr -> Bool
betaEquiv = on (==) betaNF