{-# LANGUAGE OverloadedStrings #-}
module Funcons.Tools (
mkMain, mkMainWithLibrary, mkMainWithLibraryEntities,
mkMainWithLibraryTypes, mkMainWithLibraryEntitiesTypes,
mkFullyFreshInterpreter, mkFreshInterpreter,
run, runWithExtensions, runWithExtensionsNoCore, runWithExtensionsNoNothing,
FunconLibrary, libEmpty, libUnion, libOverride, libUnions, libOverrides, libFromList,
TypeRelation, DataTypeMembers(..), DataTypeAltt(..), TypeParam,
emptyTypeRelation, typeEnvUnion, typeEnvUnions, typeEnvFromList,
EntityDefaults, EntityDefault(..), noEntityDefaults,
) where
import Funcons.EDSL (library)
import Funcons.RunOptions
import Funcons.Types
import Funcons.Entities
import Funcons.MSOS
import Funcons.Core.Library
import Funcons.Core.Manual
import Funcons.Printer
import Funcons.Parser
import System.Environment (getArgs)
import Data.Text (unpack)
import Data.List ((\\), intercalate)
import qualified Data.Map as M
import Control.Monad (forM_, when, unless,join)
noEntityDefaults :: [EntityDefault]
noEntityDefaults :: [EntityDefault]
noEntityDefaults = []
mkMain :: IO ()
mkMain :: IO ()
mkMain = FunconLibrary -> IO ()
mkMainWithLibrary FunconLibrary
libEmpty
mkMainWithLibrary :: FunconLibrary -> IO()
mkMainWithLibrary :: FunconLibrary -> IO ()
mkMainWithLibrary FunconLibrary
lib = FunconLibrary -> [EntityDefault] -> IO ()
mkMainWithLibraryEntities FunconLibrary
lib []
mkMainWithLibraryEntities :: FunconLibrary -> EntityDefaults -> IO ()
mkMainWithLibraryEntities :: FunconLibrary -> [EntityDefault] -> IO ()
mkMainWithLibraryEntities FunconLibrary
lib [EntityDefault]
ents =
FunconLibrary -> [EntityDefault] -> TypeRelation -> IO ()
mkMainWithLibraryEntitiesTypes FunconLibrary
lib [EntityDefault]
ents TypeRelation
emptyTypeRelation
mkMainWithLibraryTypes :: FunconLibrary -> TypeRelation -> IO ()
mkMainWithLibraryTypes :: FunconLibrary -> TypeRelation -> IO ()
mkMainWithLibraryTypes FunconLibrary
lib TypeRelation
tys = FunconLibrary -> [EntityDefault] -> TypeRelation -> IO ()
mkMainWithLibraryEntitiesTypes FunconLibrary
lib [] TypeRelation
tys
mkMainWithLibraryEntitiesTypes :: FunconLibrary -> EntityDefaults -> TypeRelation -> IO ()
mkMainWithLibraryEntitiesTypes :: FunconLibrary -> [EntityDefault] -> TypeRelation -> IO ()
mkMainWithLibraryEntitiesTypes FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv = do
[String]
args <- IO [String]
getArgs
FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensions FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv [String]
args Maybe Funcons
forall a. Maybe a
Nothing
mkFullyFreshInterpreter :: FunconLibrary -> EntityDefaults -> TypeRelation -> IO ()
mkFullyFreshInterpreter :: FunconLibrary -> [EntityDefault] -> TypeRelation -> IO ()
mkFullyFreshInterpreter FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv = do
[String]
args <- IO [String]
getArgs
FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensionsNoNothing FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv [String]
args Maybe Funcons
forall a. Maybe a
Nothing
mkFreshInterpreter :: FunconLibrary -> EntityDefaults -> TypeRelation -> IO ()
mkFreshInterpreter :: FunconLibrary -> [EntityDefault] -> TypeRelation -> IO ()
mkFreshInterpreter FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv = do
[String]
args <- IO [String]
getArgs
FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensionsNoCore FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv [String]
args Maybe Funcons
forall a. Maybe a
Nothing
runWithExtensions ::
FunconLibrary -> EntityDefaults -> TypeRelation -> [String] -> Maybe Funcons -> IO ()
runWithExtensions :: FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensions FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv =
FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensionsNoCore
([FunconLibrary] -> FunconLibrary
libUnions [FunconLibrary
Funcons.Core.Library.funcons, FunconLibrary
lib])
([[EntityDefault]] -> [EntityDefault]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[EntityDefault]
defaults, [EntityDefault]
forall a. [a]
Funcons.Core.Library.entities])
([TypeRelation] -> TypeRelation
typeEnvUnions [TypeRelation
tyenv, TypeRelation
Funcons.Core.Library.types])
runWithExtensionsNoCore ::
FunconLibrary -> EntityDefaults -> TypeRelation -> [String] -> Maybe Funcons -> IO ()
runWithExtensionsNoCore :: FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensionsNoCore FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv = FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensionsNoNothing FunconLibrary
full_lib [EntityDefault]
defaults TypeRelation
tyenv
where full_lib :: FunconLibrary
full_lib = [FunconLibrary] -> FunconLibrary
libUnions [FunconLibrary
lib
,FunconLibrary
Funcons.EDSL.library
,FunconLibrary
Funcons.Core.Manual.library]
runWithExtensionsNoNothing ::
FunconLibrary -> EntityDefaults -> TypeRelation -> [String] -> Maybe Funcons -> IO ()
runWithExtensionsNoNothing :: FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensionsNoNothing FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv = FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
emulate FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv
run :: [String] -> Maybe Funcons -> IO ()
run :: [String] -> Maybe Funcons -> IO ()
run = FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
runWithExtensions FunconLibrary
libEmpty [] TypeRelation
emptyTypeRelation
emulate :: FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> [String]
-> Maybe Funcons
-> IO ()
emulate FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv [String]
args Maybe Funcons
mf0 = do
(RunOptions
opts, [String]
unknown_opts) <- [String] -> IO (RunOptions, [String])
run_options [String]
args
[String] -> (String -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [String]
unknown_opts ((String -> IO ()) -> IO ()) -> (String -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \String
arg -> do
String -> IO ()
putStrLn (String
"unknown option: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
arg)
case RunOptions -> Bool
interactive_mode RunOptions
opts of
Bool
True -> (Name -> IO Funcons)
-> FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> RunOptions
-> Maybe Funcons
-> IO ()
forall (m :: * -> *).
Interactive m =>
(Name -> m Funcons)
-> FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> RunOptions
-> Maybe Funcons
-> IO ()
emulate' (Bool -> Name -> IO Funcons
forall (m :: * -> *). Interactive m => Bool -> Name -> m Funcons
fread (RunOptions -> Bool
string_inputs RunOptions
opts) :: Name -> IO Funcons) FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv RunOptions
opts Maybe Funcons
mf0
Bool
False -> (Name -> StateT InputValues Identity Funcons)
-> FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> RunOptions
-> Maybe Funcons
-> IO ()
forall (m :: * -> *).
Interactive m =>
(Name -> m Funcons)
-> FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> RunOptions
-> Maybe Funcons
-> IO ()
emulate' (Bool -> Name -> StateT InputValues Identity Funcons
forall (m :: * -> *). Interactive m => Bool -> Name -> m Funcons
fread (RunOptions -> Bool
string_inputs RunOptions
opts) :: Name -> SimIO Funcons) FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv RunOptions
opts Maybe Funcons
mf0
emulate' :: Interactive m => (Name -> m Funcons) ->
FunconLibrary -> EntityDefaults -> TypeRelation -> RunOptions -> Maybe Funcons -> IO ()
emulate' :: (Name -> m Funcons)
-> FunconLibrary
-> [EntityDefault]
-> TypeRelation
-> RunOptions
-> Maybe Funcons
-> IO ()
emulate' Name -> m Funcons
reader FunconLibrary
lib [EntityDefault]
defaults TypeRelation
tyenv RunOptions
opts Maybe Funcons
mf0 = do
let f0 :: Funcons
f0 = Funcons -> (Funcons -> Funcons) -> Maybe Funcons -> Funcons
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (RunOptions -> Funcons
funcon_term RunOptions
opts) Funcons -> Funcons
forall a. a -> a
id Maybe Funcons
mf0
msos_ctxt :: MSOSReader m
msos_ctxt = RewriteReader
-> InputValues
-> DownControl
-> (Name -> m Funcons)
-> MSOSReader m
forall (m :: * -> *).
RewriteReader
-> InputValues
-> DownControl
-> (Name -> m Funcons)
-> MSOSReader m
MSOSReader (FunconLibrary
-> TypeRelation
-> RunOptions
-> Funcons
-> Funcons
-> RewriteReader
RewriteReader FunconLibrary
lib TypeRelation
tyenv RunOptions
opts Funcons
f0 Funcons
f0) InputValues
emptyINH DownControl
emptyDCTRL Name -> m Funcons
reader
((Either IException StepRes
e_exc_f, MSOSState m
mut, MSOSWriter
wr), InputValues
rem_ins) <-
m (Either IException StepRes, MSOSState m, MSOSWriter)
-> InputValues
-> IO
((Either IException StepRes, MSOSState m, MSOSWriter), InputValues)
forall (m :: * -> *) a.
Interactive m =>
m a -> InputValues -> IO (a, InputValues)
fexec (MSOS StepRes
-> MSOSReader m
-> MSOSState m
-> m (Either IException StepRes, MSOSState m, MSOSWriter)
forall a.
MSOS a
-> forall (m :: * -> *).
Interactive m =>
MSOSReader m
-> MSOSState m -> m (Either IException a, MSOSState m, MSOSWriter)
runMSOS ([EntityDefault] -> MSOS StepRes -> MSOS StepRes
setEntityDefaults [EntityDefault]
defaults (RunOptions -> Int -> StepRes -> MSOS StepRes
stepTrans RunOptions
opts Int
0 (Funcons -> StepRes
toStepRes Funcons
f0)))
MSOSReader m
msos_ctxt (MSOSState Any
forall (m :: * -> *). MSOSState m
emptyMSOSState {inp_es :: Input m
inp_es = Input m
forall a. Map Name ([a], Maybe (m Funcons))
inputs})) (RunOptions -> InputValues
inputValues RunOptions
opts)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (RunOptions -> Bool
interactive_mode RunOptions
opts)
([EntityDefault]
-> MSOSReader m
-> Either IException StepRes
-> MSOSState m
-> MSOSWriter
-> InputValues
-> IO ()
forall (m :: * -> *).
[EntityDefault]
-> MSOSReader m
-> Either IException StepRes
-> MSOSState m
-> MSOSWriter
-> InputValues
-> IO ()
withResults [EntityDefault]
defaults MSOSReader m
msos_ctxt Either IException StepRes
e_exc_f MSOSState m
mut MSOSWriter
wr InputValues
rem_ins)
where inputs :: Map Name ([a], Maybe (m Funcons))
inputs = (Name
-> [Values]
-> Map Name ([a], Maybe (m Funcons))
-> Map Name ([a], Maybe (m Funcons)))
-> Map Name ([a], Maybe (m Funcons))
-> InputValues
-> Map Name ([a], Maybe (m Funcons))
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey Name
-> [Values]
-> Map Name ([a], Maybe (m Funcons))
-> Map Name ([a], Maybe (m Funcons))
forall p a.
Name
-> p
-> Map Name ([a], Maybe (m Funcons))
-> Map Name ([a], Maybe (m Funcons))
op Map Name ([a], Maybe (m Funcons))
forall k a. Map k a
M.empty (RunOptions -> InputValues
inputValues RunOptions
opts)
where op :: Name
-> p
-> Map Name ([a], Maybe (m Funcons))
-> Map Name ([a], Maybe (m Funcons))
op Name
nm p
_ = Name
-> ([a], Maybe (m Funcons))
-> Map Name ([a], Maybe (m Funcons))
-> Map Name ([a], Maybe (m Funcons))
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Name
nm ([], m Funcons -> Maybe (m Funcons)
forall a. a -> Maybe a
Just (Name -> m Funcons
reader Name
nm))
withResults :: [EntityDefault]
-> MSOSReader m
-> Either IException StepRes
-> MSOSState m
-> MSOSWriter
-> InputValues
-> IO ()
withResults [EntityDefault]
defaults MSOSReader m
msos_ctxt Either IException StepRes
e_exc_f MSOSState m
msos_state MSOSWriter
wr InputValues
rem_ins
| RunOptions -> Bool
show_tests RunOptions
opts =
case Either IException StepRes
e_exc_f of
Left IException
ie -> String -> IO ()
putStrLn (IException -> String
showIException IException
ie)
Right StepRes
efvs -> [Funcons]
-> [EntityDefault]
-> MSOSReader m
-> MSOSState m
-> MSOSWriter
-> InputValues
-> IO ()
forall (m :: * -> *).
[Funcons]
-> [EntityDefault]
-> MSOSReader m
-> MSOSState m
-> MSOSWriter
-> InputValues
-> IO ()
printTestResults ((Funcons -> [Funcons])
-> ([Values] -> [Funcons]) -> StepRes -> [Funcons]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Funcons -> [Funcons] -> [Funcons]
forall a. a -> [a] -> [a]
:[]) ((Values -> Funcons) -> [Values] -> [Funcons]
forall a b. (a -> b) -> [a] -> [b]
map Values -> Funcons
FValue) StepRes
efvs)
[EntityDefault]
defaults MSOSReader m
msos_ctxt MSOSState m
msos_state MSOSWriter
wr InputValues
rem_ins
| Bool
otherwise = do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (RunOptions -> Bool
show_output_only RunOptions
opts) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
IO ()
printCounts
case Either IException StepRes
e_exc_f of
Left IException
ie -> String -> IO ()
putStrLn (IException -> String
showIException IException
ie)
Right StepRes
f -> [Funcons] -> IO ()
printResult ((Funcons -> [Funcons])
-> ([Values] -> [Funcons]) -> StepRes -> [Funcons]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Funcons -> [Funcons] -> [Funcons]
forall a. a -> [a] -> [a]
:[]) ((Values -> Funcons) -> [Values] -> [Funcons]
forall a b. (a -> b) -> [a] -> [b]
map Values -> Funcons
FValue) StepRes
f)
IO ()
printMutable
IO ()
printControl
InputValues -> [Name] -> IO ()
printInput InputValues
rem_ins (RunOptions -> [Name]
hide_input RunOptions
opts)
IO ()
printOutput
where
muts :: Mutable
muts = MSOSState m -> Mutable
forall (m :: * -> *). MSOSState m -> Mutable
mut_entities MSOSState m
msos_state
opts :: RunOptions
opts = RewriteReader -> RunOptions
run_opts (MSOSReader m -> RewriteReader
forall (m :: * -> *). MSOSReader m -> RewriteReader
ereader MSOSReader m
msos_ctxt)
printResult :: [Funcons] -> IO ()
printResult [Funcons]
f = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RunOptions -> Bool
show_result RunOptions
opts) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
String -> IO ()
putStrLn String
"Result:"
String -> IO ()
putStrLn (RunOptions -> [Funcons] -> String
ppFunconsSeq RunOptions
opts [Funcons]
f)
String -> IO ()
putStrLn String
""
printCounts :: IO ()
printCounts = do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RunOptions -> Bool
show_counts RunOptions
opts) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RunOptions -> Bool
csv_output_with_keys RunOptions
opts) (String -> IO ()
putStrLn String
counterKeys)
if (RunOptions -> Bool
csv_output RunOptions
opts)
then String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Counters -> String
displayCounters (RewriteWriterr -> Counters
counters (MSOSWriter -> RewriteWriterr
ewriter MSOSWriter
wr))
else String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Counters -> String
ppCounters (RewriteWriterr -> Counters
counters (MSOSWriter -> RewriteWriterr
ewriter MSOSWriter
wr))
printMutable :: IO ()
printMutable = [Name] -> (Name -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Name]
toShow Name -> IO ()
display
where toShow :: [Name]
toShow = RunOptions -> [Name]
show_mutable RunOptions
opts
display :: Name -> IO ()
display Name
name = case Name -> Mutable -> Maybe Values
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name Mutable
muts of
Maybe Values
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Values
v -> String -> IO ()
putStrLn (String
"Mutable Entity: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name) IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
String -> IO ()
putStrLn (Values -> String
displayValue Values
v) IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> IO ()
putStrLn String
""
printControl :: IO ()
printControl = [Name] -> (Name -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (DownControl -> [Name]
forall k a. Map k a -> [k]
M.keys DownControl
ctrl [Name] -> [Name] -> [Name]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Name]
toHide) Name -> IO ()
display
where ctrl :: DownControl
ctrl = MSOSWriter -> DownControl
ctrl_entities MSOSWriter
wr
toHide :: [Name]
toHide = RunOptions -> [Name]
hide_control RunOptions
opts
display :: Name -> IO ()
display Name
name = case Name -> DownControl -> Maybe (Maybe Values)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name DownControl
ctrl of
Just (Just Values
v) -> do
String -> IO ()
putStrLn (String
"Control Entity: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name)
String -> IO ()
putStrLn (Values -> String
displayValue Values
v) IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> IO ()
putStrLn String
""
Maybe (Maybe Values)
_ -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
printOutput :: IO ()
printOutput = [Name] -> (Name -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (InputValues -> [Name]
forall k a. Map k a -> [k]
M.keys InputValues
out [Name] -> [Name] -> [Name]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Name]
toHide) Name -> IO ()
display
where out :: InputValues
out = MSOSWriter -> InputValues
out_entities MSOSWriter
wr
display :: Name -> IO ()
display Name
name = do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (RunOptions -> Bool
show_output_only RunOptions
opts)
(String -> IO ()
putStrLn (String
"Output Entity: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name))
case (Values -> Bool) -> [Values] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Values -> Bool
forall t. HasValues t => Values t -> Bool
isString_ [Values]
vs Bool -> Bool -> Bool
&& RunOptions -> Bool
pp_string_outputs RunOptions
opts of
Bool
True -> (Values -> IO ()) -> [Values] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (String -> IO ()
putStr (String -> IO ()) -> (Values -> String) -> Values -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Values -> String
forall t. HasValues t => Values t -> String
unString) [Values]
vs
Bool
False -> String -> IO ()
putStrLn ([Values] -> String
displayValues [Values]
vs)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (RunOptions -> Bool
show_output_only RunOptions
opts) (String -> IO ()
putStrLn String
"")
where vs :: [Values]
vs = InputValues
out InputValues -> Name -> [Values]
forall k a. Ord k => Map k a -> k -> a
M.! Name
name
toHide :: [Name]
toHide = RunOptions -> [Name]
hide_output RunOptions
opts
printInput :: InputValues -> [Name] -> IO ()
printInput InputValues
ios [Name]
toHide = [Name] -> (Name -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (InputValues -> [Name]
forall k a. Map k a -> [k]
M.keys InputValues
ios [Name] -> [Name] -> [Name]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Name]
toHide) Name -> IO ()
display
where display :: Name -> IO ()
display Name
name = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Values] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Values]
vs) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
String -> IO ()
putStrLn (String
"Input Entity: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name)
String -> IO ()
putStrLn ([Values] -> String
displayValues [Values]
vs)
String -> IO ()
putStrLn String
""
where vs :: [Values]
vs = InputValues
ios InputValues -> Name -> [Values]
forall k a. Ord k => Map k a -> k -> a
M.! Name
name
displayValues :: [Values] -> String
displayValues [Values]
vs = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"," ((Values -> String) -> [Values] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Values -> String
displayValue [Values]
vs)
displayValue :: Values -> String
displayValue (Map ValueMaps Values
m) = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n"
[ Values -> String
displayValue Values
key String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" |-> " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Values] -> String
displayValues [Values]
val
| (Values
key, [Values]
val) <- ValueMaps Values -> [(Values, [Values])]
forall k a. Map k a -> [(k, a)]
M.assocs ValueMaps Values
m ]
displayValue (ADTVal Name
"variable" [FValue (Atom String
a)
,FValue (ComputationType (Type Types Funcons
t))]) =
String
"variable(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Values -> String
displayValue (String -> Values
forall t. String -> Values t
Atom String
a) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Funcons -> String) -> Types Funcons -> String
forall t. HasValues t => (t -> String) -> Types t -> String
ppTypes (RunOptions -> Funcons -> String
ppFuncons RunOptions
opts) Types Funcons
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
displayValue (Atom String
a) = String
a
displayValue Values
val | Values -> Bool
forall t. HasValues t => Values t -> Bool
isString_ Values
val = String -> String
forall a. Show a => a -> String
show (Values -> String
forall t. HasValues t => Values t -> String
unString Values
val)
displayValue Values
val = (Funcons -> String) -> Values -> String
forall t. HasValues t => (t -> String) -> Values t -> String
ppValues (RunOptions -> Funcons -> String
ppFuncons RunOptions
opts) Values
val
printTestResults :: [Funcons] -> EntityDefaults -> MSOSReader m ->
MSOSState m -> MSOSWriter -> InputValues -> IO ()
printTestResults :: [Funcons]
-> [EntityDefault]
-> MSOSReader m
-> MSOSState m
-> MSOSWriter
-> InputValues
-> IO ()
printTestResults [Funcons]
fs [EntityDefault]
defaults MSOSReader m
msos_ctxt MSOSState m
msos_state MSOSWriter
wr InputValues
rem_ins = do
[Name] -> (Name -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Map Name [Funcons] -> [Name]
forall k a. Map k a -> [k]
M.keys Map Name [Funcons]
opts) Name -> IO ()
printNotExists
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Name -> Map Name [Funcons] -> Bool
forall k a. Ord k => k -> Map k a -> Bool
M.member Name
"result-term" Map Name [Funcons]
opts) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Funcons]
result_term [Funcons] -> [Funcons] -> Bool
forall a. Eq a => a -> a -> Bool
== [Funcons]
fs) (Name -> String -> String -> IO ()
reportError Name
"result-term" ([Funcons] -> String
showFunconsSeq [Funcons]
result_term) ([Funcons] -> String
showFunconsSeq [Funcons]
fs))
IO ()
printMutable
IO ()
printControl
InputValues -> IO ()
printInputOutput InputValues
out
InputValues -> IO ()
printInputOutput InputValues
rem_ins
where eval_ctxt :: RewriteReader
eval_ctxt = MSOSReader m -> RewriteReader
forall (m :: * -> *). MSOSReader m -> RewriteReader
ereader MSOSReader m
msos_ctxt
muts :: Mutable
muts = MSOSState m -> Mutable
forall (m :: * -> *). MSOSState m -> Mutable
mut_entities MSOSState m
msos_state
eval_state :: RewriteState
eval_state = MSOSState m -> RewriteState
forall (m :: * -> *). MSOSState m -> RewriteState
estate MSOSState m
msos_state
localEval :: Name -> Funcons -> Values
localEval Name
name Funcons
term = case Rewrite Rewritten
-> RewriteReader
-> RewriteState
-> (Either IException Rewritten, RewriteState, RewriteWriterr)
forall a.
Rewrite a
-> RewriteReader
-> RewriteState
-> (Either IException a, RewriteState, RewriteWriterr)
runRewrite (Funcons -> Rewrite Rewritten
rewriteFuncons Funcons
term) RewriteReader
eval_ctxt RewriteState
eval_state
of (Left IException
ie,RewriteState
_,RewriteWriterr
_) -> String -> Values
forall a. HasCallStack => String -> a
error (String
"internal exception in " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" evaluation:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ IException -> String
showIException IException
ie)
(Right (ValTerm [Values
v]),RewriteState
_,RewriteWriterr
_) -> Values
v
(Right (ValTerm [Values]
vs),RewriteState
_,RewriteWriterr
_) -> String -> Values
forall a. HasCallStack => String -> a
error
(String
"evaluation of " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" results in sequence")
(Right Rewritten
_,RewriteState
_,RewriteWriterr
_) ->
String -> Values
forall a. HasCallStack => String -> a
error (String
"evaluation of " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" requires step")
mLocalEval :: Funcons -> Maybe Values
mLocalEval Funcons
term = case Rewrite Rewritten
-> RewriteReader
-> RewriteState
-> (Either IException Rewritten, RewriteState, RewriteWriterr)
forall a.
Rewrite a
-> RewriteReader
-> RewriteState
-> (Either IException a, RewriteState, RewriteWriterr)
runRewrite(Funcons -> Rewrite Rewritten
rewriteFuncons Funcons
term) RewriteReader
eval_ctxt RewriteState
eval_state of
(Right (ValTerm [Values
v]),RewriteState
_,RewriteWriterr
_) -> Values -> Maybe Values
forall a. a -> Maybe a
Just Values
v
(Either IException Rewritten, RewriteState, RewriteWriterr)
_ -> Maybe Values
forall a. Maybe a
Nothing
result_term :: [Funcons]
result_term = case [Maybe Values] -> Maybe [Values]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ((Funcons -> Maybe Values) -> [Funcons] -> [Maybe Values]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Funcons -> Maybe Values
recursiveFunconValue [Funcons]
rf) of
Maybe [Values]
Nothing -> case [Maybe Values] -> Maybe [Values]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ((Funcons -> Maybe Values) -> [Funcons] -> [Maybe Values]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Funcons -> Maybe Values
mLocalEval [Funcons]
rf) of
Maybe [Values]
Nothing -> [Funcons]
rf
Just [Values]
vs -> (Values -> Funcons) -> [Values] -> [Funcons]
forall a b. (a -> b) -> [a] -> [b]
map Values -> Funcons
FValue [Values]
vs
Just [Values]
vs -> (Values -> Funcons) -> [Values] -> [Funcons]
forall a b. (a -> b) -> [a] -> [b]
map Values -> Funcons
FValue [Values]
vs
where rf :: [Funcons]
rf = (Map Name [Funcons]
opts Map Name [Funcons] -> Name -> [Funcons]
forall k a. Ord k => Map k a -> k -> a
M.! Name
"result-term")
opts :: Map Name [Funcons]
opts = RunOptions -> Map Name [Funcons]
expected_outcomes (RewriteReader -> RunOptions
run_opts RewriteReader
eval_ctxt)
reportError :: Name -> String -> String -> IO ()
reportError Name
name String
expected String
actual = do
String -> IO ()
putStrLn (String
"expected " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
expected)
String -> IO ()
putStrLn (String
"actual " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
actual)
printNotExists :: Name -> IO ()
printNotExists Name
"result-term" = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
printNotExists Name
name =
case (Name -> Mutable -> Maybe Values
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name Mutable
muts, Name -> InputValues -> Maybe [Values]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name InputValues
out
,Name -> DownControl -> Maybe (Maybe Values)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name DownControl
ctrl, Name -> InputValues -> Maybe [Values]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name InputValues
rem_ins) of
(Maybe Values
Nothing, Maybe [Values]
Nothing, Maybe (Maybe Values)
Nothing, Maybe [Values]
Nothing) ->
String -> IO ()
putStrLn (String
"unknown entity: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name)
(Maybe Values, Maybe [Values], Maybe (Maybe Values),
Maybe [Values])
_ -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
printMutable :: IO ()
printMutable = [(Name, Values)] -> ((Name, Values) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Mutable -> [(Name, Values)]
forall k a. Map k a -> [(k, a)]
M.assocs Mutable
muts) ((Name -> Values -> IO ()) -> (Name, Values) -> IO ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Name -> Values -> IO ()
display)
where display :: Name -> Values -> IO ()
display Name
name Values
val = case Name -> Map Name [Funcons] -> Maybe [Funcons]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name Map Name [Funcons]
opts of
Maybe [Funcons]
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just [Funcons]
expected -> Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
((Funcons -> Values) -> [Funcons] -> [Values]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Funcons -> Values
localEval Name
name) [Funcons]
expected [Values] -> [Values] -> Bool
forall a. Eq a => a -> a -> Bool
== [Values
val])
(Name -> String -> String -> IO ()
reportError Name
name ([String] -> String
showL ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Funcons -> String) -> [Funcons] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Funcons -> String
showFuncons [Funcons]
expected) (Values -> String
showValues Values
val))
ctrl :: M.Map Name (Maybe Values)
ctrl :: DownControl
ctrl = (EntityDefault -> DownControl -> DownControl)
-> DownControl -> [EntityDefault] -> DownControl
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr EntityDefault -> DownControl -> DownControl
forall a. EntityDefault -> Map Name (Maybe a) -> Map Name (Maybe a)
op (MSOSWriter -> DownControl
ctrl_entities MSOSWriter
wr) [EntityDefault]
defaults
where op :: EntityDefault -> Map Name (Maybe a) -> Map Name (Maybe a)
op (DefControl Name
name) Map Name (Maybe a)
ctrl
| Bool -> Bool
not (Name -> Map Name (Maybe a) -> Bool
forall k a. Ord k => k -> Map k a -> Bool
M.member Name
name Map Name (Maybe a)
ctrl) = Name -> Maybe a -> Map Name (Maybe a) -> Map Name (Maybe a)
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Name
name Maybe a
forall a. Maybe a
Nothing Map Name (Maybe a)
ctrl
op EntityDefault
_ Map Name (Maybe a)
ctrl = Map Name (Maybe a)
ctrl
out :: InputValues
out = (EntityDefault -> InputValues -> InputValues)
-> InputValues -> [EntityDefault] -> InputValues
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr EntityDefault -> InputValues -> InputValues
forall a. EntityDefault -> Map Name [a] -> Map Name [a]
op (MSOSWriter -> InputValues
out_entities MSOSWriter
wr) [EntityDefault]
defaults
where op :: EntityDefault -> Map Name [a] -> Map Name [a]
op (DefOutput Name
name) Map Name [a]
out
| Bool -> Bool
not (Name -> Map Name [a] -> Bool
forall k a. Ord k => k -> Map k a -> Bool
M.member Name
name Map Name [a]
out) = Name -> [a] -> Map Name [a] -> Map Name [a]
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Name
name [] Map Name [a]
out
op EntityDefault
_ Map Name [a]
out = Map Name [a]
out
printControl :: IO ()
printControl = [(Name, Maybe Values)] -> ((Name, Maybe Values) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (DownControl -> [(Name, Maybe Values)]
forall k a. Map k a -> [(k, a)]
M.assocs DownControl
ctrl) ((Name -> Maybe Values -> IO ()) -> (Name, Maybe Values) -> IO ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Name -> Maybe Values -> IO ()
display)
where display :: Name -> Maybe Values -> IO ()
display :: Name -> Maybe Values -> IO ()
display Name
name Maybe Values
Nothing = case Name -> Map Name [Funcons] -> Maybe [Funcons]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name Map Name [Funcons]
opts of
Maybe [Funcons]
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just [Funcons]
vals -> String -> IO ()
putStrLn (String
"expected "String -> String -> String
forall a. [a] -> [a] -> [a]
++Name -> String
unpack Name
nameString -> String -> String
forall a. [a] -> [a] -> [a]
++String
": "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
showL ((Funcons -> String) -> [Funcons] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Values -> String
showValues (Values -> String) -> (Funcons -> Values) -> Funcons -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> Funcons -> Values
localEval Name
name) [Funcons]
vals))
display Name
name (Just Values
val) = case Name -> Map Name [Funcons] -> Maybe [Funcons]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name Map Name [Funcons]
opts of
Maybe [Funcons]
Nothing -> String -> IO ()
putStrLn (String
"unexpected " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Values -> String
showValues Values
val)
Just [Funcons]
expected -> Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
((Funcons -> Values) -> [Funcons] -> [Values]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Funcons -> Values
localEval Name
name) [Funcons]
expected [Values] -> [Values] -> Bool
forall a. Eq a => a -> a -> Bool
== [Values
val])
(Name -> String -> String -> IO ()
reportError Name
name ([String] -> String
showL ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Funcons -> String) -> [Funcons] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Funcons -> String
showFuncons [Funcons]
expected) (Values -> String
showValues Values
val))
printInputOutput :: InputValues -> IO ()
printInputOutput InputValues
remaining = [(Name, [Values])] -> ((Name, [Values]) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (InputValues -> [(Name, [Values])]
forall k a. Map k a -> [(k, a)]
M.assocs InputValues
remaining) ((Name -> [Values] -> IO ()) -> (Name, [Values]) -> IO ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Name -> [Values] -> IO ()
display)
where
display :: Name -> [Values] -> IO ()
display Name
name [] | Maybe [Funcons]
Nothing <- Name -> Map Name [Funcons] -> Maybe [Funcons]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name Map Name [Funcons]
opts = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
display Name
name [Values]
vals = case Name -> Map Name [Funcons] -> Maybe [Funcons]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
name Map Name [Funcons]
opts of
Maybe [Funcons]
Nothing -> String -> IO ()
putStrLn (String
"unexpected " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
unpack Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
showL ((Values -> String) -> [Values] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Values -> String
showValues [Values]
vals))
Just [Funcons]
expected -> case (Funcons -> Values) -> [Funcons] -> [Values]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Funcons -> Values
localEval Name
name) [Funcons]
expected of
[ADTVal Name
"list" [Funcons]
exps] -> Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Funcons]
exps [Funcons] -> [Funcons] -> Bool
forall a. Eq a => a -> a -> Bool
== (Values -> Funcons) -> [Values] -> [Funcons]
forall a b. (a -> b) -> [a] -> [b]
map Values -> Funcons
FValue [Values]
vals) (Name -> String -> String -> IO ()
reportError Name
name ([String] -> String
showL ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Funcons -> String) -> [Funcons] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Funcons -> String
showFuncons [Funcons]
exps) ([String] -> String
showL ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Values -> String) -> [Values] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Values -> String
showValues [Values]
vals))
[Values]
val -> String -> IO ()
forall a. HasCallStack => String -> a
error (String
"non-list given as expected output entity ("String -> String -> String
forall a. [a] -> [a] -> [a]
++
Name -> String
unpack Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"): " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
showL ((Values -> String) -> [Values] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Values -> String
showValues [Values]
val))
showL :: [String] -> String
showL :: [String] -> String
showL [String]
elems = String
"[" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"," [String]
elems String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"]"