Skip to content
Draft

Import #2540

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
6a97194
replace some TSyntax -> Syntax, plus a bit of refactoring
byorgey Jul 22, 2024
7db1194
[wip] import ... in ... syntax
byorgey Jul 22, 2024
a1936b0
[wip] import ... in syntax is now a no-op
byorgey Sep 21, 2024
bd2e599
import doesn't require any capabilities for now
byorgey Sep 21, 2024
8ab8fbb
[wip] start working on recursive import loading
byorgey Sep 22, 2024
53393d3
use `SystemFailure` instead of custom placeholder
byorgey Sep 30, 2024
0af336e
[wip] add some notes
byorgey Sep 30, 2024
cdcdff3
[wip] a bit more work on recursive import loading
byorgey Sep 30, 2024
3ffd89b
[wip] import locations
byorgey Sep 30, 2024
9361428
[wip] get things compiling again
byorgey Oct 7, 2024
93210b7
[wip] org, comments, tests, converting import location -> filepath
byorgey Oct 7, 2024
6f02979
[wip] resolving import locations
byorgey Oct 7, 2024
6fa6ef8
import parsing
byorgey Oct 8, 2024
aea1a24
resolve imports and turn them into filepaths
byorgey Oct 8, 2024
f18c840
add todo
byorgey Oct 8, 2024
282c0bd
work on recursively loading imports
byorgey Oct 9, 2024
bb1f8ad
get it compiling
byorgey Oct 9, 2024
5bef556
simplify: remove indexing for ImportDir
byorgey Oct 9, 2024
cf53109
export `SourceMap` type alias
byorgey Oct 9, 2024
4de9ac9
store import list in Module along with each AST
byorgey Oct 20, 2024
ea86484
add some Hashable instances
byorgey Jan 15, 2025
726c78a
typo fix
byorgey Jan 15, 2025
1441ca7
check for import cycles
byorgey Jan 16, 2025
9acec8a
topsort imports, and add context to Module
byorgey Jan 17, 2025
beb3717
[wip] start updating typechecking to process imports
byorgey Jan 17, 2025
23a8fc3
[wip] work on typechecking imports
byorgey Jan 19, 2025
adcd1be
progress on converting processing pipeline to include I/O for imports
byorgey Mar 4, 2025
97fe75c
fix processParsedTerm call
byorgey Mar 5, 2025
11d1021
split out processParsedTermWithSrcMap and use it to resolve S.L.Text.…
byorgey Mar 7, 2025
4e48479
fix more calls to processing pipeline
byorgey Mar 7, 2025
2b7b4c7
some merge cleanup
byorgey Jun 10, 2025
59a19a7
LSP fixes; move SrcLoc to swarm-util
byorgey Jun 10, 2025
f9cc359
parameterize a bunch of records by typing phase
byorgey Jun 10, 2025
bcba8ac
termSyntax traversal + erasure
byorgey Jun 26, 2025
f376611
more progress converting to type phase parameter
byorgey Jun 27, 2025
6406d9a
more progress; remove Eq instance for Robot; change Ord (FoundStructu…
byorgey Jun 27, 2025
9281005
more tedious progress
byorgey Jun 27, 2025
15fe335
progress
byorgey Jun 27, 2025
59c3234
fix REPL validation
byorgey Jun 28, 2025
2fdcdcb
IT COMPILES!!!
byorgey Jun 28, 2025
676409a
Processable class; it runs!!!!
byorgey Jun 28, 2025
b32feac
fix FromJSON Syntax instance
byorgey Jun 30, 2025
871be7a
remove Run command; fix infinite loop for module that imports itself
byorgey Jun 30, 2025
8b29589
add new Resolved phase, after loading imports
byorgey Jun 30, 2025
a18de79
parameterize ImportLoc by phase
byorgey Jun 30, 2025
813bff8
[wip] work on refactoring Load.hs
byorgey Jun 30, 2025
1638d33
more cleanup in Load
byorgey Jun 30, 2025
0271ad1
more progress resolving imports
byorgey Jun 30, 2025
24045c8
[wip] import resolution
byorgey Jun 30, 2025
1fe26d3
[wip] bunch more refactoring
byorgey Jul 1, 2025
47bb991
getting things to compile again
byorgey Jul 1, 2025
26b0b6d
fix processParsedTermNoImports
byorgey Jul 1, 2025
f202961
merge
byorgey Jul 11, 2025
4b6c289
refactor: rename `Swarm.Util.SrcLoc` back to `Swarm.Language.Syntax.Loc`
byorgey Jul 14, 2025
1e66c11
refactor: move `Swarm.Language.{Syntax.Import, Phase}` to `swarm-util`
byorgey Jul 14, 2025
22e71a7
add ImportLoc to SrcLoc and reorganize some code
byorgey Jul 14, 2025
39504ae
redo Import stuff to track Raw vs Resolved
byorgey Jul 15, 2025
bdae27f
make it compile again
byorgey Jul 21, 2025
42979b6
record ImportLoc in SrcLoc while parsing
byorgey Jul 26, 2025
5b65b46
fix Pretty instance for Anchor
byorgey Jul 26, 2025
58cc060
fix import cycle checking, and stop filtering typechecking stack
byorgey Jul 26, 2025
c15fd6b
make processTerm + friends return a SourceMap
byorgey Jul 26, 2025
8f7a4f9
fix up some XXX comments
byorgey Jul 27, 2025
357d854
move import parsing into a dedicated module
byorgey Jul 27, 2025
7a42142
add a new Elaborated phase
byorgey Jul 28, 2025
5854520
use type family for module imports field
byorgey Jul 28, 2025
e4324f4
get rid of gunfold implementation for Anchor, and write a note
byorgey Jul 28, 2025
85009b6
remove outdated comment + unused instance
byorgey Jul 28, 2025
39f9ad8
some refactoring
byorgey Jul 28, 2025
c5104e7
add some comments
byorgey Jul 28, 2025
a9b8db5
update some comments
byorgey Jul 28, 2025
bd7bbc3
merge
byorgey Jul 28, 2025
1b182a7
redo Anchors in a way that gets rid of unsafe stuff
byorgey Jul 28, 2025
d1f3f74
fix constructScenario
byorgey Jul 29, 2025
516a4ee
add envSourceMap to Env
byorgey Jul 29, 2025
c43266f
get basic imports working with envSourceMap
byorgey Jul 30, 2025
97cab95
reinstate run for now
byorgey Aug 1, 2025
0507437
merge cleanup
byorgey Aug 1, 2025
f97eea0
fix mistake re: suspend made while trying to get import to work
byorgey Aug 1, 2025
27a5e0c
don't require capability for import (for now)
byorgey Aug 1, 2025
2062418
normalize cabal
byorgey Aug 1, 2025
837dbfb
add some comments
byorgey Aug 1, 2025
027e5ed
clean up some warnings
byorgey Aug 1, 2025
999dae3
add another comment about the use of unsafeCoerce in elaboration
byorgey Aug 1, 2025
b0c3781
address some hlint warnings
byorgey Aug 1, 2025
f4bc15c
Restyled by fourmolu (#2541)
github-actions[bot] Aug 2, 2025
691afaf
more hlint warnings
byorgey Aug 2, 2025
25a88b6
more hlint
byorgey Aug 2, 2025
f6a13cb
fix tournament server + benchmarks
byorgey Aug 2, 2025
9503b27
Restyled by fourmolu (#2559)
github-actions[bot] Aug 2, 2025
5eb5e7b
fix swarm-doc
byorgey Aug 2, 2025
1be8b43
update + simplify Pipeline
byorgey Aug 2, 2025
bbb1125
ignore imports when validating REPL input on the fly
byorgey Aug 15, 2025
7512dc6
fix readValue
byorgey Aug 15, 2025
3e286e3
fix up processTermNoImports comment
byorgey Aug 15, 2025
06d646c
more informative error message when import fails fatally
byorgey Aug 17, 2025
e6fbd18
add SyntaxWithImports type and thread SrcMap through a bunch more stuff
byorgey Aug 17, 2025
7b1b50b
try making move tutorial use import
byorgey Aug 23, 2025
4a7ead7
CESK fix for imports
byorgey Aug 24, 2025
5fad032
add a few comments
byorgey Aug 24, 2025
181502f
read URL imports
byorgey Aug 24, 2025
a6d91bf
Make import integration tests compile again (#2565)
xsebek Aug 26, 2025
459a64a
fix swarm-tournament
byorgey Aug 26, 2025
cbc0981
fix tmQ quasiquoter
byorgey Aug 27, 2025
1e81ab3
get unit tests to compile again
byorgey Aug 27, 2025
f3c0ed2
fix type error pretty-printing
byorgey Aug 29, 2025
235389c
restore parse error messages
byorgey Aug 29, 2025
fdcf390
fix JSON round-tripping
byorgey Aug 29, 2025
20d6a9c
fix integration tests
byorgey Aug 30, 2025
06afafd
clean up URL loading
byorgey Sep 7, 2025
3561411
merge fixup: add phase parameter
byorgey Sep 7, 2025
d6a3f91
add some tests (one failing)
byorgey Sep 7, 2025
6f40e78
fix module context inference with imports
byorgey Sep 7, 2025
62ac623
a couple more tests
byorgey Sep 7, 2025
f658197
add import from URL test
byorgey Sep 7, 2025
2198901
validate that imports only contain definitions + imports
byorgey Sep 14, 2025
7aa07f7
collect type definitions in imported modules
byorgey Sep 16, 2025
8bc4c40
fix up TDCtx merging
byorgey Oct 5, 2025
1e7943e
Restyled by fourmolu (#2560)
github-actions[bot] Oct 5, 2025
6caa91e
hlint hints
byorgey Oct 5, 2025
ae20f33
fix up merge
byorgey Oct 6, 2025
c3ce32e
remove unused imports
byorgey Oct 6, 2025
8985adf
fix benchmark suite
byorgey Oct 6, 2025
5474b70
add some weeder exceptions
byorgey Oct 6, 2025
efbb912
interpret first import relative to location of containing file
byorgey Oct 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
, TestLanguagePipeline
]
}
- {name: Unsafe.Coerce.unsafeCoerce, within: [Swarm.Language.Elaborate.elaborate]}

# Add custom hints for this project
#
Expand Down
6 changes: 3 additions & 3 deletions app/game/Swarm/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module Swarm.App (

import Brick
import Brick.BChan
import Control.Carrier.Error.Either (runError)
import Control.Carrier.Lift (runM)
import Control.Carrier.Throw.Either (runThrow)
import Control.Concurrent (forkIO, threadDelay)
import Control.Exception (bracket, try)
import Control.Lens (Setter', view, (%~), (?~), (^.))
Expand Down Expand Up @@ -75,7 +75,7 @@ app eventHandler =
appMain :: AppOpts -> IO ()
appMain opts = do
chan <- createChannel
res <- runM . runThrow $ initAppState opts (Just chan)
res <- runM . runError $ initAppState opts (Just chan)
case res of
Left err -> do
T.hPutStrLn stderr (prettyText @SystemFailure err)
Expand Down Expand Up @@ -120,7 +120,7 @@ demoWeb = do
let demoPort = 8080
chan <- createChannel
res <-
runM . runThrow $ initAppState (defaultAppOpts {userScenario = demoScenario}) (Just chan)
runM . runError $ initAppState (defaultAppOpts {userScenario = demoScenario}) (Just chan)
case res of
Left err -> T.putStrLn (prettyText @SystemFailure err)
Right s -> do
Expand Down
20 changes: 7 additions & 13 deletions data/scenarios/Tutorials/move.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,29 @@ robots:
#################
- name: check
system: true
program: instant {run "scenarios/Tutorials/move_check.sw"}
program: instant {import "scenarios/Tutorials/move_check.sw"; main}
#################
## WALLS ##
#################
- name: 1P wall
system: true
program: |
def main = \a. pure noop end
instant {
run "scenarios/Tutorials/move_surveil.sw";
import "scenarios/Tutorials/move_surveil.sw";
main [entity="wall", room=1]
}
- name: 2P wall
system: true
program: |
def main = \a. noop end
instant {
run "scenarios/Tutorials/move_surveil.sw";
import "scenarios/Tutorials/move_surveil.sw";
main [entity="wall", room=2]
}
- name: 3P wall
system: true
program: |
def main = \a. noop end
instant {
run "scenarios/Tutorials/move_surveil.sw";
import "scenarios/Tutorials/move_surveil.sw";
main [entity="wall", room=3]
}
#################
Expand All @@ -176,25 +173,22 @@ robots:
- name: 1P flower
system: true
program: |
def main = \a. noop end
instant {
run "scenarios/Tutorials/move_surveil.sw";
import "scenarios/Tutorials/move_surveil.sw";
main [entity="flower", room=1]
}
- name: 2P flower
system: true
program: |
def main = \a. noop end
instant {
run "scenarios/Tutorials/move_surveil.sw";
import "scenarios/Tutorials/move_surveil.sw";
main [entity="flower", room=2]
}
- name: 3P flower
system: true
program: |
def main = \a. noop end
instant {
run "scenarios/Tutorials/move_surveil.sw";
import "scenarios/Tutorials/move_surveil.sw";
main [entity="flower", room=3]
}
entities:
Expand Down
2 changes: 0 additions & 2 deletions data/scenarios/Tutorials/move_check.sw
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,3 @@ def main =
room2;
room3;
end

main
1 change: 1 addition & 0 deletions data/test/import/a.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def a : Int = 3 end
3 changes: 3 additions & 0 deletions data/test/import/b.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "c"

def b : Int = 1 end
1 change: 1 addition & 0 deletions data/test/import/c.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def c : Int = 7 end
3 changes: 3 additions & 0 deletions data/test/import/d.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "e"

def d : Int = e * 5 end
1 change: 1 addition & 0 deletions data/test/import/e.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def e : Int = 9 end
3 changes: 3 additions & 0 deletions data/test/import/f.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "g"

def f : Int = g + 7 end
1 change: 1 addition & 0 deletions data/test/import/g.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def g : Int = 9 end
34 changes: 25 additions & 9 deletions src/swarm-doc/Swarm/Doc/Pedagogy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Swarm.Doc.Pedagogy (

import Control.Lens (universe, view, (^.))
import Control.Monad (guard)
import Data.Data (Data, Typeable)
import Data.Foldable (Foldable (..))
import Data.List (intercalate, sort, sortOn)
import Data.List.Extra (zipFrom)
Expand Down Expand Up @@ -55,9 +56,9 @@ import Swarm.Game.ScenarioInfo (
scenarioCollectionToList,
)
import Swarm.Game.World.DSL (loadWorlds)
import Swarm.Language.Load (getSyntax)
import Swarm.Language.Syntax
import Swarm.Language.Text.Markdown (docToText, findCode)
import Swarm.Language.Types (Polytype)
import Swarm.Util.Effect (ignoreWarnings)
import Prelude hiding (Foldable (..))

Expand Down Expand Up @@ -100,19 +101,27 @@ extractCommandUsages :: Int -> ScenarioWith ScenarioPath -> TutorialInfo
extractCommandUsages idx siPair@(ScenarioWith s _si) =
TutorialInfo siPair idx solnCommands $ getDescCommands s
where
solnCommands = getCommands maybeSoln
solnCommands = getCommands . fmap getSyntax $ maybeSoln
maybeSoln = view (scenarioOperation . scenarioSolution) s

-- | Obtain the set of all commands mentioned by
-- name in the tutorial's goal descriptions.
getDescCommands :: Scenario -> Set Const
getDescCommands ::
forall phase.
( Data (Anchor (ImportPhaseFor phase))
, Data (SwarmType phase)
, Typeable phase
, Typeable (ImportPhaseFor phase)
) =>
Scenario phase ->
Set Const
getDescCommands s = S.fromList $ concatMap filterConst allCode
where
goalTextParagraphs = view objectiveGoal <$> view (scenarioOperation . scenarioObjectives) s
allCode = concatMap findCode goalTextParagraphs
filterConst :: Syntax -> [Const]
filterConst :: Syntax phase -> [Const]
filterConst sx = mapMaybe toConst $ universe (sx ^. sTerm)
toConst :: Term -> Maybe Const
toConst :: Term phase -> Maybe Const
toConst = \case
TConst c -> Just c
_ -> Nothing
Expand All @@ -129,14 +138,21 @@ isConsidered c = isUserFunc c && c `S.notMember` ignoredCommands
-- the player did not write it explicitly in their code.
--
-- Also, the code from `run` is not parsed transitively yet.
getCommands :: Maybe TSyntax -> Map Const [SrcLoc]
getCommands ::
forall phase.
( Data (Anchor (ImportPhaseFor phase))
, Data (SwarmType phase)
, Typeable phase
, Typeable (ImportPhaseFor phase)
) =>
Maybe (Syntax phase) ->
Map Const [SrcLoc]
getCommands Nothing = mempty
getCommands (Just tsyn) =
M.fromListWith (<>) $ mapMaybe isCommand nodelist
where
nodelist :: [Syntax' Polytype]
nodelist = universe tsyn
isCommand (Syntax' sloc t _ _) = case t of
isCommand (Syntax sloc t _ _) = case t of
TConst c -> guard (isConsidered c) >> Just (c, [sloc])
_ -> Nothing

Expand Down Expand Up @@ -208,7 +224,7 @@ renderUsagesMarkdown (CoverageInfo (TutorialInfo (ScenarioWith s (ScenarioPath s
, renderTutorialTitle idx s
]

renderTutorialTitle :: (Show a) => a -> Scenario -> Text
renderTutorialTitle :: (Show a) => a -> Scenario Elaborated -> Text
renderTutorialTitle idx s =
T.unwords
[ T.pack $ show idx <> ":"
Expand Down
49 changes: 30 additions & 19 deletions src/swarm-engine/Swarm/Game/CESK.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module Swarm.Game.CESK (
continue,
cancel,
prepareTerm,
insertSuspend,

-- ** Extracting information
finalValue,
Expand All @@ -81,10 +82,11 @@ module Swarm.Game.CESK (
cont,
) where

import Control.Lens (Lens', Traversal', lens, traversal, (^.))
import Control.Lens (Lens', Traversal', lens, traversal, (%~), (&), (.~), (^.))
import Data.Aeson (FromJSON (..), ToJSON (..), genericParseJSON, genericToJSON)
import Data.IntMap.Strict (IntMap)
import Data.IntMap.Strict qualified as IM
import Data.Map qualified as M
import GHC.Generics (Generic)
import Prettyprinter (Doc, Pretty (..), encloseSep, hsep, (<+>))
import Swarm.Game.Entity (Entity)
Expand All @@ -93,6 +95,7 @@ import Swarm.Game.Ingredients (Count)
import Swarm.Game.Tick
import Swarm.Game.World (WorldUpdate (..))
import Swarm.Language.Elaborate (insertSuspend)
import Swarm.Language.Load (SyntaxWithImports (..))
import Swarm.Language.Requirements.Type (Requirements)
import Swarm.Language.Syntax
import Swarm.Language.Types
Expand All @@ -111,7 +114,7 @@ data Frame
= -- | We were evaluating the first component of a pair; next, we
-- should evaluate the second component which was saved in this
-- frame (and push a 'FFst' frame on the stack to save the first component).
FSnd Term Env
FSnd (Term Resolved) Env
| -- | We were evaluating the second component of a pair; when done,
-- we should combine it with the value of the first component saved
-- in this frame to construct a fully evaluated pair.
Expand All @@ -121,7 +124,7 @@ data Frame
-- term @t@ (the right-hand side, /i.e./ argument of the
-- application) in environment @e@. We will also push an 'FApp'
-- frame on the stack.
FArg Term Env
FArg (Term Resolved) Env
| -- | @FVArg v@ says that we were evaluating the left-hand side of
-- an application, and the next thing we should do is apply it
-- to the given value. This does not normally occur as part of
Expand All @@ -138,7 +141,7 @@ data Frame
-- is, we were evaluating the definition of @x@; the next thing we
-- should do is evaluate @t2@ in the environment @e@ extended with
-- a binding for @x@.
FLet Var (Maybe (Polytype, Requirements)) Term Env
FLet Var (Maybe (Polytype, Requirements)) (Term Resolved) Env
| -- | We are executing inside a 'Try' block. If an exception is
-- raised, we will execute the stored term (the "catch" block).
FTry Value
Expand All @@ -149,7 +152,10 @@ data Frame
-- bind; once done, we should also execute the second component
-- in the given environment (extended by binding the variable,
-- if there is one, to the output of the first command).
FBind (Maybe Var) (Maybe (Polytype, Requirements)) Term Env
FBind (Maybe Var) (Maybe (Polytype, Requirements)) (Term Resolved) Env
| -- | We are in the process of evaluating an import; once done, we
-- should proceed to evaluate the given body.
FImport (Term Resolved)
| -- | Apply specific updates to the world and current robot.
--
-- The 'Const' is used to track the original command for error messages.
Expand All @@ -161,7 +167,7 @@ data Frame
| -- | We are in the middle of evaluating a record: some fields have
-- already been evaluated; we are focusing on evaluating one
-- field; and some fields have yet to be evaluated.
FRcd Env [(Var, Value)] Var [(Var, Maybe Term)]
FRcd Env [(Var, Value)] Var [(Var, Maybe (Term Resolved))]
| -- | We are in the middle of evaluating a record field projection.
FProj Var
| -- | We should suspend with the given environment once we finish
Expand Down Expand Up @@ -239,7 +245,7 @@ data CESK
-- currently focused term to evaluate in the environment, a store,
-- and a continuation. In this mode we generally pattern-match on the
-- 'Term' to decide what to do next.
In Term Env Store Cont
In (Term Resolved) Env Store Cont
| -- | Once we finish evaluating a term, we end up with a 'Value'
-- and we switch into "out" mode, bringing the value back up
-- out of the depths to the context that was expecting it. In
Expand Down Expand Up @@ -323,21 +329,21 @@ cont = lens get set
Waiting t c -> Waiting t (set c k)
Suspended v e s _ -> Suspended v e s k

-- | Create a brand new CESK machine, with empty environment and
-- store, to evaluate a given term. We always initialize the
-- machine with a single FExec frame as the continuation; if the
-- given term does not have a command type, we wrap it in @pure@.
initMachine :: TSyntax -> CESK
initMachine t = In (prepareTerm V.emptyEnv t) V.emptyEnv emptyStore [FExec]
-- | Create a brand new CESK machine to evaluate a given term. We
-- always initialize the machine with a single FExec frame as the
-- continuation; if the given term does not have a command type, we
-- wrap it in @pure@.
initMachine :: SyntaxWithImports Elaborated -> CESK
initMachine (SyntaxWithImports _ srcMap t) = In (prepareTerm V.emptyEnv t) (V.envFromSrcMap srcMap) emptyStore [FExec]

-- | Load a program into an existing robot CESK machine: either
-- continue from a suspended state, or, as a fallback, start from
-- scratch with an empty environment but the same store.
--
-- Also insert a @suspend@ primitive at the end, so the resulting
-- term is suitable for execution by the base (REPL) robot.
continue :: TSyntax -> CESK -> CESK
continue t = \case
continue :: SyntaxWithImports Elaborated -> CESK -> CESK
continue (SyntaxWithImports _ srcMap t) = \case
-- The normal case is when we are continuing from a suspended state. We:
--
-- (1) insert a suspend call at the end of the term, so that in
Expand All @@ -351,24 +357,28 @@ continue t = \case
-- environment e (any names brought into scope by executing the
-- term will be discarded). If the term succeeds, the extra
-- FRestoreEnv frame will be discarded.
Suspended _ e s k -> In (insertSuspend $ prepareTerm e t) e s (FExec : FRestoreEnv e : k)
Suspended _ e s k ->
let e' = e & envSourceMap %~ M.union srcMap
in In (insertSuspend $ prepareTerm e' t) e' s (FExec : FRestoreEnv e : k)
-- In any other state, just start with an empty environment. This
-- happens e.g. when running a program on the base robot for the
-- very first time.
cesk -> In (insertSuspend $ prepareTerm V.emptyEnv t) V.emptyEnv (cesk ^. store) (FExec : (cesk ^. cont))
cesk ->
let e = V.emptyEnv & envSourceMap .~ srcMap
in In (insertSuspend $ prepareTerm e t) e (cesk ^. store) (FExec : (cesk ^. cont))

-- | Prepare a term for evaluation by a CESK machine in the given
-- environment: erase all type annotations, and optionally wrap it
-- in @pure@ if it does not have a command type. Note that since
-- the environment might contain type aliases, we have to be careful
-- to expand them before concluding whether the term has a command
-- type or not.
prepareTerm :: Env -> TSyntax -> Term
prepareTerm :: Env -> Syntax Elaborated -> Term Resolved
prepareTerm e t = case whnfType (e ^. envTydefs) (ptBody (t ^. sType)) of
TyCmd _ -> t'
_ -> TApp (TConst Pure) t'
where
t' = eraseS t
t' = erase t ^. sTerm

-- | Cancel the currently running computation.
cancel :: CESK -> CESK
Expand Down Expand Up @@ -418,6 +428,7 @@ prettyFrame f (p, inner) = case f of
FExec -> prettyPrefix "E·" (p, inner)
FBind Nothing _ t _ -> (0, pparens (p < 1) inner <+> ";" <+> ppr t)
FBind (Just x) _ t _ -> (0, hsep [ppr x, "<-", pparens (p < 1) inner, ";", ppr t])
FImport t -> (11, hsep ["import", inner, "in", ppr t])
FImmediate c _worldUpds _robotUpds -> prettyPrefix ("I[" <> ppr c <> "]·") (p, inner)
FUpdate {} -> (p, inner)
FFinishAtomic -> prettyPrefix "A·" (p, inner)
Expand Down
Loading
Loading