Rename from sapling to tricu

This commit is contained in:
James Eversole 2024-12-29 08:29:25 -06:00
parent 064bed26c5
commit c30f17367f
10 changed files with 136 additions and 135 deletions

View File

@ -1,10 +1,12 @@
# sapling # tricu
## Introduction ## Introduction
sapling is a "micro-language" that [I'm](https://eversole.co) working on to investigate [Tree Calculus](https://github.com/barry-jay-personal/typed_tree_calculus/blob/main/typed_program_analysis.pdf) . tricu (pronounced like "tree-shoe") is a "micro-language" that [I'm](https://eversole.co) working on to investigate [Tree Calculus](https://github.com/barry-jay-personal/typed_tree_calculus/blob/main/typed_program_analysis.pdf).
It offers a minimal amount of syntax sugar yet provides a complete and intuitive programming environment. Sapling offers: tricu [means tree in Lojban](https://en.wiktionary.org/wiki/Appendix:Lojban/tricu). This project was named "sapling" until I discovered the name was already being used for other projects in programming language development.
tricu offers a minimal amount of syntax sugar yet provides a complete and intuitive programming environment. tricu offers:
- `t` operator behaving by the rules of Tree Calculus - `t` operator behaving by the rules of Tree Calculus
- Function ("variable") definitions - Function ("variable") definitions
@ -21,36 +23,35 @@ _ = t
true = t t true = t t
-- We can define functions as lambda expressions that are eliminated to tree -- We can define functions as lambda expressions that are eliminated to tree
-- calculus terms. -- calculus terms.
id = (\a : a) -- t (t (t t)) t id = (\a : a) -- `id` evaluates to the TC form of: t (t (t t)) t
triage = (\a b c : t (t a b) c)
triage = (\a b c : t (t a b) c) triage = (\a b c : t (t a b) c)
-- Intensionality !!! -- Intensionality! We can inspect program structure, not just inputs/outputs:
test = triage "Leaf" (\_ : "Stem") (\_ _ : "Fork") test = triage "Leaf" (\_ : "Stem") (\_ _ : "Fork")
-- REPL -- REPL
-- `sapling <` is the input prompt -- `tricu <` is the input prompt
-- `sapling >` is the Tree Calculus form output. Most are elided below. -- `tricu >` is the Tree Calculus form output. Most are elided below.
-- `DECODE -:` is an attempt to interpret the TC output as strings/numbers. -- `READ -:` is an attempt to interpret the TC output as strings/numbers.
sapling < test t tricu < test t
sapling > Fork (Fork Leaf (Fork ...) ... ) tricu > Fork (Fork Leaf (Fork ...) ... )
DECODE -: "Leaf" READ -: "Leaf"
sapling < test (t t) tricu < test (t t)
DECODE -: "Stem" READ -: "Stem"
sapling < test (t t t) tricu < test (t t t)
DECODE -: "Fork" READ -: "Fork"
sapling < map (\i : listConcat i " is super cool!") [("He") ("She") ("Everybody")] tricu < map (\i : listConcat i " is super cool!") [("Tree Calculus") ("Intensionality") ("tricu")]
DECODE -: ["He is super cool!", "She is super cool!", "Everybody is super cool!"] READ -: ["Tree Calculus is super cool!", "Intensionality is super cool!", "tricu is super cool!"]
``` ```
## Installation ## Installation
You can easily build and/or run this project using [Nix](https://nixos.org/download/). You can easily build and/or run this project using [Nix](https://nixos.org/download/).
- Build REPL binary: `nix build git+https://git.eversole.co/James/sapling` - Build REPL binary: `nix build git+https://git.eversole.co/James/tricu`
- Run REPL: `nix run git+https://git.eversole.co/James/sapling` - Run REPL: `nix run git+https://git.eversole.co/James/tricu`
## Acknowledgements ## Acknowledgements
Tree Calculus was discovered by [Barry Jay](https://github.com/barry-jay-personal/blog). Tree Calculus was discovered by [Barry Jay](https://github.com/barry-jay-personal/blog).
[treecalcul.us](https://treecalcul.us) is an excellent website with an intuitive playground created by [Johannes Bader](https://johannes-bader.com/) that introduced me to Tree Calculus. If sapling sounds interesting but compiling this repo sounds like a hassle, you should check out his site. [treecalcul.us](https://treecalcul.us) is an excellent website with an intuitive playground created by [Johannes Bader](https://johannes-bader.com/) that introduced me to Tree Calculus. If tricu sounds interesting but compiling this repo sounds like a hassle, you should check out his site.

View File

@ -1,5 +1,5 @@
{ {
description = "sapling"; description = "tricu";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs"; nixpkgs.url = "github:NixOS/nixpkgs";
@ -10,7 +10,7 @@
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
packageName = "sapling"; packageName = "tricu";
containerPackageName = "${packageName}-container"; containerPackageName = "${packageName}-container";
customGHC = pkgs.haskellPackages.ghcWithPackages (hpkgs: with hpkgs; [ customGHC = pkgs.haskellPackages.ghcWithPackages (hpkgs: with hpkgs; [
@ -22,7 +22,7 @@
enableSharedExecutables = false; enableSharedExecutables = false;
enableSharedLibraries = false; enableSharedLibraries = false;
sapling = pkgs.haskell.lib.justStaticExecutables self.packages.${system}.default; tricu = pkgs.haskell.lib.justStaticExecutables self.packages.${system}.default;
in { in {
packages.${packageName} = packages.${packageName} =

View File

@ -8,7 +8,7 @@ import qualified Data.Map as Map
import Data.List (foldl') import Data.List (foldl')
import qualified Data.Set as Set import qualified Data.Set as Set
evalSingle :: Map String T -> SaplingAST -> Map String T evalSingle :: Map String T -> TricuAST -> Map String T
evalSingle env term = case term of evalSingle env term = case term of
SFunc name [] body -> SFunc name [] body ->
let lineNoLambda = eliminateLambda body let lineNoLambda = eliminateLambda body
@ -28,18 +28,18 @@ evalSingle env term = case term of
let result = evalAST env term let result = evalAST env term
in Map.insert "__result" result env in Map.insert "__result" result env
evalSapling :: Map String T -> [SaplingAST] -> Map String T evalTricu :: Map String T -> [TricuAST] -> Map String T
evalSapling env [] = env evalTricu env [] = env
evalSapling env [lastLine] = evalTricu env [lastLine] =
let lastLineNoLambda = eliminateLambda lastLine let lastLineNoLambda = eliminateLambda lastLine
updatedEnv = evalSingle env lastLineNoLambda updatedEnv = evalSingle env lastLineNoLambda
in Map.insert "__result" (result updatedEnv) updatedEnv in Map.insert "__result" (result updatedEnv) updatedEnv
evalSapling env (line:rest) = evalTricu env (line:rest) =
let lineNoLambda = eliminateLambda line let lineNoLambda = eliminateLambda line
updatedEnv = evalSingle env lineNoLambda updatedEnv = evalSingle env lineNoLambda
in evalSapling updatedEnv rest in evalTricu updatedEnv rest
evalAST :: Map String T -> SaplingAST -> T evalAST :: Map String T -> TricuAST -> T
evalAST env term = case term of evalAST env term = case term of
SVar name -> case Map.lookup name env of SVar name -> case Map.lookup name env of
Just value -> value Just value -> value
@ -56,7 +56,7 @@ evalAST env term = case term of
++ " in evalAST; define via evalSingle." ++ " in evalAST; define via evalSingle."
SLambda {} -> error "Internal error: SLambda found in evalAST after elimination." SLambda {} -> error "Internal error: SLambda found in evalAST after elimination."
eliminateLambda :: SaplingAST -> SaplingAST eliminateLambda :: TricuAST -> TricuAST
eliminateLambda (SLambda (v:vs) body) eliminateLambda (SLambda (v:vs) body)
| null vs = lambdaToT v (eliminateLambda body) | null vs = lambdaToT v (eliminateLambda body)
| otherwise = eliminateLambda (SLambda [v] (SLambda vs body)) | otherwise = eliminateLambda (SLambda [v] (SLambda vs body))
@ -69,7 +69,7 @@ eliminateLambda other = other
-- This is my attempt to implement the lambda calculus elimination rules defined -- This is my attempt to implement the lambda calculus elimination rules defined
-- in "Typed Program Analysis without Encodings" by Barry Jay. -- in "Typed Program Analysis without Encodings" by Barry Jay.
-- https://github.com/barry-jay-personal/typed_tree_calculus/blob/main/typed_program_analysis.pdf -- https://github.com/barry-jay-personal/typed_tree_calculus/blob/main/typed_program_analysis.pdf
lambdaToT :: String -> SaplingAST -> SaplingAST lambdaToT :: String -> TricuAST -> TricuAST
lambdaToT x (SVar y) lambdaToT x (SVar y)
| x == y = tI | x == y = tI
lambdaToT x (SVar y) lambdaToT x (SVar y)
@ -83,7 +83,7 @@ lambdaToT x body
| not (isFree x body) = SApp tK body | not (isFree x body) = SApp tK body
| otherwise = SApp (SApp tS (lambdaToT x body)) TLeaf | otherwise = SApp (SApp tS (lambdaToT x body)) TLeaf
freeVars :: SaplingAST -> Set.Set String freeVars :: TricuAST -> Set.Set String
freeVars (SVar v) = Set.singleton v freeVars (SVar v) = Set.singleton v
freeVars (SInt _) = Set.empty freeVars (SInt _) = Set.empty
freeVars (SStr _) = Set.empty freeVars (SStr _) = Set.empty
@ -95,23 +95,23 @@ freeVars (TStem t) = freeVars t
freeVars (TFork l r) = freeVars l <> freeVars r freeVars (TFork l r) = freeVars l <> freeVars r
freeVars (SLambda vs b) = foldr Set.delete (freeVars b) vs freeVars (SLambda vs b) = foldr Set.delete (freeVars b) vs
isFree :: String -> SaplingAST -> Bool isFree :: String -> TricuAST -> Bool
isFree x = Set.member x . freeVars isFree x = Set.member x . freeVars
toAST :: T -> SaplingAST toAST :: T -> TricuAST
toAST Leaf = TLeaf toAST Leaf = TLeaf
toAST (Stem a) = TStem (toAST a) toAST (Stem a) = TStem (toAST a)
toAST (Fork a b) = TFork (toAST a) (toAST b) toAST (Fork a b) = TFork (toAST a) (toAST b)
-- We need the SKI operators in an unevaluated SaplingAST tree form so that we -- We need the SKI operators in an unevaluated TricuAST tree form so that we
-- can keep the evaluation functions straightforward -- can keep the evaluation functions straightforward
tI :: SaplingAST tI :: TricuAST
tI = SApp (SApp TLeaf (SApp TLeaf (SApp TLeaf TLeaf))) TLeaf tI = SApp (SApp TLeaf (SApp TLeaf (SApp TLeaf TLeaf))) TLeaf
tK :: SaplingAST tK :: TricuAST
tK = SApp TLeaf TLeaf tK = SApp TLeaf TLeaf
tS :: SaplingAST tS :: TricuAST
tS = SApp (SApp TLeaf (SApp TLeaf (SApp (SApp TLeaf TLeaf) TLeaf))) TLeaf tS = SApp (SApp TLeaf (SApp TLeaf (SApp (SApp TLeaf TLeaf) TLeaf))) TLeaf
result :: Map String T -> T result :: Map String T -> T

View File

@ -86,8 +86,8 @@ comment = do
sc :: Lexer () sc :: Lexer ()
sc = skipMany (void (char ' ') <|> void (char '\t') <|> void comment) sc = skipMany (void (char ' ') <|> void (char '\t') <|> void comment)
saplingLexer :: Lexer [LToken] tricuLexer :: Lexer [LToken]
saplingLexer = many (sc *> choice tricuLexer = many (sc *> choice
[ try identifier [ try identifier
, try keywordT , try keywordT
, try integerLiteral , try integerLiteral
@ -102,7 +102,7 @@ saplingLexer = many (sc *> choice
, lnewline , lnewline
] <* sc) <* eof ] <* sc) <* eof
lexSapling :: String -> [LToken] lexTricu :: String -> [LToken]
lexSapling input = case runParser saplingLexer "" input of lexTricu input = case runParser tricuLexer "" input of
Left err -> error $ "Lexical error:\n" ++ errorBundlePretty err Left err -> error $ "Lexical error:\n" ++ errorBundlePretty err
Right tokens -> tokens Right tokens -> tokens

View File

@ -7,7 +7,7 @@ import Research
import qualified Data.Map as Map import qualified Data.Map as Map
library :: Map.Map String T library :: Map.Map String T
library = evalSapling Map.empty $ parseSapling $ unlines library = evalTricu Map.empty $ parseTricu $ unlines
[ "false = t" [ "false = t"
, "true = t t" , "true = t t"
, "_ = t" , "_ = t"

View File

@ -12,11 +12,11 @@ import Text.Megaparsec (runParser)
main :: IO () main :: IO ()
main = do main = do
putStrLn "Welcome to the Sapling Interpreter" putStrLn "Welcome to the Tricu Interpreter"
putStrLn "You can exit at any time by typing and entering: " putStrLn "You can exit at any time by typing and entering: "
putStrLn ":_exit" putStrLn ":_exit"
repl library repl library
runSapling :: String -> T runTricu :: String -> T
runSapling s = result (evalSapling Map.empty $ parseSapling s) runTricu s = result (evalTricu Map.empty $ parseTricu s)
runSaplingEnv env s = result (evalSapling env $ parseSapling s) runTricuEnv env s = result (evalTricu env $ parseTricu s)

View File

@ -13,33 +13,33 @@ import Text.Megaparsec.Error (errorBundlePretty, ParseErrorBundle)
type Parser = Parsec Void [LToken] type Parser = Parsec Void [LToken]
data SaplingAST data TricuAST
= SVar String = SVar String
| SInt Int | SInt Int
| SStr String | SStr String
| SList [SaplingAST] | SList [TricuAST]
| SFunc String [String] SaplingAST | SFunc String [String] TricuAST
| SApp SaplingAST SaplingAST | SApp TricuAST TricuAST
| TLeaf | TLeaf
| TStem SaplingAST | TStem TricuAST
| TFork SaplingAST SaplingAST | TFork TricuAST TricuAST
| SLambda [String] SaplingAST | SLambda [String] TricuAST
deriving (Show, Eq, Ord) deriving (Show, Eq, Ord)
parseSapling :: String -> [SaplingAST] parseTricu :: String -> [TricuAST]
parseSapling input = parseTricu input =
let nonEmptyLines = filter (not . null) (lines input) let nonEmptyLines = filter (not . null) (lines input)
in map parseSingle nonEmptyLines in map parseSingle nonEmptyLines
parseSingle :: String -> SaplingAST parseSingle :: String -> TricuAST
parseSingle input = case runParser parseExpression "" (lexSapling input) of parseSingle input = case runParser parseExpression "" (lexTricu input) of
Left err -> error $ handleParseError err Left err -> error $ handleParseError err
Right ast -> ast Right ast -> ast
scnParser :: Parser () scnParser :: Parser ()
scnParser = skipMany (satisfy isNewline) scnParser = skipMany (satisfy isNewline)
parseExpression :: Parser SaplingAST parseExpression :: Parser TricuAST
parseExpression = choice parseExpression = choice
[ try parseFunction [ try parseFunction
, try parseLambda , try parseLambda
@ -50,7 +50,7 @@ parseExpression = choice
, parseLiteral , parseLiteral
] ]
parseFunction :: Parser SaplingAST parseFunction :: Parser TricuAST
parseFunction = do parseFunction = do
LIdentifier name <- satisfy isIdentifier LIdentifier name <- satisfy isIdentifier
args <- many (satisfy isIdentifier) args <- many (satisfy isIdentifier)
@ -58,20 +58,20 @@ parseFunction = do
body <- parseExpression body <- parseExpression
return (SFunc name (map getIdentifier args) body) return (SFunc name (map getIdentifier args) body)
parseAtomicBase :: Parser SaplingAST parseAtomicBase :: Parser TricuAST
parseAtomicBase = choice parseAtomicBase = choice
[ try parseVarWithoutAssignment [ try parseVarWithoutAssignment
, parseTreeLeaf , parseTreeLeaf
, parseGrouped , parseGrouped
] ]
parseVarWithoutAssignment :: Parser SaplingAST parseVarWithoutAssignment :: Parser TricuAST
parseVarWithoutAssignment = do parseVarWithoutAssignment = do
LIdentifier name <- satisfy isIdentifier LIdentifier name <- satisfy isIdentifier
if (name == "t" || name == "__result") if (name == "t" || name == "__result")
then fail $ "Reserved keyword: " ++ name ++ " cannot be assigned." then fail $ "Reserved keyword: " ++ name ++ " cannot be assigned."
else notFollowedBy (satisfy (== LAssign)) *> return (SVar name) else notFollowedBy (satisfy (== LAssign)) *> return (SVar name)
parseLambda :: Parser SaplingAST parseLambda :: Parser TricuAST
parseLambda = between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) $ do parseLambda = between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) $ do
satisfy (== LBackslash) satisfy (== LBackslash)
param <- satisfy isIdentifier param <- satisfy isIdentifier
@ -81,13 +81,13 @@ parseLambda = between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) $ do
let nestedLambda = foldr (\v acc -> SLambda [v] acc) body (map getIdentifier rest) let nestedLambda = foldr (\v acc -> SLambda [v] acc) body (map getIdentifier rest)
return (SLambda [getIdentifier param] nestedLambda) return (SLambda [getIdentifier param] nestedLambda)
parseLambdaExpression :: Parser SaplingAST parseLambdaExpression :: Parser TricuAST
parseLambdaExpression = choice parseLambdaExpression = choice
[ try parseLambdaApplication [ try parseLambdaApplication
, parseAtomicLambda , parseAtomicLambda
] ]
parseAtomicLambda :: Parser SaplingAST parseAtomicLambda :: Parser TricuAST
parseAtomicLambda = choice parseAtomicLambda = choice
[ parseVar [ parseVar
, parseTreeLeaf , parseTreeLeaf
@ -97,32 +97,32 @@ parseAtomicLambda = choice
, between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseLambdaExpression , between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseLambdaExpression
] ]
parseApplication :: Parser SaplingAST parseApplication :: Parser TricuAST
parseApplication = do parseApplication = do
func <- parseAtomicBase func <- parseAtomicBase
args <- many parseAtomic args <- many parseAtomic
return $ foldl (\acc arg -> SApp acc arg) func args return $ foldl (\acc arg -> SApp acc arg) func args
parseLambdaApplication :: Parser SaplingAST parseLambdaApplication :: Parser TricuAST
parseLambdaApplication = do parseLambdaApplication = do
func <- parseAtomicLambda func <- parseAtomicLambda
args <- many parseAtomicLambda args <- many parseAtomicLambda
return $ foldl (\acc arg -> SApp acc arg) func args return $ foldl (\acc arg -> SApp acc arg) func args
isTreeTerm :: SaplingAST -> Bool isTreeTerm :: TricuAST -> Bool
isTreeTerm TLeaf = True isTreeTerm TLeaf = True
isTreeTerm (TStem _) = True isTreeTerm (TStem _) = True
isTreeTerm (TFork _ _) = True isTreeTerm (TFork _ _) = True
isTreeTerm _ = False isTreeTerm _ = False
parseTreeLeaf :: Parser SaplingAST parseTreeLeaf :: Parser TricuAST
parseTreeLeaf = satisfy isKeywordT *> notFollowedBy (satisfy (== LAssign)) *> pure TLeaf parseTreeLeaf = satisfy isKeywordT *> notFollowedBy (satisfy (== LAssign)) *> pure TLeaf
getIdentifier :: LToken -> String getIdentifier :: LToken -> String
getIdentifier (LIdentifier name) = name getIdentifier (LIdentifier name) = name
getIdentifier _ = error "Expected identifier" getIdentifier _ = error "Expected identifier"
parseTreeTerm :: Parser SaplingAST parseTreeTerm :: Parser TricuAST
parseTreeTerm = do parseTreeTerm = do
base <- parseTreeLeafOrParenthesized base <- parseTreeLeafOrParenthesized
rest <- many parseTreeLeafOrParenthesized rest <- many parseTreeLeafOrParenthesized
@ -133,18 +133,18 @@ parseTreeTerm = do
TStem t -> TFork t next TStem t -> TFork t next
TFork _ _ -> TFork acc next TFork _ _ -> TFork acc next
parseTreeLeafOrParenthesized :: Parser SaplingAST parseTreeLeafOrParenthesized :: Parser TricuAST
parseTreeLeafOrParenthesized = choice parseTreeLeafOrParenthesized = choice
[ between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseTreeTerm [ between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseTreeTerm
, parseTreeLeaf , parseTreeLeaf
] ]
foldTree :: [SaplingAST] -> SaplingAST foldTree :: [TricuAST] -> TricuAST
foldTree [] = TLeaf foldTree [] = TLeaf
foldTree [x] = x foldTree [x] = x
foldTree (x:y:rest) = TFork x (foldTree (y:rest)) foldTree (x:y:rest) = TFork x (foldTree (y:rest))
parseAtomic :: Parser SaplingAST parseAtomic :: Parser TricuAST
parseAtomic = choice parseAtomic = choice
[ parseVar [ parseVar
, parseTreeLeaf , parseTreeLeaf
@ -153,44 +153,44 @@ parseAtomic = choice
, parseLiteral , parseLiteral
] ]
parseGrouped :: Parser SaplingAST parseGrouped :: Parser TricuAST
parseGrouped = between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseExpression parseGrouped = between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseExpression
parseLiteral :: Parser SaplingAST parseLiteral :: Parser TricuAST
parseLiteral = choice parseLiteral = choice
[ parseIntLiteral [ parseIntLiteral
, parseStrLiteral , parseStrLiteral
] ]
parens :: Parser SaplingAST -> Parser SaplingAST parens :: Parser TricuAST -> Parser TricuAST
parens p = do parens p = do
satisfy (== LOpenParen) satisfy (== LOpenParen)
result <- p result <- p
satisfy (== LCloseParen) satisfy (== LCloseParen)
return result return result
parseListLiteral :: Parser SaplingAST parseListLiteral :: Parser TricuAST
parseListLiteral = do parseListLiteral = do
satisfy (== LOpenBracket) satisfy (== LOpenBracket)
elements <- many parseListItem elements <- many parseListItem
satisfy (== LCloseBracket) satisfy (== LCloseBracket)
return (SList elements) return (SList elements)
parseListItem :: Parser SaplingAST parseListItem :: Parser TricuAST
parseListItem = choice parseListItem = choice
[ parseGroupedItem [ parseGroupedItem
, parseListLiteral , parseListLiteral
, parseSingleItem , parseSingleItem
] ]
parseGroupedItem :: Parser SaplingAST parseGroupedItem :: Parser TricuAST
parseGroupedItem = do parseGroupedItem = do
satisfy (== LOpenParen) satisfy (== LOpenParen)
inner <- parseExpression inner <- parseExpression
satisfy (== LCloseParen) satisfy (== LCloseParen)
return inner return inner
parseSingleItem :: Parser SaplingAST parseSingleItem :: Parser TricuAST
parseSingleItem = do parseSingleItem = do
token <- satisfy isListItem token <- satisfy isListItem
case token of case token of
@ -203,19 +203,19 @@ isListItem (LIdentifier _) = True
isListItem LKeywordT = True isListItem LKeywordT = True
isListItem _ = False isListItem _ = False
parseVar :: Parser SaplingAST parseVar :: Parser TricuAST
parseVar = do parseVar = do
LIdentifier name <- satisfy isIdentifier LIdentifier name <- satisfy isIdentifier
if (name == "t" || name == "__result") if (name == "t" || name == "__result")
then fail $ "Reserved keyword: " ++ name ++ " cannot be assigned." then fail $ "Reserved keyword: " ++ name ++ " cannot be assigned."
else return (SVar name) else return (SVar name)
parseIntLiteral :: Parser SaplingAST parseIntLiteral :: Parser TricuAST
parseIntLiteral = do parseIntLiteral = do
LIntegerLiteral value <- satisfy isIntegerLiteral LIntegerLiteral value <- satisfy isIntegerLiteral
return (SInt value) return (SInt value)
parseStrLiteral :: Parser SaplingAST parseStrLiteral :: Parser TricuAST
parseStrLiteral = do parseStrLiteral = do
LStringLiteral value <- satisfy isStringLiteral LStringLiteral value <- satisfy isStringLiteral
return (SStr value) return (SStr value)

View File

@ -15,7 +15,7 @@ repl env = runInputT defaultSettings (loop env)
where where
loop :: Map.Map String T -> InputT IO () loop :: Map.Map String T -> InputT IO ()
loop env = do loop env = do
minput <- getInputLine "sapling < " minput <- getInputLine "tricu < "
case minput of case minput of
Nothing -> outputStrLn "Goodbye!" Nothing -> outputStrLn "Goodbye!"
Just ":_exit" -> outputStrLn "Goodbye!" Just ":_exit" -> outputStrLn "Goodbye!"
@ -27,7 +27,7 @@ repl env = runInputT defaultSettings (loop env)
newEnv = evalSingle clearEnv (parseSingle input) newEnv = evalSingle clearEnv (parseSingle input)
case Map.lookup "__result" newEnv of case Map.lookup "__result" newEnv of
Just r -> do Just r -> do
outputStrLn $ "sapling > " ++ show r outputStrLn $ "tricu > " ++ show r
outputStrLn $ "DECODE -: " ++ decodeResult r outputStrLn $ "DECODE -: " ++ decodeResult r
Nothing -> return () Nothing -> return ()
loop newEnv loop newEnv

View File

@ -17,11 +17,11 @@ import qualified Data.Set as Set
main :: IO () main :: IO ()
main = defaultMain tests main = defaultMain tests
runSapling :: String -> String runTricu :: String -> String
runSapling s = show $ result (evalSapling Map.empty $ parseSapling s) runTricu s = show $ result (evalTricu Map.empty $ parseTricu s)
tests :: TestTree tests :: TestTree
tests = testGroup "Sapling Tests" tests = testGroup "Tricu Tests"
[ lexerTests [ lexerTests
, parserTests , parserTests
, integrationTests , integrationTests
@ -35,32 +35,32 @@ lexerTests = testGroup "Lexer Tests"
[ testCase "Lex simple identifiers" $ do [ testCase "Lex simple identifiers" $ do
let input = "x a b = a" let input = "x a b = a"
expect = Right [LIdentifier "x", LIdentifier "a", LIdentifier "b", LAssign, LIdentifier "a"] expect = Right [LIdentifier "x", LIdentifier "a", LIdentifier "b", LAssign, LIdentifier "a"]
runParser saplingLexer "" input @?= expect runParser tricuLexer "" input @?= expect
, testCase "Lex Tree Calculus terms" $ do , testCase "Lex Tree Calculus terms" $ do
let input = "t t t" let input = "t t t"
expect = Right [LKeywordT, LKeywordT, LKeywordT] expect = Right [LKeywordT, LKeywordT, LKeywordT]
runParser saplingLexer "" input @?= expect runParser tricuLexer "" input @?= expect
, testCase "Lex escaped characters in strings" $ do , testCase "Lex escaped characters in strings" $ do
let input = "\"hello\\nworld\"" let input = "\"hello\\nworld\""
expect = Right [LStringLiteral "hello\\nworld"] expect = Right [LStringLiteral "hello\\nworld"]
runParser saplingLexer "" input @?= expect runParser tricuLexer "" input @?= expect
, testCase "Lex mixed literals" $ do , testCase "Lex mixed literals" $ do
let input = "t \"string\" 42" let input = "t \"string\" 42"
expect = Right [LKeywordT, LStringLiteral "string", LIntegerLiteral 42] expect = Right [LKeywordT, LStringLiteral "string", LIntegerLiteral 42]
runParser saplingLexer "" input @?= expect runParser tricuLexer "" input @?= expect
, testCase "Lex invalid token" $ do , testCase "Lex invalid token" $ do
let input = "$invalid" let input = "$invalid"
case runParser saplingLexer "" input of case runParser tricuLexer "" input of
Left _ -> return () Left _ -> return ()
Right _ -> assertFailure "Expected lexer to fail on invalid token" Right _ -> assertFailure "Expected lexer to fail on invalid token"
, testCase "Drop trailing whitespace in definitions" $ do , testCase "Drop trailing whitespace in definitions" $ do
let input = "x = 5 " let input = "x = 5 "
expect = [LIdentifier "x",LAssign,LIntegerLiteral 5] expect = [LIdentifier "x",LAssign,LIntegerLiteral 5]
case (runParser saplingLexer "" input) of case (runParser tricuLexer "" input) of
Left _ -> assertFailure "Failed to lex input" Left _ -> assertFailure "Failed to lex input"
Right i -> i @?= expect Right i -> i @?= expect
, testCase "Error when using invalid characters in identifiers" $ do , testCase "Error when using invalid characters in identifiers" $ do
case (runParser saplingLexer "" "__result = 5") of case (runParser tricuLexer "" "__result = 5") of
Left _ -> return () Left _ -> return ()
Right _ -> assertFailure "Expected failure when trying to assign the value of __result" Right _ -> assertFailure "Expected failure when trying to assign the value of __result"
] ]
@ -68,12 +68,12 @@ lexerTests = testGroup "Lexer Tests"
parserTests :: TestTree parserTests :: TestTree
parserTests = testGroup "Parser Tests" parserTests = testGroup "Parser Tests"
[ --testCase "Error when parsing incomplete definitions" $ do [ --testCase "Error when parsing incomplete definitions" $ do
-- let input = lexSapling "x = " -- let input = lexTricu "x = "
-- case (runParser parseExpression "" input) of -- case (runParser parseExpression "" input) of
-- Left _ -> return () -- Left _ -> return ()
-- Right _ -> assertFailure "Expected failure on invalid input" -- Right _ -> assertFailure "Expected failure on invalid input"
testCase "Error when assigning a value to T" $ do testCase "Error when assigning a value to T" $ do
let input = lexSapling "t = x" let input = lexTricu "t = x"
case (runParser parseExpression "" input) of case (runParser parseExpression "" input) of
Left _ -> return () Left _ -> return ()
Right _ -> assertFailure "Expected failure when trying to assign the value of T" Right _ -> assertFailure "Expected failure when trying to assign the value of T"
@ -142,7 +142,7 @@ parserTests = testGroup "Parser Tests"
, testCase "Grouping T terms with parentheses in function application" $ do , testCase "Grouping T terms with parentheses in function application" $ do
let input = "x = (\\a : a)\nx (t)" let input = "x = (\\a : a)\nx (t)"
expect = [SFunc "x" [] (SLambda ["a"] (SVar "a")),SApp (SVar "x") TLeaf] expect = [SFunc "x" [] (SLambda ["a"] (SVar "a")),SApp (SVar "x") TLeaf]
parseSapling input @?= expect parseTricu input @?= expect
] ]
integrationTests :: TestTree integrationTests :: TestTree
@ -182,16 +182,16 @@ evaluationTests = testGroup "Evaluation Tests"
Fork (Fork (Stem Leaf) (Fork Leaf Leaf)) Leaf Fork (Fork (Stem Leaf) (Fork Leaf Leaf)) Leaf
, testCase "Environment updates with definitions" $ do , testCase "Environment updates with definitions" $ do
let input = "x = t\ny = x" let input = "x = t\ny = x"
env = evalSapling Map.empty (parseSapling input) env = evalTricu Map.empty (parseTricu input)
Map.lookup "x" env @?= Just Leaf Map.lookup "x" env @?= Just Leaf
Map.lookup "y" env @?= Just Leaf Map.lookup "y" env @?= Just Leaf
, testCase "Variable substitution" $ do , testCase "Variable substitution" $ do
let input = "x = t t\ny = t x\ny" let input = "x = t t\ny = t x\ny"
env = evalSapling Map.empty (parseSapling input) env = evalTricu Map.empty (parseTricu input)
(result env) @?= Stem (Stem Leaf) (result env) @?= Stem (Stem Leaf)
, testCase "Multiline input evaluation" $ do , testCase "Multiline input evaluation" $ do
let input = "x = t\ny = t t\nx" let input = "x = t\ny = t t\nx"
env = evalSapling Map.empty (parseSapling input) env = evalTricu Map.empty (parseTricu input)
(result env) @?= Leaf (result env) @?= Leaf
, testCase "Evaluate string literal" $ do , testCase "Evaluate string literal" $ do
let input = "\"hello\"" let input = "\"hello\""
@ -211,40 +211,40 @@ evaluationTests = testGroup "Evaluation Tests"
\ z = y\n \ \ z = y\n \
\ variablewithamuchlongername = z\n \ \ variablewithamuchlongername = z\n \
\ variablewithamuchlongername" \ variablewithamuchlongername"
env = evalSapling Map.empty (parseSapling input) env = evalTricu Map.empty (parseTricu input)
(result env) @?= (Stem (Stem Leaf)) (result env) @?= (Stem (Stem Leaf))
, testCase "Evaluate variable shadowing" $ do , testCase "Evaluate variable shadowing" $ do
let input = "x = t t\nx = t\nx" let input = "x = t t\nx = t\nx"
env = evalSapling Map.empty (parseSapling input) env = evalTricu Map.empty (parseTricu input)
(result env) @?= Leaf (result env) @?= Leaf
, testCase "Apply identity to Boolean Not" $ do , testCase "Apply identity to Boolean Not" $ do
let not = "(t (t (t t) (t t t)) t)" let not = "(t (t (t t) (t t t)) t)"
let input = "x = (\\a : a)\nx " ++ not let input = "x = (\\a : a)\nx " ++ not
env = evalSapling Map.empty (parseSapling input) env = evalTricu Map.empty (parseTricu input)
result env @?= Fork (Fork (Stem Leaf) (Fork Leaf Leaf)) Leaf result env @?= Fork (Fork (Stem Leaf) (Fork Leaf Leaf)) Leaf
, testCase "Constant function matches" $ do , testCase "Constant function matches" $ do
let input = "k = (\\a b : a)\nk (t t) t" let input = "k = (\\a b : a)\nk (t t) t"
env = evalSapling Map.empty (parseSapling input) env = evalTricu Map.empty (parseTricu input)
result env @?= Stem Leaf result env @?= Stem Leaf
, testCase "Boolean AND_ TF" $ do , testCase "Boolean AND_ TF" $ do
let input = "and (t t) (t)" let input = "and (t t) (t)"
env = evalSapling library (parseSapling input) env = evalTricu library (parseTricu input)
result env @?= Leaf result env @?= Leaf
, testCase "Boolean AND_ FT" $ do , testCase "Boolean AND_ FT" $ do
let input = "and (t) (t t)" let input = "and (t) (t t)"
env = evalSapling library (parseSapling input) env = evalTricu library (parseTricu input)
result env @?= Leaf result env @?= Leaf
, testCase "Boolean AND_ FF" $ do , testCase "Boolean AND_ FF" $ do
let input = "and (t) (t)" let input = "and (t) (t)"
env = evalSapling library (parseSapling input) env = evalTricu library (parseTricu input)
result env @?= Leaf result env @?= Leaf
, testCase "Boolean AND_ TT" $ do , testCase "Boolean AND_ TT" $ do
let input = "and (t t) (t t)" let input = "and (t t) (t t)"
env = evalSapling library (parseSapling input) env = evalTricu library (parseTricu input)
result env @?= Stem Leaf result env @?= Stem Leaf
, testCase "Verifying Equality" $ do , testCase "Verifying Equality" $ do
let input = "equal (t t t) (t t t)" let input = "equal (t t t) (t t t)"
env = evalSapling library (parseSapling input) env = evalTricu library (parseTricu input)
result env @?= Stem Leaf result env @?= Stem Leaf
] ]
@ -252,67 +252,67 @@ lambdaEvalTests :: TestTree
lambdaEvalTests = testGroup "Lambda Evaluation Tests" lambdaEvalTests = testGroup "Lambda Evaluation Tests"
[ testCase "Lambda Identity Function" $ do [ testCase "Lambda Identity Function" $ do
let input = "id = (\\x : x)\nid t" let input = "id = (\\x : x)\nid t"
runSapling input @?= "Leaf" runTricu input @?= "Leaf"
, testCase "Lambda Constant Function (K combinator)" $ do , testCase "Lambda Constant Function (K combinator)" $ do
let input = "k = (\\x y : x)\nk t (t t)" let input = "k = (\\x y : x)\nk t (t t)"
runSapling input @?= "Leaf" runTricu input @?= "Leaf"
, testCase "Lambda Application with Variable" $ do , testCase "Lambda Application with Variable" $ do
let input = "id = (\\x : x)\nval = t t\nid val" let input = "id = (\\x : x)\nval = t t\nid val"
runSapling input @?= "Stem Leaf" runTricu input @?= "Stem Leaf"
, testCase "Lambda Application with Multiple Arguments" $ do , testCase "Lambda Application with Multiple Arguments" $ do
let input = "apply = (\\f x y : f x y)\nk = (\\a b : a)\napply k t (t t)" let input = "apply = (\\f x y : f x y)\nk = (\\a b : a)\napply k t (t t)"
runSapling input @?= "Leaf" runTricu input @?= "Leaf"
, testCase "Nested Lambda Application" $ do , testCase "Nested Lambda Application" $ do
let input = "apply = (\\f x y : f x y)\nid = (\\x : x)\napply (\\f x : f x) id t" let input = "apply = (\\f x y : f x y)\nid = (\\x : x)\napply (\\f x : f x) id t"
runSapling input @?= "Leaf" runTricu input @?= "Leaf"
, testCase "Lambda with a complex body" $ do , testCase "Lambda with a complex body" $ do
let input = "f = (\\x : t (t x))\nf t" let input = "f = (\\x : t (t x))\nf t"
runSapling input @?= "Stem (Stem Leaf)" runTricu input @?= "Stem (Stem Leaf)"
, testCase "Lambda returning a function" $ do , testCase "Lambda returning a function" $ do
let input = "f = (\\x : (\\y : x))\ng = f t\ng (t t)" let input = "f = (\\x : (\\y : x))\ng = f t\ng (t t)"
runSapling input @?= "Leaf" runTricu input @?= "Leaf"
, testCase "Lambda with Shadowing" $ do , testCase "Lambda with Shadowing" $ do
let input = "f = (\\x : (\\x : x))\nf t (t t)" let input = "f = (\\x : (\\x : x))\nf t (t t)"
runSapling input @?= "Stem Leaf" runTricu input @?= "Stem Leaf"
, testCase "Lambda returning another lambda" $ do , testCase "Lambda returning another lambda" $ do
let input = "k = (\\x : (\\y : x))\nk_app = k t\nk_app (t t)" let input = "k = (\\x : (\\y : x))\nk_app = k t\nk_app (t t)"
runSapling input @?= "Leaf" runTricu input @?= "Leaf"
, testCase "Lambda with free variables" $ do , testCase "Lambda with free variables" $ do
let input = "y = t t\nf = (\\x : y)\nf t" let input = "y = t t\nf = (\\x : y)\nf t"
runSapling input @?= "Stem Leaf" runTricu input @?= "Stem Leaf"
, testCase "SKI Composition" $ do , testCase "SKI Composition" $ do
let input = "s = (\\x y z : x z (y z))\nk = (\\x y : x)\ni = (\\x : x)\ncomp = s k i\ncomp t (t t)" let input = "s = (\\x y z : x z (y z))\nk = (\\x y : x)\ni = (\\x : x)\ncomp = s k i\ncomp t (t t)"
runSapling input @?= "Stem (Stem Leaf)" runTricu input @?= "Stem (Stem Leaf)"
, testCase "Lambda with multiple parameters and application" $ do , testCase "Lambda with multiple parameters and application" $ do
let input = "f = (\\a b c : t a b c)\nf t (t t) (t t t)" let input = "f = (\\a b c : t a b c)\nf t (t t) (t t t)"
runSapling input @?= "Stem Leaf" runTricu input @?= "Stem Leaf"
, testCase "Lambda with nested application in the body" $ do , testCase "Lambda with nested application in the body" $ do
let input = "f = (\\x : t (t (t x)))\nf t" let input = "f = (\\x : t (t (t x)))\nf t"
runSapling input @?= "Stem (Stem (Stem Leaf))" runTricu input @?= "Stem (Stem (Stem Leaf))"
, testCase "Lambda returning a function and applying it" $ do , testCase "Lambda returning a function and applying it" $ do
let input = "f = (\\x : (\\y : t x y))\ng = f t\ng (t t)" let input = "f = (\\x : (\\y : t x y))\ng = f t\ng (t t)"
runSapling input @?= "Fork Leaf (Stem Leaf)" runTricu input @?= "Fork Leaf (Stem Leaf)"
, testCase "Lambda applying a variable" $ do , testCase "Lambda applying a variable" $ do
let input = "id = (\\x : x)\na = t t\nid a" let input = "id = (\\x : x)\na = t t\nid a"
runSapling input @?= "Stem Leaf" runTricu input @?= "Stem Leaf"
, testCase "Nested lambda abstractions in the same expression" $ do , testCase "Nested lambda abstractions in the same expression" $ do
let input = "f = (\\x : (\\y : x y))\ng = (\\z : z)\nf g t" let input = "f = (\\x : (\\y : x y))\ng = (\\z : z)\nf g t"
runSapling input @?= "Leaf" runTricu input @?= "Leaf"
, testCase "Lambda with a string literal" $ do , testCase "Lambda with a string literal" $ do
let input = "f = (\\x : x)\nf \"hello\"" let input = "f = (\\x : x)\nf \"hello\""
runSapling input @?= "Fork (Fork Leaf (Fork Leaf (Fork Leaf (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) (Fork (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork Leaf (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) (Fork (Fork Leaf (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) (Fork (Fork Leaf (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) (Fork (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) Leaf))))" runTricu input @?= "Fork (Fork Leaf (Fork Leaf (Fork Leaf (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) (Fork (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork Leaf (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) (Fork (Fork Leaf (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) (Fork (Fork Leaf (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) (Fork (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork (Stem Leaf) Leaf))))))) Leaf))))"
, testCase "Lambda with an integer literal" $ do , testCase "Lambda with an integer literal" $ do
let input = "f = (\\x : x)\nf 42" let input = "f = (\\x : x)\nf 42"
runSapling input @?= "Fork Leaf (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) Leaf)))))" runTricu input @?= "Fork Leaf (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) (Fork Leaf (Fork (Stem Leaf) Leaf)))))"
, testCase "Lambda with a list literal" $ do , testCase "Lambda with a list literal" $ do
let input = "f = (\\x : x)\nf [t (t t)]" let input = "f = (\\x : x)\nf [t (t t)]"
runSapling input @?= "Fork Leaf (Fork (Stem Leaf) Leaf)" runTricu input @?= "Fork Leaf (Fork (Stem Leaf) Leaf)"
] ]
propertyTests :: TestTree propertyTests :: TestTree
propertyTests = testGroup "Property Tests" propertyTests = testGroup "Property Tests"
[ testProperty "Lexing and parsing round-trip" $ \input -> [ testProperty "Lexing and parsing round-trip" $ \input ->
case runParser saplingLexer "" input of case runParser tricuLexer "" input of
Left _ -> property True Left _ -> property True
Right tokens -> case runParser parseExpression "" tokens of Right tokens -> case runParser parseExpression "" tokens of
Left _ -> property True Left _ -> property True

View File

@ -1,6 +1,6 @@
cabal-version: 1.12 cabal-version: 1.12
name: sapling name: tricu
version: 0.4.0 version: 0.4.0
description: A micro-language for exploring Tree Calculus description: A micro-language for exploring Tree Calculus
author: James Eversole author: James Eversole
@ -12,7 +12,7 @@ build-type: Simple
extra-source-files: extra-source-files:
README.md README.md
executable sapling executable tricu
main-is: Main.hs main-is: Main.hs
hs-source-dirs: hs-source-dirs:
src src
@ -41,7 +41,7 @@ executable sapling
Research Research
default-language: Haskell2010 default-language: Haskell2010
test-suite sapling-tests test-suite tricu-tests
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
main-is: Spec.hs main-is: Spec.hs
hs-source-dirs: test, src hs-source-dirs: test, src