From e376d13a93cdf7cc32ca27d0f9b94736b292ea65 Mon Sep 17 00:00:00 2001 From: James Eversole Date: Sat, 28 Dec 2024 07:24:19 -0600 Subject: [PATCH] Stop using to/of conventions backwards --- src/Eval.hs | 6 +++--- src/Parser.hs | 6 +++--- src/REPL.hs | 6 +++--- src/Research.hs | 42 +++++++++++++++++++++--------------------- test/Spec.hs | 6 +++--- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Eval.hs b/src/Eval.hs index a76e5c0..3732342 100644 --- a/src/Eval.hs +++ b/src/Eval.hs @@ -48,9 +48,9 @@ evalAST env term = case term of TStem t -> Stem (evalAST env t) TFork t1 t2 -> Fork (evalAST env t1) (evalAST env t2) SApp t1 t2 -> apply (evalAST env t1) (evalAST env t2) - SStr str -> toString str - SInt num -> toNumber num - SList elems -> toList (map (evalAST Map.empty) elems) + SStr str -> ofString str + SInt num -> ofNumber num + SList elems -> ofList (map (evalAST Map.empty) elems) SFunc name args body -> error $ "Unexpected function definition " ++ name ++ " in evalAST; define via evalSingle." diff --git a/src/Parser.hs b/src/Parser.hs index f3ecc46..8ee22e8 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -74,10 +74,10 @@ parseVarWithoutAssignment = do parseLambda :: Parser SaplingAST parseLambda = between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) $ do satisfy (== LBackslash) - param <- satisfy isIdentifier - rest <- many (satisfy isIdentifier) + param <- satisfy isIdentifier + rest <- many (satisfy isIdentifier) satisfy (== LColon) - body <- parseLambdaExpression + body <- parseLambdaExpression let nestedLambda = foldr (\v acc -> SLambda [v] acc) body (map getIdentifier rest) return (SLambda [getIdentifier param] nestedLambda) diff --git a/src/REPL.hs b/src/REPL.hs index cfeb7e9..1347061 100644 --- a/src/REPL.hs +++ b/src/REPL.hs @@ -33,10 +33,10 @@ repl env = runInputT defaultSettings (loop env) loop newEnv decodeResult :: T -> String -decodeResult tc = case ofNumber tc of +decodeResult tc = case toNumber tc of Right num -> show num - Left _ -> case ofString tc of + Left _ -> case toString tc of Right str -> "\"" ++ str ++ "\"" - Left _ -> case ofList tc of + Left _ -> case toList tc of Right list -> "[" ++ intercalate ", " (map decodeResult list) ++ "]" Left _ -> "" diff --git a/src/Research.hs b/src/Research.hs index dcecd14..317b842 100644 --- a/src/Research.hs +++ b/src/Research.hs @@ -51,41 +51,41 @@ _not :: T _not = Fork (Fork _true (Fork Leaf _false)) Leaf -- Marshalling -toString :: String -> T -toString str = toList (map toNumber (map fromEnum str)) +ofString :: String -> T +ofString str = ofList (map ofNumber (map fromEnum str)) -toNumber :: Int -> T -toNumber 0 = Leaf -toNumber n = +ofNumber :: Int -> T +ofNumber 0 = Leaf +ofNumber n = Fork (if odd n then Stem Leaf else Leaf) - (toNumber (n `div` 2)) + (ofNumber (n `div` 2)) -toList :: [T] -> T -toList [] = Leaf -toList (x:xs) = Fork x (toList xs) +ofList :: [T] -> T +ofList [] = Leaf +ofList (x:xs) = Fork x (ofList xs) -ofNumber :: T -> Either String Int -ofNumber Leaf = Right 0 -ofNumber (Fork Leaf rest) = case ofNumber rest of +toNumber :: T -> Either String Int +toNumber Leaf = Right 0 +toNumber (Fork Leaf rest) = case toNumber rest of Right n -> Right (2 * n) Left err -> Left err -ofNumber (Fork (Stem Leaf) rest) = case ofNumber rest of +toNumber (Fork (Stem Leaf) rest) = case toNumber rest of Right n -> Right (1 + 2 * n) Left err -> Left err -ofNumber _ = Left "Invalid Tree Calculus number" +toNumber _ = Left "Invalid Tree Calculus number" -ofString :: T -> Either String String -ofString tc = case ofList tc of - Right list -> traverse (fmap toEnum . ofNumber) list +toString :: T -> Either String String +toString tc = case toList tc of + Right list -> traverse (fmap toEnum . toNumber) list Left err -> Left "Invalid Tree Calculus string" -ofList :: T -> Either String [T] -ofList Leaf = Right [] -ofList (Fork x rest) = case ofList rest of +toList :: T -> Either String [T] +toList Leaf = Right [] +toList (Fork x rest) = case toList rest of Right xs -> Right (x : xs) Left err -> Left err -ofList _ = Left "Invalid Tree Calculus list" +toList _ = Left "Invalid Tree Calculus list" -- Utility toAscii :: T -> String diff --git a/test/Spec.hs b/test/Spec.hs index 85f14bc..603bc7d 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -196,15 +196,15 @@ evaluationTests = testGroup "Evaluation Tests" , testCase "Evaluate string literal" $ do let input = "\"hello\"" let ast = parseSingle input - (result $ evalSingle Map.empty ast) @?= toString "hello" + (result $ evalSingle Map.empty ast) @?= ofString "hello" , testCase "Evaluate list literal" $ do let input = "[t (t t)]" let ast = parseSingle input - (result $ evalSingle Map.empty ast) @?= toList [Leaf, Stem Leaf] + (result $ evalSingle Map.empty ast) @?= ofList [Leaf, Stem Leaf] , testCase "Evaluate empty list" $ do let input = "[]" let ast = parseSingle input - (result $ evalSingle Map.empty ast) @?= toList [] + (result $ evalSingle Map.empty ast) @?= ofList [] , testCase "Evaluate variable dependency chain" $ do let input = "x = t (t t)\n \ \ y = x\n \