Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: added constants support #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example.lambda
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
((λ x (λ y x)) "Hello, world!")


constant = (λ x (λ y x))
5 changes: 5 additions & 0 deletions src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data Expr
| Term Identifier
| Abs Identifier Expr
| App Expr Expr
| Const Identifier Expr
deriving (Show, Eq)

data Value
Expand All @@ -45,6 +46,7 @@ eval :: Env Value -> Expr -> Either Error Value
eval _ (Lit string) = Right $ Value string
eval env (Term identifier) = maybeToEither (UndeclaredVar identifier) $ lookup identifier env
eval env (Abs identifier expr) = Right $ Closure expr env identifier
eval env (Const identifier expr) = Right $ Closure expr env identifier
eval env (App t u) = do
vt <- eval env t
vu <- eval env u
Expand All @@ -56,6 +58,7 @@ betaReduce :: Env Expr -> Expr -> Either Error Expr
betaReduce _ (Lit string) = Right $ Lit string
betaReduce env term@(Term identifier) = Right $ fromMaybe term $ lookup identifier env
betaReduce env (Abs identifier expr) = Abs identifier <$> betaReduce env expr
betaReduce env (Const identifier expr) = Abs identifier <$> betaReduce env expr
betaReduce env (App t u) = do
vt <- betaReduce env t
vu <- betaReduce env u
Expand Down Expand Up @@ -91,6 +94,7 @@ checkUnused = go []
mergeUnused unused (go unused expr_l) (go unused expr_r)
go unused (Abs "_" expr) = go unused expr
go unused (Abs id expr) = go (id : unused) expr
go unused (Const id expr) = go (id : unused) expr

mergeUnused :: (Eq a) => [a] -> [a] -> [a] -> [a]
mergeUnused env left right = (env \\ removed) ++ added
Expand All @@ -114,6 +118,7 @@ compile :: Expr -> Either Error String
compile (Lit string) = Right $ printf "'%s'" string
compile (Term identifier) = Right $ identifier
compile (Abs identifier expr) = printf "(%s => %s)" identifier <$> compile expr
compile (Const identifier expr) = printf "%s = %s;\n" identifier <$> compile expr
compile (App (Lit id) _) = Left $ NonFunctionApp id
compile (App t u) = printf "%s(%s)" <$> compile t <*> compile u

Expand Down
29 changes: 23 additions & 6 deletions src/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Control.Monad (guard)
import Control.Arrow (first)
import Data.Char (isLetter)

import Lib (Expr(Lit, Term, Abs, App), Identifier)
import Lib (Expr(Lit, Term, Abs, App, Const), Identifier)

data Parser s m a = Parser { runParser :: s -> m (a, s) }

Expand Down Expand Up @@ -56,10 +56,13 @@ identifier :: (Alternative m, Monad m) => Parser String m Identifier
identifier = some $ satisfy isLetter

term :: (Alternative m, Monad m) => Parser String m Expr
term = Term <$> identifier
term = do
many $ char '\n'
Term <$> identifier

lambda :: (Alternative m, Monad m) => Parser String m Expr
lambda = do
many $ char '\n'
char '('
char 'λ'
char ' '
Expand All @@ -71,15 +74,29 @@ lambda = do

app :: (Alternative m, Monad m) => Parser String m Expr
app = do
many $ char '\n'
char '('
t <- expr
char ' '
u <- expr
char ')'
pure $ App t u

constant :: (Alternative m, Monad m) => Parser String m Expr
constant = do
many $ char '\n'
alias <- many $ satisfy (/= ' ')
char ' '
char '='
char ' '
val <- expr
char '\n'
pure $ Const alias val

expr :: (Alternative m, Monad m) => Parser String m Expr
expr = lit
<|> term
<|> lambda
<|> app
expr
= lit
<|> constant
<|> term
<|> lambda
<|> app