Last active
October 5, 2019 23:43
-
-
Save pedrominicz/67786ec4cd939e1f4412824369ba476b to your computer and use it in GitHub Desktop.
Haskell doodle (dependently typed lambda calculus, very incomplete)
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
-- https://www.youtube.com/watch?v=AT4JJm_ujRg | |
-- https://en.wikiversity.org/wiki/Foundations_of_Functional_Programming/Pure_type_systems | |
module Hal where | |
import Control.Monad | |
import Data.Set | |
type Name = String | |
data Expr | |
= Var Name | |
| Lam Name Type Expr | |
| Pi Name Type Type | |
| App Expr Expr | |
| Type | |
deriving (Eq, Show) | |
type Type = Expr | |
free :: Expr -> Set Name | |
free (Var x) = singleton x | |
free (Lam x t y) = free t `union` delete x (free y) | |
free (Pi x t y) = free t `union` delete x (free y) | |
free (App x y) = free x `union` free y | |
free Type = empty | |
check :: [(Name, Expr)] -> Expr -> Maybe Type | |
check env (Var x) = lookup x env | |
check env (Lam x t y) = do | |
ty <- check ((x, t):env) y | |
return $ Pi x t ty | |
check env (Pi x t y) = do | |
t' <- check env t | |
guard $ t' == Type | |
ty <- check ((x, Type):env) y | |
guard $ ty == Type | |
return Type | |
check env (App x y) = do | |
tx <- check env x | |
ty <- check env y | |
case tx of | |
Pi x' t y' -> do | |
guard $ t == ty | |
return $ substitute x' y y' | |
_ -> Nothing | |
check _ Type = return $ Type | |
substitute :: Name -> Expr -> Expr -> Expr | |
substitute = undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment