futhark-0.25.27: An optimising compiler for a functional, array-oriented language.

Safe Haskell | Safe-Inferred
Language | GHC2021

Futhark.Builder

Contents

- A concrete `MonadBuilder` monad.
- The `MonadBuilder` typeclass

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

- data BuilderT rep m a
- runBuilderT :: MonadFreshNames m => BuilderT rep m a -> Scope rep -> m (a, Stms rep)
- runBuilderT_ :: MonadFreshNames m => BuilderT rep m () -> Scope rep -> m (Stms rep)
- runBuilderT' :: (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => BuilderT rep m a -> m (a, Stms rep)
- runBuilderT'_ :: (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => BuilderT rep m a -> m (Stms rep)
- class ASTRep rep => BuilderOps rep where
  - mkExpDecB :: (MonadBuilder m, Rep m ~ rep) => Pat (LetDec rep) -> Exp rep -> m (ExpDec rep)
  - mkBodyB :: (MonadBuilder m, Rep m ~ rep) => Stms rep -> Result -> m (Body rep)
  - mkLetNamesB :: (MonadBuilder m, Rep m ~ rep) => [VName] -> Exp rep -> m (Stm rep)
- type Builder rep = BuilderT rep (State VNameSource)
- runBuilder :: (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => Builder rep a -> m (a, Stms rep)
- runBuilder_ :: (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => Builder rep a -> m (Stms rep)
- runBodyBuilder :: (Buildable rep, MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => Builder rep Result -> m (Body rep)
- runLambdaBuilder :: (Buildable rep, MonadFreshNames m, HasScope somerep m, SameScope somerep rep) => [LParam rep] -> Builder rep Result -> m (Lambda rep)
- module Futhark.Builder.Class

# 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 Source # catchError :: BuilderT rep m a -> (e -> BuilderT rep m a) -> BuilderT rep m a Source #
MonadReader r m => MonadReader r (BuilderT rep m) Source # |
Instance details Defined in Futhark.Builder Methods ask :: BuilderT rep m r Source # local :: (r -> r) -> BuilderT rep m a -> BuilderT rep m a Source # reader :: (r -> a) -> BuilderT rep m a Source #
MonadState s m => MonadState s (BuilderT rep m) Source # |
Instance details Defined in Futhark.Builder Methods get :: BuilderT rep m s Source # put :: s -> BuilderT rep m () Source # state :: (s -> (a, s)) -> BuilderT rep m a Source #
MonadWriter w m => MonadWriter w (BuilderT rep m) Source # |
Instance details Defined in Futhark.Builder Methods writer :: (a, w) -> BuilderT rep m a Source # tell :: w -> BuilderT rep m () Source # listen :: BuilderT rep m a -> BuilderT rep m (a, w) Source # pass :: BuilderT rep m (a, w -> w) -> BuilderT rep m a Source #
MonadTrans (BuilderT rep) Source # |
Instance details Defined in Futhark.Builder Methods lift :: Monad m => m a -> BuilderT rep m a Source #
Monad m => Applicative (BuilderT rep m) Source # |

Instance details

Defined in Futhark.Builder

Methods

pure :: a -> BuilderT rep m a Source #

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

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

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

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

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 Source # (<$) :: a -> BuilderT rep m b -> BuilderT rep m a Source #
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 Source # (>>) :: BuilderT rep m a -> BuilderT rep m b -> BuilderT rep m b Source # return :: a -> BuilderT rep m a Source #
(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 Methods getNameSource :: BuilderT rep m VNameSource Source # putNameSource :: VNameSource -> BuilderT rep m () Source #
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 builder 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 Methods mkExpDecB :: (MonadBuilder m, Rep m ~ GPUMem) => Pat (LetDec GPUMem) -> Exp GPUMem -> m (ExpDec GPUMem) Source # mkBodyB :: (MonadBuilder m, Rep m ~ GPUMem) => Stms GPUMem -> Result -> m (Body GPUMem) Source # mkLetNamesB :: (MonadBuilder m, Rep m ~ GPUMem) => [VName] -> Exp GPUMem -> m (Stm GPUMem) Source #
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 Methods mkExpDecB :: (MonadBuilder m, Rep m ~ MCMem) => Pat (LetDec MCMem) -> Exp MCMem -> m (ExpDec MCMem) Source # mkBodyB :: (MonadBuilder m, Rep m ~ MCMem) => Stms MCMem -> Result -> m (Body MCMem) Source # mkLetNamesB :: (MonadBuilder m, Rep m ~ MCMem) => [VName] -> Exp MCMem -> m (Stm MCMem) Source #
BuilderOps SOACS Source # |
Instance details Defined in Futhark.IR.SOACS Methods mkExpDecB :: (MonadBuilder m, Rep m ~ SOACS) => Pat (LetDec SOACS) -> Exp SOACS -> m (ExpDec SOACS) Source # mkBodyB :: (MonadBuilder m, Rep m ~ SOACS) => Stms SOACS -> Result -> m (Body SOACS) Source # mkLetNamesB :: (MonadBuilder m, Rep m ~ SOACS) => [VName] -> Exp SOACS -> m (Stm SOACS) Source #
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 Methods mkExpDecB :: (MonadBuilder m, Rep m ~ SeqMem) => Pat (LetDec SeqMem) -> Exp SeqMem -> m (ExpDec SeqMem) Source # mkBodyB :: (MonadBuilder m, Rep m ~ SeqMem) => Stms SeqMem -> Result -> m (Body SeqMem) Source # mkLetNamesB :: (MonadBuilder m, Rep m ~ SeqMem) => [VName] -> Exp SeqMem -> m (Stm SeqMem) Source #
(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

Methods

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

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

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

BuilderOps (Wise GPUMem) Source # |

Instance details

Defined in Futhark.IR.GPUMem

Methods

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

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

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

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

Methods

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

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

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

BuilderOps (Wise SOACS) Source # |

Instance details

Defined in Futhark.IR.SOACS.Simplify

Methods

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

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

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

BuilderOps (Wise Seq) Source # |
Instance details Defined in Futhark.IR.Seq Methods mkExpDecB :: (MonadBuilder m, Rep m ~ Wise Seq) => Pat (LetDec (Wise Seq)) -> Exp (Wise Seq) -> m (ExpDec (Wise Seq)) Source # mkBodyB :: (MonadBuilder m, Rep m ~ Wise Seq) => Stms (Wise Seq) -> Result -> m (Body (Wise Seq)) Source # mkLetNamesB :: (MonadBuilder m, Rep m ~ Wise Seq) => [VName] -> Exp (Wise Seq) -> m (Stm (Wise Seq)) Source #
BuilderOps (Wise SeqMem) Source # |

Instance details

Defined in Futhark.IR.SeqMem

Methods

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

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

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

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

The most commonly used builder monad.

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

Run a builder 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 Result -> m (Body rep) Source #

Run a builder that produces a `Result` and construct a body that contains that result alongside the statements produced during the builder.

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

module Futhark.Builder.Class