Skip to content

Instantly share code, notes, and snippets.

@rybla
Last active August 5, 2026 17:55
Show Gist options
  • Select an option

  • Save rybla/1a6c0bc34a89ca8bc45703e52e0060da to your computer and use it in GitHub Desktop.

Select an option

Save rybla/1a6c0bc34a89ca8bc45703e52e0060da to your computer and use it in GitHub Desktop.
Using domains to construct a non-positive datatype for the untyped lambda calculus
--------------------------------
-- Embedding and Projecting
--------------------------------
-- | Embedding and projection between `a` and `b`. Note that `project` can fail.
data EP a b = EP
{ embed :: a -> b,
project :: b -> Maybe a
}
--------------------------------
-- Term Domains
--------------------------------
-- | Trivial base case domain.
type Term0 = ()
-- | Signature bifunctor for untyped lambda-calculus terms, which encodes lambda
-- as a function in the host language, which is non-positive. The full datatype
-- would be `data Term = Var String | Lam (Term -> Term) | App Term Term`. The
-- signature bifunctor is set up so that the first parameter, `x`, abstracts all
-- the *covariant* recursive uses of `Term`, and the second parameter `y`
-- abstracts all the *contravariant* recursive uses of `Term. It also seems like
-- I need to make `Lam` partial since projection can fail during evaluation.
data TermF x y
= Var String
| Lam (x -> Maybe y)
| App y y
type Term1 = TermF Term0 Term0
type Term2 = TermF Term1 Term1
type Term3 = TermF Term2 Term2
-- | A simple way of rendering a term as a string.
showTermF :: x -> (x -> String) -> (y -> String) -> TermF x y -> String
showTermF _dx _show_x _show_y (Var x) = x
showTermF dx show_x show_y (Lam f) = "(\\" <> show_x dx <> " -> " <> maybe "⊥" show_y (f dx) <> ")"
showTermF _dx _show_x show_y (App f a) = "(" <> show_y f <> " " <> show_y a <> ")"
showTerm1 :: Term1 -> String
showTerm1 = showTermF () show show
showTerm2 :: Term2 -> String
showTerm2 = showTermF (Var "x1") showTerm1 showTerm1
showTerm3 :: Term3 -> String
showTerm3 = showTermF (Var "x2") showTerm2 showTerm2
--------------------------------
-- Lifting an embedding and projection up a domain level
--------------------------------
-- | Lift an embedding and projection to the next domain level.
liftEP :: forall a b. EP a b -> EP (TermF a a) (TermF b b)
liftEP (EP {embed, project}) = EP {embed = embed', project = project'}
where
embed' :: TermF a a -> TermF b b
embed' (Var s) = Var s
embed' (App x y) = App (embed x) (embed y)
-- project contravariant input; embed covariant output
embed' (Lam f) = Lam $ \b -> case project b of
Nothing -> Nothing
Just a -> embed <$> f a
project' :: TermF b b -> Maybe (TermF a a)
project' (Var s) = Just (Var s)
project' (App x y) = App <$> project x <*> project y
-- embed contravariant input; project covariant output
project' (Lam g) = Just $ Lam $ \a -> project =<< g (embed a)
--------------------------------
-- Chain of embedding and projections
--------------------------------
ep0 :: EP Term0 Term1
ep0 =
EP
{ embed = \() -> Lam (\() -> Nothing),
project = \_ -> Just ()
}
ep1 :: EP Term1 Term2
ep1 = liftEP ep0
ep2 :: EP Term2 Term3
ep2 = liftEP ep1
--------------------------------
-- Examples
--------------------------------
id1 :: Term1
id1 = Lam $ \x -> Just x
x1 :: Term1
x1 = Var "y"
-- | `Term1` can't have any lambdas, so, I guess there's no redexes.
reduce1 :: Term1 -> Maybe Term0
reduce1 _ = Nothing
identity2 :: Term2
identity2 = Lam $ \x -> Just x
x2 :: Term2
x2 = Var "y"
example2 :: Term2
example2 = App id1 x1
reduce2 :: Term2 -> Maybe Term1
reduce2 (App (Lam f) a) = do
a' <- project ep0 a
embed ep0 <$> f a'
reduce2 _ = Nothing
id3 :: Term3
id3 = Lam $ \x -> Just x
x3 :: Term3
x3 = Var "y"
example3 :: Term3
example3 = App identity2 x2
reduce3 :: Term3 -> Maybe Term2
reduce3 (App (Lam f) a) = do
a' <- project ep1 a
embed ep1 <$> f a'
reduce3 _ = Nothing
--------------------------------
-- Demo
--------------------------------
main :: IO ()
main =
putStrLn . unlines $
[ --
-- example2
"project ep0 id1 = " <> (maybe "⊥" show $ project ep0 id1), -- ()
"project ep0 x1 = " <> (maybe "⊥" show $ project ep0 x1), -- ()
"App id1 x1 = " <> showTerm2 (App id1 x1), -- ((\() -> ()) y)
-- [TODO]: if `project ep0 identityTerm1 = ()`, then why does `reduce2 appTerm2 = \() -> ⊥`??
"reduce2 (App id1 x1) = " <> (maybe "⊥" showTerm1 $ reduce2 (App id1 x1)), -- (\() -> ⊥)
--
-- example3
"App identity2 x2 = " <> showTerm3 example3, -- ((\x1 -> x1) y)
"reduce3 (App identity2 x2) = " <> (maybe "⊥" showTerm2) (reduce3 example3) -- y
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment