futhark-0.25.22: An optimising compiler for a functional, array-oriented language.
Safe HaskellSafe-Inferred
LanguageGHC2021

Futhark.Builder

Description

This module defines a convenience monad/typeclass for building ASTs. The fundamental building block is BuilderT and its execution functions, but it is usually easier to use Builder.

See Futhark.Construct for a high-level description.

Synopsis

A concrete MonadBuilder monad.

data BuilderT rep m a Source #

A monad transformer that tracks statements and provides a MonadBuilder instance, assuming that the underlying monad provides a name source. In almost all cases, this is what you will use for constructing statements (possibly as part of a larger monad stack). If you find yourself needing to implement MonadBuilder from scratch, then it is likely that you are making a mistake.

Instances

Instances details
(ASTRep rep, Monad m) => HasScope rep (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

lookupType :: VName -> BuilderT rep m Type Source #

lookupInfo :: VName -> BuilderT rep m (NameInfo rep) Source #

askScope :: BuilderT rep m (Scope rep) Source #

asksScope :: (Scope rep -> a) -> BuilderT rep m a Source #

(ASTRep rep, Monad m) => LocalScope rep (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

localScope :: Scope rep -> BuilderT rep m a -> BuilderT rep m a Source #

MonadError e m => MonadError e (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

throwError :: e -> BuilderT rep m a #

catchError :: BuilderT rep m a -> (e -> BuilderT rep m a) -> BuilderT rep m a #

MonadReader r m => MonadReader r (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

ask :: BuilderT rep m r #

local :: (r -> r) -> BuilderT rep m a -> BuilderT rep m a #

reader :: (r -> a) -> BuilderT rep m a #

MonadState s m => MonadState s (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

get :: BuilderT rep m s #

put :: s -> BuilderT rep m () #

state :: (s -> (a, s)) -> BuilderT rep m a #

MonadWriter w m => MonadWriter w (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

writer :: (a, w) -> BuilderT rep m a #

tell :: w -> BuilderT rep m () #

listen :: BuilderT rep m a -> BuilderT rep m (a, w) #

pass :: BuilderT rep m (a, w -> w) -> BuilderT rep m a #

MonadTrans (BuilderT rep) Source # 
Instance details

Defined in Futhark.Builder

Methods

lift :: Monad m => m a -> BuilderT rep m a #

Monad m => Applicative (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

pure :: a -> BuilderT rep m a #

(<*>) :: BuilderT rep m (a -> b) -> BuilderT rep m a -> BuilderT rep m b #

liftA2 :: (a -> b -> c) -> BuilderT rep m a -> BuilderT rep m b -> BuilderT rep m c #

(*>) :: BuilderT rep m a -> BuilderT rep m b -> BuilderT rep m b #

(<*) :: BuilderT rep m a -> BuilderT rep m b -> BuilderT rep m a #

Functor m => Functor (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

fmap :: (a -> b) -> BuilderT rep m a -> BuilderT rep m b #

(<$) :: a -> BuilderT rep m b -> BuilderT rep m a #

Monad m => Monad (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Methods

(>>=) :: BuilderT rep m a -> (a -> BuilderT rep m b) -> BuilderT rep m b #

(>>) :: BuilderT rep m a -> BuilderT rep m b -> BuilderT rep m b #

return :: a -> BuilderT rep m a #

(MonadFreshNames m, BuilderOps rep) => MonadBuilder (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

Associated Types

type Rep (BuilderT rep m) Source #

Methods

mkExpDecM :: Pat (LetDec (Rep (BuilderT rep m))) -> Exp (Rep (BuilderT rep m)) -> BuilderT rep m (ExpDec (Rep (BuilderT rep m))) Source #

mkBodyM :: Stms (Rep (BuilderT rep m)) -> Result -> BuilderT rep m (Body (Rep (BuilderT rep m))) Source #

mkLetNamesM :: [VName] -> Exp (Rep (BuilderT rep m)) -> BuilderT rep m (Stm (Rep (BuilderT rep m))) Source #

addStm :: Stm (Rep (BuilderT rep m)) -> BuilderT rep m () Source #

addStms :: Stms (Rep (BuilderT rep m)) -> BuilderT rep m () Source #

collectStms :: BuilderT rep m a -> BuilderT rep m (a, Stms (Rep (BuilderT rep m))) Source #

certifying :: Certs -> BuilderT rep m a -> BuilderT rep m a Source #

MonadFreshNames m => MonadFreshNames (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

type Rep (BuilderT rep m) Source # 
Instance details

Defined in Futhark.Builder

type Rep (BuilderT rep m) = rep

runBuilderT :: MonadFreshNames m => BuilderT rep m a -> Scope rep -> m (a, Stms rep) Source #

Run a binder action given an initial scope, returning a value and the statements added (addStm) during the action.

runBuilderT_ :: MonadFreshNames m => BuilderT rep m () -> Scope rep -> m (Stms rep) Source #

Like runBuilderT, but return only the statements.

runBuilderT' :: (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => BuilderT rep m a -> m (a, Stms rep) Source #

Like runBuilderT, but get the initial scope from the current monad.

runBuilderT'_ :: (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => BuilderT rep m a -> m (Stms rep) Source #

Like runBuilderT_, but get the initial scope from the current monad.

class ASTRep rep => BuilderOps rep where Source #

A BuilderT (and by extension, a Builder) is only an instance of MonadBuilder for representations that implement this type class, which contains methods for constructing statements.

Minimal complete definition

Nothing

Methods

mkExpDecB :: (MonadBuilder m, Rep m ~ rep) => Pat (LetDec rep) -> Exp rep -> m (ExpDec rep) Source #

default mkExpDecB :: (MonadBuilder m, Buildable rep) => Pat (LetDec rep) -> Exp rep -> m (ExpDec rep) Source #

mkBodyB :: (MonadBuilder m, Rep m ~ rep) => Stms rep -> Result -> m (Body rep) Source #

default mkBodyB :: (MonadBuilder m, Buildable rep) => Stms rep -> Result -> m (Body rep) Source #

mkLetNamesB :: (MonadBuilder m, Rep m ~ rep) => [VName] -> Exp rep -> m (Stm rep) Source #

default mkLetNamesB :: (MonadBuilder m, Rep m ~ rep, Buildable rep) => [VName] -> Exp rep -> m (Stm rep) Source #

Instances

Instances details
BuilderOps GPU Source # 
Instance details

Defined in Futhark.IR.GPU

Methods

mkExpDecB :: (MonadBuilder m, Rep m ~ GPU) => Pat (LetDec GPU) -> Exp GPU -> m (ExpDec GPU) Source #

mkBodyB :: (MonadBuilder m, Rep m ~ GPU) => Stms GPU -> Result -> m (Body GPU) Source #

mkLetNamesB :: (MonadBuilder m, Rep m ~ GPU) => [VName] -> Exp GPU -> m (Stm GPU) Source #

BuilderOps GPUMem Source # 
Instance details

Defined in Futhark.IR.GPUMem

BuilderOps MC Source # 
Instance details

Defined in Futhark.IR.MC

Methods

mkExpDecB :: (MonadBuilder m, Rep m ~ MC) => Pat (LetDec MC) -> Exp MC -> m (ExpDec MC) Source #

mkBodyB :: (MonadBuilder m, Rep m ~ MC) => Stms MC -> Result -> m (Body MC) Source #

mkLetNamesB :: (MonadBuilder m, Rep m ~ MC) => [VName] -> Exp MC -> m (Stm MC) Source #

BuilderOps MCMem Source # 
Instance details

Defined in Futhark.IR.MCMem

BuilderOps SOACS Source # 
Instance details

Defined in Futhark.IR.SOACS

BuilderOps Seq Source # 
Instance details

Defined in Futhark.IR.Seq

Methods

mkExpDecB :: (MonadBuilder m, Rep m ~ Seq) => Pat (LetDec Seq) -> Exp Seq -> m (ExpDec Seq) Source #

mkBodyB :: (MonadBuilder m, Rep m ~ Seq) => Stms Seq -> Result -> m (Body Seq) Source #

mkLetNamesB :: (MonadBuilder m, Rep m ~ Seq) => [VName] -> Exp Seq -> m (Stm Seq) Source #

BuilderOps SeqMem Source # 
Instance details

Defined in Futhark.IR.SeqMem

(ASTRep rep, AliasedOp (OpC rep), Buildable (Aliases rep)) => BuilderOps (Aliases rep) Source # 
Instance details

Defined in Futhark.IR.Aliases

Methods

mkExpDecB :: (MonadBuilder m, Rep m ~ Aliases rep) => Pat (LetDec (Aliases rep)) -> Exp (Aliases rep) -> m (ExpDec (Aliases rep)) Source #

mkBodyB :: (MonadBuilder m, Rep m ~ Aliases rep) => Stms (Aliases rep) -> Result -> m (Body (Aliases rep)) Source #

mkLetNamesB :: (MonadBuilder m, Rep m ~ Aliases rep) => [VName] -> Exp (Aliases rep) -> m (Stm (Aliases rep)) Source #

BuilderOps (Wise GPU) Source # 
Instance details

Defined in Futhark.IR.GPU.Simplify

BuilderOps (Wise GPUMem) Source # 
Instance details

Defined in Futhark.IR.GPUMem

BuilderOps (Wise MC) Source # 
Instance details

Defined in Futhark.IR.MC

Methods

mkExpDecB :: (MonadBuilder m, Rep m ~ Wise MC) => Pat (LetDec (Wise MC)) -> Exp (Wise MC) -> m (ExpDec (Wise MC)) Source #

mkBodyB :: (MonadBuilder m, Rep m ~ Wise MC) => Stms (Wise MC) -> Result -> m (Body (Wise MC)) Source #

mkLetNamesB :: (MonadBuilder m, Rep m ~ Wise MC) => [VName] -> Exp (Wise MC) -> m (Stm (Wise MC)) Source #

BuilderOps (Wise MCMem) Source # 
Instance details

Defined in Futhark.IR.MCMem

BuilderOps (Wise SOACS) Source # 
Instance details

Defined in Futhark.IR.SOACS.Simplify

BuilderOps (Wise Seq) Source # 
Instance details

Defined in Futhark.IR.Seq

BuilderOps (Wise SeqMem) Source # 
Instance details

Defined in Futhark.IR.SeqMem

type Builder rep = BuilderT rep (State VNameSource) Source #

The most commonly used binder monad.

runBuilder :: (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => Builder rep a -> m (a, Stms rep) Source #

Run a binder action, returning a value and the statements added (addStm) during the action. Assumes that the current monad provides initial scope and name source.

runBuilder_ :: (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => Builder rep a -> m (Stms rep) Source #

Like runBuilder, but throw away the result and just return the added statements.

runBodyBuilder :: (Buildable rep, MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => Builder rep (Body rep) -> m (Body rep) Source #

Run a binder that produces a Body, and prefix that Body by the statements produced during execution of the action.

runLambdaBuilder :: (Buildable rep, MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => [LParam rep] -> Builder rep Result -> m (Lambda rep) Source #

Given lambda parameters, Run a builder action that produces the statements and returns the Result of the lambda body.

The MonadBuilder typeclass