Skip to content

Commit f9a39fd

Browse files
committed
Property and unit tests success. Also cleanup.
1 parent 883619d commit f9a39fd

8 files changed

Lines changed: 33 additions & 104 deletions

File tree

app/Evaluare.hs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# LANGUAGE RecursiveDo #-}
2-
31
module Main where
42

53
import Control.Comonad.Cofree (Cofree ((:<)))
@@ -66,7 +64,7 @@ node n0 = do
6664
tile ( fixed . pure . (+1) . T.length . _node_label $ n0) $ do
6765
grout flex . text . pure . _node_label $ n0
6866
pure ()
69-
value :: Dynamic t Bool <- tile (fixed 4) $ checkbox def $ _node_selected n0
67+
value :: Dynamic t Bool <- tile (fixed 4) . checkbox def $ _node_selected n0
7068
pure $ NodeOutput
7169
{ _nodeOutput_node = Node (_node_label n0) (_node_eval n0) <$> value
7270
, _nodeOutput_expand = updated value
@@ -150,9 +148,9 @@ nodify = removeExtraNumbers . fmap go . allNodes 0 where
150148
-> Cofree IExprF (Int, IExpr)
151149
-> [(Int, Cofree IExprF (Int, IExpr))]
152150
allNodes i = \case
153-
x@(_ :< ZeroF) -> (i, x) : []
154-
x@(_ :< EnvF) -> (i, x) : []
155-
x@(_ :< TraceF) -> (i, x) : []
151+
x@(_ :< ZeroF) -> [(i, x)]
152+
x@(_ :< EnvF) -> [(i, x)]
153+
x@(_ :< TraceF) -> [(i, x)]
156154
x@(_ :< (SetEnvF a)) -> (i, x) : allNodes (i + 1) a
157155
x@(_ :< (DeferF a)) -> (i, x) : allNodes (i + 1) a
158156
x@(_ :< (PLeftF a)) -> (i, x) : allNodes (i + 1) a

app/Main.hs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,18 @@
33
module Main where
44

55
import qualified Options.Applicative as O
6-
import qualified System.IO.Strict as Strict
7-
import Telomare.Eval (runMain)
86
import System.Directory (listDirectory)
97
import System.FilePath (takeBaseName, takeExtension)
8+
import qualified System.IO.Strict as Strict
9+
import Telomare.Eval (runMain)
1010

11-
data TelomareOpts = TelomareOpts
12-
{ telomareFile :: String
13-
-- , preludeFile :: String
14-
} deriving Show
11+
newtype TelomareOpts
12+
= TelomareOpts {telomareFile :: String}
13+
deriving Show
1514

1615
telomareOpts :: O.Parser TelomareOpts
1716
telomareOpts = TelomareOpts
1817
<$> O.argument O.str (O.metavar "TELOMARE-FILE")
19-
-- <*> O.strOption
20-
-- ( O.long "prelude"
21-
-- <> O.metavar "PRELUDE-FILE"
22-
-- <> O.showDefault
23-
-- <> O.value "./Prelude.tel"
24-
-- <> O.short 'p'
25-
-- <> O.help "Telomare prelude file" )
2618

2719
getAllModules :: IO [(String, String)]
2820
getAllModules = do
@@ -41,5 +33,4 @@ main = do
4133
<> O.progDesc "A simple but robust virtual machine" )
4234
topts <- O.execParser opts
4335
allModules :: [(String, String)] <- getAllModules
44-
-- putStrLn . show $ fst <$> allModules
4536
runMain allModules . takeBaseName . telomareFile $ topts

src/Telomare.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
module Telomare where
1717

18-
import Data.Functor.Classes (eq1)
1918
import Control.Applicative (Applicative (liftA2), liftA, liftA3)
2019
import Control.Comonad.Cofree (Cofree ((:<)))
2120
import qualified Control.Comonad.Trans.Cofree as CofreeT (CofreeF (..))
@@ -27,7 +26,7 @@ import qualified Control.Monad.State as State
2726
import Data.Bool (bool)
2827
import Data.Char (chr, ord)
2928
import Data.Eq.Deriving (deriveEq1)
30-
import Data.Functor.Classes (Eq1 (..), Eq2 (..), Show1 (..), Show2 (..))
29+
import Data.Functor.Classes (Eq1 (..), Eq2 (..), Show1 (..), Show2 (..), eq1)
3130
import Data.Functor.Foldable (Base, Corecursive (embed),
3231
Recursive (cata, project))
3332
import Data.Functor.Foldable.TH (MakeBaseFunctor (makeBaseFunctor))

src/Telomare/Eval.hs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import System.Process (CreateProcess (std_out), StdStream (CreatePipe),
2222
createProcess, shell)
2323
import Telomare
2424
import Telomare.Optimizer (optimize)
25-
import Telomare.Parser (AnnotatedUPT, parseOneExprOrTopLevelDefs, parseModule)
25+
import Telomare.Parser (AnnotatedUPT, parseModule, parseOneExprOrTopLevelDefs)
2626
import Telomare.Possible (AbortExpr, abortExprToTerm4, evalA, sizeTerm,
2727
term3ToUnsizedExpr)
2828
import Telomare.Resolver (main2Term3, process, resolveAllImports)
@@ -213,7 +213,7 @@ runMainCore modulesStrings s e =
213213
parsedModules = (fmap . fmap) parseModule modulesStrings
214214
parsedModulesErrors :: [(String, Either String [Either AnnotatedUPT (String, AnnotatedUPT)])]
215215
parsedModulesErrors = filter (\(moduleStr, parsed) -> case parsed of
216-
Left _ -> True
216+
Left _ -> True
217217
Right _ -> False)
218218
parsedModules
219219
flattenLeft = \case
@@ -233,15 +233,10 @@ runMainCore modulesStrings s e =
233233
in error . unlines $ joinModuleError <$> moduleWithError
234234

235235
in
236-
-- do
237-
-- putStrLn . show $ lookup "Prelude" modules
238-
-- putStrLn "--------------------------------------------------"
239-
-- putStrLn "--------------------------------------------------"
240-
-- putStrLn "--------------------------------------------------"
241-
case compileMain <$> main2Term3 modules s of
242-
Left e -> error $ concat ["failed to parse ", s, " ", e]
243-
Right (Right g) -> e g
244-
Right z -> error $ "compilation failed somehow, with result " <> show z
236+
case compileMain <$> main2Term3 modules s of
237+
Left e -> error $ concat ["failed to parse ", s, " ", e]
238+
Right (Right g) -> e g
239+
Right z -> error $ "compilation failed somehow, with result " <> show z
245240

246241
runMain_ :: [(String, String)] -> String -> IO String
247242
runMain_ modulesStrings s = runMainCore modulesStrings s evalLoop_
@@ -315,11 +310,6 @@ calculateRecursionLimits t3 =
315310
Left a -> Left . StaticCheckError . convertAbortMessage $ a
316311
Right t -> pure t
317312

318-
-- resolveAllImports :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])]
319-
-- -> [Either AnnotatedUPT (String, AnnotatedUPT)]
320-
-- -> [(String, AnnotatedUPT)]
321-
322-
323313
eval2IExpr :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])] -> String -> Either String IExpr
324314
eval2IExpr extraModuleBindings str =
325315
first errorBundlePretty (runParser (parseOneExprOrTopLevelDefs resolved) "" str)

src/Telomare/Parser.hs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,6 @@ parseImportQualified = do
343343
qualifier <- identifier <* scn
344344
pure $ x :< ImportQualifiedUPF qualifier m
345345

346-
-- resolveMain :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])] -- ^Modules: [(ModuleName, [Either Import (VariableName, BindedUPT)])]
347-
-- -> String -- ^Module name with main
348-
-- -> Either String AnnotatedUPT
349-
350-
351346
-- |Parse top level expressions.
352347
parseTopLevelWithExtraModuleBindings :: [(String, AnnotatedUPT)]
353348
-> TelomareParser AnnotatedUPT
@@ -356,17 +351,6 @@ parseTopLevelWithExtraModuleBindings lst = do
356351
bindingList <- scn *> many parseAssignment <* eof
357352
pure $ x :< LetUPF (lst <> bindingList) (fromJust $ lookup "main" bindingList)
358353

359-
360-
-- -- |Parse top level expressions.
361-
-- parseTopLevelWithExtraModuleBindings :: [(String, AnnotatedUPT)] -- ^Extra Module Bindings
362-
-- -> TelomareParser AnnotatedUPT
363-
-- parseTopLevelWithExtraModuleBindings mb = do
364-
-- x <- getLineColumn
365-
-- importList' <- scn *> many (try parseImportQualified <|> try parseImport) <* scn
366-
-- let importList = undefined -- concat $ resolveImports mb <$> importList'
367-
-- bindingList <- scn *> many parseAssignment <* eof
368-
-- pure $ x :< LetUPF (importList <> bindingList) (fromJust $ lookup "main" bindingList)
369-
370354
parseDefinitions :: TelomareParser (AnnotatedUPT -> AnnotatedUPT)
371355
parseDefinitions = do
372356
x <- getLineColumn
@@ -381,10 +365,6 @@ runTelomareParser_ parser str = runTelomareParser parser str >>= print
381365
runTelomareParserWDebug :: Show a => TelomareParser a -> String -> IO ()
382366
runTelomareParserWDebug parser str = runTelomareParser (dbg "debug" parser) str >>= print
383367

384-
-- modulesAux :: [(String, [(String, AnnotatedUPT)])]
385-
-- modulesAux = [("Prelude",[("id", DummyLoc :< LamUPF "x" (DummyLoc :< VarUPF "x")), ("id2", DummyLoc :< LamUPF "x2" (DummyLoc :< VarUPF "x2"))])]
386-
-- parseImportOrAssignment
387-
388368
-- |Helper function to test Telomare parsers with any result.
389369
runTelomareParser :: Monad m => TelomareParser a -> String -> m a
390370
runTelomareParser parser str =
@@ -421,18 +401,6 @@ parseImportOrAssignment = do
421401
Just a -> pure $ Right a
422402
Just imp -> pure $ Left imp
423403

424-
input = unlines
425-
[ "import Prelude"
426-
, "foo = bar"
427-
, "baz = wee"
428-
, "import Foo"
429-
, "main = id"
430-
]
431-
aux = runTelomareParserWDebug (scn *> many parseImportOrAssignment <* eof) input
432-
433-
-- parseTopLevelWithExtraModuleBindings :: [(String, AnnotatedUPT)]
434-
-- -> TelomareParser AnnotatedUPT
435-
436404
parseWithPrelude :: [(String, AnnotatedUPT)] -- ^Prelude
437405
-> String -- ^Raw string to be parsed
438406
-> Either String AnnotatedUPT -- ^Error on Left

src/Telomare/Resolver.hs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -446,20 +446,6 @@ runTelomareParser2Term2 :: TelomareParser AnnotatedUPT -- ^Parser to run
446446
runTelomareParser2Term2 parser str =
447447
first errorBundlePretty (runParser parser "" str) >>= process2Term2
448448

449-
-- resolveImport :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])]
450-
-- -> AnnotatedUPT -- ^Import UPT ----- REMOVE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
451-
-- -> [Either AnnotatedUPT (String, AnnotatedUPT)] -- List with just Right
452-
-- resolveImport modules = \case
453-
-- (_ :< (ImportUPF var)) ->
454-
-- case lookup var modules of
455-
-- Nothing -> error $ "Import error from " <> var
456-
-- Just x -> x
457-
-- (_ :< (ImportQualifiedUPF q v)) ->
458-
-- case lookup v modules of
459-
-- Nothing -> error $ "Import error from " <> v
460-
-- Just x-> (fmap . fmap . first) (\str -> q <> "." <> str) x
461-
-- _ -> error "Expected import statement"
462-
463449
resolveImports' :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])]
464450
-> [Either AnnotatedUPT (String, AnnotatedUPT)] -- ^Main module with both Import and Assignment
465451
-> [Either AnnotatedUPT (String, AnnotatedUPT)]
@@ -472,17 +458,17 @@ resolveImports' modules xs = lefts <> rights
472458
(Left (_ :< (ImportUPF var))) ->
473459
case lookup var modules of
474460
Nothing -> error $ "Import error from " <> var
475-
Just x -> x
461+
Just x -> x
476462
(Left (_ :< (ImportQualifiedUPF q v))) ->
477463
case lookup v modules of
478464
Nothing -> error $ "Import error from " <> v
479-
Just x-> (fmap . fmap . first) (\str -> q <> "." <> str) x
465+
Just x -> (fmap . fmap . first) (\str -> q <> "." <> str) x
480466
e -> error $ "Expected import statement. Got:\n" <> show e
481467
rights = filter isRight xs
482468
isLeft (Left _) = True
483-
isLeft _ = False
469+
isLeft _ = False
484470
isRight (Right _) = True
485-
isRight _ = False
471+
isRight _ = False
486472

487473
resolveAllImports' :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])]
488474
-> [Either AnnotatedUPT (String, AnnotatedUPT)]
@@ -509,7 +495,7 @@ resolveImports modules moduleName = resolveAllImports modules principal
509495
where
510496
principal = case lookup moduleName modules of
511497
Nothing -> error $ "resolveImports: Module " <> moduleName <> " not found"
512-
Just x -> x
498+
Just x -> x
513499

514500
resolveMain :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])] -- ^Modules: [(ModuleName, [Either Import (VariableName, BindedUPT)])]
515501
-> String -- ^Module name with main
@@ -518,13 +504,10 @@ resolveMain allModules mainModule = case lookup mainModule allModules of
518504
Nothing -> Left $ "Module " <> mainModule <> " not found"
519505
Just lst -> let resolved :: [(String, AnnotatedUPT)]
520506
resolved = resolveImports allModules mainModule
521-
-- resolvedPretty :: [(String, UnprocessedParsedTerm)]
522-
-- resolvedPretty = (fmap . fmap) forget resolved
523507
maybeMain = lookup "main" resolved
524508
in case maybeMain of
525509
Nothing -> Left $ "No main function found in " <> mainModule
526-
Just x -> Right $ DummyLoc :< LetUPF resolved x
527-
-- Just x -> Right $ DummyLoc :< LetUPF (trace ("!!!!!!@@@@@@@!!!!!!!! " <> (show resolvedPretty)) resolved) x
510+
Just x -> Right $ DummyLoc :< LetUPF resolved x
528511

529512
main2Term3 :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])] -- ^Modules: [(ModuleName, [Either Import (VariableName, BindedUPT)])]
530513
-> String -- ^Module name with main

test/ResolverTests.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ qcProps = testGroup "Property tests (QuickCheck)"
5555
, QC.testProperty "Check recursive imports work" $
5656
\() -> withMaxSuccess 16 . QC.idempotentIOProperty $ do
5757
modules <- generate genRecursiveImports
58-
let expectedValue = "bla"
58+
let expectedValue = recursiveImportsResult modules <> "\ndone"
5959
result <- runMain_ modules "Main"
6060
pure $ result === expectedValue
6161
]
6262

63+
recursiveImportsResult :: [(String, String)] -> String
64+
recursiveImportsResult = init . tail . drop 2 . dropWhile (/= '=') . snd . last
65+
6366
-- Variable and Import str generator
6467
genName :: Gen String
6568
genName = do
@@ -78,7 +81,7 @@ genInteger = choose (0, 100)
7881
genAssignment :: Gen (String, String)
7982
genAssignment = do
8083
varName <- genName
81-
value <- genInteger
84+
value <- genName
8285
pure (varName, varName <> " = " <> show value)
8386

8487
genImport :: Gen String
@@ -92,8 +95,8 @@ genRecursiveImports = do
9295
moduleNames <- vectorOf numModules genName
9396
(varName, assignmentStr) <- genAssignment
9497
let
95-
assignments = map ( "import " <> ) (tail moduleNames) <> [assignmentStr]
96-
mainModule = ("Main", "import " <> head moduleNames <> "\nmain = " <> varName)
98+
assignments = fmap ( "import " <> ) (tail moduleNames) <> [assignmentStr]
99+
mainModule = ("Main", "import " <> head moduleNames <> "\nmain = \\input -> (" <> varName <> ",0)")
97100
pure $ mainModule : zip moduleNames assignments
98101

99102
aux222 =
@@ -212,6 +215,9 @@ unitTests = testGroup "Unit tests"
212215
, testCase "test tictactoe.tel" $ do
213216
res <- tictactoe
214217
res @?= fullRunTicTacToeString
218+
, testCase "test recursive imports" $ do
219+
res <- runMain_ aux222 "Main"
220+
res @?= "whattt\ndone"
215221
]
216222

217223
tictactoe :: IO String

test/Spec.hs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,6 @@ unitTestSameResult' parse a b = it ("comparing to " <> a) $ case (parse a, parse
798798
_ -> expectationFailure "unitTestSameResult failed parsing somewhere"
799799
-}
800800

801-
-- main2Term3 :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])] -- ^Modules: [(ModuleName, [Either Import (VariableName, BindedUPT)])]
802-
-- -> String -- ^Module name with main
803-
-- -> Either String Term3 -- ^Error on Left
804-
805-
-- parseModule :: String -> Either String [Either AnnotatedUPT (String, AnnotatedUPT)]
806-
807801
main = do
808802
preludeFile <- Strict.readFile "Prelude.tel"
809803

@@ -816,7 +810,7 @@ main = do
816810
parseAuxModule :: String -> (String, [Either AnnotatedUPT (String, AnnotatedUPT)])
817811
parseAuxModule str =
818812
case sequence ("AuxModule", parseModule ("import Prelude\n" <> str)) of
819-
Left e -> error $ show e
813+
Left e -> error $ show e
820814
Right pam -> pam
821815
parse :: String -> Either String Term3
822816
parse str = main2Term3 (parseAuxModule str:prelude) "AuxModule"

0 commit comments

Comments
 (0)