shivers-cfg-0.1.1: Implementation of Shivers' Control-Flow Analysis

Safe HaskellNone
LanguageHaskell98

CPSScheme

Contents

Description

This module defines the syntax of the simple, continuation-passing-style functional language used here, as well as some examples.

Synopsis

The CPS styntax

type Prog = Lambda Source

A program is defined as a lambda abstraction. The calling convention is that the program has one paramater, the final continuation.

newtype Label Source

Labels are used throughout the code to refer to various positions in the code.

Integers are used here, but they are wrapped in a newtype to hide them from the implementation.

Constructors

Label Integer 

data Var Source

Variable names are just strings. Again, they are wrapped so they can be treated abstractly. They also carry the label of their binding position.

Constructors

Var Label String 

binder :: Var -> Label Source

The label of the Lambda or Let that bound this variable.

data Lambda Source

A lambda expression has a label, a list of abstract argument names and a body.

Constructors

Lambda Label [Var] Call 

data Call Source

The body of a lambda expression is either

Constructors

App Label Val [Val]

an application of a value to a list of arguments, or

Let Label [(Var, Lambda)] Call

it is the definition of a list of (potentially mutable recursive) lambda expression, defined for a nother call expression.

data Val Source

A value can either be

Constructors

L Lambda

a lambda abstraction,

R Label Var

a reference to a variable (which contains the label of the binding position of the variable for convenience),

C Label Const

a constant value or

P Prim

a primitive operation.

type Const = Integer Source

As constants we only have integers.

data Prim Source

Primitive operations. The primitive operations are annotated by labels. These mark the (invisible) call sites that call the continuations, and are one per continuation.

Constructors

Plus Label

Integer addition. Expected parameters: two integers, one continuation.

If Label Label

Conditional branching. Expected paramters: one integer, one continuation to be called if the argument is nonzero, one continuation to be called if the argument is zero ("false")

Smart constructors

newtype Inv a Source

This wrapper marks values created using the smart constructors that are not yet finished by passing them to prog and therefore invalid.

Constructors

Inv 

Fields

unsafeFinish :: a
 

Instances

Eq a => Eq (Inv a) Source 
Num (Inv Val) Source 
Show a => Show (Inv a) Source 
IsString a => IsString (Inv a) Source 

prog :: Inv Lambda -> Prog Source

This converts code generated by the smart constructors below to a fully annotated CPSScheme syntax tree, by assigning labels and resolving references

noProg :: a Source

Internal error value

Some example Programs

ex1 :: Prog Source

Returns 0

ex2 :: Prog Source

Returns 1 + 1

ex3 :: Prog Source

Returns the sum of the first 10 integers

ex4 :: Prog Source

Does not Terminate

puzzle :: Prog Source

The puzzle from Shiver's dissertation