sequent-core-0.5.0.1: Alternative Core language for GHC plugins

Maintainermaurerl@cs.uoregon.edu
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Language.SequentCore.Syntax

Contents

Description

The AST for Sequent Core, with basic operations.

Synopsis

AST Types

data Term b Source

An expression producing a value. These include literals, lambdas, and variables, as well as types and coercions (see GHC's Expr for the reasoning). They also include computed values, which bind the current continuation in the body of a command.

Constructors

Lit Literal

A primitive literal value.

Var Id

A term variable. Must not be a nullary constructor; use Cons for this.

Lam [b] b (Command b)

A function. Binds some arguments and a continuation. The body is a command.

Cons DataCon [Term b]

A value formed by a saturated constructor application.

Compute b (Command b)

A value produced by a computation. Binds a continuation.

Type Type

A type. Used to pass a type as an argument to a type-level lambda.

Coercion Coercion

A coercion. Used to pass evidence for the cast operation to a lambda.

Cont (Cont b)

A continuation. Allowed only as the body of a non-recursive let.

Instances

data Cont b Source

A continuation, representing a strict context of a Haskell expression. Computation in the sequent calculus is expressed as the interaction of a value with a continuation.

Constructors

App (Term b) (Cont b)

Apply the value to an argument.

Case b [Alt b]

Perform case analysis on the value.

Cast Coercion (Cont b)

Cast the value using the given coercion.

Tick (Tickish Id) (Cont b)

Annotate the enclosed frame. Used by the profiler.

Return ContId

Reference to a bound continuation.

Instances

data Command b Source

A general computation. A command brings together a list of bindings, some term, and a continuation saying what to do with the value produced by the term. The term and continuation comprise a cut in the sequent calculus.

Invariant: If cmdTerm is a variable representing a constructor, then cmdCont must not begin with as many App frames as the constructor's arity. In other words, the command must not represent a saturated application of a constructor. Such an application should be represented by a Cons value instead. When in doubt, use mkCommand to enforce this invariant.

Constructors

Command 

Fields

cmdLet :: [Bind b]

Bindings surrounding the computation.

cmdTerm :: Term b

The term producing the value.

cmdCont :: Cont b

What to do with the value.

Instances

data Bind b Source

A binding. Similar to the Bind datatype from GHC. Can be either a single non-recursive binding or a mutually recursive block.

Constructors

NonRec b (Term b)

A single non-recursive binding.

Rec [(b, Term b)]

A block of mutually recursive bindings.

Instances

data Alt b Source

A case alternative. Given by the head constructor (or literal), a list of bound variables (empty for a literal), and the body as a Command.

Constructors

Alt AltCon [b] (Command b) 

Instances

data AltCon :: *

A case alternative constructor (i.e. pattern match)

Constructors

DataAlt DataCon 
LitAlt Literal

A literal: case e of { 1 -> ... } Invariant: always an *unlifted* literal See Note [Literal alternatives]

DEFAULT

Trivial alternative: case e of { _ -> ... }

data Expr a Source

Some expression -- a term, a command, or a continuation. Useful for writing mutually recursive functions.

Constructors

T 

Fields

unT :: Term a
 
C 

Fields

unC :: Command a
 
K 

Fields

unK :: Cont a
 

type Program a = [Bind a] Source

An entire program.

type ContId = Id Source

The identifier for a covariable, which is like a variable but it binds a continuation.

type SeqCoreTerm = Term Var Source

Usual instance of Term, with Vars for binders

type SeqCoreCont = Cont Var Source

Usual instance of Cont, with Vars for binders

type SeqCoreCommand = Command Var Source

Usual instance of Command, with Vars for binders

type SeqCoreBind = Bind Var Source

Usual instance of Bind, with Vars for binders

type SeqCoreBndr = Var Source

Usual binders for Sequent Core terms

type SeqCoreAlt = Alt Var Source

Usual instance of Alt, with Vars for binders

type SeqCoreExpr = Expr Var Source

Usual instance of Expr, with Vars for binders

type SeqCoreProgram = Program Var Source

Usual instance of Term, with Vars for binders

Constructors

mkCommand :: HasId b => [Bind b] -> Term b -> Cont b -> Command b Source

Constructs a command, given let bindings, a term, and a continuation.

This smart constructor enforces the invariant that a saturated constructor invocation is represented as a Cons value rather than using App frames.

mkCompute :: HasId b => b -> Command b -> Term b Source

Wraps a command that returns to the given continuation id in a term using Compute. If the command is a value command (see asValueCommand), unwraps it instead.

addLets :: [Bind b] -> Command b -> Command b Source

Adds the given bindings outside those in the given command.

addNonRec :: HasId b => b -> Term b -> Command b -> Command b Source

Adds the given binding outside the given command, possibly using a case binding rather than a let. See CoreSyn on the let/app invariant.

Deconstructors

lambdas :: Term b -> ([b], Maybe (b, Command b)) Source

collectArgs :: Cont b -> ([Term b], Cont b) Source

Divide a continuation into a sequence of arguments and an outer continuation. If k is not an application continuation, then collectArgs k == ([], k).

collectTypeArgs :: Cont b -> ([KindOrType], Cont b) Source

Divide a continuation into a sequence of type arguments and an outer continuation. If k is not an application continuation or only applies non-type arguments, then collectTypeArgs k == ([], k).

collectTypeAndOtherArgs :: Cont b -> ([KindOrType], [Term b], Cont b) Source

Divide a continuation into a sequence of type arguments, then a sequence of non-type arguments, then an outer continuation. If k is not an application continuation, then collectTypeAndOtherArgs k == ([], [], k).

collectArgsUpTo :: Int -> Cont b -> ([Term b], Cont b) Source

Divide a continuation into a sequence of up to n arguments and an outer continuation. If k is not an application continuation, then collectArgsUpTo n k == ([], k).

partitionTypes :: [Term b] -> ([KindOrType], [Term b]) Source

Divide a list of terms into an initial sublist of types and the remaining terms.

isLambda :: Command b -> Bool Source

True if the given command is a simple lambda, with no let bindings and no continuation.

isTypeTerm :: Term b -> Bool Source

True if the given term is a type. See Type.

isCoTerm :: Term b -> Bool Source

True if the given term is a coercion. See Coercion.

isErasedTerm :: Term b -> Bool Source

True if the given term is a type or coercion.

isRuntimeTerm :: Term b -> Bool Source

True if the given term appears at runtime, i.e. is neither a type nor a coercion.

isProperTerm :: Term b -> Bool Source

True if the given term can appear in an expression without restriction. Not true if the term is a type, coercion, or continuation; these can only appear on the RHS of a let or (except for continuations) as an argument in a function call.

isTrivial :: HasId b => Command b -> Bool Source

True if the given command is so simple we can duplicate it freely. This means it has no bindings and its term and continuation are both trivial.

isTrivialTerm :: HasId b => Term b -> Bool Source

True if the given term is so simple we can duplicate it freely. Some literals are not trivial, and a lambda whose argument is not erased or whose body is non-trivial is also non-trivial.

isTrivialCont :: Cont b -> Bool Source

True if the given continuation is so simple we can duplicate it freely. This is true of casts and of applications of erased arguments (types and coercions). Ticks are not considered trivial, since this would cause them to be inlined.

isReturnCont :: Cont b -> Bool Source

True if the given continuation is a return continuation, Return _.

commandAsSaturatedCall :: HasId b => Command b -> Maybe (Term b, [Term b], Cont b) Source

If a command represents a saturated call to some function, splits it into the function, the arguments, and the remaining continuation after the arguments.

asSaturatedCall :: HasId b => Term b -> Cont b -> Maybe ([Term b], Cont b) Source

If the given term is a function, and the given continuation would provide enough arguments to saturate it, returns the arguments and the remainder of the continuation.

asValueCommand :: ContId -> Command b -> Maybe (Term b) Source

If a command does nothing but provide a value to the given continuation id, returns that value.

flattenBind :: Bind b -> [(b, Term b)] Source

flattenBinds :: [Bind b] -> [(b, Term b)] Source

bindersOf :: Bind b -> [b] Source

bindersOfBinds :: [Bind b] -> [b] Source

Calculations

termArity :: HasId b => Term b -> Int Source

Compute (a conservative estimate of) the arity of a term.

termType :: HasId b => Term b -> Type Source

Compute the type of a term.

contType :: HasId b => Cont b -> Type Source

Compute the type a continuation accepts. If contType cont is Foo and cont is bound to k, then k's idType will be !Foo.

termIsBottom :: Term b -> Bool Source

Find whether an expression is definitely bottom.

commandIsBottom :: Command b -> Bool Source

Find whether a command definitely evaluates to bottom.

needsCaseBinding :: Type -> Term b -> Bool Source

Decide whether a term should be bound using case rather than let. See needsCaseBinding.

Continuation ids

isContId :: Id -> Bool Source

Find whether an id is a continuation id.

asContId :: Id -> ContId Source

Tag an id as a continuation id.

Alpha-equivalence

(=~=) :: AlphaEq a => a -> a -> Bool infix 4 Source

True if the two given terms are the same, up to renaming of bound variables.

class AlphaEq a where Source

The class of types that can be compared up to alpha-equivalence.

Minimal complete definition

aeqIn

Methods

aeq :: a -> a -> Bool infix 4 Source

True if the two given terms are the same, up to renaming of bound variables.

aeqIn :: AlphaEnv -> a -> a -> Bool Source

True if the two given terms are the same, up to renaming of bound variables and the specified equivalences between free variables.

Instances

AlphaEq Coercion 
AlphaEq Type 
AlphaEq a => AlphaEq [a] 
HasId b => AlphaEq (Alt b) 
HasId b => AlphaEq (Bind b) 
HasId b => AlphaEq (Command b) 
HasId b => AlphaEq (Cont b) 
HasId b => AlphaEq (Term b) 

type AlphaEnv = RnEnv2 Source

The type of the environment of an alpha-equivalence comparison. Only needed by user code if two terms need to be compared under some assumed correspondences between free variables. See GHC's VarEnv module for operations.

class HasId a where Source

A class of types that contain an identifier. Useful so that we can compare, say, elements of Command b for any b that wraps an identifier with additional information.

Methods

identifier :: a -> Id Source

The identifier contained by the type a.

Instances