swarm-0.6.0.0: 2D resource gathering game with programmable robots
LicenseBSD-3-Clause
Safe HaskellSafe-Inferred
LanguageHaskell2010

Swarm.Language.Syntax.Util

Description

Helper functions for working with Terms and Syntax

Synopsis

Documentation

mkOp :: Const -> Syntax -> Syntax -> Syntax Source #

Make an infix operation (e.g. 2 + 3) a curried function application (e.g. ((+) 2) 3).

mkOp' :: Const -> Term -> Term -> Term Source #

Make an infix operation, discarding any location information

unfoldApps :: Syntax' ty -> NonEmpty (Syntax' ty) Source #

Turn function application chain into a list.

>>> syntaxWrap f = fmap (^. sTerm) . f . Syntax NoLoc
>>> syntaxWrap unfoldApps (mkOp' Mul (TInt 1) (TInt 2)) -- 1 * 2
TConst Mul :| [TInt 1,TInt 2]

mkTuple :: [Syntax] -> Syntax Source #

Create a nested tuple out of a list of syntax nodes.

unTuple :: Syntax' ty -> [Syntax' ty] Source #

Decompose a nested tuple into a list of components.

Erasure

erase :: Functor t => t ty -> t () Source #

Erase the type annotations from a Syntax or Term tree.

eraseS :: Syntax' ty -> Term Source #

Erase all annotations from a Syntax node, turning it into a bare Term.

Term traversal

freeVarsS :: forall ty. Traversal' (Syntax' ty) (Syntax' ty) Source #

Traversal over those subterms of a term which represent free variables. The S suffix indicates that it is a Traversal over the Syntax nodes (which contain type and source location info) containing free variables inside a larger Syntax value. Note that if you want to get the list of all Syntax nodes representing free variables, you can do so via toListOf freeVarsS.

freeVarsT :: forall ty. Traversal' (Syntax' ty) (Term' ty) Source #

Like freeVarsS, but traverse over the Terms containing free variables. More direct if you don't need to know the types or source locations of the variables. Note that if you want to get the list of all Terms representing free variables, you can do so via toListOf freeVarsT.

freeVarsV :: Traversal' (Syntax' ty) Var Source #

Traversal over the free variables of a term. Like freeVarsS and freeVarsT, but traverse over the variable names themselves. Note that if you want to get the set of all free variable names, you can do so via setOf freeVarsV.

mapFreeS :: Var -> (Syntax' ty -> Syntax' ty) -> Syntax' ty -> Syntax' ty Source #

Apply a function to all free occurrences of a particular variable.

asTree :: Data a => Syntax' a -> Tree (Syntax' a) Source #

Transform the AST into a Tree datatype. Useful for pretty-printing (e.g. via "Data.Tree.drawTree").

measureAstSize :: Data a => Syntax' a -> Int Source #

Each constructor is a assigned a value of 1, plus any recursive syntax it entails.