Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Forked from puffnfresh/Expr.hs
Created July 9, 2017 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiantakle/f50d9d655cb428ce3b79528fb44fff4f to your computer and use it in GitHub Desktop.
Save christiantakle/f50d9d655cb428ce3b79528fb44fff4f to your computer and use it in GitHub Desktop.
{-# 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