-
-
Save christiantakle/f50d9d655cb428ce3b79528fb44fff4f to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE TupleSections #-} | |
import Control.Lens | |
data Expr | |
= Var String | |
| App Expr Expr | |
| Lam String Expr | |
deriving Show | |
var :: Prism' Expr String | |
var = prism' f g | |
where f = Var | |
g (Var a) = Just a | |
g _ = Nothing | |
app :: Prism' Expr (Expr, Expr) | |
app = prism' f g | |
where f (a, b) = App a b | |
g (App a b) = Just (a, b) | |
g _ = Nothing | |
lam :: Prism' Expr (String, Expr) | |
lam = prism' f g | |
where f (a, b) = Lam a b | |
g (Lam a b) = Just (a, b) | |
g _ = Nothing | |
id' :: Expr | |
id' = lam # ("a", var # "a") | |
const' :: Expr | |
const' = id' & lam . _2 %~ (#) lam . ("b",) | |
constN :: Int -> Expr | |
constN n = foldr (\c e -> e & lam . _2 %~ (#) lam . (c:"",)) id' vars | |
where vars = enumFromTo 'b' (toEnum (n + fromEnum 'a')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment