Copyright | Brent Yorgey |
---|---|
License | BSD-3-Clause |
Maintainer | byorgey@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Abstract syntax for terms of the Swarm programming language.
Synopsis
- data Direction
- data DirInfo = DirInfo {}
- applyTurn :: Direction -> V2 Int64 -> V2 Int64
- toDirection :: V2 Int64 -> Maybe Direction
- fromDirection :: Direction -> V2 Int64
- allDirs :: [Direction]
- isCardinal :: Direction -> Bool
- dirInfo :: Direction -> DirInfo
- north :: V2 Int64
- south :: V2 Int64
- east :: V2 Int64
- west :: V2 Int64
- data Const
- = Noop
- | Wait
- | Selfdestruct
- | Move
- | Turn
- | Grab
- | Harvest
- | Place
- | Give
- | Install
- | Make
- | Has
- | Installed
- | Count
- | Drill
- | Build
- | Salvage
- | Reprogram
- | Say
- | Listen
- | Log
- | View
- | Appear
- | Create
- | Time
- | Whereami
- | Blocked
- | Scan
- | Upload
- | Ishere
- | Self
- | Parent
- | Base
- | Whoami
- | Setname
- | Random
- | Run
- | If
- | Inl
- | Inr
- | Case
- | Fst
- | Snd
- | Force
- | Return
- | Try
- | Undefined
- | Fail
- | Not
- | Neg
- | Eq
- | Neq
- | Lt
- | Gt
- | Leq
- | Geq
- | Or
- | And
- | Add
- | Sub
- | Mul
- | Div
- | Exp
- | Format
- | Concat
- | Chars
- | Split
- | AppF
- | Swap
- | Atomic
- | Teleport
- | As
- | RobotNamed
- | RobotNumbered
- | Knows
- allConst :: [Const]
- data ConstInfo = ConstInfo {}
- data ConstDoc = ConstDoc {}
- data ConstMeta
- data MBinAssoc
- data MUnAssoc
- constInfo :: Const -> ConstInfo
- arity :: Const -> Int
- isCmd :: Const -> Bool
- isUserFunc :: Const -> Bool
- isOperator :: Const -> Bool
- isBuiltinFunction :: Const -> Bool
- isTangible :: Const -> Bool
- isLong :: Const -> Bool
- data Syntax = Syntax {}
- data Location
- noLoc :: Term -> Syntax
- pattern STerm :: Term -> Syntax
- pattern TPair :: Term -> Term -> Term
- pattern TLam :: Var -> Maybe Type -> Term -> Term
- pattern TApp :: Term -> Term -> Term
- pattern (:$:) :: Term -> Syntax -> Term
- pattern TLet :: Bool -> Var -> Maybe Polytype -> Term -> Term -> Term
- pattern TDef :: Bool -> Var -> Maybe Polytype -> Term -> Term
- pattern TBind :: Maybe Var -> Term -> Term -> Term
- pattern TDelay :: DelayType -> Term -> Term
- type Var = Text
- data DelayType
- = SimpleDelay
- | MemoizedDelay (Maybe Var)
- data Term
- = TUnit
- | TConst Const
- | TDir Direction
- | TInt Integer
- | TAntiInt Text
- | TText Text
- | TAntiText Text
- | TBool Bool
- | TRobot Int
- | TRef Int
- | TRequireDevice Text
- | TRequire Int Text
- | TVar Var
- | SPair Syntax Syntax
- | SLam Var (Maybe Type) Syntax
- | SApp Syntax Syntax
- | SLet Bool Var (Maybe Polytype) Syntax Syntax
- | SDef Bool Var (Maybe Polytype) Syntax
- | SBind (Maybe Var) Syntax Syntax
- | SDelay DelayType Syntax
- mkOp :: Const -> Syntax -> Syntax -> Syntax
- mkOp' :: Const -> Term -> Term -> Term
- fvT :: Traversal' Term Term
- fv :: Traversal' Term Var
- mapFree1 :: Var -> (Term -> Term) -> Term -> Term
Directions
The type of directions. Used e.g. to indicate which way a robot will turn.
Instances
toDirection :: V2 Int64 -> Maybe Direction Source #
Possibly convert a vector into a Direction
---that is, if the
vector happens to be a unit vector in one of the cardinal
directions.
Constants
Constants, representing various built-in functions and commands.
IF YOU ADD A NEW CONSTANT, be sure to also update:
1. the constInfo
function (below)
2. the capability checker (Swarm.Language.Capability)
3. the type checker (Swarm.Language.Typecheck)
4. the runtime (Swarm.Game.Step)
5. the emacs mode syntax highlighter (contribs/swarm-mode.el
)
GHC will warn you about incomplete pattern matches for the first
four, so it's not really possible to forget. Note you do not
need to update the parser or pretty-printer, since they are
auto-generated from constInfo
.
Noop | Do nothing. This is different than |
Wait | Wait for a number of time steps without doing anything. |
Selfdestruct | Self-destruct. |
Move | Move forward one step. |
Turn | Turn in some direction. |
Grab | Grab an item from the current location. |
Harvest | Harvest an item from the current location. |
Place | Try to place an item at the current location. |
Give | Give an item to another robot at the current location. |
Install | Install a device on a robot. |
Make | Make an item. |
Has | Sense whether we have a certain item. |
Installed | Sense whether we have a certain device installed. |
Count | Sense how many of a certain item we have. |
Drill | Drill through an entity. |
Build | Construct a new robot. |
Salvage | Deconstruct an old robot. |
Reprogram | Reprogram a robot that has executed it's command with a new command |
Say | Emit a message. |
Listen | Listen for a message from other robots. |
Log | Emit a log message. |
View | View a certain robot. |
Appear | Set what characters are used for display. |
Create | Create an entity out of thin air. Only available in creative mode. |
Time | Get current time |
Whereami | Get the current x, y coordinates |
Blocked | See if we can move forward or not. |
Scan | Scan a nearby cell |
Upload | Upload knowledge to another robot |
Ishere | See if a specific entity is here. (This may be removed.) |
Self | Get a reference to oneself |
Parent | Get the robot's parent |
Base | Get a reference to the base |
Whoami | Get the robot's display name |
Setname | Set the robot's display name |
Random | Get a uniformly random integer. |
Run | Run a program loaded from a file. |
If | If-expressions. |
Inl | Left injection. |
Inr | Right injection. |
Case | Case analysis on a sum type. |
Fst | First projection. |
Snd | Second projection. |
Force | Force a delayed evaluation. |
Return | Return for the cmd monad. |
Try | Try/catch block |
Undefined | Undefined |
Fail | User error |
Not | Logical negation. |
Neg | Arithmetic negation. |
Eq | Logical equality comparison |
Neq | Logical unequality comparison |
Lt | Logical lesser-then comparison |
Gt | Logical greater-then comparison |
Leq | Logical lesser-or-equal comparison |
Geq | Logical greater-or-equal comparison |
Or | Logical or. |
And | Logical and. |
Add | Arithmetic addition operator |
Sub | Arithmetic subtraction operator |
Mul | Arithmetic multiplication operator |
Div | Arithmetic division operator |
Exp | Arithmetic exponentiation operator |
Format | Turn an arbitrary value into a string |
Concat | Concatenate string values |
Chars | Count number of characters. |
Split | Split string into two parts. |
AppF | Application operator - helps to avoid parentheses:
|
Swap | Swap placed entity with one in inventory. Essentially atomic grab and place. |
Atomic | When executing |
Teleport | Teleport a robot to the given position. |
As | Run a command as if you were another robot. |
RobotNamed | Find a robot by name. |
RobotNumbered | Find a robot by number. |
Knows | Check if an entity is known. |
Instances
ConstMFunc Int Bool | Function with arity of which some are commands |
ConstMUnOp MUnAssoc | Unary operator with fixity and associativity. |
ConstMBinOp MBinAssoc | Binary operator with fixity and associativity. |
The meta type representing associativity of binary operator.
L | Left associative binary operator (see |
N | Non-associative binary operator (see |
R | Right associative binary operator (see |
The meta type representing associativity of unary operator.
constInfo :: Const -> ConstInfo Source #
Information about constants used in parsing and pretty printing.
It would be more compact to represent the information by testing whether the constants are in certain sets, but using pattern matching gives us warning if we add more constants.
arity :: Const -> Int Source #
The arity of a constant, i.e. how many arguments it expects.
The runtime system will collect arguments to a constant (see
VCApp
) until it has enough, then dispatch
the constant's behavior.
isCmd :: Const -> Bool Source #
Whether a constant represents a command. Constants which are
not commands are functions which are interpreted as soon as
they are evaluated. Commands, on the other hand, are not
interpreted until being executed, that is, when meeting an
FExec
frame. When evaluated, commands simply turn into a
VCApp
.
isUserFunc :: Const -> Bool Source #
Function constants user can call with reserved words (wait
,...).
isOperator :: Const -> Bool Source #
Whether the constant is an operator. Useful predicate for documentation.
isBuiltinFunction :: Const -> Bool Source #
Whether the constant is a function which is interpreted as soon as it is evaluated, but *not* including operators.
Note: This is used for documentation purposes and complements isCmd
and isOperator
in that exactly one will accept a given constant.
isTangible :: Const -> Bool Source #
Whether the constant is a tangible command, that has an external effect on the world. At most one tangible command may be executed per tick.
isLong :: Const -> Bool Source #
Whether the constant is a long command, that is, a tangible
command which could require multiple ticks to execute. Such
commands cannot be allowed in atomic
blocks.
Syntax
The surface syntax for the language
Instances
FromJSON Syntax Source # | |
ToJSON Syntax Source # | |
Defined in Swarm.Language.Syntax | |
Data Syntax Source # | |
Defined in Swarm.Language.Syntax gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Syntax -> c Syntax # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Syntax # toConstr :: Syntax -> Constr # dataTypeOf :: Syntax -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Syntax) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Syntax) # gmapT :: (forall b. Data b => b -> b) -> Syntax -> Syntax # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Syntax -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Syntax -> r # gmapQ :: (forall d. Data d => d -> u) -> Syntax -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Syntax -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Syntax -> m Syntax # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Syntax -> m Syntax # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Syntax -> m Syntax # | |
Generic Syntax Source # | |
Show Syntax Source # | |
Eq Syntax Source # | |
type Rep Syntax Source # | |
Defined in Swarm.Language.Syntax type Rep Syntax = D1 ('MetaData "Syntax" "Swarm.Language.Syntax" "swarm-0.1.0.0-CFIPFkeeTOhKLDsfeG4aYn" 'False) (C1 ('MetaCons "Syntax" 'PrefixI 'True) (S1 ('MetaSel ('Just "sLoc") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Location) :*: S1 ('MetaSel ('Just "sTerm") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Term))) |
Instances
FromJSON Location Source # | |
ToJSON Location Source # | |
Defined in Swarm.Language.Syntax | |
Data Location Source # | |
Defined in Swarm.Language.Syntax gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Location -> c Location # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Location # toConstr :: Location -> Constr # dataTypeOf :: Location -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Location) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Location) # gmapT :: (forall b. Data b => b -> b) -> Location -> Location # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Location -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Location -> r # gmapQ :: (forall d. Data d => d -> u) -> Location -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Location -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Location -> m Location # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Location -> m Location # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Location -> m Location # | |
Monoid Location Source # | |
Semigroup Location Source # | |
Generic Location Source # | |
Show Location Source # | |
Eq Location Source # | |
type Rep Location Source # | |
Defined in Swarm.Language.Syntax type Rep Location = D1 ('MetaData "Location" "Swarm.Language.Syntax" "swarm-0.1.0.0-CFIPFkeeTOhKLDsfeG4aYn" 'False) (C1 ('MetaCons "NoLoc" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Location" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Int))) |
pattern (:$:) :: Term -> Syntax -> Term infixl 0 Source #
Convenient infix pattern synonym for application.
pattern TLet :: Bool -> Var -> Maybe Polytype -> Term -> Term -> Term Source #
Match a TLet without syntax
Terms
COMPLETE pragma tells GHC using this set of pattern is complete for Term
Different runtime behaviors for delayed expressions.
SimpleDelay | A simple delay, implemented via a (non-memoized) |
MemoizedDelay (Maybe Var) | A memoized delay, implemented by allocating a mutable cell
with the delayed expression and returning a reference to it.
When the |
Instances
FromJSON DelayType Source # | |
ToJSON DelayType Source # | |
Defined in Swarm.Language.Syntax | |
Data DelayType Source # | |
Defined in Swarm.Language.Syntax gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> DelayType -> c DelayType # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c DelayType # toConstr :: DelayType -> Constr # dataTypeOf :: DelayType -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c DelayType) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c DelayType) # gmapT :: (forall b. Data b => b -> b) -> DelayType -> DelayType # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> DelayType -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> DelayType -> r # gmapQ :: (forall d. Data d => d -> u) -> DelayType -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> DelayType -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> DelayType -> m DelayType # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> DelayType -> m DelayType # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> DelayType -> m DelayType # | |
Generic DelayType Source # | |
Show DelayType Source # | |
Eq DelayType Source # | |
type Rep DelayType Source # | |
Defined in Swarm.Language.Syntax type Rep DelayType = D1 ('MetaData "DelayType" "Swarm.Language.Syntax" "swarm-0.1.0.0-CFIPFkeeTOhKLDsfeG4aYn" 'False) (C1 ('MetaCons "SimpleDelay" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "MemoizedDelay" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 (Maybe Var)))) |
Terms of the Swarm language.
TUnit | The unit value. |
TConst Const | A constant. |
TDir Direction | A direction literal. |
TInt Integer | An integer literal. |
TAntiInt Text | An antiquoted Haskell variable name of type Integer. |
TText Text | A text literal. |
TAntiText Text | An antiquoted Haskell variable name of type Text. |
TBool Bool | A Boolean literal. |
TRobot Int | A robot value. These never show up in surface syntax, but are here so we can factor pretty-printing for Values through pretty-printing for Terms. |
TRef Int | A memory reference. These likewise never show up in surface syntax, but are here to facilitate pretty-printing. |
TRequireDevice Text | Require a specific device to be installed. |
TRequire Int Text | Require a certain number of an entity. |
TVar Var | A variable. |
SPair Syntax Syntax | A pair. |
SLam Var (Maybe Type) Syntax | A lambda expression, with or without a type annotation on the binder. |
SApp Syntax Syntax | Function application. |
SLet Bool Var (Maybe Polytype) Syntax Syntax | A (recursive) let expression, with or without a type
annotation on the variable. The |
SDef Bool Var (Maybe Polytype) Syntax | A (recursive) definition command, which binds a variable to a
value in subsequent commands. The |
SBind (Maybe Var) Syntax Syntax | A monadic bind for commands, of the form |
SDelay DelayType Syntax | Delay evaluation of a term, written |
Instances
mkOp :: Const -> Syntax -> Syntax -> Syntax Source #
Make infix operation (e.g. 2 + 3
) a curried function
application (((+) 2) 3
).
mkOp' :: Const -> Term -> Term -> Term Source #
Make infix operation, discarding any syntax related location
Term traversal
fvT :: Traversal' Term Term Source #
Traversal over those subterms of a term which represent free variables.