@@ -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 )
4748import Telomare.Resolver (main2Term3 , main2Term3let , process , resolveAllImports )
4849import Telomare.RunTime (rEval )
49- import Telomare.TypeChecker (TypeCheckError ( .. ), typeCheck )
50+ import Telomare.TypeChecker (typeCheck )
5051import Text.Megaparsec (errorBundlePretty , runParser )
5152
5253debug :: Bool
@@ -55,13 +56,6 @@ debug = False
5556debugTrace :: String -> a -> a
5657debugTrace 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-
6559convertPT :: (UnsizedRecursionToken -> Int ) -> Term3 -> Term4
6660convertPT 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
147143compileMain' :: 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
212207runMain_ :: [(String , String )] -- ^ All modules as (Module_Name, Module_Content)
213208 -> String -- ^ Module's name with `main` function
@@ -292,9 +287,9 @@ calculateRecursionLimits doCap t3 =
292287showSizingInSource :: String -> String -> String
293288showSizingInSource 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
321316showFunctionIndexesInSource 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
343338parseMain :: [(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.
346341parseMain prelude' str =
347342 let prelude :: [(String , [Either AnnotatedUPT (String , AnnotatedUPT )])]
348343 prelude = [(" Prelude" , Right <$> prelude')]
@@ -355,15 +350,15 @@ parseMain prelude' str =
355350
356351getAbortPossibilities :: String -> String -> Set IExpr
357352getAbortPossibilities 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
364359getAbortPossibilities' :: String -> String -> Set String
365360getAbortPossibilities' 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
372367eval2IExpr :: [(String , [Either AnnotatedUPT (String , AnnotatedUPT )])] -> String -> Either String IExpr
373368eval2IExpr 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 )
438433tagUPTwithIExpr 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
0 commit comments