Skip to content

Commit 52bc4ad

Browse files
committed
fixed tests, except for arithmetic test
1 parent b3726ca commit 52bc4ad

10 files changed

Lines changed: 115 additions & 95 deletions

File tree

src/Telomare.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,28 @@ instance NFData IExpr where
476476
rnf (PRight e) = rnf e
477477
rnf Trace = ()
478478

479+
data TypeCheckError
480+
= UnboundType Int
481+
| InconsistentTypes PartialType PartialType
482+
| RecursiveType Int
483+
deriving (Eq, Ord, Show)
484+
485+
data ResolverError
486+
= NoMainFunction String
487+
| ModuleNotFound String
488+
| DefinitionCycle [String]
489+
| MissingDefinitions [String]
490+
| ParseError String
491+
deriving (Eq, Ord, Show)
492+
493+
data EvalError = RTE RunTimeError
494+
| TCE TypeCheckError
495+
| RE ResolverError
496+
| StaticCheckError String
497+
| CompileConversionError
498+
| RecursionLimitError UnsizedRecursionToken
499+
deriving (Eq, Ord, Show)
500+
479501
data RunTimeError
480502
= AbortRunTime IExpr
481503
| GenericRunTimeError String IExpr

src/Telomare/Eval.hs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ import Telomare (AbstractRunTime, BreakState, BreakState', ExprA (..),
3030
FragExpr (..), FragExprF (..), FragIndex (FragIndex),
3131
IExpr (..), IExprF (..), LocTag (..), PartialType (..),
3232
Pattern, RecursionPieceFrag, RecursionSimulationPieces (..),
33-
RunTimeError (..), TelomareLike (..), Term3 (Term3),
34-
Term4 (Term4), UnprocessedParsedTerm (..),
33+
RunTimeError (..), TelomareLike (..), Term2, Term3 (Term3),
34+
Term4 (Term4), UnprocessedParsedTerm (..), EvalError (..),
35+
ResolverError (..),
3536
UnprocessedParsedTermF (..), UnsizedRecursionToken (..), app,
3637
appF, convertAbortMessage, deferF, eval, forget, g2s,
3738
innerChurchF, insertAndGetKey, pairF, rootFrag, s2g, setEnvF,
@@ -46,7 +47,7 @@ import Telomare.PossibleData (AbortExpr, CompiledExpr (..), SizedRecursion (..),
4647
rightB, setEnvB)
4748
import Telomare.Resolver (main2Term3, main2Term3let, process, resolveAllImports)
4849
import Telomare.RunTime (rEval)
49-
import Telomare.TypeChecker (TypeCheckError (..), typeCheck)
50+
import Telomare.TypeChecker (typeCheck)
5051
import Text.Megaparsec (errorBundlePretty, runParser)
5152

5253
debug :: Bool
@@ -55,13 +56,6 @@ debug = False
5556
debugTrace :: String -> a -> a
5657
debugTrace s x = if debug then trace s x else x
5758

58-
data EvalError = RTE RunTimeError
59-
| TCE TypeCheckError
60-
| StaticCheckError String
61-
| CompileConversionError
62-
| RecursionLimitError UnsizedRecursionToken
63-
deriving (Eq, Ord, Show)
64-
6559
convertPT :: (UnsizedRecursionToken -> Int) -> Term3 -> Term4
6660
convertPT ll (Term3 termMap) =
6761
let unURedMap = Map.map unFragExprUR termMap
@@ -112,7 +106,7 @@ findChurchSizeD so t3 = case so of
112106
-- _ -> pure (convertPT (const 255) t3)
113107
NoSizing -> pure (convertPT (const 255) t3)
114108
UnitTestSizing -> calculateRecursionLimits False t3
115-
MainSizing -> calculateRecursionLimits True t3
109+
MainSizing -> pure (convertPT (const 255) t3) -- calculateRecursionLimits True t3
116110

117111
-- rather than remove checks, we should extract them so that they can be run separately, if that gives a performance benefit
118112
{-
@@ -138,10 +132,12 @@ runStaticChecks t@(Term4 termMap) =
138132
Nothing -> pure t
139133
Just e -> Left . StaticCheckError $ convertAbortMessage e
140134

141-
compileMain :: Term3 -> Either EvalError CompiledExpr
142-
compileMain term = case typeCheck (PairTypeP (ArrTypeP ZeroTypeP ZeroTypeP) AnyType) term of
143-
Just e -> Left $ TCE e
144-
_ -> compile MainSizing pure term -- TODO add runStaticChecks back in
135+
compileMain :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])] -> String -> Either EvalError CompiledExpr
136+
compileMain modules term = do
137+
tcTerm <- first RE $ main2Term3 modules term
138+
case typeCheck (PairTypeP (ArrTypeP ZeroTypeP ZeroTypeP) AnyType) tcTerm of
139+
Just e -> Left $ TCE e
140+
_ -> first RE (main2Term3let modules term) >>= compile MainSizing pure
145141

146142
-- for testing
147143
compileMain' :: Term3 -> Either EvalError CompiledExpr
@@ -204,10 +200,9 @@ runMainCore modulesStrings s e =
204200
in error . unlines $ joinModuleError <$> moduleWithError
205201

206202
in
207-
case compileMain <$> main2Term3let modules s of
208-
Left e -> error $ concat ["failed to parse ", s, " ", e]
209-
Right (Right g) -> e g
210-
Right (Left z) -> error $ "compilation failed somehow, with result:\n" <> show z
203+
case compileMain modules s of
204+
Left e -> error $ concat ["runMainCore failed: ", show e]
205+
Right g -> e g
211206

212207
runMain_ :: [(String, String)] -- ^All modules as (Module_Name, Module_Content)
213208
-> String -- ^Module's name with `main` function
@@ -292,9 +287,9 @@ calculateRecursionLimits doCap t3 =
292287
showSizingInSource :: String -> String -> String
293288
showSizingInSource prelude s
294289
= let asLines = zip [0..] $ lines s
295-
parsed = parsePrelude prelude >>= (`parseMain` s)
296-
unsizedExpr = term3ToUnsizedExpr 256 <$> parsed
297-
sizedRecursion = unsizedExpr >>= (first (("Could not size token: " <>) . show) . getSizesM 256)
290+
parsed = first ParseError (parsePrelude prelude) >>= (`parseMain` s)
291+
unsizedExpr = term3ToUnsizedExpr 256 <$> first RE parsed
292+
sizedRecursion = unsizedExpr >>= (first RecursionLimitError . getSizesM 256)
298293
sizeLocs = error "TODO showSizingInSource implement sizeLocs" --Map.toAscList . buildUnsizedLocMap <$> unsizedExpr
299294
-- (orphanLocs, lineLocs) = partition ((== DummyLoc) . snd) sizeLocs
300295
(orphanLocs, lineLocs) = case sizeLocs of
@@ -321,7 +316,7 @@ showFunctionIndexesInSource :: String -> String -> String
321316
showFunctionIndexesInSource prelude s
322317
= let asLines = zip [1..] $ lines s
323318
-- parsed = parsePrelude prelude >>= (\p -> parseMain p s)
324-
funMap = case parsePrelude prelude >>= (`parseMain` s) of
319+
funMap = case first ParseError (parsePrelude prelude) >>= (`parseMain` s) of
325320
Right (Term3 f) -> f
326321
e -> error ("could not parse " <> show e)
327322
unAss (a :< _) = a
@@ -342,7 +337,7 @@ showFunctionIndexesInSource prelude s
342337

343338
parseMain :: [(String, AnnotatedUPT)] -- ^Prelude: [(VariableName, BindedUPT)]
344339
-> String -- ^Raw string to be parserd.
345-
-> Either String Term3 -- ^Error on Left.
340+
-> Either ResolverError Term3 -- ^Error on Left.
346341
parseMain prelude' str =
347342
let prelude :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])]
348343
prelude = [("Prelude", Right <$> prelude')]
@@ -355,15 +350,15 @@ parseMain prelude' str =
355350

356351
getAbortPossibilities :: String -> String -> Set IExpr
357352
getAbortPossibilities prelude s
358-
= let parsed = parsePrelude prelude >>= (`parseMain` s)
353+
= let parsed = first ParseError (parsePrelude prelude) >>= (`parseMain` s)
359354
unsizedExpr = term3ToUnsizedExpr 256 <$> parsed
360355
in case unsizedExpr of
361356
Left e -> error $ "getAbortPossibilities: " <> show e
362357
Right ue -> abortPossibilities 256 ue
363358

364359
getAbortPossibilities' :: String -> String -> Set String
365360
getAbortPossibilities' prelude s
366-
= let parsed = parsePrelude prelude >>= (`parseMain` s)
361+
= let parsed = first ParseError (parsePrelude prelude) >>= (`parseMain` s)
367362
unsizedExpr = term3ToUnsizedExpr 256 <$> parsed
368363
in case unsizedExpr of
369364
Left e -> error $ "getAbortPossibilities: " <> show e
@@ -372,11 +367,11 @@ getAbortPossibilities' prelude s
372367
eval2IExpr :: [(String, [Either AnnotatedUPT (String, AnnotatedUPT)])] -> String -> Either String IExpr
373368
eval2IExpr extraModuleBindings str =
374369
first errorBundlePretty (runParser (parseOneExprOrTopLevelDefs resolved) "" str)
375-
>>= process
370+
>>= first show . process
376371
>>= tt . first show . compileUnitTest
377372
where
378373
tt = \case
379-
Left e -> Left e
374+
Left e -> Left $ show e
380375
Right x -> case toTelomare x of
381376
Just ie -> pure ie
382377
_ -> Left $ "eval2IExpr conversion error back to iexpr:\n" <> prettyPrint x
@@ -437,7 +432,7 @@ tagUPTwithIExpr :: [(String, AnnotatedUPT)]
437432
-> Cofree UnprocessedParsedTermF (Int, Either String IExpr)
438433
tagUPTwithIExpr prelude upt = evalState (para alg upt) 0 where
439434
upt2iexpr :: UnprocessedParsedTerm -> Either String IExpr
440-
upt2iexpr u = process (tag DummyLoc u) >>= tt . first show . compileUnitTest
435+
upt2iexpr u = first show (process (tag DummyLoc u)) >>= tt . first show . compileUnitTest
441436
tt = \case
442437
Left e -> Left e
443438
Right x -> case toTelomare x of

src/Telomare/Possible.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,8 @@ sizeTermM maxSize doCap x = tidyUp . ($ []) . runReaderT . transformNoDeferM eva
11181118
setSizes :: Map UnsizedRecursionToken (Maybe Int) -> UnsizedExpr -> UnsizedExpr
11191119
setSizes sizeMap = cata $ \case
11201120
UnsizedFW us@(UnsizedStubF tok _) -> case Map.lookup tok sizeMap of
1121-
Just (Just n) -> trace ("sizeTermM setting size: " <> show (tok, n)) iterate (basicEE . SetEnvSF) envB !! (n + 1)
1122-
_ -> trace ("no size found for " <> show tok) setEnvB $ leftB envB
1121+
Just (Just n) -> debugTrace ("sizeTermM setting size: " <> show (tok, n)) iterate (basicEE . SetEnvSF) envB !! (n + 1)
1122+
_ -> debugTrace ("no size found for " <> show tok) setEnvB $ leftB envB
11231123
UnsizedFW (TraceF _ x) -> x
11241124
x -> embed x
11251125
foldAborted = cata f where

0 commit comments

Comments
 (0)