@@ -84,7 +84,7 @@ updateModules modules st = st { psciLoadedModules = psciLoadedModules st ++ modu
8484-- Updates the state to have more let bindings.
8585--
8686updateLets :: (P.Value -> P.Value) -> PSCiState -> PSCiState
87−updateLets name st = st { psciLetBindings = psciLetBindings st ++ [name] }
87+updateLets name st = st { psciLetBindings = name : psciLetBindings st }
8888
8989-- File helpers
9090-- |
@@ -194,25 +194,27 @@ options = P.Options True False True (Just "Main") True "PS" []
194194-- |
195195-- Makes a volatile module to execute the current expression.
196196--
197−createTemporaryModule :: Bool -> [P.ModuleName] -> [P.Value -> P.Value] -> P.Value -> P.Module
198−createTemporaryModule exec imports lets value =
197+createTemporaryModule :: Bool -> PSCiState -> P.Value -> P.Module
198+createTemporaryModule exec PSCiState{psciImportedModuleNames = imports, psciLetBindings = lets} value =
199199 let
200200 moduleName = P.ModuleName [P.ProperName "Main"]
201201 importDecl m = P.ImportDeclaration m Nothing
202202 traceModule = P.ModuleName [P.ProperName "Debug", P.ProperName "Trace"]
203203 trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
204− value' = foldr ($) value lets
205− itDecl = P.ValueDeclaration (P.Ident "it") [] Nothing value'
206− mainDecl = P.ValueDeclaration (P.Ident "main") [] Nothing (P.App trace (P.Var (P.Qualified Nothing (P.Ident "it"))))
204+ itValue = foldl (\x f -> f x) value lets
205+ mainValue = P.App trace (P.Var (P.Qualified Nothing (P.Ident "it")))
206+ itDecl = P.ValueDeclaration (P.Ident "it") [] Nothing itValue
207+ mainDecl = P.ValueDeclaration (P.Ident "main") [] Nothing mainValue
208+ decls = if exec then [itDecl, mainDecl] else [itDecl]
207209 in
208− P.Module moduleName $ map importDecl imports ++ if exec then [itDecl, mainDecl] else [itDecl]
210+ P.Module moduleName $ map importDecl imports ++ decls
209211
210212-- |
211213-- Takes a value declaration and evaluates it with the current state.
212214--
213215handleDeclaration :: P.Value -> PSCiState -> InputT (StateT PSCiState IO) ()
214216handleDeclaration value st = do
215− let m = createTemporaryModule True (psciImportedModuleNames st) (psciLetBindings st) value
217+ let m = createTemporaryModule True st value
216218 case P.compile options (psciLoadedModules st ++ [m]) of
217219 Left err -> outputStrLn err
218220 Right (js, _, _) -> do
@@ -228,7 +230,7 @@ handleDeclaration value st = do
228230--
229231handleTypeOf :: P.Value -> PSCiState -> InputT (StateT PSCiState IO) ()
230232handleTypeOf value st = do
231− let m = createTemporaryModule False (psciImportedModuleNames st) (psciLetBindings st) value
233+ let m = createTemporaryModule False st value
232234 case P.compile options { P.optionsMain = Nothing } (psciLoadedModules st ++ [m]) of
233235 Left err -> outputStrLn err
234236 Right (_, _, env') ->