imperative-edsl-0.4.1: Deep embedding of imperative programs with code generation

Safe HaskellNone
LanguageHaskell2010

Language.Embedded.Imperative

Contents

Description

Deep embedding of imperative programs with code generation. This is the main module for users who want to write imperative programs.

The Program type is parameterized by an instruction set that can be combined in a modular way; e.g:

type MyProg exp a = Program (RefCMD exp :+: FileCMD exp) a

Also, instructions are parameterized on the expression language. In the above example, exp can be any type (of kind * -> *) that implements the EvalExp and CompExp classes.

Some examples of using the library are found in the examples directory.

Synopsis

Documentation

module Data.Int

module Data.Word

Program monad

data ProgramT instr m a :: ((* -> *) -> * -> *) -> (* -> *) -> * -> *

Representation of programs parameterized by the primitive instructions

Instances

MonadTrans (ProgramT instr) 
Monad m => Monad (ProgramT instr m) 
Monad m => Functor (ProgramT instr m) 
Monad m => Applicative (ProgramT instr m) 
((:<:) (* -> *) * (FileCMD (IExp instr)) instr, (~) * a ()) => PrintfType (ProgramT instr m a) Source 
type PrintfExp (ProgramT instr m a) = IExp instr Source 

type Program instr = ProgramT instr Identity

Representation of programs parameterized by its primitive instructions

interpretT :: (Interp i m, HFunctor * * i, Monad m) => (forall b. n b -> m b) -> ProgramT i n a -> m a

Interpret a program in a monad. The interpretation of primitive instructions is provided by the Interp class.

interpret :: (Interp i m, HFunctor * * i, Monad m) => Program i a -> m a

Interpret a program in a monad. The interpretation of primitive instructions is provided by the Interp class.

Imperative instructions

data RefCMD exp prog a Source

Commands for mutable references

Instances

HFunctor * * (RefCMD exp) Source 
CompExp exp => DryInterp (RefCMD exp) Source 
EvalExp exp => Interp (RefCMD exp) IO Source 
type IExp (RefCMD e) = e Source 
type IExp ((:+:) (* -> *) * (RefCMD e) i) = e Source 

data ArrCMD exp prog a Source

Commands for mutable arrays

Instances

HFunctor * * (ArrCMD exp) Source 
CompExp exp => DryInterp (ArrCMD exp) Source 
EvalExp exp => Interp (ArrCMD exp) IO Source 
type IExp (ArrCMD e) = e Source 
type IExp ((:+:) (* -> *) * (ArrCMD e) i) = e Source 

data ControlCMD exp prog a Source

Instances

HFunctor * * (ControlCMD exp) Source 
DryInterp (ControlCMD exp) Source 
EvalExp exp => Interp (ControlCMD exp) IO Source 
type IExp (ControlCMD e) = e Source 
type IExp ((:+:) (* -> *) * (ControlCMD e) i) = e Source 

data FileCMD exp prog a Source

Instances

HFunctor * * (FileCMD exp) Source 
CompExp exp => DryInterp (FileCMD exp) Source 
EvalExp exp => Interp (FileCMD exp) IO Source 
type IExp (FileCMD e) = e Source 
type IExp ((:+:) (* -> *) * (FileCMD e) i) = e Source 

data CallCMD exp prog a Source

Instances

HFunctor * * (CallCMD exp) Source 
CompExp exp => DryInterp (CallCMD exp) Source 
EvalExp exp => Interp (CallCMD exp) IO Source 
type IExp (CallCMD e) = e Source 
type IExp ((:+:) (* -> *) * (CallCMD e) i) = e Source 

Types of Printf arguments

data PrintfArg exp Source

Composing instruction sets

data (f :+: g) a b :: (k -> k1 -> *) -> (k -> k1 -> *) -> k -> k1 -> * infixr 9

Coproducts

Instances

(:<:) k k1 f ((:+:) k k1 f g) 
(:<:) k k1 f h => (:<:) k k1 f ((:+:) k k1 g h) 
(HFunctor k k1 h1, HFunctor k k1 h2) => HFunctor k k1 ((:+:) (k -> *) k1 h1 h2) 
(DryInterp i1, DryInterp i2) => DryInterp ((:+:) (* -> *) * i1 i2) Source 
(Interp i1 m, Interp i2 m) => Interp ((:+:) (* -> *) * i1 i2) m 
(Functor (f a), Functor (g a)) => Functor ((:+:) k * f g a) 
type IExp ((:+:) (* -> *) * (ChanCMD e) i) = e 
type IExp ((:+:) (* -> *) * ThreadCMD i) = IExp i 
type IExp ((:+:) (* -> *) * (CallCMD e) i) = e 
type IExp ((:+:) (* -> *) * (ObjectCMD e) i) = e 
type IExp ((:+:) (* -> *) * (FileCMD e) i) = e 
type IExp ((:+:) (* -> *) * (ControlCMD e) i) = e 
type IExp ((:+:) (* -> *) * (ArrCMD e) i) = e 
type IExp ((:+:) (* -> *) * (RefCMD e) i) = e 

class f :<: g

A constraint f :<: g expresses that the signature f is subsumed by g, i.e. f can be used to construct elements in g.

Minimal complete definition

inj, prj

Instances

(:<:) k k1 f f 
(:<:) k k1 f ((:+:) k k1 f g) 
(:<:) k k1 f h => (:<:) k k1 f ((:+:) k k1 g h) 

type family IExp i :: * -> *

Extract the expression type from an instruction set

IExp is needed to avoid types like (SomeInstr exp :<: i) => Program i (). Here it is not possible to constrain exp by constraining i, so the instance search will always fail. Functions like injE solve this by using IExp to determine exp from i. For this to work, one must use an instruction set i that has an instance of IExp.

It is common for all instructions in a sum (using :+:) to use the same expression type. For this common case, it is enough to get the expression type from the first summand. This can be achieved by giving two IExp instances for each instruction:

type instance IExp (SomeInstr exp)       = exp
type instance IExp (SomeInstr exp :+: i) = exp

Instances

type IExp (ChanCMD e) = e 
type IExp (CallCMD e) = e 
type IExp (ObjectCMD e) = e 
type IExp (FileCMD e) = e 
type IExp (ControlCMD e) = e 
type IExp (ArrCMD e) = e 
type IExp (RefCMD e) = e 
type IExp ((:+:) (* -> *) * (ChanCMD e) i) = e 
type IExp ((:+:) (* -> *) * ThreadCMD i) = IExp i 
type IExp ((:+:) (* -> *) * (CallCMD e) i) = e 
type IExp ((:+:) (* -> *) * (ObjectCMD e) i) = e 
type IExp ((:+:) (* -> *) * (FileCMD e) i) = e 
type IExp ((:+:) (* -> *) * (ControlCMD e) i) = e 
type IExp ((:+:) (* -> *) * (ArrCMD e) i) = e 
type IExp ((:+:) (* -> *) * (RefCMD e) i) = e 

Interpreting expressions

type family VarPred exp :: * -> Constraint Source

Constraint on the types of variables in a given expression language

Instances

class EvalExp exp Source

General interface for evaluating expressions

Minimal complete definition

litExp, evalExp

Instances

class CompExp exp Source

General interface for compiling expressions

Minimal complete definition

varExp, compExp, (compType | compTypeP | compTypePP | compTypePP2)

Instances

Front end