<<

Functional Programming

and InputOutput

Andrew Donald Gordon

Kings College

A dissertation submitted to the University of

towards the degree of Do ctor of Philosophy

August

Contents

Summary vii

Preface ix

Preface to the Bo ok Edition xi

Intro duction

A brief history of functional IO

Semantics of functional languages

Hyp othesis

Synopsis

Results

How to read the dissertation

Mathematical preliminaries

A calculus of recursive typ es

Typ es of

Terms of

Reduction in

Combinators to simulate Elim and Intro

Two examples

Strong normalisation for

A metalanguage for semantics

Syntax of M

Typ e assignmentinM

Op erational semantics of M i

ii CONTENTS

Convergence

Op erational precongruence

Contexts

Exp eriments and contextualexp eriments

Ground and conned relations in M

Contextual and exp erimental order

Applicative and compatible similarity

Contextual order and applicative similarity

Theory of the metalanguage

Laws of op erational equivalence in M

Empty and onep ointtyp es in M

Iterated pro ducts and sums in M

Bo oleans in M

Natural numbers in M

An op erational theory of functional programming

H a small functional language

Ground and conned relations in H

Op erational equivalence for H

Denotational equivalence for H

A theory of H programming

Derived laws of programming

HX an extension of H with exceptions

teletyp e IO Four mechanisms for

Sideeecting IO

Lab elled transition systems and bisimilarity

Stream transformers

Landinstream IO

Synchronisedstream IO

Continuationpassing IO

Maps b etween three of the mechanisms

Discussion

CONTENTS iii

Monadic IO

Programming IO in a monadic style

Aweak monad for teletyp e IO

Aweak monad for the MRC computer

Pro of of the motivating example

Conclusion

Summary

A calculus of recursivetyp es

A metalanguage for semantics

Op erational precongruence

Theory of the metalanguage

An op erational theory of functional programming

Four mechanisms for telet yp e IO

Monadic IO

Bibliography

Notation Index

Index

iv CONTENTS

List of Tables

Terms of M

Typ e assignment rules for M

Evaluation rules for M

A translation of M typ es and terms into

b

Denition of R

Rules concerning conned relations

Laws of equivalence in M

Syntax of H

Typ e assignment rules for H

Some standard functions

Interpretations of Haskell functions in H

Op erational semantics of H

Denotational semantics of H

b

Denition of R and rules concerning conned relations

Laws of equivalence in H

HX an extension of H with exceptions

Three mechanisms for functional IO

Translations in HX between three styles of IO

Aweak monad for teletyp e IO

Deriving the MRC programming mo del

The motivating example

A monadic style of IO for Standard ML v

vi LIST OF TABLES

Summary

A common attraction to functional programming is the ease with which pro ofs can be

given of program prop erties A common disapp ointment with functional programming

is the diculty of expressing inputoutput IO while at the same time b eing able to

verify programs In this dissertation we show how a theory of functional programming

can b e smo othly extended to admit b oth an op erational semantics for functional IO and

verication of programs engaged in IO

The rst half develops the op erational theory of a semantic metalanguage used in the sec

ond half The metalanguage M is a simplytyp ed calculus with pro duct sum function

lifted and recursive typ es We study two denitions of op erational equivalence Morris

We style contextual equivalence and a typ ed form of Abramskys applicative bisimulation

prove op erational extensionality for Mthat these two denitions give rise to the same

op erational equivalence We prove equational laws that are analogous to the axiomatic

domain theory of LCF and derive a coinduction principle

The second half denes a small functional language H and shows how the semantics of

H can b e extended to accommo date IO H is essentially a fragmentofHaskell Wegive

b oth op erational and denotational semantics for H The denotational semantics uses M

in a case study of Moggis prop osal to use monads to parameterise semantic descriptions

We dene op erational and denotational equivalences on H and show that denotational

implies op erational equivalence We develop a theory of H based on equational laws and

a coinduction principle

We study simplied forms of four widelyimplemented IO mechanisms sideeecting

Landinstream synchronisedstream and continuationpassing IO We give reasons why

sideeecting IO is unsuitable for lazy languages We extend the semantics of H to

include the other three mechanisms and prove that the three are equivalenttoeach other

in expressivepower

We investigate monadic IO a highlevel mo del for functional IO based on Wadlers

suggestion that monads can express interaction with state in a functional language We

describ e a simple monadic programming mo del and give its semantics as a particular form

of state transformer Using the semantics weverify a simple programming example vii

viii SUMMARY

Prayer

Let the light of truth and the help of grace be vital principles of action in us

that we may in the time of life attain the ends for which we live

and that our religion which begins in know ledge

may proceed in action settle in temper and end in happiness

Preface

This dissertation is the result of myown work and includes nothing which is the outcome

of work done in collab oration This dissertation is not substantially the same as any that I

have submitted or am currently submitting for a degree diploma or any other qualication

at any other university

Iacknowledge here many p eople who have help ed me complete this work whether named

here or not I thank all the p eople who have beengoodtomeover mytimeinCambridge

y sup ervisor Larry Paulson who has read commented and Iam extremely grateful to m

discussed many drafts of my work Perhaps the most imp ortant thing Ive learnt from

Larry is to keep computer science relevant to practical computational concerns I am

grateful also to Alan Mycroft who acted as joint sup ervisor for fruitful discussions ab out

many drafts and ideas

Ihave had many useful conversations with my friend Roy Crole His help over the years

with understanding many mathematical ideas has been invaluable I am particularly

grateful to him for explaining Moggis approach to denotational semantics to me in simple

terms and for commenting on an early draft of this dissertation in detail Robin Milners

ideas have b een an enormous inuence on this dissertation I am grateful to have b een in

his undergraduate concurrency class at Edinburgh and to have had many helpful conver

with him during his sabbatical in Cambridge Jon Fairbairn enlivened my early sations

years in Cambridge with his idiosyncratic views he graciously passed on to me debates

ab out IO in the Haskell committee During his absence from the Lab he is greatly missed

I thank Andy Pitts for reading and discussing this work on many o ccasions and for his

encouragement Mike Gordon provided funding for me to try out my semantic ideas on

a dataow language for hardware description a go o d way to work out how semantics ix

x PREFACE

can serve practical needs Ken Mo o dy oered me students to teach many encouraging

conversations and the o dd b ottle of wine I thank Roger Needham head of department

for many things not least that he suggest I apply for an internship at DEC SRCinPalo

Alto I learnt a great deal from my host Jim Horning and others at DEC for which I

am very grateful

I thank Ian Poole of the Medical Research Council at the Western General Hospital in

Edinburgh for the time he sp ent explaining their system to me asking searching questions

ab out functional IO and suggesting the example that motivates Chapter

This work was supp orted partially by an SERC studentship This dissertation was pre

pared on a Firey computer donated by DEC Mart yn Johnson keeps the Fireies going

one of his many services to the Lab The exp erimental side of this work owes much to

Dave Matthews and Mark Jones Dave Matthews made available his p ort of PolyML to

the Firey and Mark Jones his Gofer interpreter for Haskell

Juanito Camilleri Jo e Dixon Lee Fedder and Mads Rosendahl started with me they are

good friends Thanks to the following who read and discussed drafts of this work Nick

Benton Gavin Bierman Paul Curzon Eike Ritter and Stuart Wray My ocemates in

and TM have b een go o d company Mike Burrows was a go o d friend in my rst year

and during my time at DEC Mike Gordons Hardware Verication Group is a stimulating

and friendly environment Iv e enjoyed conversation with Tom Melham in all sorts of

places I thank the following for helpful conversations ab out functional IO and semantics

Peter Breuer John Cupitt Andrew Dwelly Nicholas Graham John Hughes Eugenio

Moggi Luke Ong Nigel Perry Gordon Plotkin Raman Sundaresh Allen Stoughton

Simon Thompson David N Turner John Williams and Ed Wimmers

I do not knowhow I could have sustained this work without the supp ort of p eople in two

good veggiesharedhouses Bryants Bend in Palo Alto and Glob House in Cambridge

Evensong at Kings signies beyond what I can say My op ening prayer used often

is by Benjamin Whichcote provost Stephen Cherry the present at Kings

chaplain has given me much My friends in the Student Christian Movement at Holy

Trinity St Columbas and the Romsey Lab our Club have b een go o d to know Thanks

esp ecially to Shelagh Phil Sue Cathy and Julie Yvonne is an Irish blessing

Thanks to my brother Dave for haggis and thuds amongst other gifts My rst and

greatest debt is to my Mum and Dad who never fail to love and supp ort me

Andrew D Gordon August

Preface to the Bo ok Edition

This b o ok is a slight revision of the submitted dissertation Ihave simplied the language

M intro duced in Chapter incorp orated some changes made to Chapter when it was

published in FPCA and simplied Chapter somewhat I have not however

attempted to take account in the main text of the publications that have app eared or

come to my attention since submission These include the following

Semantics of Functional Languages Recent textb o oks cover domain

theoretic semantics of recursivelytyp ed languages such as M Pitts gives a

simple pro of of adequacy in the context of recursivelydened domains a

result derived from his theory of relational prop erties of domains A domain

theoretic semantics for a fragmentofM app ears in a pap er with Crole Mason

and Talcott and Felleisen et al have written series of pap ers on entirely

op erational theories of functional programming Their work go es b eyond this disser

tation in considering sideeects It do es not however takeadvantage of applicative

bisimulation and coinduction in the way Chapter do es Applicative bisimulation

is studied by Pitts and Stark in the presence of dynamically allo cated names

and by Sands as a measure of p erformance Jerey and Launchbury

haverecently develop ed theories of callbyneed evaluation

Graphical User Interfaces Several recent implementations including Concurrent

Clean Fudgets and Budgets express graphical user interfaces in a

functional style Noble is an extensivesurvey

Monadic IO Peyton Jones and Wadlers work has been further develop ed by

Launchbury and Peyton Jones The MRC pro ject using monadic IO is still in

progress Programs and sp ecications are based on the monadic mo del suggested in

Chapter although formal pro ofs of program prop erties are impractical at present

This is a good opp ortunityto thank my examiners Simon Jones and Andrew Pitts Fi

company is nally I want to thank Lindy for all her supp ort and encouragement Her

good

Andrew D Gordon

May xi

xii PREFACE TO THE BOOK EDITION

Chapter

Intro duction

One of the main arguments in favour of functional programming is that it is easier to

prove prop erties of functional programs than of imp erative programs Pro ofs of functional

programs are claimed to be easier to construct than pro ofs of imp erative programs On

the other hand inputoutput IO has long b een viewed as problematic for functional

languages IO mechanisms for functional languages have either been impractically in

expressiveornotbeenintegrated into the language semantics The working programmer

can argue that there is no published evidencesuch as realistic examplesof how prop

erties can be proved of functional programs that p erform realistic IO Hence a ma jor

theoretical advantage of functional programmingthat programs are easy to understand

and verifydo es not carry over to practical programs engaged in IO

This dissertation is a study of how to give semantics to IO mechanisms for functional

languages and how to use such semantics to prove prop erties of programs engaged in

IO It is meant as a step towards convincing the working programmer that functional

programming can b e practical though much remains to b e done

The purp ose of this chapter is to intro duce the problem of functional IO survey previ

ous work and outline the contribution of this dissertation x denes terminology used

here concerning functional programming x discusses previous work on IO in func

tional languages and identies four widelyimplemented mechanisms Any semantics of

functional IO has to build on a semantics of functional languages x reviews semantic

metho ds suitable for functional languages x states the hyp othesis of this dissertation

x outlines each chapter x states the original results of this dissertation and x

oers advice to the reader Finally x intro duces some of the mathematical material

needed here

Functional programming

Many functional or applicative languages have been put forward since the pioneering

of this work on LISP ISWIM and POP in the s For the purp ose

dissertation we distinguish two classes of functional languages dep ending on the seman

tics of function application Recall the terms callbyvalue and callbyname from ALGOL

CHAPTER INTRODUCTION

When a function is applied to an actual parameter under callbyvalue semantics

the functions formal parameter is b ound to the value obtained by evaluating the actual

parameter Under callbyname semantics the formal parameter is bound to the un

evaluated actual parameter and each time the value of the formal is required the actual

parameter is evaluated An eager language is one in which function application has call

byvalue semantics a lazy language is one in which function application has callbyname

semantics By this denition LISP Scheme and ML are eager whereas Miranda Lazy

ML and Haskell are lazy For the sake of eciency application in lazy languages is usually

implemented using callbyneed as in graph reduction for instance Callby

need is the same as callbyname except that after the rst evaluation of the actual

parameter its value is retained and used whenever the formal parameter is subsequently

used

The primary fo cus of this dissertation is IO for lazy languages such as Miranda or Haskell

although we discuss eager languages briey Wemake no claim to have considered all the

varieties of functional language for instance the work here is not immediately applicable

to dataow languages like Lucid or Silage in which every expression stands

for an innite stream

Much has b een written in praise of functional programming The curious reader is referred

to the pap er by Hughes or any of the many textb o oks on functional programming

b eginning this research was the thought My own motivation when

that unlike imp erative programs lazy functional programs are easy to manipulate when

proving program prop erties but it was not clear howto reason ab out programs engaged

in IO

A brief history of functional IO

Many mechanisms have b een implemented and prop osed for functional IO We identify

four classes of IO mechanism which together cover most of the prop osed schemes

Sideeecting IO

Like functional programming functional IO b egins with McCarthy LISP had a

sideeecting IO mechanism The core of LISP can b e explained as applications of

functions to arguments but the LISP Programming System needed other op erations such

as commands to eect an action such as the op eration of inputoutput whichwere called

pseudofunctions The pseudofunction print wrote its Sexpression argument to

the printer The pseudofunction read to ok no arguments but returned an Sexpression

from the input device This sideeecting style of IO p ersists in LISP and is also used in

other eager languages suchasScheme or ML Many language theorists hav e viewed side

eecting IO with suspicion b ecause unlike pure LISP the evaluation of programs using

sideeects cannot simply b e explained as the applications of functions to arguments To

paraphrase Stoy there is more to the meaning of an expression than just its value

the sideeects and order of evaluation of sub expressions b ecome signicant The same

suspicions are aroused by LISP or ML programs that use the assignment statement

A BRIEF HISTORY OF FUNCTIONAL IO

Although suspicious to some sideeecting IO is by far the most widelyused IO mech

anism in eager languages In an eager language it is fairly easy to predict the order in

which expressions are evaluated so programs using sideeecting IO can b e fairly simple

to write and debug if not to reason ab out formally To the b est of the authors knowledge

Williams and Wimmers pap er is the only work to consider howtoprove prop erties

of programs using sideeecting IO in an eager language They develop an algebra for

FL a descendantofBackus FP IO is achieved in FL by op erations on histories

ob jects that enco de the status of all IO devices and which are implicitly passed to and

from every function One can view this as a form of sideeecting IO

On the other hand it is not usually easy to predict the order in which a program in a lazy

language will call such sideeecting pseudofunctions Evaluation order is determined

ance Sideeects mixed with by data dep endencies which can be hard to predict in adv

lazy evaluation make programs hard to understand Another reason why sideeecting

IO is hard to use with a lazy language is that callbyneed can cease to be a correct

implementation of callbyname as weshow in Chapter

Landinstream IO

A stream is a potentially endless list of values generated as need arises Streams were

used by Landin in his calculus semantics of ALGOL to represent the values of lo op

variables he remarked that streams could have been used to represent IO in AL

GOL Streams were b eing used ab out the same time by Strachey in his imp erative

GPM language to represent IO Streams can b e represented as elements of certain

recursively dened domains In an inuential pap er Kahn applied domain theory

to concurrency using collections of streampro cessing functions to mo del the semantics

of certain kinds of pro cess network With MacQueen he showed that these pro cess

networks could b e implemented in POP extended with certain sideeecting op erations

on streams

Ab out the same time the pioneers of lazy languages argued that the list cons

op eration likeany other function should not evaluate its arguments In an eager language

like LISP every list is nite On the other hand if the cons op eration do es not evaluate

its arguments innite lists can b e represented whose elements are computed on demand

Notionally innite lists are an imp ortant to ol for the programmer in a lazy language

emerged that the input and output streams used in giving semantics to IO The idea

could be implemented within a lazy language itself In what we call Landinstream

IO interaction with a teletyp e is sp ecied by a functional program that maps a lazy list

of input characters to a lazy list of output characters The reduction mechanism needs

to be extended so that demand for values in the input stream is met by obtaining fresh

input from the keyb oard Jones and Sinclair credit Henderson as b eing the rst

to prop ose the use of a streampro cessing function to implement teletyp e IO By the

covered in intro ductory textb o oks mids this was a standard technique

CHAPTER INTRODUCTION

Synchronisedstream IO

Synchronisedstream IO is a generalisation of Landinstream IO where the program

is a function mapping a stream of acknowledgements to a stream of requests In Landin

stream IO inputs and outputs need not be synchronised inputs occur when demand

arises for the value in the input stream outputs o ccur when the value of the next item in

the output stream has b een determined In synchronisedstream IO input and output is

synchronised the functional program must pro duce an output request before examining

the corresp onding input acknowledgement The p ower of synchronisedstreams is that the

typ e of requests can enco de any kind of imp erative command Synchronisedstreams were

rst rep orted as the underlying implementation technique for Karlssons Nebula op erating

system The same essential idea was indep endently discovered by Stoye in his

op erating system for SKIMI I and also by ODonnell Synchronisedstreams were

hosen as the basic IO mechanism in Haskell

Continuationpassing IO

Karlsson derived continuationpassing IO op erations from the underlying

synchronisedstream mechanism of Nebula In the context of teletyp e IO

continuationpassing IO is based on a typ e CPS short for continuationpassing style

with three op erations INPUTChar CPS CPS OUTPUTChar CPS CPS and

DONECPS The typ e CPS can b e implemented as an algebraic typ e within the functional

language There is no change to the languages evaluation mechanism but a toplevel

program of typ e CPS can be interpreted or executed as follows To execute INPUTk

input a character v from the keyb oard and then execute k v To execute OUTPUT v q

output character v to the printer and then execute program q To execute DONE simply

terminate The style is called continuationpassing b ecause the argument k to the INPUT

op eration is reminiscent of continuations in denotational semantics Holm

strom used a continuationpassing style in PFL an eager dialect of ML extended

primitives Perry and McLoughlin and Hayes implemented with concurrency

continuationpassing IO mechanisms in lazy dialects of Hop e Reb elskys recent prop osal

of IO trees is essentially a form of continuationpassing IO Unlike sideeecting

and either kind of streambased IO the continuationpassing style is suitable for either

lazy or eager languages

The Haskell IO system is based on synchronisedstream IO based on a typ e called

Dialogue but there is a standard set of continuationpassing op erations These op erations

are programmed in terms of the underlying synchronisedstream mechanism in the same

spirit as Nebula Hudak and Sundaresh discuss translations b etween the two mechanisms

that were discovered by the Haskell committee One fruit of the formal semantics for

functional IO develop ed in Chapter is a pro of of correctness of translations between

Landinstream synchronisedstream and continuationpassing IO

A BRIEF HISTORY OF FUNCTIONAL IO

Combinators for IO

Programmers using eager languages nd that programs using sideeecting IO are fairly

easy to understand if not to reason ab out formally The order of evaluation and hence of

sideeects is fairly easy to control On the other hand programs using streambased IO

can b e hard to develop for two reasons explicit plumbing of streams around a program

is easy to get wrong the order in which input and output is interleaved can be hard to

predict b ecause of lazy evaluation Wray and Dwelly rep ort problems of this

sort

Several authors have derived combinators to abstract op erations that are commonly needed

with streambased programming Karlsson programmed continuationpassing op er

ations using a synchronisedstream mechanism Wray suggested combinators for

sequential comp osition and iteration In his seminal work on the semantics and pragmat

binators ics of Landinstream IO in lazy languages Thompson suggested a range of com

with which to construct Landinstream programs The combinators construct programs of

typ e interact a b a program of this typ e is intended to representaninteractive compu

tation with state of typ e a that when executed will return a value of typ e b Thompsons

combinators include op erations such as sequential comp osition and iteration He devel

op ed a trace theory to verify their correctnessthe rst work on semantics of IO for lazy

languages

In developing the Kent Applicative Op erating System KAOS a line

Miranda program John Cupitt rened Thompsons combinators He worked with a typ e

interact awhich represented interactive computations that return values of typ e a He

used two basic combinators return and comp

return a interact a

interact a interact b comp a interact b

A program return v is the trivial computation that immediately returns the value v a

program comp f p is a sequential comp osition rst execute p to return a value v and

then execute program f v Streambased programs such as KAOS written using these

and other combinators have neither of the disadvantages mentioned earlier There is no

explicit plumbing of streams The order of input and output is controlled by sequential

comp osition

Moggi has shown that structures of the form interact return comp occur

often in the denotational semantics of programming languages The semantics of such

a structure can be given as a computational mo del in the sense of Moggi a categorical

structure based on a strong monad Wadler showed that such structures are a

versatile to ol for functional programming particularly when writing programs to interact

with state

Inuenced bythework of Cupitt Moggi and Wadler Chapter of this dissertation advo

cates what wecallmonadic IO in whichcombinators like Cupitts are used to structure

programs Monadic IO is a highlevel construct that can be implemented using any of

the four lowlevel IO mechanisms Monadic programs are easier to understand than pro

grams written in the three lowlevel styles suitable for lazy languages there are no explicit

streams or continuations to tangle a program

CHAPTER INTRODUCTION

Summary of previous work on functional IO

Wehave discussed four classes of IO mechanism These will b e covered in greater length

in Chapter in the context of teletyp e IO The semantic to ols develop ed earlier in the

dissertation will allowustogive semanticstoeach of the four IO mechanisms

To summarise we considered sideeecting Landinstream synchronisedstream and

continuationpassing mechanisms of IO Sideeecting IO is not suitable for lazy lan

guages b ecause of the diculty of predicting the order in which sideeects o ccur The

semantics of b oth eager and lazy languages are complicated by the presence of sideeects

making program prop erties harder to prove Although the input stream needs to be im

plemen ted sp ecially the semantics of a lazy language need not b e aected by the presence

of streambased IO input and output streams are simply lazy lists IO mechanisms in

certain eager languages have b een based on streams but the typ e of streams

is kept distinct from lists and used only for IO Continuationpassing IO has b een used

with both lazy and eager languages Evaluation of expressions remains unchanged but

some kind of interpreter needs to b e added to the implementation to execute continuation

passing programs Various sets of combinators have b een prop osed for programming at a

higher level than the basic IO mechanism

The four classes cover the most widelyimplemented mechanisms for functional IO To the

b est of the authors knowledge the only mechanism suitable for teletyp e IO not covered

here is the extension of Landinstream IO with hiatons as implemented in Lazy ML

The problem is for a program to b e able to p oll the keyb oard Landinstream IO has

blo cking input in the sense that once demand arises for the next input value computation

is halted until a key is typ ed The solution is that a sp ecial value a hiaton app ears

arisen for a character but none is available in the input stream whenever demand has

from the keyb oard Hiatons have not b een widely implemented Another solution to the

p olling problem is to add a command to poll the keyb oard to synchronisedstream

or continuationpassing IO but we do not pursue this idea here

There are go o d literature surveys on functional IO by Hudak and Sundaresh Jones

and Perry Historicallymany ideas ab out functional IO have arisen from adding

nondeterminism or concurrency to functional languages We do not study such mecha

nisms in this dissertation We refer the interested reader to pap ers containing surveys on

the following topics functional programming and op erating systems nondeterminism

and streambased semantics realtime functional programming and concur

rent extensions of ML Kellys b o ok cites manyworks on parallel systems based

on functional languages

Although there has b een a great deal of work on functional IO there has b een very little

work on semantics The primary goal of this dissertation is to explain the semantics of

functional IO and hence make prop erties provable of functional programs engaged in

IO To the b est of the authors knowledge Thompsons pap er on the semantics

of Landinstreams in Miranda is the only prior work on the semantics of IO in lazy

languages In the context of eager languages there is Williams and Wimmers

work on semantics for what is essentially sideeecting IO and several pap ers giving

op erational semantics for concurrent constructs but with no development

of a theory for program pro ofs Dyb jer and Sander rep ort work on the related problem

SEMANTICS OF FUNCTIONAL LANGUAGES

of expressing concurrency using streams They verify a communications proto col expressed

as a concurrentnetwork of streambased functions

Semantics of functional languages

To obtain a theory of functional IO wemust b egin with a theory of functional program

ming itself In the context of this dissertation such a theory has two purp oses to sp ecify

precisely the computational behaviour of functional programs so that implementations

could b e veried and to enable program prop erties to b e stated and proved

Abramsky p oints out that although the untyp ed calculus has often b een viewed as the

prototypical functional language actual implementations of lazy languages do not conform

to the standard theory Abramsky considers two functional programs x and

where is a lo oping or divergent program He p oints out that according to the standard

theory the two are equal but in the implementation of lazy languages such as Miranda

of the second diverges or Lazy ML evaluation of the rst converges whereas evaluation

Motivated by this example Abramsky develops his lazy calculus as a step towards a

theory of lazy functional programming Following Plotkins study of PCF Abramsky

equips the lazy calculus with a structural op erational semantics and a domain

theoretic denotational semantics He then proves an adequacy theorem to relate the two

semantics

As far as this dissertation is concerned Abramskys key mano euvre is to view his lazy

calculus as a pro cess calculus Led by Milner a great many op erationallybased metho ds

have b een develop ed for the CCS theory of concurrency Bisimilarity found by

taking the greatest xp oint of a certain functional is a cornerstone of this theory

Since it is a greatest xp ointit admits coinductive pro ofs Abramsky builds

a bridge between CCS and the calculus by prop osing applicative bisimulation as

the notion of op erational equivalence in the lazy calculus Applicativebisimulation is a

reworking of CCS bisimulation for the calculus

We follow Abramsky and construct a theory of functional programming based on struc

tural op erational semantics and applicative bisimulation Verication of an implemen

tation could be based on the op erational semantics but this is beyond the scop e of the

dissertation Pro ofs of program prop erties are based on a theory of applicative bisimulation

that parallels that of CCS It is imp ortant that applicative bisimulation is a congruence

relation that is a substitutive equivalence relation Abramskys original pro of that ap

plication bisimulation is a congruence dep ended on domaintheoretic results Stoughton

and Howe made two suggestions for how congruence could be proved directly from the

op erational semantics Stoughton suggested a variant of Milners context lemma

Howe with an ingenious construction proved congruence for a broad range of lazy compu

tation systems In Chapter wewillinvestigate b oth the context lemma and Howes

metho d In related work Milner and Sangiorgi link the lazy calculus with the

theory of calculus a development of CCS with mobile pro cesses Smith builds

on Howes work to construct semantic domains from op erational semantics

A semantics of functional programs has often been based on domaintheoretic

denotational seman tics Stoys classic pap er shows how domain

CHAPTER INTRODUCTION

theoretic metho ds such as Scott induction can b e applied to prove prop erties of functional

programs Instead of domain theory we use op erational semantics to sp ecify the IO b e

haviour of functional programs Were we to app eal to domaintheoretic principles in pro ofs

of functional programs wewould need to relate the op erational and domaintheoretic se

mantics Lester Simpson and Burn have proved such a relation usually

known as adequacy in the context of a lazy functional language other related work is

more theoretical Weleave the relation b etween the op erational and

domaintheoretic semantics of the functional languages studied here as future work For

the purp ose of proving program prop erties wehave not felt the lack of a domaintheoretic

semantics as a loss examples arising here that might have required Scott induction in a

domaintheoretic setting have b een proved using coinduction

In summary asasteptowards a theory of functional IO wedevelop a theory of functional

programming in which the functional language is viewed as a kind of pro cess calculus The

theory is based on structural op erational semantics and applicative bisimulation

Hyp othesis

This dissertation aims to show the following

An op erational theory of functional programming is suitable for precisely sp ecifying

a functional language and proving prop erties of functional programs

Such an op erational theory can b e extended to sp ecify and prove prop erties of the

most widelyimplemented mechanisms for IO in lazy functional languages

A semantics for a simple form of monadic IO may be expressed within the func

tional language Hence programs using monadic IO maybeveried using standard

techniques

Synopsis

The rst half of the dissertation denes a semantic metalanguage M which is used in

the second half for the investigation of functional IO

typ es In this chapter we provea technical result Chapter A calculus of recursive

needed in Chapter Mendler has proved conuence and strong normalisation for

the GirardReynolds p olymorphic calculus extended with p ositive recursivetyp es

This chapter proves strong normalisation for an extension called of Mendlers

calculus

Chapter A metalanguage for semantics The metalanguage M is a simplytyp ed

calculus with pro duct sum function lifted and recursive typ es This chapter

denes its syntax typ e assignment relation and its lazy and deterministic op erational

semantics The main result of this chapter is a convergence theoremthat apart

from terms of lifted typ es evaluation of every term converges

Chapter Op erational precongruence We investigate two op erationally dened

preorders on the terms of M contextual order after Morris and Plotkin and ap

SYNOPSIS

plicative similarity after Milner and Abramsky We dene a notion of op erational

adequacy to mean that a preorder resp ects evaluation in a certain way We show

that each preorder is an op erationally adequate precongruence The pro ofs use a

variantofMilnerscontext lemma for contextual order and a typ ed reworking

of a metho d due to Howe for applicative similarity Given that applicativesim

ilarity is a precongruence it is routine to establish op erational extensionality

that applicative similarity coincides with contextual order

Chapter Theory of the metalanguage We adopt applicative bisimilarity the

equivalence corresp onding to contextual order and applicative similarit yas equiva

lence on terms of M We prove equational laws that are analogues of the axiomatic

domain theory of LCF We derive a principle of coinduction from the

denition of applicative bisimilarity We investigate prop erties of empty onep oint

iterated sum iterated pro duct b o olean and natural number typ es

The second half investigates a range of ways in which functional languages can b e extended

to express IO

Chapter An op erational theory of functional programming We dene a

functional language H which is essentially a subset of Haskell H has lazy al

gebraic typ es and both callbyname and callbyvalue function applications We

give a deterministic op erational semantics and a denotational semantics using M

The denotational semantics is a case study of Moggis prop osal to use monads to

parameterise semantic descriptions We prove a close corresp ondence between the

op erational and denotational semantics We dene op erational and denotational

equivalences as ob jectlevel applicative bisimilarity and equivalence in the metalan

guage resp ectively We show that a theory of programming which consists of a

set of equational laws together with a coinduction principle holds for op erational

but we equivalence The equational laws are valid for denotational equivalence

leave op en whether the coinduction principle holds We conclude the chapter by

considering the semantics and theory of HX a language obtained from H by adding

a parameterless exception mechanism

Chapter Four mechanisms for teletyp e IO We take teletyp e IOinteraction

with a keyb oard and printeras a simple IO mo del In this context we discuss

the formal semantics of four widelyimplemented mechanisms for functional IO

sideeecting Landinstream synchronisedstream and continuationpassing IO

We explain why sideeecting IO combines badly with callbyname semantics of

function application The other three mechanisms are suitable for use with call

byname semantics We prove in a precise sense that they are of equal expressive

power

Chapter Monadic IO Wedevelop a monadic style of functional IO to supp ort an

application of functional IO at the Medical Research Council MRC in Edinburgh

We describ e a simple monadic programming mo del and express its semantics within

H as a particular form of state transformer Using the semantics weverify a simple

programming example

Chapter Conclusion Conclusions are drawn and further work suggested

CHAPTER INTRODUCTION

Results

The main contribution of this dissertation is to develop an op erational theory of lazy

functional programming to extend it to accommo date various IO constructs and to

show how it can be applied to prove prop erties of functional programs engaged in IO

Here are sp ecic original results

A theory of a nontrivial functional language H based on equational reasoning and

coinduction and develop ed op erationally from rst principles

A casestudy of the monadic approach to denotational semantics based on an

op erationallydened metalanguage M Pro of that the denotational semantics cor

resp onds closely to the op erational semantics

A formal semantics for sideeecting Landinstream synchronisedstream and

continuationpassing IO

uationpassing IO are A pro of that Landinstream synchronisedstream and contin

equally expressiveinthecontext of teletyp e IO

A casestudy of the monadic approach to IO motivated by an application of func

tional programming to medical electronics

An investigation of the relationship b etween the context lemma and Howes metho d

of proving precongruence for op erationallydened preorders

How to read the dissertation

A birds eye view can b e had by reading this chapter reading the unnumbered intro ductory

sections of Chapters to and then the whole of the last chapter The intro ductory

sections are intended to motivate and sketch the results of eachchapter without mentioning

any mathematical details

As mentioned ab ove the rst half develops a semantic metalanguage M for use in the

the second half to give denotational semantics for a functional ob ject language H H is

basis for the study of functional IO in the second half The argument in the dissertation

is linear in that almost every chapter dep ends on all its predecessors to some degree but

none on its successors That said a great deal of the second half can be understo o d

without knowing the development of M in detail After obtaining a birds eye view the

reader primarily interested in functional IO mightskimx to x to get an impression

of the denition of M browse Chapter on the theory of M and b egin reading more

thoroughly at Chapter

The extension of Mendlers calculus develop ed in Chapter is only used explicitly in x

The reader wishing to understand M in detail is advised to b egin at Chapter and skip

x on rst reading and then to study Chapter b efore reading x

MATHEMATICAL PRELIMINARIES

Mathematical preliminaries

Syntax alphaconversion and the variable convention

We deal with three formal languages in this dissertation intro duced in Chapter

M intro duced in Chapter and H intro duced in Chapter Chapter also intro duces

a variant of H called HX Here we state general syntactic conventions that apply to all

these languages except that term variables are treated sp ecially in

We assume two countably innite sets of typ e variables and term variables ranged

over by the metavariables X Y Z and f g u v w x y z resp ectively We almost

always refer to term variables simply as variables We will p oint out binding o ccurrences

of variables when dening each formal language If t is a phrase of syntax we write ftv t

and fv t for the sets of typ e and term variables resp ectively that occur free in t The

iterated notation fv t t is short for fv t fv t we use a similarly iterated

n n

t t

X andt x for the outcomes of and t are phrases of syntax we write t form of ftv If t

substituting t for each free o ccurrence of X or xrespectivelyintwithchange of b ound

variables in t to avoid variable capture We refer the reader to Hindley and Seldins

textb o ok for a clear treatment of substitution and alphaconversion We make free

use of the prop erties of substitution and alphaconversion develop ed in Section B of their

b o ok

We follow the standard practice of identifying phrases of syntax up to alphaconversion

that is treating a syntactic phrase as if it were its alphaequivalence class We use the

symbol for alphaconversion We adopt Barendregts variable convention and as

sume that all the b ound variables in a term are distinct from each other and from any free

variables This is legitimate when phrases of syntax are identied up to alphaconversion

We will make it clear whenever we are not treating a phrase of syntax as its alpha

equivalence class When we are not we can write bv t for the set of b ound term variables

in t A context C or D is a term p ossibly containing one or more holes written as

We write C t which we call an instantiation for the term obtained by lling in each

hole in C with the term t Contexts are not identied up to alphaconversion we write

C D to mean that contexts C and D are literally the same The only signicant use of

contexts is in Chapter contexts are covered in greater detail in x

Typ es and p olymorphic denitions

Each of the formal languages M and H is a typ ed calculus in which terms are

information Such typ e information can often be inferred from the tagged with typ e

narrative text and then we omit it Occasionally we omit typ e information from the

dening equation of an M or H term and sp ecify a typ e scheme For instance we might

def

make the denition id x x and comment that term id has typ e scheme The

idea of typ e schemes or p olymorphic typ es is widely used in functional languages like

ML or Haskell Rather than burden the reader with a formal mechanism of p olymorphic

def

typ es we trust that the equation id x x and others like it can be understo o d as

the denition of a whole family of terms id xx for eac htyp e

CHAPTER INTRODUCTION

Relations

If S and T are sets we treat a binary relation b etween S and T as a subset of the pro duct

S T If RS T we write sRt and s t R interchangeably Relational comp osition

is written as juxtap osition that is if R S S and R S S then R R S S



is the comp osition of R and R If RS S then R and R are its transitive closure

and reexive transitive closure resp ectively We write Id for the identity relation If R is



a relation R is its inverse

Bisimulation and coinduction

We make extensive use of preorders and equivalence relations dened to be the greatest

xp oints of certain functionals The prototypical use of this technique in computer science

is bisimilarity in CCS suggested byPark and develop ed by Milner

We will intro duce such relations by rst dening the functional denoted parenthetically

by or hi Then we dene notions of simulation and similarity as intro duced in the

following theorem which states general prop erties of the relations dened in this way

Prop osition Supp ose the following

Metavariable t ranges over a set of terms Term

Metavariable S ranges over subsets of Term Term

Functional is a monotone function over subsets of Term Term

that is if S S then S S

A simulation isarelationS such that S S

is dened to b e the union of all simulations Similarity

Wehave

Similarity is the greatest xp ointof

A xp oint of is a relation S suchthat S S

Similarity is the greatest simulation

t t i there is a simulation S such that tS t

If the identity relation on Term is a simulation then is reexive

If S S is a simulation whenever b oth S and S are then is transitive



If S is simulation whenever S is then is symmetric

Pro of Part is a sp ecial case of the KnasterTarski theorem in xp oint theory see

Davey and Priestley pages

That similarityisa simulation follows from It is the largest since by denition it

contains any other

simulation S to be itself For the backwards For the forwards direction take the

direction wehave S sot t S implies t t

For any t pair t t is in a simulation the identity relation so bypart we have

MATHEMATICAL PRELIMINARIES

t t

t Since similarityisa simu t and t For arbitrary t t and t supp ose that t

t by denition of relational comp osition lation so is the comp osition Wehave t

t as required so pair t t iscontained in a simulation Hence by wehave t

t as required Supp ose that t t Since is a simulation wehave t

Prop erty of this prop osition is an imp ortant pro of technique to prove t t itsuces

to nd some S containing pair t t and prove that S S This technique has been

called Parks rule or greatest xp oint induction or coinduction

If the relation dened by a functional is a preorder a reexive and transitive relation

we call a similarity and any S such that S S we call a simulation as we did

ab ove On the other hand if relation is an equivalence we call a bisimilarity and

any S such that SS wecallabisimulation

CHAPTER INTRODUCTION

Chapter

A calculus of recursive typ es

This chapter prepares the way for Chapter where we will dene a simplytyp ed metalan

guage for semantics M We wish to prove in Chapter that for

every term in a certain class of M typ es evaluation cannot diverge This result is used in

Chapter where wewilldevelop a theory of M We obtain this result by translating such

terms of M into a p olymorphic calculus that p ossesses a strong normalisation prop erty

that no innite reduction sequence starts from anyterm

The typ es of M we wish to enco de are sums pro ducts functions and a certain class of

recursive typ es called p ositive recursive typ es A recursive typ e X is p ositive

within typ e is to the left of an just when each o ccurrence of the b ound typ e variable X

even numb er of function arrows For instance the typ es X X of natural numb ers

or X X of nite lists of typ e are p ositive recursivetyp es As an example

of a nonp ositivetyp e consider X X X which can enco de any term of the untyp ed

calculus Evidently there are terms of this typ e whose evaluation diverges

We seek a calculus with a strong normalisation result whose typ e structure is expressive

enough to enco de the typ es of M and hence to be the basis of a normalisation pro of

for M One candidate is the p olymorphic calculus of Girard and Reynolds which

has a rich typ e structure able to enco de a wide range of typ es Functions are primitive

in and there are standard enco dings of sums and pro ducts As for recursive typ es

Girard outlines a scheme for enco ding the class of recursivetyp es which are sums of

pro ducts and equipping each with primitive recursive functions Wraith describ es

a general scheme for enco ding any p ositive recursivetyp e and primitive recursion within

A second candidate is Mendlers extension of which adds new typ e

constructors for p ositive recursive typ es together with constants and reduction rules to

recursive typ es and compute primitive recursive functions Mendler construct terms of

proves that his extension of isChurchRosser and strongly normalising

We use Mendlers calculus b ecause it allows a direct representation of recursivetyp es In

this chapter we augment Mendlers calculus with two new families of constants that are

needed to simulate certain op erators in M The purp ose of the chapter is to prove that

even with the additional constants and their reduction rules all the terms of the calculus

Paul Francis Mendler is also known as Nax Paul Mendler

CHAPTER A CALCULUS OF RECURSIVE TYPES

are strongly normalisable

x denes the typ e structure of the calculus develop ed in this chapter to b e the

same as in Mendlers calculus x denes the term structure of to be the same as

in Mendlers calculus but for the addition of two new families of constants x denes

the reduction relation for terms of and states Mendlers theorem that anytermof

is strongly normalisable provided none of the new constants o ccurs in it x denes

combinators in Mendlers calculus that corresp ond to the new constants x shows in two

examples that the combinators can simulate the new constants x proves that the new

constants can always be simulated by combinators contained within Mendlers original

calculus

Typ es of  

Calculus is a generalisation of the GirardReynolds p olymorphic calculus We

typ e variables ranged over bymetavariables X Y Z assume a countably innite set of

The set of typ es with metavariables and isgiven by the grammar

X typ e variable

j function typ e

j X p olymorphic typ e X b ound in

j recursivetyp e

j

X initial typ e X b ound in

X terminal typ e X b ound in

Conventional notions of free typ e variables and substitution apply as stated in x Typ es

are identied up to alphaconversion

W esaythatatyp e variable X o ccurs p ositively in a typ e i each o ccurrence of X in

is to the left of an even number of s Duallyatyp e variable X o ccurs negatively

in a typ e i each o ccurrence of X in is to the left of an o dd number of s

Recursivetyp es take one of the forms X or X Beware that the symbol or

is used b oth as a metavariable for initial typ es or terminal typ es and as part of the

syntax as the typ e constructor itself For an M typ e to b e wellformed we require two

any recursive typ e X or X that o ccurs in First we require prop erties of

that eachsuch recursivetyp e is p ositive whichistosay that the b ound variable X o ccurs

positively in Second we require that each recursivetyp e is closed or equivalently that

ftv fX g For instance typ e X X X is closed but not p ositive typ e X Y X

is closed and p ositive but is not wellformed b ecause it contains a typ e Y X whichis

positive but not closed If we make the standard denitions of sums and pro ducts

def

Z ftv Z Z Z Z

def

Z Z Z Z ftv

Mendler p oints out that X o ccurs p ositively in suchtyp es i X o ccurs p ositively in typ es

and Let typ e b e X X a onep ointtyp e whichweinvestigate in the context of

M in x If is a wellformed closed typ e we can dene wellformed typ es X X

TERMS OF

X X and X X of natural numb ers nite lists and notionally innite

streams resp ectively

We insist that recursivetyp es b e closed and p ositive for the following reasons Recursive

typ es that are not p ositive are problematic because they can typ e terms that are not

normalisable For instance the typ e X X X can typ e a version of the combinator

from the untyp ed calculus Positive recursivetyp es that are not closed complicate some

of the syntactic constructions used in this chapter We rule them out as a convenience

just for this reason Mendler requires recursive typ es to be positive but not necessarily

closed We will assume implicitly that any typ e we deal with is wellformed Note that

the set of wellformed typ es is closed under substitution

Parentheses will often b e omitted when the intended grouping is implied by asso ciativity

and scop e rules These rules are that the function arrow asso ciates to the right

and the scop e of bound variables continues as far to the right as p ossible For example

the typ e is short for

Terms of  

As mentioned in x typ e variables are treated sp ecially in We assume given a

countably innite set of names ranged over by metavariables f g x y z This set

of names is the same as the set of term variables used in M and H In a term

variable called simply a variable in the following is of the form x where x is a name

and is a typ e The raw terms of with metavariables L M and N are pro duced

by the grammar

M x typ ed variable

j x M abstraction variable x bound in M

j MN application

j X M typ e abstraction X b ound in M

X not free in anytyp e o ccurring in fv M

j M typ e application

j k constant

where constants k are drawn from the set of constants Con given b elow

def

Con fIntro Elim R Intro Elim S g

Let ftv M andfv M b e the sets of all typ e and term variables that o ccur free in M

M ftv M fv M

x ftv fx g

x M ftv ftv M fv M fx g

MN ftv M ftv N fv M fv N

ftv M fX g fv M X M

M ftv M ftv fv M

k

N

Typ e and term substitution is written as M X and M x Alphaconversion is up to

renaming of the names of b ound term variables but not their typ e lab els x M

CHAPTER A CALCULUS OF RECURSIVE TYPES

y

y M x if y fv M We identify terms up to alphaconversion The restriction

X

on variables in typ e abstractions comes from the without it fv X x is not well

dened up to alphaconversion of terms We adopt a name convention for

that all the names of all the b ound variables in a term are distinct from each other and

from the names of anyfreevariables we can always achieve this by alphacon version

The only dierence between the calculus given here and Mendlers calculus is the

presence of the constants Elim and Intro To obtain his calculus Mendler added the

initial and terminal typ es to the GirardReynolds calculus together with the constants

Intro R Elim and S We use a notation slightly dierent from Mendlers Elim instead

of In Intro instead of out X instead of X and dierent metavariables We need

the extra constants so as to mo del corresp onding Intro and Elim op erators in M

The typ e assignment relation of a predicate of the form M where M is a raw

term and is a typ e is dened inductively by the rules

Intro X

Elim X

R Y X X X Y Y Y

Intro X

Elim X

S Y X X Y X Y Y

x

M M

M N M X

x M X M X

MN M X

where X and X If M can be inferred we say that M is a term and

that it has typ e Henceforth the metavariables L M and N will range over just the

terms rather than all the rawterms

For the sake of legibilitytyp e sup erscripts will often be omitted Parentheses will often

b e omitted when the intended grouping is implied by asso ciativity and scop e rules These

rules are that typ e and term applications asso ciate to the left and the scop e of b ound

variables continues as far to the right as p ossible For example the term LM N is short

for LM N and the term x y x y y is short for x y xy y

Reduction in  

Let us say that a binary relation R on terms of iscompatible just when it is closed

under the following rules

M R N M RN M RN

x MR x N MLR NL LMR LN

M RN M RN

X M R X N MR N

COMBINATORS TO SIMULATE ELIM AND INTRO

Let Id stand for the identity function x x The reduction relation on terms is

the least compatible relation closed under the following eight axiom schemes

N

x M N M x

x Mx M if x fv M

X M M X

X MX M if X ftv M

Elim Intro M M

R M Intro N M Id R M N

Elim Intro M M

Elim S M N M Id S M N

In this dissertation wetakeadvantage of the following theorem

Theorem Mendler If no constant Elim or Intro o ccurs in a term M then M

is strongly normalisable which is to say that there is no innite sequence of reductions

starting from M

Pro of The pro of is contained in an article by Mendler His calculus do es not contain

the new families of constants Elim or Intro The reduction rules applicable to terms of

that do not contain the new constants are exactly the same as the rules in Mendlers

article If M M is a reduction and M do es not contain one of the new constants

then neither do es M Therefore any sequence of reductions starting from a term not

containing the new constants can be exactly simulated by a sequence of reductions in

Mendlers calculus Therefore his result tells us that any term M in is strongly

normalisable provided that none of the new constants o ccurs in M

In a private communication June Mendler said that he omitted the Elim and Intro

constants from his calculus to simplify the conuence pro of whichwe make no use of in

this dissertation and was not concerned at the loss of expressiveness b ecause they can b e

mimicked using the recursion constants R or S resp ectively This presentwork app ears

to be the rst to work through a construction of combinators to mimic the constants

omitted by Mendler

The rest of this chapter is devoted to a pro of that anytermof is strongly normalis

able

Combinators to simulate Elim and Intro

We prove that every term of is strongly normalisable by simulating the new con

stants Elim and Intro with combinators elim and intro denable in Mendlers original

calculus from constants R and S resp ectively Before dening these combinators in

Denition we dene two auxiliary functions Spec and Gen

Denition We dene the partial functions not terms of the calculus Spec and

Gen that map quadruples of the form X M to a term where is a recursivetyp e

X is a typ e variable is a typ e and M is a term First cho ose some name f Then dene

CHAPTER A CALCULUS OF RECURSIVE TYPES

the two functions simultaneously by structural induction on the third argument

def

X 

Spec X X M f M

def

Spec X Y M M if X Y

def

Spec X M M

def

Spec X Y M Y Spec X M Y where Y ftv X M

def

Spec X M x Spec X M Gen X x

where X

and name x f do es not o ccur in fv M

def

X

Gen X X M f M

def

Gen X Y M M if X Y

def

Gen X M M

def

Gen X Y M Y Gen X M Y where Y ftv X M

def

M x Gen X M Spec X x Gen X

where name x f do es not o ccur in fv M

The functions are partial only so far as Spec X M andGen X M are undened

when is the typ e variable X this is unproblematic b ecause we only apply Spec to a

terminal typ e X orGen to an initial typ e X when X o ccurs negatively in

Denition For each initial typ e X let be the typ e X and dene

the combinator family elim as follows

def

X  X 

elim R X f g x Spec X x

Dually for each terminal typ e X let be X and dene the combinator

family intro as follows

def

X X 

g x Gen X x intro S X f

The name f is the same as the one chosen in the denition of Spec and Gen

In x we will prove that these combinators p ossess the following prop erties

elim Intro M M

Elim intro M M

for any suitably typ ed term M Given these prop erties it is not hard to show that every

term in is strongly normalising First we showtwo examples

Two examples

First recall the typ e X X We can calculate the combinator intro as follows

 X  X

S o X f g x Gen XXx intr

 X  X X

S X f g x f x

STRONG NORMALISATION FOR

Nowwe can checkthatElim intro M M for any term M of typ e

 X  X X

Elim intro M Elim S X f g x f x M

X  X X

X f g x f x Id intro M

  

f x Id intro M f g x



g x Id x intro M

x Id x M

Id M

M

Second let be the typ e X and initial typ e be X We b egin by

calculating Spec X x which has typ e

 

y Spec X x Gen X X y Spec X x



y Spec X x

X  X

z Gen X y Spec X X z



y Spec X x

X  X  X

z Gen X y f z

X  X  X 

ec X x z y f z y Sp

 X  X  X

y x z y f z

The combinator elim is dened as follows where is

X  X 

elim R X f g x Spec X x

Wecancheck that elim Intro M M for any term M of typ e

X  X 

elim Intro M R X f g x Spec X x

Intro M

X  X 

X f g x Spec X x

Id elim M

 

f g x

  

f z y x z y

Id elim M

  

g x y x z y Id z

elim M

 

x y x z y Id z M

 

z y M z y Id

 

z y M z y

 

y M y

M

Notice that the reductions make use of the b eta and eta rules and dep end on the variable

named f b eing replaced by the identity function

Strong normalisation for  

Weprove a series of prop ositions that culminates in a strong normalisation result for

The rst prop osition states all the prop erties that we rely on in the functions Spec and

CHAPTER A CALCULUS OF RECURSIVE TYPES

Gen parts a and d are of particular interest

Prop osition Let be any initial typ e X and let s be the substitution



Id

X f Then for anytyp e and term M

If M and X occurs positively in let N Spec X M and then

a N has typ e X

b fv N fv M and name f do es not o ccur in fv N

ftv ftv M fX gand c ftv N



d Ns Ms

If M X and X o ccurs negatively in letN Gen X M and then

a N has typ e and

b fv N fv M and name f do es not o ccur in fv N

c ftv N ftv ftv M fX gand



d Ns Ms

Pro of We prove all parts simultaneously by induction on the size of the typ e and

pro ceed by analysis of its structure For the sake of brevity we omit all the details of

parts b and c They are no harder to prove than the other parts

Case X

X 

a Here N f M whichhastyp e which equals X

d Ns Id Ms Ms

Gen X X M is undened but this case cannot arise b ecause X o ccurs p os

itively in X

Case Y X

a N M Y and Y X



d Ns Ms Ms

a N M Y and Y



d Ns Ms Ms

Case This case is trivial because of the restriction in that all recursive

typ es b e closed If we had not made this restriction the denitions of Spec and Gen

would b e rather more complicated

a Here N M which has typ e All recursivetyp es are closed so X



d Ns Ms Ms

N M which has typ e X since all recursivetyp es are closed a



d Ns Ms Ms

Case Y We may assume Y ftv X M since typ es are identied up to

alphaconversion

a Here N Y Spec X MY By IH we have that Spec X MY is

of typ e X Therefore N is of typ e Y X X

d Ns Y Spec X MY s



Y MY s IH

Y MsY

Ms

STRONG NORMALISATION FOR

a N Y Gen X MY By IH Gen X MY isoftyp e Therefore

N is of typ e Y

d Ns Y Gen X MY s



Y MY s IH

Y MsY

Ms

Case

a N x Spec X M Gen X x where x has t yp e X

Since X o ccurs negatively in by IH we have Gen X x Then

M Gen X x has typ e Since X o ccurs p ositively in by IH we

have Spec X M Gen X x has typ e X Therefore N has typ e

X X which equals X

d Ns x Spec X M Gen X xs



x M Gen X xs IH

x MsGen X xs



x Msxs IH

x Ms x

Ms

a Dually N x Gen X M Spec X x where x has typ e Since

X o ccurs p ositively in by IH we have Spec X x has typ e X Then

M Spec X x has typ e X Since X o ccurs negatively in by IH we

N has typ e have Gen X M Spec X x has typ e Finally

which equals

d Ns x Gen X M Spec X xs



x M Spec X xs IH

x MsSpec X xs



x Msxs IH

x Ms x

Ms

The following is analogous to Prop osition but for terminal typ es

Prop osition Let be any terminal typ e X and let s be the substitution



Id

X f Then for anytyp e and term M

If M and X o ccurs negatively in letN Spec X M and then

a N has typ e X and

b fv N fv M and name f do es not o ccur in fv N

ftv M fX gand c ftv N ftv



d Ns Ms

If M X and X o ccurs p ositively in letN Gen X M and then

a N has typ e and

b fv N fv M and name f do es not o ccur in fv N

c ftv N ftv ftv M fX gand



d Ns Ms

CHAPTER A CALCULUS OF RECURSIVE TYPES

Pro of Omitted but dual to the previous pro of

Nowweprove prop erties of the combinators elim and intro

Prop osition For any recursivetyp es X and X

There are no typ e or term variables free in elim or intro that is they are combi

nators

The typ e assignments elim X and intro X are derivable that

is the combinators are welltyp ed

For anytermM

a if M X then elim Intro M M

b if M X then Elim intro M M

Pro of Consider the term elim Let be the typ e X Since

fn Spec X x fx f g we can calculate fn elim fn Spec X x

ff x gg Therefore fv elim Similarly ftv Spec X x ftv fX g

fX g since ftv fX g Wehave

ftv elim ftv X X ftv Spec X x fX g

Similarly wecancheck that the term intro has no free typ e or term variables

Combinator elim will b e welltyp ed if Spec X x isoftyp e X and Prop o

X

sition a says so Similarlyfor intro to b e welltyp ed the term Gen X x

needs to havetyp e and according to Prop osition a it do es

Again let be X By app eal to Prop osition d let us calculate

elim Intro M

X  X 

R X f g x Spec X x Intro M

X  X 

X f g x Spec X x

Id elim M

 

f g x Spec X x X

Id elim M





Id

g x Spec X x X f

elim M

 

elim Id

g M x Spec X x X f



Id

x Spec X x X f M





Id

x x X f M

Id M

M

By a similar calculation we can compute the other part of the prop osition

Nowthatweknowhowtosimulate the new constants that app ear in our extended calculus

in terms of the original calculus we can simulate each term N of the extended calculus

by a term M with each of the new constants replaced by one of the combinators wehave just dened

STRONG NORMALISATION FOR

Denition Dene the simulation relation M N on terms of as the least

compatible relation closed under the following axiom schemes

elim Elim

intro Intro

kk for any k Con fElim Intro g

Later we will rely on the fact that simulation is preserved by substitution

Lemma For any terms M and N such that M N

If M N then fv M fv N and ftv M ftv N

M X N X for any and X and

L L

M x N x for any L L and x with LL

Pro of Part is an easy induction on the depth of inference of M N Part is also

by induction on the depth of inference of M N We showseveral cases

Case M elim Elim N Since neither side contains any free typ e variable

free term M X M N N X Similarly since neither side contains a

L L

variable M x N x

Case M intro Elim N The result follows by the same argument as the previous

case that neither side containsafreetyp e or term variable

Case M kk N Same again

Case M y y N Wehave M X N X so M X N X For either

L L L L

x y or not If so M x LL N x If not M x y y N x

Case M y M y N N We may pick the b ound variable y such that

L L

y x By induction hyp othesis wehave M X N X and M x N x

X X

Then we can derive that M X y M X x M X

L L L L

N X and M x y M x x M x N x as required

Case M M M N N N By induction hyp othesis M X N X and

i i

L L

M x N x for i Then wehave

i i

M X M X M X N X N X N X

L L L L L L

x N x N x as required x M x N and M x M

The case for abstractions is similar Part follows by a similar argument

Now we come to the key prop erty of the simulation relation that if M N then any

reduction of N can b e mimicked by a nonempty sequence of reductions starting from M

Prop osition If M N and N N then there exists M such that M M and

M N

Pro of The pro of is by induction on the depth of inference of N N pro ceeding by

o cases where an analysis of how the inference was derived We show here only the tw

combinators do the work of constants and one of the inductive cases

CHAPTER A CALCULUS OF RECURSIVE TYPES

Case N Elim Intro N N N

Since M N wehave M elim Intro M withM N

Set M M and by Prop osition a wehave M M and weknow M N

Case N Elim Intro N N N

Since M N wehave M Elim intro N withM N

Set M M and by Prop osition b wehave M M and weknow M N

Case N x N x N N

where N N Since M N wehave M x M withM N

By induction hyp othesis there is M such that M M and M N

x M and wehave M M and M N Set M

The other cases of the pro of follow the same pattern

A simple consequence of Prop osition is strong normalisation

Theorem Each term N of is strongly normalisable

Pro of Supp ose to the contrary that there is a welltyp ed term N that admits an innite

chain of reductions Construct a term M such that M N by replacing each constant

of form Elim or Intro that o ccurs in N by the corresp onding combinator elim and

intro resp ectively From Prop osition it follows that since there is an innite chain of

reductions starting from N there is also one starting at M But M is a term of Mendlers

published calculus whichis known to be strongly normalising Theorem so we have

reached a contradiction Therefore eachtermof is strongly normalisable

We have extended Mendlers secondorder calculus with recursive typ es to have two

new families of constants Elim and Intro This provides a basis for the normalisation

pro of of the semantic metalanguage Mdevelop ed in later chapters of this dissertation

Chapter

A metalanguage for semantics

The purp ose of this chapter is to dene a metalanguage for programming language se

mantics called M In Chapter we dene a small functional language called H Wewill

give a denotational semantics for H in terms of M and also show how the denotational

semantics can b e extended to account for an exception mechanism and sideeecting IO

Hence each of the four classes of functional IO discussed in Chapter can be dened

from the denotational semantics for H extended as necessary

We adopt a variant of Plotkins threefold metho dology for denotational semantics

First he develop ed a theory of partial functions and socalled b ottomless domains Second

calculus as a semantic metalanguage his calculus had pro duct he prop osed a typ ed

sum function lifted and recursive typ es His calculus had both domaintheoretic and

op erational semantics He proved adequacy results relating the op erational and domain

theoretic semantics of his metalanguage Third to study a particular ob ject language he

gave a direct op erational semantics and an indirect domaintheoretic semantics induced by

mapping the ob ject language into the metalanguage Pro of of the corresp ondence b etween

the op erational and domaintheoretic semantics of the ob ject language can takeadvantage

of the adequacy results for the metalanguage This approach is general in that for each

ob ject language studied the pro of of adequacy factors into two a general result proved

once and for all for the metalanguage and a comparatively simpler pro of relating the

semantics of the ob ject and metalanguages Plotkins work has b een develop ed by Moggi

and Jones

We develop in this dissertation a simplytyp ed calculus called M for the denotational

semantics of H and its variants The most imp ortant dierence b etween the op erational

semantics of Plotkins calculus and M is that in M the evaluation of any term of a certain

class of typ es cannot diverge In Plotkins calculus there is a divergent term at every typ e

We partition the typ es of M in two the p ossiblydivergent typ es are the lifted typ es

the certainlyconvergent typ es are all the others One reason for this is to convey

information ab out op erational b ehaviour in the typ e system for instance anynumeral in

M term of typ e Num which is a certainlyconvergenttyp e representing H is mapp ed to an

the natural numbers The typ e of the translation of the numeral conveys that it cannot

diverge

In this and the following two chapters we investigate M b efore applying it to the study

CHAPTER A METALANGUAGE FOR SEMANTICS

of functional programming and IO in Chapters and In this chapter we dene M

and assign it an op erationallybased rather than domaintheoretic semantics M has a

typ e system similar to Plotkins calculus pro duct sum function lifted and recursive

typ es Using a metho dology similar to Plotkins we use the metalanguage to decomp ose

a more complex ob ject language H In Chapter we investigate two op erationally

dened preorders on the terms of M Morrisstyle contextual order and a typ ed form

of Abramskys applicative bisim ulation We will prove them to be equal and take the

equivalence they generate as equivalence on terms of M In Chapter we develop a

theory of M based on equational laws and a coinduction principle We study certain

derived M typ es that are needed for the semantics of H b o oleans natural numb ers and

iterated sums and pro ducts

x denes the syntax of M The typ e system is dened in x and the op erational

semantics in x x proves the ma jor result of this c hapter that only terms of lifted

typ e can diverge

Syntax of M

We assume countably innite sets of typ e variables and term variables ranged over by

letters X Y Z and f g x y z resp ectively The syntactic conventions of x apply to

M The typ es are given by the following grammar

X j j j j j X



and the terms by the grammar in Table

Atyp e is p ossiblydivergent i it is a lifted typ e that is has the form Any other



typ e is certainlyconvergent The main result of this chapter is a convergence theorem

Theorem which says that evaluation of any closed term of a certainlyconvergent

typ e must converge A corollary is that only terms of a p ossiblydivergent typ e may

diverge The pro duct disjoint sum and function typ es and

resp ectively are certainlyconvergenttyp es

They take the form X Just as in Chapter Recursivetyp es are certainlyconvergent

the symbol is used b oth as a metavariable for recursivetyp es and as part of the syntax

as the typ e constructor itself There are two syntactic restrictions on these typ es

each free o ccurrence of the typ e variable X in must b e to the left of an even number of

s and the only typ e variable free in can b e X Restriction rules out mutually

recursive typ es These conditions are required so that the convergence theorem can be

proved by mapping any certainlyconvergenttyp e in M to a corresp onding typ e in

ellformed typ es is We will assume implicitly that typ es are wellformed The set of w

closed under substitution

Typ e assignment in M

An environment is a nite mapping from variables to closed typ es written as

x x where the variables in the list are pairwise distinct The domain of

n n

TYPE ASSIGNMENT IN M

Syntax

U V hM N i pair

j Inl M j Inr M injection

j x M abstraction x b ound in M

j LiftM Liftterm

j Intro M Introterm

L M N x variable

j V canonical term

j SplitM x y N Splitterm x y bound in N

j CaseM x N x N Caseterm x bound in N i

i i

j MN application

j SeqM x N Seqterm x bound in N

j Fix x M Fixterm x bound in M

j ElimM Elimterm

Table Terms of M

x x

M N

M xy N

hM N i

SplitM x y N

M M

Inl M Inr M

M x N x N

CaseM x N x N

x M

M N

x M

MN

M M x N x M

   

LiftM SeqM x N Fix x M

  

M X M X X

Intro M ElimM X

Table Typ e assignment rules for M

CHAPTER A METALANGUAGE FOR SEMANTICS

an environment is denoted by Dom We write for the concatenation of two

environments this notation is wellformed only when Dom Dom

Denition The M typ e assignment relation M is inductively dened by

the rules in Table Each rule has the implicit sidecondition that any environments

appearingintherulearewellformed

Weshow that the typ e assignment relation p ossesses some standard prop erties

Prop osition

If M and M then

N

If x M and N then M x

If M then fv M Dom

If M and then M

Pro of By structural induction on M The pro of is straightforward b ecause terms are

lab elled with typ e information where necessary to ensure unique typ e assignment

By induction on the depth of inference of the typ e assignmentx M

By simple inductions on the depth of inference of M

We dene classes of programs and conned terms

Denition

A program is a term M such that M and hence ftv M for some

necessarily unique typ e The typ e is called the typ e of M which itself is

called a program

A conned term is a pair M such that there is a necessarily unique typ e

with M The typ e is called the typ e of M and is called the

Occasionally we represent a conned term M of environment of M

typ e with the typ e assignmentsentence M itself

Op erational semantics of M

We dene a deterministic op erational semantics for M programs We use an auxiliary

notion of an exp eriment E which is a function on programs such that E M is obtained

by wrapping a selector around program M Exp eriments are discussed at greater length

in x They are an alternativetoFelleisens evaluation contexts

Denition Exp eriments E are dened by the grammar at the top of Table

Write E M for the term obtained by replacing the o ccurrence of in E by the term

M

The reduction and evaluation relations for M are the binary relations on M programs

and resp ectively dened inductively by the rules in Table

The canonical programs are the outcomes of evaluation The op erational semantics is

lazy in the sense that subterms of canonical programs may be noncanonical Witness

OPERATIONAL SEMANTICS OF M

Reduction Semantics

E SplitxyM j Casex N x N j M j SeqxN j Elim

M N

E M E N

M M

SplithM M ix x N N x x

M

CaseInlM x N x N N x

M

CaseInr M x N x N N x

N

x M N M x

M

M xN N x SeqLift

Fixx M

Fix x M M x

ElimIntroM M

Evaluation Semantics

V V

M M

L hM M i N x x V

SplitL x x N V

M M

L InlM N x V L Inr M N x V

CaseL x N x N V CaseL x N x N V

N

L x M M x V

LN V

M

L LiftM N x V

SeqL x N V

Fixx M

M x V

Fix x M V

M IntroN N V

ElimM V

Table Evaluation rules for M

CHAPTER A METALANGUAGE FOR SEMANTICS

Inr Fixx x which is canonical but has noncanonical Fixx x as a subterm

Prop osition

If M and M N then N

If M and M V then V

If L M and L N then M N

If L U and L V then U V

The canonical terms are the normal forms of onestep reduction where a normal

M such that for no term form is a program that cannot b e reduced that is a term

N do es M N

Supp ose M N Then for any V N V implies M V



M V just when M V

Pro of Parts and followby straightforward inductions on the depth of inference of

M N and M V resp ectively Parts and follow similarly by inductions on the

depth of inference of L M and L U resp ectively

For part we must show that each canonical program has no reductions and that

if a term has no reductions then it is canonical First no canonical program has any

reductions b ecause none of the reduction rules in Table is applicable to any canonical

program Second each program is either canonical a Fixterm or of the form E M for

some exp eriment E and program M There are rules in Table to reduce a Fixterm

and each p ossible welltyp ed term E M so any program with no reductions must be

canonical

Part is by induction on the depth of inference of the reduction M N The forwards

direction of is by induction on the depth of inference of evaluation M V We prove

the backwards direction of via a metho d used in Croles dissertation Supp ose that

M M M M M V We show that M V by lling in the following

n n

diagram from right to left starting with the fact that M V V and establishing for

n

each i that M V from M V and part of the prop osition

i i

M M M M M V

n n

V V V V

Both directions established part is proved

We dene terminology for termination of evaluation

Denition Supp ose that M is a program Say that M converges and write M

i there is a necessarily unique canonical program V such that M V Conversely say

that M diverges and write M i M do es not converge

Convergence

The argument that certainlyconvergent typ es deserve the name pro ceeds as follows

CONVERGENCE

Typ es

def

X X

def

X X X where X ftv

def

X X X X where X ftv

def

def

X X X



def

X X

Terms

def

x

x x

def

 X

hM N i X f f M N

def

SplitM x x N M



x x x x N

where M

def

X X

InlM X f g f M

def

X X

Inr M X f g g M

def

CaseM x N x N M

x x N



x x N

where M

def

x M x x M

def

MN M N

def

X X

LiftM X x x

def

X X

SeqM x N X x x

def

X X

Fixx M X x x

def

Intro M Intro M

def

ElimM Elim M where M

Table A translation of M typ es and terms into

In Table we dene a translation of M typ es and terms into Prop osition

proves that welltyp ed terms of M are translated into welltyp ed terms of

Lemma is a substitution lemma saying that the translation of an M term into

is preserved under typ e and term substitution

We prove a simulation theorem Theorem if an M program M of certainly

convergenttyp e reduces to program N then the translation of M into reduces

to the translation of N

The Convergence Theorem for certainlyconvergent programs Theorem then

follows easily from the simulation theorem and the fact Theorem that no

innite chains of reductions exists in

Denition Each M typ e is translated to a typ e according to the mapping

inductively dened by the rules in Table

CHAPTER A METALANGUAGE FOR SEMANTICS

Each M conned term M is translated to a term M according to the

mapping inductively dened by the rules in Table Any b ound names that app ear in

the righthand side of translations but not in the lefthand side are assumed to b e new

The purp ose of this construction is to show that each reduction of a certainlyconvergent

term in M is simulated by one or more reduction steps in so as to prove by con

tradiction that no certainlyconvergent term diverges The translations of pro ducts and

sums are the standard ones mentioned in x For the present purp ose there is no need

for reductions of p ossiblydivergent terms to b e simulated by their translations indeed it

would b e imp ossible Hence we simply map each p ossiblydivergenttyp e to X X X

X X

and can map any p ossiblydivergent term to X x x

Prop osition

For any M typ e ftv ftv

For any M conned term M of typ e ftv M and the set of

names free in M equals the set of M variables free in M

If M conned term M has typ e term M has typ e

Pro of Parts and are by induction on the structure of and the depth of inference

of M resp ectively

By induction on the depth of inference of the typ e assignment M

We need some substitution prop erties of the translation

Prop osition

X X

If x M and N

N N

then M x x M x

Pro of By induction on the structure of Weshowseveral cases Supp ose Y If

X Y then b oth sides equal If not then b oth sides equal Supp ose

Then lhs Y X X Y Y where we may assume that b ound

Y Y rhs The variable Y ftv X By IH lhs Y X X

other cases are similar

By induction on the depth of inference of x M

Here is the key theorem of the argument

Prop osition For any two M programs of the same certainlyconvergent typ e M

and N ifM N then M N

Pro of The pro of is by induction on the depth of inference of M N As one might

exp ect as the translations are standard the reductions of pro ducts sums and functions

go through straightforwardly We show the case of pro ducts as an example and also the

inductive case

CONVERGENCE

M M

x N We may assume typ e x Case M SplithM M ix x M M

assignments hM M i N and x x M Wehave



hM M i x x x x M M

and

 X



hM M i X f f M M

So



M x x x x M M M

M M

x x M x x

and hence by the substitution lemma Prop osition wehave

M N

as required

Case M E M E N N where M N First note from the typ e assignment

rules in Table that since M is a program of certainlyconvergenttyp e then so to o

are M and N Therefore the induction hyp othesis applies to the reduction M N

and wehave M N Then the conclusion that M N

follows by insp ection of the translation in Table of any p ossible E M and E N

Reductions involving recursive typ es also go through straightforwardly Reductions con

cerning lifted typ es do not arise b ecause only terms of certainlyconvergent typ e are

considered

Theorem If is a certainlyconvergent typ e and M is a program then there is

a canonical program V for which M V

Pro of By contradiction assume that there is an innite chain of M terms b eginning

M M M By the previous prop osition there is a chain of terms

b eginning M M M But this is a contradiction b ecause

by Theorem is strongly normalising so no suchchain can exist Therefore the

theorem follows

CHAPTER A METALANGUAGE FOR SEMANTICS

Chapter

Op erational precongruence

We will develop in the next chapter an entirely op erational theory of the semantic meta

language M The purp ose of the present chapter is to investigate how an op erational

equivalence can b e dened on terms of M For the sake of simplicitywe consider in this

chapter op erational preorders Each such preorder induces an equivalence in the usual

way

The theory of M in Chapter dep ends on M having an op erationally adequate precon

gruence An op erationally adequate preorder is one that resp ects M evaluation in certain

ways A preorder is a precongruence i whenever M N then C M C N for all

contexts C Roughly sp eaking a context C is a term containing holes the term C M is

obtained by lling in eachholeincontext C with the term M

C

and applicative similarity We consider two candidate preorders contextual order

A

x investigates contextual order attributed by Abramsky to Morris and used

by Plotkin and Milner If M and N are two M programs of the same typ e

C

It is not hard to N i for all contexts C ifC M converges then so do es C N then M

showthatcontextual order is a precongruence We show that it is op erationally adequate

E

which is evidently op erationally by dening an auxiliary preorder exp erimental order

adequate and showing that exp erimental and contextual order are the same To do so

weuseavariant of Milners context lemma

A

The other candidate preorder is applicative similarity investigated in x This

is a typ ed formulation of Abramskys applicative bisimulation Applicative similarity

is the greatest xp oint of a certain functional Roughly sp eaking if M and N are two M

A

programs of the same typ e then M N i whenever M V there is U with N U such

that U and V have the same outermost syntactic constructor and their corresp onding

subterms are applicatively similar It is not hard to prove that applicative similarity is

op erationally adequate We show that it is a precongruence via a typ ed reworking of an

ingenious metho d due to Howe

In x we proveanop erational extensionality result that contextual order equals

applicative similarity Hence wehave two indep endentcharacterisations of the same pre

order This preorder generates an equivalence in the usual way which we refer to as

or applicative bisimilarity in the remainder of the disserta op erational equivalence

CHAPTER OPERATIONAL PRECONGRUENCE

tion

Mathematical material needed to do the work of the chapter is intro duced at the b eginning

Contexts are dened in x Exp eriments were used to dene the reduction semantics of

M in Chapter and they are needed here to dene exp erimental order In x werecall

the denition of an exp eriment and dene contextualexp eriments which are exp eriments

that act on contexts and terms The detailed syntactic op erations in x and x are

used here only in the study of contextual and exp erimental orders The theory of M in

Chapter is based on programs and conned terms whichwere dened in Chapter In

x we formulate certain prop erties of relations on conned terms or programs that are

needed later

Contexts

p ossibly containing holes written A context is pro Intuitively a context is a term

duced from the grammar for M terms with metavariables C and D instead of M and

N augmented with an additional rule C Contexts are not identied up to alpha

conversion unlike phrases of abstract syntax such as terms which are Write C C to

mean that contexts C and C are literally the same The sets of free and b ound variables

is a of a context C are written fv C and bv C resp ectively A canonical context V

context generated from the grammar for M canonical terms with metavariable V instead

of V augmented with an additional rule V

For any term M and context C dene the instantiation of M in C C M to b e the term

obtained by lling each hole in C with the term M Variables free in M may b ecome

b ound in C M suchas x when M hx y i and C x If V is a canonical context

then term V M is canonical for anytermM

Supp ose C and D are contexts and x is a v ariable If bv C fv D x dene

D

the substitution C x to be the context obtained by replacing each o ccurrence of the

variable x in C with the context D This is a literal substitution there is no renaming of

the variables b ound in C whichhave b een assumed to b e distinct from the variables free

in D

Weshow that in certain circumstances substitution and instantiation commute

Lemma Supp ose C and D are contexts x is a variable and M is a term If bv C

D D M

fv D x and x fv M then C x M CM x

Pro of By structural induction on context C We consider the following cases in detail

D M

Case C lhs M M rhs M x M since x fv M

D M

Case C x lhs DM rhs x x DM

D M

y x y Case C y x lhs y M y rhs

EXPERIMENTS AND CONTEXTUALEXPERIMENTS

Case C C C

D

lhs C C xM

D D

C x M C x M

D M D M

C M x C M x IH

D M

C M C M x

M D

x C C M

rhs

Case C y C Wehave y x since bv C fxg

D

lhs y C xM

D

y C xM

D M

y C M x IH

D M

y C M x

D M

y C M x

rhs

The other cases follow similarly to the last two and are omitted

Exp eriments and contextualexp eriments

Exp eriments were dened in Denition and used in Table as an economical notation

for dening the reduction relation on programs of M Recall the dening grammar

E SplitxyM j Casex N x N j M j SeqxN j Elim

We use exp eriments in this chapter to dene exp erimental order on programs When we

come to prove that exp erimental order coincides with contextual order we will use a variant

of Milners context lemma In this variant we need to extend the idea of an exp eriment

to contexts A contextualexp eriment E is given by the following grammar

xyC j Casex C x C j C j SeqxC j Elim E Split

The symbol o ccurs once in each exp erimentorcontextualexp eriment The term E M

and the context E C are obtained by replacing the o ccurrence of in E or E by the

term M or context C resp ectively Intuitively an exp eriment wraps a destructor term

t wraps a destructor context around a around a term similarly a contextualexp erimen

context Exp eriments are identied up to alphaconversion contextualexp eriments are

not Wedenefv E and fv E to the sets of variables free in exp eriment E and contextual

exp eriment E resp ectively

If E is a p ossiblyempty list of exp eriments E E then write E M to mean the term

n

E E M Similarly if E is a p ossiblyempty list of contextualexp eriments

n

E E write E C tomeanthecontext E E C

n n

Lemma

If M is a program then there is a unique list of exp eriments E such that either

M E V for some canonical program V or

M E Fixx N for some program Fixx N

CHAPTER OPERATIONAL PRECONGRUENCE

If C is a context then there is a unique list of contextualexp eriments E such that

either

C E or

C E x for some variable xor

C E V for some canonical context V or

C E Fixx C for some context Fixx C

Pro of By structural inductions on program M and context C

Exp eriments relate to reduction as follows

Lemma For any exp eriment E list of exp eriments E and programs M and N such

that terms E M E N E M and E N are programs

E M E N if M N

E M E N if M N

whenever E M V there is U such that M U and E U V

whenever E M V there is U such that M U and E U V

Pro of In Table reduction is closed under this inference rule so part holds

Part is a corollary of Part follows by an insp ection of the ve kinds of

exp eriment and the corresp onding evaluation rules in Table The pro of of is by

induction on the size of the list E The base case when the list is empty follows at once

Otherwise supp ose that E E M V By IH there is some U such that E M U and

E U V By there is some U such that M U and E U U From we have

  

E E M E E U E U V thatisE E M V

If E is an exp erimental context and M is a term then the instantiation of E with M

E M is the exp eriment obtained by lling in each o ccurrence of a hole in E with the term

M

Lemma For anycontextual exp eriment E context C term M and list of contextual

exp eriments E wehave

E C M EM C M

E C M E M C M

C xyC and we have Pro of Supp ose E SplitxyC Then E C Split

E M SplitxyC M so E C M SplitC M xyC M E M C M The

other four cases are similar Part is a corollary of

GROUND AND CONFINED RELATIONS IN M

b

Rules of R

b

x R x

M R N xy M RN

M R N i

i i

b

b

SplitM xyM R SplitN xyN

hM M i RhN N i

M RN M RN

b b

InlM R InlN Inr M R Inr N

M R N x M RN i

i i i i

b

CaseM x M x M R CaseN x N x N

x M RN

M R N i

i i

b

b

x M R x N

M M R N N

M RN x M R N M RN

b b

Lift M R LiftN SeqM xM R SeqN xN

b

x M R N



Fix x M R Fix x N

M RN M RN

b b

IntroM R IntroN ElimM R ElimN

b

Table Denition of R

Ground and conned relations in M

Recall that we dened notions of programs and conned terms in Denition

Denition

A ground relation R is a binary relation b etween programs of the same typ e

A conned relation R is a binary relation between conned terms of the same

typ e and environment Write M RN to mean that M N R

We can immediately make precise the notion of op erational adequacy which we seek to

of prove for contextual order and applicative similarity Let M program be Fixx x

typ e scheme for any



Denition A ground relation R is op erationally adequate i for all programs

M N and canonical programs V

If M N then N RM

If M V then V RM

M i M R

CHAPTER OPERATIONAL PRECONGRUENCE

Prop erties of Conned Relations

M RN

Weak

M RN

M RN

Stren

M RN

x M RM N

Sp ec

N N

M xRM x

M RN

Precong

CM RC N

b

M R N

Comp

M RN

x M R N M RN

Sub

M N

M x RN x

Table Rules concerning conned relations

M i for some canonical V V RM

Conned relations are imp ortant for the theory of M in Chapter We formulate some

useful op erations and prop erties of conned relations including precongruence

Denition

b

If R is a conned relation let the conned relation R be dened by the rules in

Table

We dene inference rules Weak Stren Sp ec Precong Comp and Sub in Ta

ble All these rules have the implicit sidecondition that any sentence denoting

ellformed i a pair of conned terms is wellformed A sentence M RN is w

pairs M and N are conned terms of the same typ e

A conned relation is natural i the rules Weak Stren and Sp ec are valid

A conned relation is a precongruence i the rule Precong is valid A congru

ence is a conned relation that is b oth a precongruence and an equivalence relation

Prop osition

If R is transitive and rules Sp ec and Precong are valid then rule Sub is valid to o

If R is a preorder then rule Precong is valid i rule Comp is valid

Pro of Supp ose x M RN and M RN By Precong we have

M N

M x R M x where we treat term M as a context By Sp ec we have

N M N N

x x RN x By transitivitywehave M x RN M

GROUND AND CONFINED RELATIONS IN M

Given that R is a preorder and Precong is valid we are to prove that

M RN implies M RN We pro ceed by an analysis of which rule from Table

derived M RN We examine the rule for Caseterms as a typical example Given

the inference

M RN x M R N i

i i i i

b

CaseM x M x M R CaseN x N x N

we are to show that CaseM x M x M R Case N x N x N From

Precong we can calculate

CaseM x M x M R Case N x M x M

CaseN x M x M R Case N x N x M

CaseN x N x M R Case N x N x N

These together with the transitivityof R prove the result The other cases are similar

Given that rule Comp is valid for preorder Rweproveby induction on the structure

of context C that for all M andN if M RN then CM RCN Consider any

C and cho ose anyM and N that satisfy the assumption M RN We examine three

cases

Case C Goal CM RCN is precisely M RN

Case C x xRx follows from reexivityofR

Case C CaseC xC xC By IH wehave C M RC N x C M RC N

and x C M RC N These together with the rule for Caseterms in Table

b

giveCM RCN and hence CM RCN as required

The other cases are similar

We will often need to induce a conned relation from a ground relation and vice versa

Denition

Let be an environment x x Then a closure is an iterated substi

n n

L L

n

tution x x where each L is a program The order of substitution

n

i i

do es not matter b ecause the variables are disjoint and each L is closed

i

The conned extension of a ground relation R is the conned relation R such

G

L L L

x R N x that M RN i for all closures x M

G

If R is a conned relation then its ground restriction is the ground relation

fM N j M RN g

If R is a conned relation write M RN to mean that pair M N is in the ground

restriction of R

Wetypically use the same symb ol for a ground relation and its conned extension If R

G

d b

is a ground relation and R is its conned extension we write R to mean R

G

Prop osition The conned extension of a ground relation is natural

rst two we Pro of We are to prove rule Weak Stren and Sp ec To begin with the

CHAPTER OPERATIONAL PRECONGRUENCE

must show the following inferences are valid

M RN

M RN

Weak Stren

M RN

M RN

where R is the conned extension of a ground relation R for any given M and

G

N suchthat the sentences M RN and M RN are wellformed By denition

of R these inferences would b e valid if the following condition were true

L L L L L L

M xR N x i M x x R N x x

G G

L L

for all closures x and closures x But we know that fv M N Dom from

L L L

the wellformedness of sentence M RN and therefore wehave M x x M x

L L L

N x These equations immediately imply condition as required and N x x

Finallywe are to prove the validity of the Sp ec rule

x M RM N

N N

M x RM x

L L L L

The sentence x M RM means that M x x R N x x for any program

G

L

x whichwe knowto be a program L and suitably typ ed program list L Let L be N

L

L L N x N L L

x foreach x x M x x x M since fv N Dom x Since M

i i i

N L N L N N

i we have M x x R M x x which is to say that M xR M x as

G

required

Contextual and exp erimental order

We b egin with a notion of op erational ordering whichwecallcontextual order

C

such that Denition Dene contextual order to be the conned relation

C

N i for all contexts C such that C M and C N are programs of the same M

typ e if C M converges so do es C N

Programs C M and C N can be of any typ e in Plotkins formulation they must be of

ground integer typ e

Prop osition Contextual order is a precongruence

C C

Pro of It suces to verify the Precong rule for Supp ose that M N For

C

some particular context D we are to show that DM D N This is to say that for

all contexts C suchthat C D M and C D N are programs of the same typ e if C D M

C

converges then so do es C D N This follows at once from M N by denition

It is not so straightforward to prove that contextual order is op erationally adequate

We do so by dening a second relation exp erimental order which is not hard to show

op erationally adequate and then showing that contextual and exp erimental order coincide

CONTEXTUAL AND EXPERIMENTAL ORDER

E

Denition Ground exp erimental order is the ground relation such that

E

M N i whenever E is a list of exp eriments with E M and E N programs of the

same typ e if E M converges so do es E N The conned relation exp erimental order

is the conned extension of ground exp erimental order

It is not hard to see that ground exp erimental order is reexive and transitive and hence

that exp erimental order is a preorder Weprove op erational adequacy

Prop osition Ground exp erimental order is op erationally adequate

Pro of We are to show for all programs M N and canonical programs V

E

N then N M If M

E

If M V then V M

E

M i M

E

M M i for some canonical V V

We are to show for all exp eriment lists E if E N then E M Since M N we

Corollary of have E M E N So if the latter converges so must the former

E

and the transitivityof

E

we need that for For the forwards direction supp ose that M To show M

all E if E M converges then so do es E But for no E do es E M converge For

E

So for all E if E M converges then so the backwards direction supp ose that M

do es E Pro ceed by con tradiction and supp ose that M converges Taking E to b e the

empty list wehavethatconverges since M do es Contradiction

For the forwards direction M means that M V for some V Then bypart we

E E

M From the denition M For the backwards direction supp ose that V have V

E

we have that if V converges then so do es M take the list of exp eriments to be of

empty But V converges b eing canonical

It is not hard to show the following

Prop osition Contextual order implies exp erimental order

C

N for some context x x and terms M Pro of Supp ose that M

n n

E

L L

and N It is necessary to show for any closure N whereM M x that M x

L

and N N x This is to say that for each suitably typ ed E ifE M converges then so

N Picksome list E and dene context C E x x L L do es E

n n n

n n

E N Then if E M converges then so must C M E M andC N Wehave C M

C

If C M converges then so to o do es C N given that M N But then E N must

converge as required

The other direction is harder The next two lemmas are needed to prove Prop osition

that the ground restriction of contextual order coincides with ground exp erimental order

Lemma Supp ose context C E V for some contextual exp eriment E and canonical

context V Then there is a context D such that for all programs L such that C L is a

CHAPTER OPERATIONAL PRECONGRUENCE

program C L DL

Pro of We assume that all the b ound variables in context C are distinct if they are not

we can easily nd a context C in which all b ound variables are distinct such that for

any program L C L alphaconverts to C L Since C L is a program fv C fv E

fv V Given sucha C the context D can b e dened as follows

C C

y x V hC C i and E SplitxyC then D C if

C

if V InlC andE Casex C x C then D C x

C

if V Inr C andE Casex C x C then D C x

C

x if V x C andE C then D C

C

if V LiftC andE SeqxC then D C x

if V IntroC and E Elim then D C

Given the assumptions wehave made ab out context C ineach case we can check that the

conditions hold for substitution of a context into another to be wellformed we omit the

C L is a program details From the typ e assignment rules in Table all the cases when

are covered ab ove and from the denition of onestep reduction it is not hard to see that

C L DL given Lemma

The pro of of the following lemma is based on a similar pro of due to Allen Stoughton for

the lazy calculus and based on Milners context lemma

E

Lemma Context For anytwo programs M and N of the same typ e with M

N and any context C such that C M and C N are b oth programs of the same typ e if

C M converges then C N converges

E

N We Pro of Cho ose anytwo programs M and N of the same typ e and assume M

induction on n that for any context C prove the following hyp othesis by mathematical

suchthat C M and C N are programs of the same typ e

if C M converges in n steps then C N converges

We pro ceed by a case analysis of C According to Lemma there is a p ossiblyempty

list of contextual exp eriments E such that one of four cases holds

Case C E V Either list E is emptyin which case b oth C M and C N are canonical

and hence convergeor the list takes the form E E and wehave C E E V Let

C E V and by Lemma there is a context D such that for any program L

if C L is a program then C L D L Let D E D and for any program L

wehave C L E E V L E LC L E LD L DL using Lemmas

and Since M and N are programs wehave C M DM andC N DN Now

D M must converge in n steps so by the induction hyp othesis D N converges

and therefore so to o do es C N as reduction is deterministic

Case C E Fixx C We may assume that all the b ound variables in context C are

by working with a con distinct if they are not we could prove the hyp othesis

text C in which all b ound variables are distinct and such that for any program

Fix x C

L C L alphaconverts to C L Set D E C x For any program L

CONTEXTUAL AND EXPERIMENTAL ORDER

Fix x C L

we have C L E Fixx C L E LFixx C L E LC L x

Fix x C

E LC x L D L using Lemmas and Hence we have

C M DM and C N DN Just as in the previous case since D M must

converge in n steps by the induction hyp othesis D N converges and therefore

so to o do es C N

Case C E Let context D E M and then D M E M M CM so D M

converges in n steps But context D must take one of the forms already considered

E

so we can conclude that D N E N M converges But we have M N so

E N N converges and E N N CN

Case C E x Trivial b ecause for no term L is C L a program

All cases considered the hyp othesis is proved for all n and the lemma follows

A corollary of this lemma and Prop osition is that ground exp erimental order and the

ground restriction of contextual order are the same

C E

Prop osition For all programs M and N of the same typ e M N i M N

Pro of The forwards direction is a sp ecial case of Prop osition For the backwards

E

N and that C M converges where C is a context Hence direction supp ose that M

by the context lemma C N converges to o

We can extend this result to op en terms via the next three lemmas

d

E E

N where programs M and N are as follows N then M Lemma If M

M hM M i and N hN N i

M SplitM x x L and N SplitN x x L

M InlM and N InlN

M Inr M and N Inr N

M CaseM x L x L and N CaseN x L x L

M LiftM and N LiftN

M SeqM xL and N SeqN xL

M Intro M and N Intro N

M ElimM and N ElimN

M M M and N N N

E C

Pro of Since each M and N is a program and M N ineachcasewehave M N

i i i i i i

C

by Prop osition Contextual order is a precongruence so M N in each case and

E

therefore M N by Prop osition as required

d

E E

Lemma If M N then M N where programs M and N are as follows

M x M and N x N

M SplitL x x M and N SplitL x x N

CHAPTER OPERATIONAL PRECONGRUENCE

M CaseL x M x M and N CaseL x N x N

M SeqL x M and N SeqL x N

Pro of For any list E supp ose that E M We are to showthat E N Wehave that

L L

M by Lemma Hence L hL L i and wehave M M x x and N

d

E E

L L L L L L

N x N we can deduce M x From M x x N x x Since

L L L L

E M converges we have that E M x x converges and so E N x x

L L

x wehave that E N converges as required x converges Since E N E N

Parts and followby similar arguments whichweomit

d

E E

N then M N where program M Fixx M and N Lemma If M

Fixx N

Pro of The pro of is a generalisation of the argument in the context lemma Weprove the

following hyp othesis by mathematical induction on n that for any context C such that

C M andC N are programs of the same typ e

if C M converges in n steps then C N converges

E C

N follows by Prop osition Just N from which M So we actually provethatM

as in the context lemma Lemma we pro ceed by a case analysis of C There is a

p ossiblyempty list of contextual exp eriments E such that one of four cases holds

Case C E V The argument of the context lemma is valid here b ecause it to ok no

account of the structure of programs M and N

Case C E Fixx C For the same reason the argument of the context lemma applies

Case C E Let D be the context E M x where term M is b eing used as a

context we can assume that the b ound variables in context M are distinct from x

M

So C M E M M E M M x E M x M DM and hence D M

N

converges in n steps By IH we have that D N E N M x converges

d

E E

N N N

N we have that M x Since M N x Therefore since E N M x

N N

converges so do es E N N x But C N E N N x so C N converges

L is C L a program Case C E x Trivial b ecause for no term

d

E E

Prop osition If M N then M N

Pro of By considering each rule from Table in turn We consider the rule for Case

terms in detail Wehave M CaseM x M x M andN CaseN x N x N

E E

N i M N x M

i i i i

d

E

M N

L L

Let x be any closure and for any term L write L for L x We are to prove that

E

N thatis M

E

CaseM x M x M CaseN x N x N

APPLICATIVE AND COMPATIBLE SIMILARITY

Using Lemmas and wehave

E E

N CaseM x M x M CaseN x M x M from M

E E

CaseN x M x M CaseN x N x M N from x M

E E

N CaseN x N x M CaseN x N x N from x M

Then by transitivity of exp erimental order we have as required Each of the other

cases follows similarly

This prop osition together with Prop osition establishes that exp erimental and con

textual order are the same and hence that contextual order is an op erationally adequate

precongruence

Applicative and compatible similarity

We now dene the ground preorder applicative similarity which we will prove in x to

b e an indep endentcharacterisation of contextual order

Denition Dene function over ground relations such that

b

M S N i whenever M U there is V with N V and U S V

An applicative simulation is a relation S such that S S Dene ground applica

A

tive similarity to be the union of all applicative simulations Dene applicative

A

to b e the conned extension of ground applicative similarity similarity

Prop osition

Function is monotone

The identity ground relation is an applicative simulation

If each S is an applicative simulation then so is S S

i

Ground applicative similarity is the greatest xp oint of and is the greatest ap

plicativesimulation

A

M N i there is an applicative simulation S such that M S N

Ground applicative similarity is a preorder

easy to check from the denition The remaining parts Pro of Parts and are

then follow from Prop osition

Denition An applicative bisimulation is a relation S such that both S and



A

S are applicative simulations Ground applicative bisimilarity is the ground

A A

A A

relation suchthat M N i M N and N M Applicative bisimilarity is

the conned extension of ground applicative bisimilarity

Clearly applicative bisimilarity is an equivalence relation Furthermore for any programs

A

M and N M N i for some applicativebisimulation S M N S

A

A

Prop osition Ground relations and are b oth op erationally adequate

CHAPTER OPERATIONAL PRECONGRUENCE

Pro of When R stands either for applicative similarityor bisimilaritywe need to prove

each of the following prop erties

If M N then N RM

If M V then V RM

M i M R

M i for some canonical V V RM

Toprove parts and it is sucienttoshowthatS is an applicative bisimulation

where S is given by

def



fN M j M N g S



Observe rst that if M N then for an y U M U i N U from Prop osition

c

and that Id S Supp ose N M S and N U Then M U and U Id U Similarly



c

supp ose M N S and M U Then N U and U Id U

For the forwards direction of part weshow that S below is an applicative bisimulation

def

fM jU M U g S

For any pair M N S neither M nor N so S is trivially an applicative bisimu

A

A

implies that pair lation For the backwards direction of either M or M

M is contained in some applicativesimulation Hence as cannot converge neither

can M

The forwards direction of part follows immediately from the denition of M and part

A

A

For the backwards direction either U M or U M implies that pair U M is

contained in some applicative simulation so M since U

We prove that applicative similarity for M is a precongruence by a typ ed reworking of

Howes metho d



to be the Denition Dene the conned relation compatible similarity

least set closed under the following rule

 A

c

L M M N



N L



Beware the ambiguous notation the in do es not denote reexive transitive clo

sure

Prop osition

Applicative similarity is natural that is rules Sp ec Weak and Stren are valid

 

c

Conned relations and are reexive

 A 

c

b oth imply Conned relations and

 A 

M M then M M and M If M

  

M N

Sub Ifx M N and M x x N then M N

APPLICATIVE AND COMPATIBLE SIMILARITY

 

c

If U M then there is V such that M V and U V

 

If M N and M U then U N

 A

If M N then M N

Applicative similarity is a precongruence

Pro of Applicative similarity is natural b ecause it is the conned extension of a ground

relation Prop osition

 

By structural induction on M one can easily verify that M M so is reexive



c

is reexive A corollary is that

Corollary of part and the denition of compatible similarity

 A A 

c

N and N M Since is M then for some N M If M

 A

M transitive wehave N M Then by denition wehave M



The pro of is by induction on the depth of inference of x M N By denition

 A

c

there is N such that x N and x N N By Sp ec we have M

A

N N

N x Pro ceed by a structural analysis of M and showineach case N x



M N

N that M x x The result will then follow by part We show three

The other cases are similar cases



Case M x So N N by assumption x Wehave M



Case M y x So N y by reexivity part y Wehave y



Case M y M So N N By IH y y N and x y M

 

c

M N M N

M x N x So M x N x by denition Then we have



M N

N M x x by part as required

A 

c

M Pro ceed by N and N In each case there must b e a program N such that U

a structural analysis of canonical program U

 A

M So N x N with x M N Then N M implies that Case U x

A 

M x N with x N N Therefore x M N by part which



c

as required x N implies U

A 

N for i Then N M implies Case U hM M i So N hN N i with M

i i

A 

that M hL L i with N L So M L by part and U hM M i so

i i i i



c

hL L i as required U

By induction on the depth of inference of M U pro ceeding by analysis of M

Case M canonical Immediate



M

N there x U From M Case M M M We have M x M and M

 A

N and N N N Since M x M and are N N such that each M

i i

 

N wehavex M N by IH By there is N with N x N and M

  

N N M

N x M N x N x and then byIHwehave U By Sub M x

A

N N

Since N N N x we have N x N N by op erational adequacy

CHAPTER OPERATIONAL PRECONGRUENCE

A A 

N N

From N N x N wehave N N by transitivity Finally from U x N



wehave U N by

The other cases follow the same pattern as the ones ab ove

First we prove that the ground restriction of compatible similarity is an applicative



e are to show that simulation Supp ose for any programs M and N that M N W



c

V Supp ose that M U By whenever M U there is V such that M V and U

 

c

wehave U N By there is V such that N V and U V

 A

Supp ose nowthat M N We are to prove that M N whichistosaythatfor

L L L

all closures x programs M x and N x are paired in an applicative simulation



L L

But by Sub and reexivitywehave M x N x and since the ground restriction of

compatible similarity is an applicativesimulation we are done

A corollary of and is that applicative and compatible similarityare the same

conned relation We know applicative similarity is a preorder from Prop osition

We know that rule Comp holds for compatible similarity from part Therefore by

Prop osition applicative similarity is a precongruence

Contextual order and applicative similarity

Contextual order equals applicative similarity Our pro of is essentially a typ ed reworking

of Howes Theorem

Lemma Ground applicative similarity equals the ground restriction of contextual

order

Pro of Let S b e the ground restriction of contextual order

def

C

N g S fM N j M

C A A

N If and showthat M S it is enough to consider anyM N Toseewhy

A A

C M for some context C thenC M C M since is a precongruence and therefore

as required C M

For the reverse inclusion it suces by coinduction to show that symmetric S is an

C

applicative simulation Supp ose then that M N S and that M V Since M N

there must b e U such that N U or else the trivial context C would distinguish M

b

and N Based on the following case analysis of V weshow that V S U as required for S

to b e an applicative simulation

Case V InlM Since U and V have the same typ e U must take one of the

forms InlN or Inr N We can rule out the latter or else the context C

CasexLy where L is some convergent program such as Liftx x would

C

distinguish M and N Let context D be CasexxyM Wehave D M D N

C

N By op erational since is a congruence Moreover D M M and D M



adequacy and determinacy the reduction relation and its inverse are in

A A A A

cluded in Hence wehave M D M and D M N Since S wehave

CONTEXTUAL ORDER AND APPLICATIVE SIMILARITY

b

M N S and therefore V S U

b

Case V xM This time U must take the form x N Toshowx M S x N

L L

it suces to provethatM x N x S for arbitrary program L Let context

C

L L

xandD N N x D b e L Wehave D M D N Since D M M

L L

wemay obtain M x N x S as in the previous case

Case V LiftM Again we may conclude that U takes the form LiftN To show

C

b

N Supp ose then for an that LiftM S LiftN it suces to prove that M

arbitrary context C thatC M and C N are programs of the same typ e and that

C M converges We must show that C N converges to o We may supp ose that

C M and C N are of lifted typ e otherwise by the convergence theorem for M it

follows that b oth always converge Set context D to be SeqxC x We have

C

that D M C M and D N C N As b efore we have that C M D M

C C C

and D N C N Since M N we have also D M D N and therefore by

C

erges so do es C N Hence transitivity that C M C N Therefore if C M conv

b

wehave established LiftM S LiftN as required

The other cases followby similar reasoning

A C

Theorem Op erational Extensionality

Pro of Wemust show

A C

N N i M M

for arbitrary M and N The backwards direction follows by the same argument as used

A

S As for the forwards direction wemust show in the previous lemma to prove that

that

A

L L

x N x M

L

x Set context D to be x for any closure x L L and we have

n n

A C

D N and since D N by precongruence By Lemma wehave DM D M

A

L L L L

x as required N x x wehave M x and D N N D M M

In summary this chapter considered two indep endent denitions of op erational order

contextual order and applicative similarity and proved that they are in fact the same



relation henceforth denoted by We use for op erational equivalence equal to

also known as contextual equivalence or applicative bisimilarity Both characterisations

are of interest By denition contextual order is p erhaps the simplest congruence induced

by the evaluation relation By denition applicative similarity is a greatest xp oint

giving rise to a powerful principle of coinduction used extensively in Chapter Given

op erational extensionalityHowes metho d and the context lemma metho d amount to the

same thing a pro of that op erational order is an op erationally adequate precongruence

In a calculus where op erational extensionality fails the two metho ds would b e of separate

interest

CHAPTER OPERATIONAL PRECONGRUENCE

Chapter

Theory of the metalanguage

We conclude the three chapters on M by obtaining results ab out evaluation and equiva

lence needed in connection with the denotational semantics of H in the next chapter We

make extensive use in this chapter of the principal results obtained in Chapters and

that no term of a certainlyconvergent typ e can diverge and that op erational equivalence

that is contextual equivalence or applicative bisimilarity is an op erationally adequate

congruence

In x we prove basic facts ab out op erational equivalence these facts parallel the axioms

of LCF but are simpler in that there are divergent terms only at lifted typ es This

is a consequence of the convergence theorem proved in Chapter via Mendlers calculus

def

X X develop ed in Chapter x investigates empty and onep oint typ es in M

def

and resp ectively There are no programs of typ e Anytwo programs of typ e

are op erationally equivalent x sketches the standard construction of iterated sum and

pro duct typ es from binary sums binary pro ducts and the empty and onep ointtyp es We

conclude the chapter byinvestigating typ es of b o oleans and natural numb ers in x and

x resp ectively

Laws of op erational equivalence in M

Recall that op erational equivalence and order and equal the conned relations of

A

A

resp ectively Weknow that b oth these applicative bisimilarity and similarity and

are op erationally adequate precongruences Let ground equivalence be the ground

restriction of op erational equivalence

Prop osition All the laws from Table hold for op erational equiv alence

Exhaustion law

Congruence

Canonical Freeness

Beta strictness and eta laws

Coinduction

CHAPTER THEORY OF THE METALANGUAGE

Exhaustion Law

If M is a program and is certainlyconvergentthenV M V

If M is a program then either M orN M Lift N



Congruence

b

If M N then M N

Canonical Freeness

b

If U V then U V

Beta Laws

M M

x x SplithM M ix x N N

M

CaseInlM x N x N N x

M

CaseInr M x N x N N x

N

x M N M x

M

SeqLift M xN N x

Fixx M

Fix x M M x

Elim IntroM M

Strictness Law

SeqxN

Eta Laws

SplitM x x hx x i M

CaseM x Inl x x Inr x M

x Mx M if x fv M

SeqM x Lift x M

IntroElimM M

Coinduction

is a ground relation S such that whenever A bisimulationupto

M S N either M N or there are canonical programs U and V

b

such that M U N V and U S V

Any bisimulationupto is a subset of ground equivalence

Table Laws of equivalence in M

LAWS OF OPERATIONAL EQUIVALENCE IN M

Pro of Supp ose that M is a program and that is certainlyconvergent By the

convergence theorem Theorem M which is to say V M V By op erational

adequacywehave M V

Supp ose that M is a program Either M or M In the rst case we are done In



the second case there is some canonical program V such that M V Since V by



insp ection of the typing rules there must be a program N such that V LiftN By

op erational adequacywehave M V LiftN as required

Congruence for applicative bisimilarity is a corollary of Prop osition in whichwe

showed that applicative similarity and compatible similarity coincide The congruence

rule in Table is the same as the Comp rule in Table The Precong rule in

Table is a common way to dene precongruence We showed in Prop osition

that for any preorder such as applicative similarity or bisimilarity rule Comp is valid i

rule Precong is valid

Note rst a corollary of Prop osition for any canonical programs U and V

b

U V implies U V

L L L

b

x Wehave U V which x V x that U We are to showforany closure

L L L

x x V x U is to say that for all closures Since U and V are canonical terms

L L

x are canonical programs Therefore the corollary of Prop osition x andV U

b

L L

x as required V x applies and wehave U

Each law takes the form M M First we show that each law holds for

L R

ground equivalence when is empty For each beta lawwe can check by insp ection that

M M In the strictness law M andM Therefore by op erational adequacy

L R R L

wehave M M

L R

There are ve eta laws We can prove each lawbya case analysis of program M based

R

on the exhaustion law In the lawforSplit there must b e programs M such that M

i R

hM M i By the beta law for Split we have M SplithM M ix x hx x i

L

M M

x M Recall the general convention that b ound variables such x hx x i

R

as x and x in the table are distinct In the law for abstraction there must be a

term M such that M y M We know that x fv M So by the beta law for

R R

x

y M The other cases are similar abstraction M x M

R L

Second we prove each law for nonempty Supp ose we are to prove a law of the form

L L L

M M We are to show for any closure x that M x M x Terms

L R L R

are identied up to alphaconversion so we may assume that any bound variable in M

L

is distinct from x Therefore the closure can be distributed into the bodies of or M

R

terms M and M to obtain programs M and M to which the law with empty can

L R L R

b e applied

Supp ose S is a bisimulationupto We will prove S where S is S the

 

relational comp osition of S and Since S S we will have established S as



required

Supp ose then that M S N and that We show that S is an applicative simulation

 

d

M U We must nd V such that N V and U S V Since M S N there must be

 

programs M and N such that M M M S N and N N Since M U and M M

CHAPTER THEORY OF THE METALANGUAGE

b

there must b e a canonical program U such that M U and U U Since M U we

have M U whichwithM S N implies there must b e canonical U and V such that

b

S V and V N Since V N there must be canonical V such that M U U

and V V From N V and N N there must b e a canonical V such that N V

b

N V and V V In all wehave

b

b b

U U U S V V V

b

b b b b

and therefore by canonical freeness that U S V Now for any conned relations R

c d

b d

and R it is not hard to verify that R R RR Hence wehave U S V whic h completes



the pro of that S is an applicative simulation Since S is symmetric we have shown

 

that S as required



Empty and onep oint typ es in M

def def

Let typ e X X and in the following prop ositions we prove that is

empty and that has just one element

Prop osition There is no program of typ e

Pro of First consider the relation on programs of typ e inductively dened by the

following two rules

M N

M N

Intro M Intro M

M N

By denition includes the reduction relation but also reduces b eneath Intro s Since

is a partial function so is In Prop osition weshowed that each reduction of

an M program can b e matched by one or more reductions of its translation into

According to Table the translation Intro M is program Intro M we can

strengthen Prop osition to the following

If M and M N in M then M N in

e have a corollary analogous to the normalisation result for M Theorem Hence w

Let an normal form b e a program M of typ e such that M



For each M there is an normal form N such that M N

Any normal form is also an normal form so it must lo ok like Intro M where M

is also an normal form So then each normal form contains another normal form

that contains one less Intro If there is an normal form it must contain a nite number

of Intro s Therefore if there is an normal form at all there must b e one containing no

Intro s But this is imp ossible as each normal form is also an normal form and

hence must contain an outermost Intro Hence there can be no normal forms and

therefore no programs of typeatall

Prop osition There is a canonical program oftyp e suchthatforany program

M oftyp e M

ITERATED PRODUCTS AND SUMS IN M

def

Pro of Let xx Toshow that is unique up to it suces to show that relation

S is a bisimulation

def

S fM j M g

Supp ose that M S Since is certainlyconvergent there must be some canonical

term xN such that M xN Since xx we must show that

b

L L

S xN that is for all programs L that x xN x S But this is vacuously

true as there are no programs suchas L

Iterated pro ducts and sums in M

Dene iterated pro duct and sum notations by induction on n

if n

def

if n

n

if n

n

if n

def

if n

n

if n

n

Prop osition Supp ose and Then

n n

there are canonical terms hM M i and In M for i n and terms

n

i

SplitM x x N and CaseM x N x N such that

n n n

M x x N

M i n

n n

i i

SplitM x x N

hM M i

n

n

M

M x N i n

i

i i i

In M

CaseM x N x N

n n

i

M hM M i

n

M M

n

SplitM x x N N x x

n

n

M In M

i

M

CaseM x N x N N x

i

n n i

Pro of We can dene the canonical terms as follows

if n

def



n

M if n

hM M i

n



n



hM hM M i i if n

n

M if n

def





n

n



Inl M if n i

M In

i



n



In M if n i Inr

i

CHAPTER THEORY OF THE METALANGUAGE

Given these denitions it is straightforward to dene terms SplitM x x N and

n

CaseM x N x N and to prove the desired prop erties

n n

Bo oleans in M

We dene a b o olean typ e Bo ol and two Bo olprograms btt c and b c

def

Bo ol

def

btt c Inl

def

b c Inr

Prop osition Any program of typ e Bo ol either equals btt c or b c Moreover btt c

do es not equal b c

Pro of Supp ose M is a program of typ e By the exhaustion law M either equals

InlN or Inr N for some program N By Prop osition program N equals so M

either equals InlorInr that is btt c or b c

Toshowthatbtt c do es not equal b c pro ceed bycontradiction and supp ose that Inl

b

Inr By canonical freeness Inl Inr But there is no rule in Table which could

b

derive Inl Inr since the two terms have dierent outermost constructors Inl and

Inr Contradiction

We dene the notation if M then N else N to mean CaseM u N uN where variable

u fv N The if notation has the following prop erties

i

Prop osition

The following typ e assignment rule is valid

M Bo ol N i

i

if M then N else N

The op erational b ehaviour is characterised by the following rules

M b c M btt c

if M then N else N N if M then N else N N

The following equational laws are valid

if btt c then N else N N

if b c then N else N N

if M then N else N N

Pro of Immediate In the rst rule since M btt cwehave M InlL for some



uN N The second rule program L So Case M u N uN CaseInlLuN

follows by a similar argument These equations are simple consequences of the b eta

and eta laws in Table

NATURAL NUMBERS IN M

Natural numbers in M

We dene a typ e Num and a Numprogram bnc for each natural number n N

def

Num X X

def

bc IntroInl

def

bn c IntroInr bnc

Prop osition Any Num program equals bnc for some natural number n N More

over bnc do es not equal bn cforany n N

Pro of The second part follows easily from canonical freeness For the rst part we dene

another extension of the reduction relation on Num programs

M N M N

M N

Intro M Intro M Intro Inr M Intro Inr N

M N

Evidently is a partial function and is contained in It is normalising in the sense that



if M is a Num program there exists an normal form N such that M N The pro of

is by app eal to the emb edding of M in from Chapter similar to Prop osition

If Numprogram M is normal then it either takes the form IntroInl N where N

or Intro Inr N where N is also normal Hence for any normal form M there is a

cby induction on the number of Intros in M Moreover for number n such that M bn

an arbitrary Num program M there is an normal form N suchthat M N and hence

anumber n such that M bnc

Let abbreviate the partial function The next prop osition provides a general



metho d for dening partial functions by recursion

Prop osition For eachtermF that satises

F

there is a term Rec F such that

Rec F

and if F is a program

Rec F M F Rec F M

for any program M

Pro of Let terms REC F and Rec F with typ es and resp ectively be as



follows

def

REC F Fixfx Lift x Seq fx gF g x

def

Rec F x Seq REC F g F g x

CHAPTER THEORY OF THE METALANGUAGE

One can easily verify that Rec F is of the desired typ e Now supp ose that M is a program

of typ e Wemay calculate as follows

REC F Fixfx Lift x Seq fx gF g x

Liftx Seq REC F g F g x

LiftRec F

Rec F M x Seq REC F g F g x M

SeqREC F g F g M

SeqLiftRec F gF g M

F Rec F M

Hence w ehave the desired reduction b ehaviour

In order to dene the elementary arithmetic op erations needed in the denotational seman

tics of H we need the following programs

def

Succ xNum Lift Intro Inr x

def

Pred xNum Case Elimxu Lift bcy Lift y

def

Iter Z F Rec g Num y Num

CaseElimy u Lift Z y Seq gy xFx

Prop osition

The typ e assignments

Succ Num Num

Pred Num Num

and

Z F

Iter Z F Num

are valid

These programs have the following reduction behaviour where Z and

F

Succ bnc Liftbn c

Pred bc Liftbc

Pred bn c Liftbnc

Iter Z F bc LiftZ

Z F bncxF x Iter Z F bn c SeqIter

Pro of By insp ection From the b eta rules of Table We calculate the last two

prop erties For any m wehave

Iter Z F bmc CaseElim bmcu Lift Z y Seq Iter Z F y xFx

When m wehave

Iter Z F bmc LiftZ

and when m n

Iter Z F bmc SeqIter Z F bncxF x

NATURAL NUMBERS IN M

as required The other equations are no harder to prove

We can dene some simple arithmetic op erations

def

Num

M bcN Iter M Succ N

def

Num

M bcN Iter M Pred N

def

Num

M bcN Iter bc x xbcM N

def

IszeroM CaseElim M y btt cy b c

def

M bcN SeqN bcM x if Iszero x then Liftb c else Liftbtt c

def

M bcN SeqM bcN b if b then Liftb celse

SeqN bcM b if b then Liftb c else Liftbtt c

Prop osition

bmcbcbncLiftbm nc

bmcbcbncLiftbm nc

bmcbcbncLiftbm nc

Iszerobc btt c

bn c b c Iszero

bmcbcbncLiftbtt c if mn

bmcbcbncLiftb c if n m

bmcbcbmcLiftbtt c

bmcbcbncLiftb c if m n

Pro of From the previous prop osition by inductive arguments

CHAPTER THEORY OF THE METALANGUAGE

Chapter

An op erational theory of

functional programming

The second half of this dissertation which begins with this chapter examines a small

functional language called H and considers how theories for various forms of functional

IO can be based on a theory of H This chapter shows how to develop an op erational

theory of programming for H Two parallel theories are develop ed one based directly

on an op erational semantics of H the other based on a denotational semantics of H in

M The former has b een develop ed from rst principles in this dissertation whereas the

latter dep ends ultimately on Mendlers result assumed in Chapter This chapter is one

of the most imp ortant in the dissertation b ecause of its p otential applications far b eyond

the study of functional IO

H is essentially a fragment of Haskell H contains the basic data and control constructs of

lazy functional programmingnatural numbers b o oleans functions recursion and lazy

algebraic typ esbut is small enough that its theory can b e develop ed in this chapter We

omit from H many constructs needed for practical programming but which are irrelevant

to the study of functional IOsuch as p olymorphic typ es typ e inference mo dules and

realistic arithmetic

In x we dene syntax and typing rules for the ob ject language H The abstract syntax

of H is rather restrictive compared to Haskell notation We show informally how certain

Haskell notations maybeinterpreted as H programs This interpretation allows us to use

of theories of functional Haskell notation in Chapters and for H in our development

IO In a study like this which is meant to be relevant to practical programming it is

go o d metho dology to work with as realistic a notation as p ossible Another merit of using

Haskell notation is that programs can b e typ e checked and executed

Wefollow the pattern set by Plotkin and givetwo semantics for H a deterministic

lazy op erational semantics and a denotational semantics in terms of the metalanguage M

The style of denotational semantics is greatly inuenced bythework of Moggi

and Pitts on the computational calculus as a semantic metalanguage The most

striking dierence between the computational calculus and M is that the formers se

Wehave used Mark Jones Gofer system and also an implementation of H on top of Standard ML

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

mantics is ultimately domaintheoretic whereas the latters semantics is entirely op era

tional We nish x by proving a corresp ondence theorem b etween the op erational and

denotational semantics that evaluation of any H term e is exactly matched byevaluation

of its M denotation e

We base our theory of functional programming on equivalence of H terms x develops

notions of ground and conned relations We develop two relations op erational equiv

alence and denotational equivalence x develops op erational equivalence directly from

the op erational semantics of H Weprove that op erational equivalence for H is a congru

es metho d just as we proved the same result for M in Chapter x ence using How

denes denotational equivalence as the equivalence relation induced on H terms from the

denotational semantics in M The main result of the section is a soundness theorem that

denotational equivalence implies op erational equivalence Weleave op en the converse full

abstraction

In x we state a collection of basic programming laws and show that they are valid for

b oth op erational and denotational equivalence x shows how a further collection of laws

and pro of principles follow from the basic programming laws of the previous section The

theory of functional programming develop ed in x and x is entirely op erational in

that it rests on op erational equivalences dened from the op erational semantics of either

M or H

Finally x considers what happ ens to the theory of functional programming if an ex

ception mec hanism is added to H The resulting language is called HX and is needed in

Chapter in the study of Landinstream IO

H a small functional language

Table shows the abstract syntax of H essentially a fragment of Haskell The only

construct of H not present in Haskell is callbyvalue function application e e Call

byvalue is included in H b ecause of the control it gives over evaluation order suchcontrol

is useful when IO is expressed using sideeects

We assume countably innite sets of typ e variables and term variables ranged over by

y z resp ectively We assume there is a countably metavariables X Y Z and x

innite set of value constructors ranged over by metavariable K There are six cate

gories of abstract syntax typ es algebraic datatyp es dataclauses dc

canonical terms c terms e p qandcaseclauses cc Binding o ccurrences of typ e

and value variables are indicated in Table The notations and conventions concerning

variables substitution and alphaconversion stated in x apply to H

We let metavariable ranges over the set of literals ftt gN We use metavariable

to range over the arithmetic op erators f g An H literal or op erator is written

as or the corresp onding canonical program in the metalanguage is written bc or bc

resp ectively The H typ e Int includes just the natural numb ers we call this typ e Int for

the sake of compatibility with Haskell

Supp ose that is the algebraic typ e data X dc dc Then dene functions

n

H A SMALL FUNCTIONAL LANGUAGE

Syntax

X typ e variable

j Bool b o oleans

j Int natural numbers

j functions

j algebraic datatyp e

data X dc dc algebraic datatyp e n

n

Con dc Con dc ii j

i j

X b ound in each dc

i

dc K dataclause m

m

c literal ftt g N

j x e abstraction x b ound in e

j K e e constructor application m

m

e x value variable

j c canonical expression

j if e then e else e conditional

e arithmetic f g j e

j e e callbyname application

j e e callbyvalue application

j rec x e recursion x bound in e

j case e of cc cc caseexpression n

n

cc K e caseclause

Table Syntax of H

Con dc Rank K andArity K as follows

def

Con K K

m

def

Rank K i if K Con dc

i

def

Arity K m if dc K

i m

Op erations Rank and Arity are welldened functions b ecause the sidecondition on alge

braic typ es in Table requires that no two dataclauses contain the same constructor

We place a further wellformedness condition on algebraic typ es in any algebraic typ e

dc for each clause dc K in each typ e we insist data X dc

n i m j

that the typ e variable X o ccurs p ositively in each and that the only typ e variable free

j

in is X Atyp e variable X o ccurs p ositively in a typ e i each o ccurrence of X in

j

is to the left of an even numb er of function s The only reason we make this restriction

is so that the typ es of H can be mapp ed into wellformed typ es of M We implicitly

assume that any H typ e wedealwithiswellformed Just as in M the set of wellformed

typ es is closed under substitution

Typ e assignment in H

An environment is a list of variables paired with closed typ es written

x x where the variables are pairwise distinct We adopt the same no

n n



We followHaskell in using the symb ol for typ e assignment

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Typ e Assignment

n N

fT F g

n Int

Bool

x x

e Bool e e

if e then e else e

e Int e Int

f g

e e Int

e Int e Int

f g

e e Bool

x e

e e

x e

e e

x e

e e

rec x e

e e

e X j m

j j

data X dc dc

n

dc K

i m

K e e

m

e

m

K e K

m

e cc dc X i n

i i

data X dc dc

n

case eofcc cc

n

Canonical Terms

If cBool then b ftt g c b

If cInt then n N c n

If c then e c x e

If c then K e e c K e e

m m

Table Typ e assignment rules for H

H A SMALL FUNCTIONAL LANGUAGE

tational conventions as dened for M environments at the b eginning of x

Denition The H typ e assignment relation e is inductively dened by

the rules in Table which make use of an auxiliary relation cc dc dened in the

rule marked with Each rule b ears the implicit sidecondition that any environments

appearingintherulearewellformed

Prop osition

If e and e then

If e and then e

e

If x e and e then e x

If e then fv e Dom

The statements ab out canonical terms in Table are true

If e then ftv e

Pro of Easy rule inductions Part dep ends on the fact that no typ e variable can o ccur

free in a typ e in the range of an environment and that the environments used to derive

the typ e assignment relation must b e wellformed

We dene classes of programs and conned terms

Denition

A program is a term e such that e for some necessarily unique typ e

The typ e is called the typ e of ewhich is called a program

A conned term is a pair e such that there is a necessarily unique typ e

such that e The typ e is called the typ e of e and is called the

environment of e

Just as in M the syntax of H carries enough typ e information that any program has a

unique typ e We often omit typ e information from terms

Interpreting Haskell in H

We show in this section how some Haskell notations absent from H can nevertheless be

interpreted as derived forms of H For tutorial and reference material on Haskell we refer

the reader to the Haskell rep ort and to Fasel and Hudaks tutorial We shall use a

Haskelllike notation for the programming examples in the remainder of this dissertation

We do not attempt a formal description of how Haskell can be interpreted in H but

instead give a series of illustrative examples Descriptions of similar interpretations are

eyton Jones textb o ok discusses in detail how Miranda wellknown for instance P

can b e translated into a calculus notation of ab out the same level as H

We will use the letters a b and c to stand for closed typ es of H Typ e and value denitions

in Haskell may b e p olymorphic that is they may dep end on one or more typ e parameters

Weinterpret such denitions in H as dening families of closed typ es or terms indexed by

the typ e parameters Here are three examples the second of which also illustrates how

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

id a a

id x x

fst ab a

fst ab a

snd ab b

snd ab b

const a b a

const a b a

curry ab c a b c

curry f a b f ab

uncurry a b c ab c

uncurry f ab f fst ab snd ab

b c a b a c

f g x f g x

a head a

head xxs x

tail a a

tail xxs xs

a a a

ys ys

xxs ys xxsys

map a b a b

map f

map f xxs f x map f xs

iterate a a a a

iterate f x x iterate f f x

foldr a b b b a b

foldr f z z

foldr f z xxs f x foldr f z xs

Table Some standard functions

H A SMALL FUNCTIONAL LANGUAGE

def

id x x

def

fst ab case ab of Tuple a b a

def

snd ab case ab of Tuple a b b

def

const a b a

def

curry f a b f a b

def

uncurry f ab f fst absnd ab

def

f g x fgx

def

head xs case xs of

Nil

Cons x xs x

def

tail xs case xs of

Nil

Cons x xs xs

def

recapp xs ys case xs of

Nil ys

Cons x xs Cons x app xs ys

def

map recmap f xs case xs of

Nil Nil

Cons x xs Cons fxmap f xs

def

reciterate f x Cons x iterate f fx iterate

def

foldr recfoldr f z xs case xs of

Nil z

Cons x xs f x foldr f z xs

Table Interpretations of Haskell functions in H

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Haskellstyle algebraic datatyp es are interpreted in H

Interpretation in H Polymorphic Haskell denition

def

a

type Endo a a a Endo aa

def

a

List data X Nil Cons a X data List a Nil Cons a List a

id a a

def

a

id x ax

id x x

We use notation in typ ewriter fontforH literals for instance interpret True False

and as the b o olean literals tt and the numeric literals and and the

addition op erator resp ectively

Write a as an abbreviation for List a Dene

def

Nil empty list

def

e e Cons e e e nonempty list

n n

def

Cons e e inx cons e e

Dene a series of p olymorphic tuple typ es indexed by m

data Tuplem a am Tuplem a am

Dene the following abbreviations for tuples where m

def

a a Tuplem a a mtuple typ e

m m

def

e e Tuplem e e tuple

m m

def

x x e y case y of tupled abstraction

m

Tuplemx x e y fv e

m

The tuple typ e is called the unit typ e

To discuss teletyp e IO we need to supp ort the Haskell typ e Char of ASCI I characters

For the b enet of simplicityat the cost of cavalier software engineering we dene Char

ell character notation to be n where n is the ASCI I to be Int We interpret any Hask

co de for the character For instance a and n are interpreted as and

resp ectively We interpret the Haskell functions ordChar Int and chrInt Char as

the identity function id We dene a typ e of strings as in Haskell

type String Char

To illustrate how Haskell function denitions are to be interpreted in H we show in

Table some standard function denitions Each of these function denitions can be

def

interpreted as an H denition as shown in Table Let H program rec x x We

use to represent undened values as in head or tail The caseclauses in Table

use a derived syntax

def

Kx x e K x x e

n n

Indentation is used instead of symbols to separate multiple caseclauses Using the

theory develop ed later in this chapter one can check that any of the denitions in Table

when treated as an equation between programs is provable from the corresp onding H

denition in Table

H A SMALL FUNCTIONAL LANGUAGE

Expressions in Haskell maycontain lo cal denitions An expression ewherex e isto

be interpreted as the H expression x ee AHaskell listcomprehension of the form

ex e is interpreted in H as map x e e

Op erational semantics of H

The op erational semantics of H is deterministic and lazy in the sense that constructors

of algebraic typ es do not evaluate their arguments

Denition Left and right exp eriments LE and RE resp ectively are functions

from terms to terms dened by the grammar at the top of Table A left exp eriment

denes the leftmost p osition in a term where a reduction may o ccur A right exp eriment

denes a p osition in a term where a reduction may o ccur provided terms to its left are

canonical These are analogous to exp eriments in M Denition

The reduction and evaluation relations for H are the binary relations on H programs

and resp ectively dened inductively by the rules Table

Prop osition

If e and e e then e

If e and e e then e

If e e and e e then e e

If e c and e c then c c

The canonical terms are the normal forms of reduction



If e c then e c

Supp ose e e Then for any c e c implies e c



e c i e c

Pro of Similar to the pro of of Prop osition

We dene terminology for termination of evaluation

Denition Supp ose e is a program Say that e converges and write e i there is a

necessarily unique canonical program c such that e c Converselysaythate diverges

and write e i e do es not converge

a a

Recall the program dened earlier to be rec x x at each closed typ e a We have

a a

hence there is a divergent program at every closed typ e of H

Denotational semantics of H

The denotational semantics of the ob ject language H is given using M as a metalanguage

given by Pitts Classical domain The semantics is based on that of TinyML

theoretic semantics use explicit environments to mo del the binding of ob ject

variables to their values These environments are typically denoted by the metavariable

and are not to be confused with the environments denoted by here Following

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Reduction Semantics

e j e j e j case ofcc cc LE if thene else e j

n

RE j x e

iftt then e else e e

if then e else e e

e

x e e e x

c

x ec e x

recx e

recx e e x

cc K e

Rank K

caseK e e of cc cc e e e

m n m

e e e e

LE e LE e RE e RE e

Evaluation Semantics

c c

e tt e c e e c

if e then e else e c if e then e else e c

e e

e e

e

x c e x e e

e e c

c

e x e e c e x c

e e c

recx e

e x c

recx e c

e K e e cc K e e e e c

m m

Rank K

case e of cc cc c

n

Table Op erational semantics of H

H A SMALL FUNCTIONAL LANGUAGE

Pitts we can take advantage of variable binding in the metalanguage and do without such

environments by mapping ob ject variables to metavariables

We structure the denotational semantics to reect the distinction made by the op erational

semantics b etween canonical and noncanonical terms Computation is characterised op

erationally either as a series of reductions or as a single evaluation Canonical terms

whichare not reducible are the answers from computation Noncanonical terms which

are reducible represent a computation which dep ending on the ob ject language may

terminate and return an answer or may diverge or may engage in other activity such as

IO We parameterise the denotational semantics on a metalanguage typ e constructor T

Let and Val that ob ey the following typing rules with asso ciated op erations

M M T v N T

ValM T Let v M in N T

Free o ccurrences of v in N become b ound in Let v M in N Typ e T represents

computations of typ e To compute ValN simply return the answer N To compute

Let v M in N rst compute M If M returns an answer L pro ceed to compute

L

N v to obtain the answer from the whole computation In principle the benet of

this parametric approach is that the same translation rules can be used with dierent

interpretations of the structure T Val Let For instance in x wegiveaninterpretation

where computations may raise an exception instead of returning an answer

This parametric approach to denotational semantics was pioneered by Moggi

who dened a metalanguage called computational calculus into which ob ject lan

guages are translated and whose own semantics is given by a computational mo del

a category with a strong monad and other prop erties Pitts prop oses the devel

evaluation logic for reasoning ab out the denotations of ob ject programs in opment of

computational calculus Crole and Pitts investigate how to obtain the power

of general recursion in computational calculus We have adopted Moggis parametric

approach but instead of using a metalanguage with a general categorical mo del we use

M a metalanguage based on Plotkins but with a sp ecic op erational semantics and

eectively a term mo del Rather than work in a general categorical framework wework in

a pragmatic op erational framework in order to study a particular programming language

and its extensions for IO

Wenowgive a denotational semantics of H in which computations may either diverge or

return an answer to reect the op erational semantics of Table

Denition Make the following denitions of parameters T Val and Let

def

T



def

ValN LiftN

def

Let v M in N SeqM v N

Given these parameters dene denotations for the abstract syntax of H inductively ac

cording to the rules in Table

to eachtyp e anM typ e

to each dataclause dcanM typ e dc

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Typ es

def

X X

def

Bool Bo ol

def

Int Num

def

T T

def

data X dc dc X dc dc

n n

Dataclauses

def

K T T

m m

Canonical terms

def

jx ej x T e

def

j bc j

def

jK e e j Intro In he e i

m m

Rank K

Terms

def

x x

def

c Valjcj

def

if e then e else e Let v e in if v then e else e

def

e e Let v e in Let v e in

Seqv bcv x Val x

def

e e e e

def

e e Let v e in Let v e in v Valv

def

recx e Fixx e

def

case eofcc cc Let v e in

n

CaseElim v v cc v cc

n n

v v

n

Caseclauses

def

u u where m Arity K K e Splitv u u e

m m

v

Table Denotational semantics of H

H A SMALL FUNCTIONAL LANGUAGE

to each canonical term can M term jcj

to each term ean M term e

to each caseclause ccanM term cc

The b ound variables v v u used to construct M denotations are assumed to b e distinct

i i

from anyvariables o ccurring free in ob ject language terms We use a derived notation for

function application

def

M N Let f M in fN

The notation asso ciates to the left that is M M M means M M M

Finally if H environment x x then is the M environment

n n

x T x T

n n

Op erations Val and Let are simply an alternative syntax for primitives Lift and Seq of M

we use these abstract op erations so that the rules of Table can b e reused with other

interpretations of T Val and Let

The only rules to makeany assumptions ab out the typ e constructor T in the denotational

and arithmetic By translating semantics rules in Table are the ones for recursion

rec to Fix and using Seq in the translation of arithmetic we are assuming that T has the

prop erty that for any T is a lifted typ e

Prop osition

If is an H typ e then is a wellformed M typ e

If e then eT

e e

If x e and e then e x e x

e e e e

n n

Pro of Part follows by structural induction on given the restriction on the form of

algebraic typ es in H Part is by induction on the depth of inference of e Part

is by induction on the depth of inference of x e and is straightforward

since the denotational semantics is comp ositional Part follows by an induction on n

Example denotations

We rep eat the denition in H of the function head from Table

def

cc Nil

def

cc Cons x xs x

def

head xs case xs of cc cc

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

To illustrate the denotational semantics we calculate the following denotations

X T TX

Cons Nil

ValIntro Inr h Nil i

ValIntro Inr hValbc Val Intro Inl i

head Valxs case xs of cc cc

Valxs Let v xs in

Int Int

CaseElim v v cc v cc

v v



Int

cc Splitv

v

Int

cc Splitv u u x xs x u u

v



x xs xs Valx Val xs x

We will return to these examples when we lo ok at denotational equivalence in x

Op erational corresp ondence between the semantics

First we show op erational prop erties of the denotational constantsthat is the M typ e

constructor T and op erations Val Letused in the denotational semantics

Lemma

For any Tprogram M eitherM or M ValN for some program N

The op erational b ehaviour of the denotational constants ob eys the following rules

M

M ValL

L

Let v M in N

Let v M in N N v

Pro of From the denitions of Val and Let

We conclude this section on the denition of H with the pro of of a close corresp ondence

between the op erational b ehaviour of each H program and its denotation

Prop osition For any H program e canonical H program c and canonical M

program V wehave

If e c then e c

If e V then there is c suchthat V c and e c

Therefore e c i e c

Pro of Part is proved by induction on the depth of inference of e c We show two

example cases

Case e e where e c is canonical Wehavee Valjcj which is canonical so e e

e Wehave c m m where each e m By app eal to the induction Case e e

i i

hyp othesis and Prop osition wehave the following

GROUND AND CONFINED RELATIONS IN H

e Let v e in Let v e in Seqv bcv x Val x

Let v Valbm c in Let v e in Seqv bcv x Val x

Let v e in Seqbm cbcv x Val x

Let v Valbm c in Seqbm cbcv x Val x

Seqbm cbcbm cx Val x

Seqbm m cx Val x

Valbm m c

c

Part is proved by induction on the depth of inference of e V pro ceeding by a

structural analysis of e Again weshowtwo example cases

Case e is canonical Wehavee V where V e and e e

Case e e e Wehave

e Let v e in Let v e in Seqv bcv x Val x

Pro ceed by a case analysis of the evaluation behaviour of e and e From

Lemma either e or N e ValN for each i But neither e nor

i i i i

e can diverge or else by Lemma e would diverge So there are N such

i

and e Valjm j Then by a calculation that e ValN By IH wehave e m

i i i i i i

similar to the one given for part wehavethate Valjm m j and also that

e m m as required

Ground and conned relations in H

Before developing op erational and denotational equivalence we need several preliminary

denitions This section reworks basic notions of ground and conned relations develop ed

rst in x Recall the notions of program and conned term from Denition

Denition

A ground relation R is a binary relation b etween programs of the same typ e

A conned relation R is a binary relation between conned terms of the same

typ e and environment Write eRe to mean that e e R

We state a sense in which a ground relation resp ects the op erational semantics Recall

yp e equals recx x from x that program of anyt

Denition A ground relation Risop erationally adequate i for all programs

e e and canonical programs c

If e e then e Re

If e c then cRe

e i eR

e i for some canonical c cRe

b

The rst half of Table shows inference rules for constructing the conned relation R

from any conned relation R The second half of the table shows six inference rules that

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

b

Rules of R

b b

x R x R

e R e i

e Re i

i

i

i

i

b

b

e e R e e

if e then e else e R if e then e else e

x eRe e R e i

i

i

b b

x e R x e e e R e e

x eRe e Re i n

i

i

b b

rec x e R rec x e K e e R K e e

n

n

i n e R e K e eRe cc K e cc

i i i i i

i i i

b

case e of cc cc R case e of cc cc

n

n

Prop erties of Conned Relations

eRe

Weak

eRe

eRe

Stren

eRe

x e Re e

Sp ec

e e

e xRe x

eRe

Precong

CeRC e

b

e R e

Comp

eRe

x e Re e Re

Sub

e e

e xRe x

b

Table Denition of R and rules concerning conned relations

GROUND AND CONFINED RELATIONS IN H

maybevalid for a conned relation

Denition

b

If R is a conned relation then conned relation R is dened by the rules in the

rst half of Table

The inference rules Weak Stren Sp ec Precong Comp and Sub are dened in

the second half of Table

A conned relation is natural i the rules Weak Stren and Sp ec are valid

A conned relation is a precongruence i the rule Precong is valid A congru

ence is a conned relation that is b oth a precongruence and an equivalence relation

As usual the rules in Table b ear the implicit sidecondition that any sentence in a rule

is wellformed In rule Stren for instance from the lower sentence eRe we may

deduce that fv e e Dom and then from the upp er sentence eRe that

Dom Dom and hence that fv e e Dom

Prop osition

If rules Sp ec and Precong are valid for a transitive conned relation then rule

Sub is valid to o

If R is a preorder rule Comp is valid i rule Precong is valid

Pro of Similar to that of Prop osition

We will often need to induce a conned relation from a ground relation and vice versa

as follows

Denition

Let be an environment x x Then a closure is an iterated substi

n n

p p

n

tution x x where each p is a program The order of substitution

n

i i

do es not matter b ecause the variables are disjoint and each p is closed

i

The conned extension of a ground relation R is the conned relation R such

G

that

p p p

eRe i for all closures x e x R e x

G

If R is a conned relation then its ground restriction is the ground relation

fp q j pRqg

If R is a conned relation write pRq to mean that pair p q is in the ground

restriction of R

Prop osition The conned extension of a ground relation is natural that is rules

and Sp ec are valid Weak Stren

Pro of Similar to that of Prop osition

Wetypically use the same symb ol for a ground relation and its conned extension If R

G

d b

is a ground relation and R is its conned extension we sometimes write R for R G

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Op erational equivalence for H

In this section we dene a notion of applicative bisimilarity directly from the op erational

semantics of H In x we will develop a second notion of equivalence from the de

notational semantics This op erational theory of H parallels the theory of applicative

bisimilarity for M develop ed in Chapter Just as in that chapter one can also develop a

theory of contextual equivalence for H and establish an op erational extensionality result

but for the purp ose of studying functional IO applicative bisimilarity is sucient Webe

gin with applicative similarity the ground preorder from whichwe shall dene op erational

equivalence

Denition Dene function over ground relations such that

b

c with q c and c S c pS q i whenever p c there is

q q p q p

An applicative simulation is a relation S such that S S Dene ground applica

tive similarity v to be the union of all applicative simulations Dene applicative

similarity v to b e the conned extension of ground applicative similarity

We can restate the denition of as follows

Lemma For any ground relation S pS q i

whenever p then q

whenever p x e then q x e

p p

and for all programs p e x e x S

whenever p K p p then q K q q

n n

and p q S for each i

i i

Prop osition

Function is monotone

The identity ground relation is an applicative simulation

If each S is an applicative simulation then so is S S

i

Ground applicative similarity is the greatest xp oint of and is the greatest ap

plicativesimulation

M v N i there is an applicative simulation S suchthat M S N

Ground applicative similarity is a preorder

Pro of Parts and are easy to check from the denition The remaining parts

then follow from Prop osition

Denition An applicative bisimulation is a relation S such that both S and



S are applicative simulations Ground applicative bisimilarity is the ground



relation v v Applicative bisimilarity is the conned extension of ground

applicative bisimilarity

OPERATIONAL EQUIVALENCE FOR H

Clearly applicative bisimilarityis an equivalence relation We have for any programs e

and e e e i for some applicative bisimulation S eS e

Prop osition Ground relations v and are b oth op erationally adequate

Pro of Similar to the pro of of Prop osition

We adopt v and as op erational order and equivalence resp ectively on H

Applicative similarity is a precongruence

We prove that applicative similarity for H is a precongruence by a typ ed reworking of

Howes metho d just as weproved that applicative similarityfor M was a precongruence

in Chapter



Denition Dene the conned relation compatible similarity v to b e the least

set closed under the rule



c

e v e e v e



e v e



Again despite the notational ambiguity v is not dened to be the reexive transitive

closure of v

Prop osition

Applicative similarity is natural that is rules Sp ec Weak and Stren are valid





c

Conned relations v and v are reexive





c

Conned relations v and v b oth imply v

 

If e v e and e v e then e v e

  

e e

x then e x v e and e v e Sub Ifx e v e





c

If c v e then there is c such that e c and c v c

 

If e v e and e c then c v e



e v e If e v e then

Applicative similarity is a precongruence

Pro of The pro of takes exactly the same form as Prop osition We omit the details of

parts to which are almost identical to the corresp onding parts of Prop osition



c

In each case there must b e a program e such that c v e and e v e Pro ceed by

a structural analysis of canonical program c

So e Then v e implies that e as required Case c



Case c x e So e x e with x e v e Then e v e implies



that e x e with x e v e Therefore x e v e by part



c

whichimpliesc v x e as required



p p So e K q q with p v q for each i Then e v e Case c K

n n i i



implies that e K q q with q v q for each i By part we have p v q

i i

n

i i

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING



c

for each i and therefore c v K q q as required

n

By induction on the depth of inference of e c pro ceeding by analysis of e

Case e canonical Immediate



Case e e e We have e with c From e e v e there are

i i

 

e i such that e v e and e e v e By IH we have v e hence

i i

i i i

  

v e From v e by denition there are e such that v e and

i

i i

e e v e By wehave e So e e and v e e

i

i



by op erational adequacy Therefore c v e by transitivity and c v e by



e

x c From e v e there are e Case e e e Wehave e x e ande

i

 

i such that e v e and e e v e Since e x e and e v e

i

i



we have x e v e by IH By there is e with e x e and

  

e e e

e c v e x x e v e By Sub e x v e x then by IH we hav

e e

Since e e e x we have e x v e e by op erational adequacy From



e e

x wehave e e v e wehave e by transitivity Finally from c v e x v e



c v e by



c

Case e e e We have e x e e c and e x c From e v e



there are e i such that e v e and e e v e Since e x e

i

i i

  

and e v e wehavex e v e by IH Since e c and e v e wehave

 

c v e by IH By there is e with e x e and x e v e





c

By there is c with e c and c v c Therefore c v c by By Sub

 

c c c c

e x v e x then by IH we have c v e x Since e e e x

c

we have e x v e e by op erational adequacy From e e v e we have

 

c c

e x v e by transitivity Finally from c v e x wehave c v e by

K e We have e K p p for some j and Case e case e of

i i j n

 

e p p c From e v e there are e suchthat e v e and case e of

j n i

i i

 

ehaveK p p v e K e v e Since e K p p and e v e w

j n i j n

i



by IH By there are p such that e K p p andp v p By Sub wehave

j i

n

i i

 

p p so since e p p c wehave c v e p p e p p v e by

j n j n

n n

j j

v p p we have e p p e of K e IH Since case e

i

n n

j j i

by op erational adequacy By transitivity and case e of K e

i

i



wehave c v e

We omit the case for conditionals which has the same structure as the cases ab ove

restriction of compatible similarity is an applicative First we prove that the ground



simulation Supp ose for any programs e and e that e v e We are to show that whenever





c

e c there is c suchthate c and c v c Supp ose that e c By wehave c v e



c

By there is c such that e c and c v c



e v e whichisto say that Supp ose now that e v e We are to prove that

p p p

for all closures x programs e xande x are paired in an applicative simulation



p p

But by Sp ec and reexivitywe have e x v e x and since the ground restriction of

compatible similarity is an applicativesimulation we are done

A corollary of and is that applicative and compatible similarityare the same conned relation just as in Prop osition

DENOTATIONAL EQUIVALENCE FOR H

To summarise this section we havetaken op erational equivalence on H to b e applicative

bisimilarityshown it to b e an op erationally adequate congruence

Denotational equivalence for H

Two ob ject terms are denotationally equal just when their denotations in the metalanguage

are equal We take equivalence in the metalanguage to be a op erational equivalence on

M the equivalence characterised as contextual equivalence and applicative bisimilarity

in Chapter

Denition Dene denotational equivalence to b e the conned relation

def

e e e e

Since op erational equivalence on M is a natural equivalence relation so is denotational

equivalence on H Next we prove that denotational equivalence resp ects the op erational

semantics of H

Prop osition Denotational equivalence is op erationally adequate

Pro of There are four parts to the denition of op erational adequacy Denition to

establish We app eal to op erational adequacy for ground applicative bisimilarityin M

Prop osition and the corresp ondence theorem between the op erational and denota

tional semantics of H Prop osition Weleave part to the end of the pro of

Part Supp ose e c Then e c from the corresp ondence theorem So e c

from op erational adequacy of M Then e c by denition

Part We have b een using the symb ol to stand for Fixx x in M and recx x in

H Note that where is interpreted in H and M in the left and righthand

sides resp ectively

e i c e c

i c e c Corresp ondence

i e

e Op erational adequacy for M i

i e since

i e

Part

e i c e c

i c e c Corresp ondence

i c e c Op erational adequacy for M

i c e c

c In the former case e Finally part Supp ose e e Either e or for some c e

to o so e e by part In the latter case e c to o and e e c by part

Moggi denes a notion of computational mo del based on a categorical strong monad

as a general framework for reasoning with denotational semantics We make an analogous

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

denition of a computational monad in the context of M

Denition A computational monad is a structure T Val Let where T maps

any M typ e toanM typ e T and Val and Let are M op erations that ob ey the typing

rules

M T v N T M

ValM T Let v M in N T

together with the equations

M T v N T

M

Let v ValM in N N v

M T

Let v M in Valv M

M T v M T v M T

Let v Let v M in M in M

Let v M in Let v M in M

and the injectivity requirement that ValM ValN i M N

Lemma stated prop erties of the evaluation behaviour of the op erations used in the

denotational semantics as a preliminary to proving the corresp ondence theorem between

the op erational and denotational semantics for H In the following lemma weprove equa

tional prop erties of the op erations used in the denotational semantics as a preliminary to

proving the main result of this section that denotational equivalence implies op erational

equivalence

Lemma The structure T Val Let that parameterised the denotational semantics

of H in Denition is a computational monad

Pro of The typing rules in Denition followby insp ection The rst two equations

follow from the b eta and eta laws for lifting in Table Asso ciativityof Let follows from

proving the following equation

SeqSeqM v M v M SeqM v Seq M v M

It suces to prove the equation for empty from which case the nonempty case follows

LiftL for some program L In By the exhaustion lawinTable either M orM

the rst case b oth sides equal by the strictness lawinTable In the second case by

L L

the b eta law from we have lhs SeqM v v M andrhs SeqM v M v

lhs since v fv M Finally the injectivity requirement follows from canonical freeness

in Table

An example calculation

Before pro ceeding we show an example calculation Recall the denotations calculated on

page We show that head by calculating as follows where we use

DENOTATIONAL EQUIVALENCE FOR H

only the laws of M from Chapter the equational laws of a computational monad and

substitution prop erties from Prop osition

head Let v head in v

Let v Valxs case xs of cc cc in v

xs case xs of cc cc

xs Let v xs in

CaseElim v v cc v cc

v v



Let v in

CaseElim v v cc v cc

v v



Let v ValIntro Inr h Nil i in

Case Elim v v cc v cc

v v



CaseInr h Niliv cc v cc

v v



h Nili

cc v

v



Niliu u x xs xs u u Splith

x xs x Nil

Let v x xs x in v Nil

Let v Let v x xs x in v in v Nil

x xs x in v Nil Let v

Let v xs in v Nil

xs Nil

The calculation is extremely detailed but each step is trivial A more tractable way to

reason ab out H programs is to app eal to a set of laws formulated at the ob ject level

such as those we develop in x Suchlaws can b e proved correct by translation into the

metalanguage and then used for ob ject level reasoning

The other point to make ab out this calculation is that it holds for any computational

monad not just the one in Denition An imp ortant reason for parameterising the

semantics is so that program calculations that do not dep end on the detail of a particular

computational monad can b e proved once and for all

Denotational implies op erational equivalence

Weprove a soundness theorem that denotational implies op erational equivalence Com

pleteness or full abstractionwhether op erational implies denotational equivalenceis left

as an op en problem

Prop osition

b

If c c then c c

For all programs e e ife e then e e

If e e then e e

Pro of Part Wehave ValjcjValjc j and therefore by the monadic injectivity

requirement wehave jcj jc j By analysis of the typ e of c and c there are three cases to consider

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Case c c Wehave bcb c therefore as required

Case c x e c x e We have x e x e and there

fore x T e e by canonical freeness in M as required

e e c K e e Supp ose c and c are of typ e in environment Case c K

m

n

Wehave

Intro in he e i Intro in he e i

m

Rank K Rank K

n

Therefore wehave K K and m n and for each i e e by canonical

i

i

freeness in M as required

For part we prove that the ground restriction S of denotational equivalence is an

applicative bisimulation

def

S fe e j e e g

that it is an applicative simulation Supp ose Since S is symmetric we need only prove

then that e c By the corresp ondence theorem wehavee c Since e e there

is a V suchthate V and c V by op erational adequacy for M By corresp ondence

there is a canonical c such that e c and V c Wehavethatc c and hence

b

b

Hence bywehave c c whichistosay c S c as required that c c

p p p

Part We are to show for any closure x that e x e x But from

p p

e e and Sp ec we have e x e x By substitution lemma we have

p p

e x e x Then by part we are done

A theory of H programming

We state a collection of programming laws in Table In his seminal study of program

ming languages Strachey b egins his discussion of expressions and evaluation with the

denition that the characteristic feature of an expression is that it has a value What

wehave called canonical expressions corresp ond to what Strachey calls values Expressions

in H are more expressivethanStracheys in that the former include recursive expressions

and hence nontermination Nonetheless what wehave named Stracheys prop ertyinTa

ble conveys the essence of Stracheys view that every H program either equals or

some canonical program

We show in this section that all these laws hold for both op erational and denotational

equivalence except the principle of bisimulationupto which has only b een proved for

op erational equivalence We conjecture without pro of that it holds also for denotational

equivalence We b egin with op erational equivalence

Prop osition All the laws from Table hold for op erational equivalence

Stracheys prop erty

congruence

canonical freeness

b eta strictness and eta laws

coinduction

ATHEORYOFH PROGRAMMING

Pro of The pro of is similar to that of Prop osition

Weprove the same results apart from the coinduction principle for denotational equiv

alence

Prop osition All the laws from Table hold for denotational equivalence with

in place of

Stracheys prop erty

congruence

canonical freeness

b eta strictness and eta laws

Pro of By denition of evaluation any program e either diverges or evaluates to some

canonical program c Then by op erational adequacy in the rst case e and in the

second e c

b

Given e e we are to show e e Pro ceed byananalysisofwhich rule

b

in Table derived e e In each case from the rules anteceden ts we can use the

denotational semantics rules in Table to prove e e as required

Canonical freeness for denotational equivalence was proved as Prop osition

Eachlawtakes the form e e By denition we are to show for all closures

L R

L L L

x Terms are identied up to alphaconversion so we may x e x that e

R L

is distinct from variables in the listx For assume that any b ound variable in e ore

R L

L L

eachbetalawwecancheckby insp ection that e x e x For each eta lawwe

L R

can check the same prop erty but with app eal also to the laws of canonical programs in

L

x Therefore for any Table For each strictness law e we can check that e

R L

one of the strictness laws the required equation follows from op erational adequacy of M

As a simple example of coinduction here is a pro of of an equivalence of two streams

used byPaulson to illustrate Scott induction Since the two streams are unb ounded

structural induction would fail on this example

Prop osition For any f and x

iterate f fxmap f iterate f x

Denitions of iterate and map are shown in Table

Pro of Consider relation S dened as follows

S fiterate f fx map f iterate f x j f x g

We shall prove that the union S is a bisim ulationupto and hence that S

Supp ose then that e and e are arbitrary programs such that eS e Set

e iterate f f fx

e map f iterate f fx

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Stracheys Prop erty

Any program e either equals or some canonical term c

Congruence

If e b e then e e

Canonical Freeness

If c c then c b c

Beta Laws

ifT then e else e e

ifF then e else e e

e

x e e e x

c

x ec e x

case K e e of K e K e e e e

i m n m

n

i

recx e

recx ee x

Strictness Laws

LE

RE

Eta Laws

if c then e else ee

x c xc if x fv c

case cof K x x e e if no x fv e

i i

Arity K

i

Coinduction

A bisimulationupto is a ground relation S such that whenever eS e

e c either e e or there are canonical programs c c such that

b

c and c S c e

Any bisimulationupto is a subset of ground equivalence

Table Laws of equivalence in H

DERIVED LAWS OF PROGRAMMING

and we can calculate as follows

e iterate f fx

fx e

e map f iterate f x

map f x iterate f fx

fx e

Since f x f x and e S e we have f x e Sf x e Since e and e were

arbitrary wehave established that S is a bisimulationupto Hence S and the

prop osition follows

Derived laws of programming

In this section we derive further laws of program equivalence Their derivations dep end

only on the laws in Table not including coinduction so they hold for both op era

tional and denotational equivalence It is reassuring that these familiar results functional

extensionality and structural induction can b e derived simply from the theory of H

Functional extensionality

Two canonical functions are equal if whenever they are applied to the same argument they

yield the same answer

Prop osition If x c x c x then c c

Pro of We may assume that the canonical terms c and c take the forms x e and

x e We have x x e x x e x Therefore by the b eta law for

functions wehavex e e Then by precongruence c c

This principle do es not extend to noncanonical functions Counterexample let programs

f and g of typ e be and x resp ectively Then x f x g xyet f g

since f whereas g

Structural induction for sumofpro duct typ es

We can derive a familiar structural induction principle for a class of sumofpro duct typ es

Denition A sumofpro ducts typ e is an algebraic datatyp e such that

data X dc dc and each clause dc takes the form K where each

n i m i

X or is closed is either the variable

For each suchtyp e dene a program size

size Int

def

size recsize x case x of cc cc

n

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

where for each iifdc K we dene cc to b e

i m i

def

cc Kx x t t

i m m

sizex if X

def

j j

where each t

j

otherwise

Dene Finite to b e the following set of programs

def

g Finite fe j e is a program and n N size e n

Finallysaythataprogram e is nite i e Finite

Prop osition Supp ose is a sumofpro duct typ e data X dc dc If

n

is a predicate on nite programs that is Finite then the following induction

principle is valid

for each clause dc K e e K e e

i m m m m

if X

def

j

where eachset

j

fe j e g otherwise

j

Finite

Pro of Supp ose that the rule hyp othesis holds By mathematical induction on n we prove

then e This amounts to a pro of that for all programs e that if sizee n

Finite as required

Supp ose then that sizeen for some e By Stracheys prop erty we have that either

e or e equals a canonical program But size by the strictness law for case

expressions so e must be canonical So there must be a clause K such that

m

e K e e From sizeK e e n wehave

m m

sizee if X

j j

where for each j t t t n

j m

otherwise

Wecanshow that each e Either X or not If X from the equation ab ove

j j j j

we have that sizee m for some mn So byIHwe have e Otherwise

j j j

weknowthate since e K e e and hence e

j j m j j

Since each e we can conclude that e from the rules hyp othesis Hence wehave

j j

that every program e of any size is contained in So Finite and wehaveveried

the induction principle

The list typ e is a sumofpro ducts typ e in the sense of Denition Given a closed typ e

a of list elements the general denition of size given there sp ecialises to the following

a

size a Int

def

a

size recsize x case x of

Nil

Consxxs sizexs

a

The set Finite consists of those aprograms xs such that size xs n for some

n We call such a list nite Lists or are nite lists

HX AN EXTENSION OF H WITH EXCEPTIONS

recxs xs which are all distinct since Cons is lazy are not nite in this sense

Prop osition yields the following structural induction principle for nite lists

Nil xa xs Consxxs

a

Finite

a

where Finite is a predicate on lists

HX an extension of H with exceptions

We develop our theory of functional programming by considering what happ ens when

we add an exception mechanism to H Languages such as Standard ML and Mo dula

have an exception mechanism which is typically used by programmers to express error

conditions We use exceptions in Chapter to mo del the demand for function arguments

yp e during evaluation In HX a computation of typ e can either return a value of t

diverge or raise an exception For the sake of simplicitywe consider a language with just

one exception the canonical term bang Raising an exception is represented by a program

evaluating to bang which is present at every typ e For the sake of brevity we say the

program has banged Program bang bangs In general if a program needs to evaluate

several subterms b efore terminating and evaluation of any one of the subterms bangs

then the whole program bangs The only exemptions from this rule are programs of the

form e e If evaluation of e returns an answer or div erges then evaluation of the

whole program do es so to o But if evaluation of e bangs then the whole program b ehaves

the same as e The op erator called biased choice is a new primitivein HX itisa

version without parameters of exception handling mechanisms found in Standard ML and

Mo dula

Denition

The abstract syntax of HX is based on the same syntactic categories as H dened by

the H rules from Table augmented by the two new syntactic rules from Table

Dene the predicate Mute c on canonical terms to hold i for no typ e do es

c bang

The typ e assignment relation e is inductively dened bythe H rules from

Table together with the new typ e assignment rules from Table

X are the binary relations on HX pro The reduction and evaluation relations for H

grams and resp ectively dened inductively by the reduction and evaluation

rules resp ectively from Tables and The rule for callbyvalue reduction in

Table is to apply only when Mute c and the rule for callbyvalue evaluation

only when Mute c

Make the following denitions of parameters T Val and Let

def

T



def

ValN LiftInl N

def

Let v M in N Seqx u Case u v N w Lift Inr

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Syntax

c bang exception

e e e biased choice

Typ e Assignment

e e

bang

e e

Reduction Semantics

e e

bang e e

e e e e

ce c if Mute c

LE bang bang if LE bang

RE bang bang if RE bang

Evaluation Semantics

e bang e e e

e bang LE e

e e bang

LE e bang

e x e e bang e e

e e bang

e bang e c

e c Mute c

e e c

e e c

Denotational Semantics

bang T

alt T T T

def

bang LiftInr

def

alt x y Seq x u Case u v Val v wy

def

bang bang

def

e e alte e

Table HX an extension of H with exceptions

HX AN EXTENSION OF H WITH EXCEPTIONS

Given these parameters together with the p olymorphic M constants bang and alt

dened in Table inductively dene the denotational semantics of HX according

to the rules in Tables and denotational rule cValjcj from Table is to

apply only when Mute c

Prop osition

The typ e assignment laws of Prop osition remain valid for HX except for the

statements ab out canonical terms in Table

In HX wehave the following prop erties of canonical terms

Bool

g c b a If cBool then either c bang or b ftt

Int

b If cInt then either c bang or n N c n

c If c then either c bang or e c x e

d If c then either c bang or K e e c K e e

m m

The op erational semantics laws of Prop osition remain valid for HX

The denotational semantics laws of Prop osition remain valid for HX

Pro of Part follows by easy rule inductions similar to Prop osition Part

follows by insp ection of the typ e assignmen t rules Pro of of part is similar to that for

Prop osition follows by simple inductions as b efore

As in H we can prove a close corresp ondence b etween evaluation of an HX program and

its denotation

Prop osition

For any Tprogram M either M orM bang or M ValN for some program

N

The op erational b ehaviour of the denotational constants ob eys the following rules

M M

Let v M in N alt MN

M ValL M ValL

L

Let v M in N N v alt MN Val L

M bang M bang

Let v M in N bang Let v M in N N

If e c then e c

If e V then there is c suchthat V c and e c

e c i e c

Pro of Parts and followby calculating from the denitions of Val Let bang and

alt

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Part follows by induction on the depth of inference of e c as b efore Part follows

by induction on the depth of inference of e V We show one case to illustrate the pro of

Supp ose that e is e e and that e V where f g Wehave

Let v e in Let v e in Seqv bcv x Val x V

Since eache isaprogramoftyp e T Num by parts and either e m e

i i i i

Valbm c or e bang Since e converges to V by app eal to part we have only

i i

three cases to consider

a e bang and e bang V

b e Valbm c e bang and e bang V

c e Valbm c e Valbm c

and e Seqbm cbcbm cx Val x V

In each case we exhibit c such that e c and c V

a Take c to b e bang By IH wehave e bang and then e e bang

b Take c to b e bang By IH wehave e m and e bang so then e e bang

By IH we have e m and e m soe m m But c Take c to be m m

Seqbm cbcbm cx Val x m m soV m m as required

Finally part is a corollary of parts and

We now rework the theory of op erational equivalence We conjecture that denotational

equivalence could b e reworked similarlybutleave this as future work

Denition We adopt all the denitions to do with conned relations and applica

tive similarity and bisimilarity from x and x for HX with the following amendment

b

In HX given a conned relation R the conned relation R is dened bythe rules in the

rst half of Table together with the additional rules

e Re

i

i

b

b

R e e e e

bang R bang

Prop osition

All the prop erties of conned and ground relations proved in x remain true

Applicative bisimilarity is an op erationally adequate precongruence

Pro of By reworking x and x We omit the details

As b efore we take op erational equivalence to be applicative bisimilarity We nish this

section on HX byreworking the laws of programming intro duced in x

Prop osition The laws of H programming in Table hold for HX given the

following mo dications

The b eta law for callbyvalue application has a new sidecondition that Mute c

There are new b eta laws for biased choice

bang ee cec if Mute c

HX AN EXTENSION OF H WITH EXCEPTIONS

Each eta law has a new sidecondition that Mute c

There is a new eta law for biased choice

ebange

There are additional strictness laws

e

LE bangbang

RE bangbang

Pro of By reworking the pro of of Prop osition

Weleave a study of denotational equivalence for HX as future work The following result

stated without pro of would b e useful

Prop osition The structure T Val Let intro duced in Denition is a compu

tational monad

This implies for instance that the example calculation shown earlier for H is valid for HX to o

OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

Chapter

Four mechanisms for teletyp e IO

In this chapter we consider four widelyimplemented mechanisms for IO in functional

languages sideeecting IO continuationpassing IO synchronisedstream IO and

Landinstream IO In the context of interaction with a teletyp e wesketch the semantics

of sideeecting IO and give detailed semantics for the other three mechanisms These

semantics are based on the theory of H and HX develop ed in the previous chapter Tele

typ e IO is a very limited mo del of IO in which the computer interacts with a keyb oard

and a character based printer By concentrating on a simple IO mo del the contrasts

between the four IO mechanisms can b e clearly seen

W e do not describ e related work on using functional languages with more complex IO

mo dels in any detail For the record the interested reader is referred to pap ers on the

following topics asynchronous interrupts p olling the keyb oard real time

behaviour interaction with a le system concurrency

controlling a window system writing an op erating system shell in a functional

language and even the whole op erating system

Surveys by Hudak and Sundaresh Jones and Sinclair and Perry cover much

of this previous work

In x wesketchhow the denotational semantics of H can b e extended to accommo date

sideeecting IO and discuss why sideeecting IO is unsuitable for lazy languages

We base the semantics of the remaining three IO mechanisms on the language HX dened

in Chapter Continuationpassing and synchronisedstream IO can b e dened in terms

e need the exception mechanism in HX We use of H but for Landinstream IO w

exceptions to represent demand for a value in the input stream

Weuseastyle of op erational semantics for the remaining three mechanisms that was rst

used by Holmstrom in his semantics of PFL Holmstrom used a continuationpassing

style to embed CCSlike op erations for communication and concurrency in a functional

language Starting with an evaluation relation for the host language he dened the

meaning of the emb edded op erations in the style of a lab elled transition system as used

in CCS A lab elled transition system is a way to formalise the idea that an agent

such as a functional program engaged in IO can p erform an action such as input or

output of a character and then b ecome a successor agent This style of semantics is

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

attractive for at least three reasons First it can mo del a wide variety of nondeterministic

and concurrent computation witness the CCS scho ol of concurrency theory Second the

evaluation relation for the host language is unmo died any prop erty of the host language

without IO will still hold after the IO mechanism has b een added Third the metho d

complements an op erational language denition such as that of Standard ML In x

we dene notions of lab elled transition system and bisimilarity on programs of HX

We go further than Holmstrom bydeveloping an equational theory of functional IO based

on lab elled transitions We adopt bisimilarity from CCS as equivalence on programs

engaged in IO Bisimilarity is an equivalence on agents induced by their op erational

behaviour two agents are bisimilar i whenever one can p erform an action the other can

to o such that their two successors are bisimilar

Bisimilarity should not be confused with applicative bisimilarity Each is dened as the

greatest xp oint of a certain functional but they relate dierent kinds of b ehaviour Ap

plicative bisimilarity as develop ed for M in Chapter and for H and HX in Chapter

relates the evaluation b ehaviour of two programs Bisimilarity as dened in this chapter

relates the observable communication b ehaviour of two programs Bisimilaritywas intro

duced in Milners theory of CCS whereas applicative bisimilarity comes from Abramskys

theory of lazy calculus which itself followed CCS in part

Both Landinstream and synchronisedstream IO are based on stream transformers func

tionsoftyp e inp outwhereinp and out are typ es of input and output values re

sp ectively x denes some general op erations on stream transformers x gives lab elled

transition system semantics for Landinstream IO and explains why HX rather than H

is needed x and x give lab elled transition system semantics for synchronisedstream

and continuationpassing IO resp ectively The main result of the chapter is prov ed in

x that there are bisimilarity preserving translations b etween the three mechanisms for

teletyp e IO other than sideeecting IO In this sense wehaveproved that the three are

of equal expressiveness The mappings between Landinstream and continuationpassing

IO are original whilst those b etween synchronisedstream and continuationpassing IO

were discovered during the design of Haskellbuthave not hitherto b een veried

formally x concludes the chapter with a discussion of the practical use of the three

mechanisms suitable for lazy languages

Sideeecting IO

The original functional language LISP had a sideeecting IO mechanism

Sideeecting IO which is by far the most widelyused mechanism for functional IO

p ersists in LISP and is found in other eager languages suchasScheme and Standard ML

o accommo date sideeecting IO in H we add two new noncanonical op erations to the T

syntax of expressions

e read read a character

j writee write a character

SIDEEFFECTING IO

with typing rules and informal intended meanings as follows

e Int

read Int

writee

read means

input a character n from the keyb oard and return n

means write n

output character n to the printer and return unit

For example here is a program to read two characters and output their sum

main

main write read read

Pardon the cavalier identication of Int and Char from x

The denotational semantics of H can be extended to accommo date sideeecting IO

Apart from diverging there are three things a computation can do return an answer ask

for a numb er as input or output a number Accordingly a computation of H typ e can b e

mo delled as a term of M typ e T with the following denition of T and corresp onding

denitions of ValM readandwritee

def

Num X X ftv T X Num X

  

def

ValM LiftIntro In M

def

read LiftIntro In v Num Val v

def

writee Let v e in LiftIntro In hv i

This construction is based on Moggis notes Exercise but the basic idea

go es back at least as far as Plotkins Pisa notes Example in Chapter This is

just a sketch of the semantics we omit the denition of Let An analogous ob ject level

construction is detailed in Chapter Were M to be used in a detailed study of side

eecting IO it would need to be extended to include mutually recursive M typ es such

as

We do not pursue this construction here because the primary fo cus of this dissertation

as is wellknown sideeecting IO do es not is the study of IO for lazy languages and

combine well with lazy languages There are three reasons why the use of sideeecting

IO in a lazy language is problematic First to use sideeecting IO in a lazy language a

programmer must b e concerned with evaluation order which b ecause of laziness is harder

to predict than in an eager language Sideeecting IO comp els programmers to think

ab out something that otherwise they can usually leave to the implementation

The other two reasons dep end on the observation that Stracheys prop erty no longer holds

Recall Stracheys prop erty from Chapter that every program either equals or some

canonical program Stracheys prop erty holds for H and HX but not for H extended

with sideeecting op erators Programs read and writen neither diverge nor equal any

canonical program

The second reason then that sideeecting IO do es not combine well with lazy languages

is that adding sideeects makes a functional language harder to reason ab out Pro ofs of

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

programs are harder to construct b ecause there are more cases to consider than covered

by Stracheys prop erty This applies to eager languages as well With sideeecting IO

one would exp ect that every program either diverges or converges to a canonical program

or do es some IO to b ecome another program Howtoformulate precisely and verify this

exp ectation is an op en question

The third reason concerns eciency Stracheys prop erty is imp ortant if languages with

callbyname semantics for function application are to be implemented eciently Such

languages are only practical if callbyneed semantics coincides with callbyname If

so callbyname applications can be implemented by callbyneed as in graph reduction

wo semantics for an application for instance Recall the dierence b etween the t

x ee In b oth regimes the function b o dy e is evaluated Under callbyname seman

tics each time need arises for the value of x argument e is evaluated Under callbyneed

semantics the rst time need arises for the value of x argument e is evaluated but its

value is retained and reused if subsequently the value of x is needed

We give an informal pro of that if evaluation is deterministic and op erational equivalence

satises op erational adequacy and Stracheys prop erty then callbyname semantics co

incides with callbyneed Consider an application x ee If the value of e do es not

t since the argument e is never evalu dep end on x then clearly b oth regimes are equivalen

ated Otherwise supp ose that the value of x is demanded one or more times By Stracheys

prop erty the argument e either equals or some canonical form so by op erational ade

quacy e either diverges or converges to a unique canonical form c If e diverges then so

do es the evaluation of e under either regime Otherwise e deterministically converges to

c so it makes no dierence whether the second and subsequent uses of the argument use

a retained copyofc or evaluate e again

If evaluation of argument e causes sideeects then callbyneed may not b e equivalentto

callbyname and hence an imp ortant implementation technique is invalid Witness the

examples x x x read or x x xwriteA where we dene e e to

mean y e e with y fv e Under callbyname execution of the two examples

involves two inputs and two outputs resp ectively under callbyneed execution of the

programs involves just one input and one output resp ectively

In summary sideeecting IO and lazy languages do not combine well b ecause of the dif

culty of predicting the order of sideeects of constructing pro ofs of program prop erties

and b ecause callbyneed semantics ceases to b e a correct implementation technique This

explains why in practice sideeecting IO is only used with callbyvalue languages such

as LISP or ML

heys prop ertyby no means rules out pro ofs of program prop erties The absence of Strac

but weleave further investigation of H with sideeecting IO as future work We conjec

ture that the b eta eta strictness precongruence and canonical freeness laws of Table

remain valid The only work to present a theory of IO in a callbyvalue language is the

algebra for FL programs develop ed by Williams and Wimmers FL uses what is es

sentially sideeecting IO One might disp ense with callbyname semantics and develop

an equational theory for callbyneed semantics and hence accommo date sideeecting

IO Such a theory would have to make a distinction b etween the rst and subsequent us

ages of b ound variables which on the face of it is much more complicated than the theory

LABELLED TRANSITION SYSTEMS AND BISIMILARITY

of callbyname and callbyvalue applications develop ed in Chapter Previous work on

the semantics of callbyneed in deterministic functional languages has fo cused on prov

ing that an op erational semantics using callbyneed correctly implements a denotational

semantics sp ecifying callbyname

In his unpublished dissertation Redelmeier sketched an IO scheme that used side

eecting op erators together with data dep endencies to ensure a predictable order of side

eects His idea was that each sideeecting op erator to ok an additional state parameter

and returned a new state He argued that if each state value has a unique successor then

there is a predictable thread of sideeects His scheme was unsatisfactory b ecause he

did not suggest how to guarantee singlethreading Hudak and Sundaresh describ e

a variant known as systems IO Peyton Jones and Wadler have implemented

in the Glasgow Haskell compiler using sideeecting op erators with the IO mechanism

an ecient translation to C Their work is a clear advance on Redelmeiers They have

ingeniously used an abstract data typ e based on a monad to guarantee single

threading Their scheme app ears to b e a promising technique for ecient implementation

of IO in lazy languages

Lab elled transition systems and bisimilarity

Weadoptlab elled transition systems and bisimilarity from the theory of CCS to

give semantics for Landinstream continuationpassing and synchronised stream IO

In eachmechanism for functional IO there is a single ob ject language typ e whose programs

can be executed to interact with the teletyp e We call this typ e the execution typ e

A program is executable i it is of this typ e For instance executable programs using

Landinstream IO are of the stream transformer typ e CharChar We formalise

the execution of programs as a lab elled transition system

Denition The set of actions ranged over by is pro duced by the following

grammar

n input character n N

n output character n N j

A lab elled transition system is a family of binary relations indexed by actions fg

suchthatifp q then p and q are HX programs of an execution typ e

WeuseHX rather than H for reasons explained in x The intuitive meaning of transition

n

p q is that program p can input the character n from the keyb oard to b ecome program

n

q Similarlytheintuitive meaning of transition p q is that program p can output the

character n to the printer to b ecome program q If p q for some and q wesay that

p has a transition

We dene bisimilarity and prove the standard results

Denition Dene function hi to be the function over binary relations on HX pro

grams such that phS iq i

whenever p p there is q with q q and p S q

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

whenever q q there is p with p p and p S q

A bisimulation is a binary relation on agents S suchthat S hSi Bisimilarity

is the union of all bisimulations

Prop osition

Function hi is monotone

The identity relation is a bisimulation

If each S is a bisimulation then so is S S

i



If S is a bisimulation then so is S

Bisimilarity is the greatest xp ointof hi and is the greatest bisimulation

h that pS q p q i there is a bisimulation S suc

Bisimilarityisanequivalence relation

Pro of Parts follow easily from the denition The remaining parts then follow

from Prop osition

Stream transformers

Manymechanisms for functional IO or for concurrency have b een based on stream

transformers A stream is a list typ e whose cons op eration is lazy such as in H

Stream transformers in H have the general typ e

type ST inp out inp out

The idea is simple a stream transformer maps a stream of values of typ e inp into a

stream of values of typ e out This mapping represents an interactive computing device

that consumes values of typ e inp and pro duces values of typ e out Intuition if the device

has b een oered the sequence of values in in for consumption applying the stream

n

transformer to the stream in in yields a stream containing the sequence

n

of values the device can pro duce The list cons op eration has to have lazy semantics

so that the partial list in in do es not simply equal which explains why

n

streambased IO is not typically used with languages like ML or Scheme where cons is

eager Implementations of streambased IO typically represent the undened value

at the end of a partial list as a memory cell that can b e instantiated to hold the next input

tuitively correct character and to p oint to a fresh undened value Such a technique is in

but we leave op en the question of how to verify formally that it correctly implements

the semantics to be given here None of the work on verication of functional language

implementation has considered IO

Stream transformers for streambased IO havetypically b een written using explicit con

struction of the output list and explicit examination of the input list Such a

programming style can be hard to read We can avoid explicit mention of input and

output lists by using the following combinators to construct stream transformers

getST inp ST inp out ST inp out

STREAM TRANSFORMERS

putST out ST inp out ST inp out

nilST ST inp out

getST k xs case xs of xxs k x xs

putST x f xs x f xs

nilST xs

Thompson and Cupitt suggest other combinators for IO which are related to

the monadic style discussed in Chapter

A programmer would use the combinators ab ove to construct stream transformers to

give semantics to streambased IO we use combinators giveST nextST and skipST The

intention is that giveST feeds an input value to a stream transformer nextST tests whether

a stream transformer can pro duce an output value without any further input and skipST

consumes an output value from a stream transformer

data Maybe a Nothing Just a

giveST inp ST inp out ST inp out

nextST ST inp out Maybe out

skipST ST inp out ST inp out

giveST c f xs f cxs

nextST f case f of

Nothing

xxs Just x

skipST f xs tailf xs

The Haskell committee discovered the technique of using a mo ck argument to test

whether a stream transformer is ready to pro duce output Of course if the next output

alue in its input stream nextST f will from a stream transformer f dep ends on the next v

lo op

The following prop osition relates the six combinators intro duced in this section

Prop osition For all programs uinp vout kinp ST inp out

and f ST inp out

giveST u getST kku

nextSTputST v f Just v

nextSTnilST Nothing

skipSTputST v ff

Pro of Straightforward calculations

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

Landinstream IO

Landin suggested that streams would b e used to mo del inputoutput if ALGOL

included such The simplest kind of stream transformer used for IO is one that maps a

stream of input characters to a stream of output characters We call such a mechanism

Landinstream IO Executable programs are stream transformers of typ e LS

type LS ST Char Char

A second kind of streambased IO synchronisedstream IO is discussed in x We use

the combinators of x to sp ecify an intended meaning for Landinstreams

getST k means

input a character n from the keyb oard and then execute k n

putST n f means

output character n to the printer and then execute f

nilST means

terminate immediately

We wish to give a semantics for each IO mechanism in terms of the op erational semantics

of H Given a function fLS we are to compute whether f can output a character with

no further input or whether f needs an input character b efore pro ducing more output or

whether f can terminate More preciselywe need a function ready of the following typ e

data RWD out R W out D

ready ST inp out RWD out

and satisfying the equations

f W n readyputST n

readygetST k R

readynilST D

WeshowthatinH there is no such program Consider programs e and e of typ e LS

e getST x putST nilST

e putST nilST

It is not hard to see that for any xs the following equations hold

if x xs xs x xs

e xs

otherwise

e xs

and hence that e v e and e v e by Stracheys prop erty To see why there can be

ve we assume there is and derive no function ready that ob eys the equations shown ab o

a contradiction We have readyeR and readyeW and R v W But

e v e so by precongruence for v Prop osition we have readye v readye

Contradiction

John Hughes showed me this argument in

LANDINSTREAM IO

Intuitively the problem is that in H there is no way to tell whether an expression dep ends

on the value of one of its sub expressions suchasanelement of the input stream Wecan

remedy this by adding an exception mechanism to H as we didinthe previous chapter

We nd that the ab ove argument do es not hold in HX In HX we have that e and e

are incomparable b ecause ebangbang ebang and bang and are

incomparable

Roughly sp eaking to tell whether an expression dep ends on the value of one of its sub ex

pressions replace the sub expression with bang and use the handler op erator to see if

ws the whole expression bangs We can dene ready in HX as follo

ready f

case f bang of

D

x W x

R

and from the theory of HX from the previous chapter it follows that the conditions ab oveon

ready are satised Intuitively sp eaking the exception bang in HX provides a computable

test of whether a function needs the value of its argument

The semantics of Landinstreams can be given for LSprograms in HX as the lab elled

transition system dened by the following tworules

ready f Wv v n

ready f R

n

n

f giveST n f

f skipST f

The following lemma shows that this formal semantics correctly reects the informal in

tended meanings given for Landinstream programsapart from termination which we

have not formalised

Lemma

readygetST k R

readyputST v k Wv

n

getST kk n

n

putST v pp if v n

Pro of Parts and follow from the denitions of ready getST and putSTFor parts

and we can calculate the following transitions

n

getST k giveST n getST k

n

putST v p skipST putST v p

These together with Prop osition establish the required results

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

Synchronisedstream IO

In the synchronisedstream mechanism for functional IO the stream transformer pro

duces a stream of requests and consumes a stream of acknowledgements The requests and

acknowledgements are in onetoone corresp ondence the computing device sp ecied by

a stream transformer alternates between pro ducing an output request and consuming an

input acknowledgement It is the programmers burden to ensure that the value of eachre

quest do es not dep end on the corresp onding acknowledgement Synchronised streams were

rst rep orted as the underlying implementation technique for Karlssons Nebula op erating

system They were indep endently discovered by Stoye whose terminology we

adopt and ODonnell They are the underlying mechanism of the Kent Applicative

Op erating System KAOS and of IO in Haskell

with Here is the typ e SS of executable programs in the setting of teletyp e IO together

intended meanings of some example programs

type SS ST Ack Req

data Req Get Put Char

data Ack Got Char Did

putST Get getST k means

input a character n from the keyb oard and then execute k Got n

getST k means putST Put n

output character n to the printer and then execute kDid

nilST means terminate immediately

A wide range of imp erative activity can b e expressed using this mechanism as illustrated

by the Haskell IO mechanism We dene an auxiliary function for use in examining the

acknowledgement obtained from a Get request

outGot Ack Char

outGot Got x x

The semantics of synchronisedstreams can b e given for SSprograms in H or in HX as the

lab elled transition system dened by the following tworules

nextST f Just r r Put v v n

nextST f Just r r Get

n

n

f giveSTGot nskipST f

f giveST Did skipST f

Unlike Landinstreams there is no need for the problematic ready op eration b ecause of the

synchronisation b etween input and output Just as for Landinstreams we state a lemma

to show that this formal semantics correctly reects the informal intended meanings given

for synchronisedstream programsapart from termination

Lemma Supp ose kChar SS and hSS are programs dene programs f and g to

be

def

f putST Get getST ack k outGot ack

def

g putST Put vgetST ack h

CONTINUATIONPASSING IO

Then wehave

nextST f Just Get

nextST g JustPut v

n

fk n

n

gh if v n

Pro of Parts and follow from the denitions of nextST putST and getSTFor parts

and we can calculate the following transitions and equations using Prop osition

n

f giveSTGot nskipST f

getST ack k outGot ack giveSTGot n

k outGot Got n

k n

n

g giveST Did skipST g

giveST Did getST ack h

h

Continuationpassing IO

In continuationpassing IO the executable typ e is an algebraic typ e with a constructor

corresp onding to each kind of expressible imp erative activity In the case of teletyp e IO

wehave

data CPS INPUT Char CPS

OUTPUT Char CPS

DONE

Holmstroms PFL was the rst functional language to take the continuationpassing

mechanism as primitive In earlier work Karlsson programmed continuationpassing op

erations on top of a synchronisedstream mechanism A similar datatyp e was used by

Plotkin in the Pisa notes as semantics for sideeecting IO as discussed in x The

mechanism is called continuationpassing b ecause of the similaritybetween the argument

to INPUT and continuations as used in denotational semantics

The intended meaning of CPSprograms is easily given

INPUT k means

input a character n from the keyb oard and then execute k n

p means OUTPUT n

output character n to the printer and then execute p

DONE means

terminate immediately

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

Landinstream IO

ready f R ready f Wv v n

n n

f giveST n f f skipST f

Synchronisedstream IO

nextST f Just r r Put v v n nextST f Just r r Get

n n

f giveSTGot nskipST f f giveST Did skipST f

Continuationpassing IO

p OUTPUT v q v n p INPUT k

n n

p k n p q

Table Three mechanisms for functional IO

These intended meanings are reected in the following two rules which dene a lab elled

transition system for CPSprograms in either H or HX

p OUTPUT v q v n

p INPUT k

n

n

p k n

p q

Maps between three of the mechanisms

Wegave detailed semantics for each of the mechanisms apart from sideeecting IO There

are two main results in this section First we show that if two executable programs are

op erationally equivalent then they are bisimilar The force of this result is that the theory

of op erational equivalence from Chapter can b e used to prove prop erties of the execution

behaviour of executable programs Second we show that each of the three mechanisms

has equivalent expressivepower in the following sense If p is an executable program with

resp ect to one mechanism then for each other mechanism there is a function f suchthat

p are fp is an executable program with resp ect to the other mechanism and p and f

bisimilar

We work in HX so that all three mechanisms are supp orted in the same language as

discussed in x the semantics of Landinstreams based on the ready function cannot b e

programming in H

Denition The teletyp e transition system is a family of binary relations on HX

programs indexed by actions fg and is dened by the rules in Table

Lemma If p and p q then q Typ e is one of LS SS or CPS dep ending

on whether transition p q was derived from one of the Landinstream synchronised

stream or continuationpassing rules resp ectivelyinTable

Pro of By insp ection

Given its simple sequential nature one would exp ect the semantics of teletyp e IO to b e

MAPS BETWEEN THREE OF THE MECHANISMS

determinate The following result makes this precise

Prop osition For any program p p p and p p implies p p

Pro of By insp ection of each of the inference rules

Given this determinacy bisimilarity can alternatively b e characterised in terms of traces

If s is a nite sequence of actions say that s is a trace of program p i there

n

n 

p Two programs are trace equivalent i are programs p with p p

n i

they have the same set of traces

In a nondeterministic calculus like CCS trace equivalence do es not in general imply bisim

ilarity Given the determinacy result ab ove however it is not hard to show that the two

equivalences coincide We omit the pro of but see Milners b o ok for a more general result

Chapter

Op erational equivalence applicative bisimilarity implies bisimilarity

Prop osition If p and q are programs of the same typ e then p q implies p q

Pro of Recall that wetake on terms of HX to b e applicative bisimilarity as dened in

Chapter It suces to show that ground applicative bisimilarityon HX is a bisimulation

Supp ose that p q for programs p and q We pro ceed by a case analysis of If

is not one of the three typ es CPS SS or LS then p and q have no transitions so p q

trivially Otherwise there are three cases to consider In each case we establish condition

of the denition of bisimulation that whenever p p there is q suchthat q q

and p q Condition follows by a symmetric argument

Case CPS

n

Then q INPUT k with k k from If p p then p INPUT k and p k n

n

applicative bisimilarity So q k n p

n

If p p then p OUTPUT v p and v n Then q OUTPUT v q withv v and

n

p q So v n and wehave q q p

Case SS

n

If p p then nextST p Just r r Get and p giveSTGot nskipST p

n

Since p q we have nextST q Just r with r r so r Get We have q

skipST q and the latter equals p since op erational equivalence is a giveSTGot n

congruence

n

If p p then nextST p Just r r Put v v n and p giveST Did skipST p

Again since p q wehave nextST q Just r with r r sor Put v with v v

n

Therefore q giveST Did skipST q which equals p so v n

The case for LS follows by a similar argument and is omitted

On the other hand bisimilarity do es not imply op erational equivalence of H programs

Prop osition There are program pairs p and q in each of the typ es CPS LS and

SS suchthat p q but not p q

Pro of Witness program pair OUTPUT Done and in typ e CPS and pair putST nilST

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

and in eachofthetyp es SS and LS

Intuitively the pro of dep ends on op erational equivalence distinguishing more junk

programs than bisimilarity Given a richer IO mo del there would be more signi

cant distinctions Supp ose we extended the CPS algebraic typ e with a new constructor

ParCPS CPS CPSwithintended meaning that Par p q is to b e the parallel execution

of programs p and q asinPFLThenifp q programs Par p q and Par q p would b e op

erationally inequivalent b ecause Par is the constructor of an algebraic typ e but bisimilar

b ecause as in CCS b oth lead to the parallel execution of p and q

Before proving the second main result of this section we imp ort the pro of technique of

bisimulationupto from CCS

Denition A binary relation on programs S is a bisimulationupto i pS q

implies

whenever p p there is q with q q and p S q

whenever q q there is p with p p and p S q

Prop osition If S is a bisimulationupto then S

Pro of Wefollow the pro of on page of Milners b o ok

Weprove that S is a bisimulation from which it follows by reexivityof that S

S is the relational comp osition of S and Supp ose that pS q that is for

some p and q wehave p p S q q Supp ose that p p we are to exhibit q such

that q q and p S q We make a sequence of deductions there is p such that

p p with p p there is q such that q q and p S q and there is q such

that q q and q q Altogether we have p p S q q hence p S q by

transitivityof This shows that S is a bisimulation

Supp ose there are programs p and q suchthatwe wish to prove p q Prop osition

says that one pro of would be to nd a bisimulation containing the pair p q On the

other hand Prop osition says that another pro of is to nd a bisimulationupto that

contains the pair It is often simpler to do the latter The same idea is used extensively

in the theory of CCS and calculus

We show in Table functions sscps and cpsss to map between the typ es SS and

CPS and functions lscps and cpsls to map b etween the typ es LS and CPSFunctions

sscps and cpsss are similar to translations discovered by the Haskell committee

The following is the main theorem of the chapter that the three IO mechanisms of

Table are isomorphic in a certain sense

Theorem

For any SSprogram f f sscps f

For any CPSprogram p p cpsss p

For any LSprogram f f lscps f

For any CPSprogram p p cpsls p

MAPS BETWEEN THREE OF THE MECHANISMS

Translations between SS and CPS

sscps SS CPS

cpsss CPS SS

sscps f

case nextST f of

Nothing DONE

Just r case r of

Get INPUT v sscps giveST Got v skipST f

Put v OUTPUT v sscps giveST Did skipST f

cpsss p

case p of

INPUT k putST Get getST ack cpsss k outGot ack

OUTPUT v q putST Put v getST ack cpsss q

DONE nilST

outGot Ack Char

outGot Got v v

Translations between LS and CPS

lscps LS CPS

cpsls CPS LS

lscps f

case ready f of

R INPUT v lscps giveST v f

W v OUTPUT v lscps skipST f

D DONE

cpsls p

case p of

INPUT k getST v cpsls k v

OUTPUT v q putST v cpsls q

DONE nilST

Table Translations in HX between three styles of IO

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

Pro of Weprove that S given by

def

S ff sscps f j f is an SSprogramg

is a bisimulation which is to say that S hS i Let f be any SSprogram and we have

that sscps f CPS Hence the synchronisedstream rules apply to f and the continuation

passing rules to sscps f We are to show that f sscps f hS i We pro ceed by

analysis of the evaluation b ehaviour of nextST f There are ve cases to consider

i nextST f or nextST f bang

ii nextST f Nothing

iii nextST f Just r and either r or r bang

iv nextST f Just r and r Get

v nextST f Just r and r Put v

Here are the p ossible transitions from programs f and sscps f

iiii There are no transitions from either sscps f or f

iv The only transitions of f are of the form

n

f giveST Got n skipST fforany n

Wehave sscps f INPUTc sscpsgiveST Got cskipST f

So the only transitions of sscps f are of the form

n

sscps f sscpsgiveST Got nskipST f for any n

n

when f giveST Did skipST f v There is no transition from f unless v n

Wehave sscps f OUTPUT v sscpsgiveST Did skipST f

So there is no transition from sscps f unless v n

n

when sscps f sscpsgiveST Did skipST f

In each case the conditions for f sscps f hSi are satised

It suces to show that S given by

def

S fp cpsss p j p is a CPSprogram g

is a bisimulationupto Supp ose that pair p cpsss p is in S We have p CPS and

cpsss pSS so the continuationpassing rules apply to p and the synchronisedstream

p then cpsss p q and rules to cpsss p We are to show that whenever p

p S q and vice versa We pro ceed by analysis of the evaluation b ehaviour of p There

are ve cases to consider

i p

ii p bang

iii p DONE

iv p INPUT k

v p OUTPUT v q

Here are the p ossible transitions from programs p and cpsss p

iiii Neither program has any transitions

MAPS BETWEEN THREE OF THE MECHANISMS

n

iv The only transitions of p are of the form p k n for any n

We have cpsss p putST Get getSTack cpsssk outGot ack So

using Lemma the only transitions of cpsss p are of the form

n

cpsss pcpsssk n for any n

n

q when v n v The only transitions of p are p

Wehave cpsss p putST Put vgetSTack cpsss q

n

So using Lemma the only transition from cpsss p is cpsss p cpsss q

In each case the conditions for bisimulationupto are satised since

Weprove that S given by

def

S ff lscps f j f is an LSprogramg

is a bisimulation that is S hS i Supp ose that pair f lscps fininS Wehave f LS

and lscps fCPS so the Landinstream rules apply to f and the continuationpassing

rules to lscps f We are to show that f lscps f hSi We pro ceed by analysis of

There are four cases to consider the evaluation b ehaviour of ready f

i ready f

ii ready f D

iii ready f R

iv ready f Wv

A fth p ossibilitythat ready f bang is imp ossible given the form of ready Here are

the p ossible transitions from programs f and lscps f

iii No transitions are p ossible from either f or lscps f

n

f for any n iii The only transitions of f are of the form f giveST n

Wehave lscps f INPUTc lscpsgiveST c f

So the only transitions of lscps f are of the form

n

lscps f lscpsgiveST n fforany n

n

when f skipST f iv There is no transition from f unless v n

Wehave lscps f OUTPUT v lscpsskipST f

So there is no transition from lscps f unless v n

n

lscpsskipST f when lscps f

In each case the conditions for f lscps f hSi are satised

It suces to show that S given by

def

S fp cpsls p j p is a CPSprogram g

is a bisimulationupto Supp ose that pair p cpsls p is in S We have p CPS and

cpsls p LS so the continuationpassing rules apply to p and the Landinstream rules to

cpsls p We are to show that whenever p p then cpsls p q and p S q

and vice versa We pro ceed by analysis of the evaluation b ehaviour of p There are ve

cases to consider

i p

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

ii p bang

iii p DONE

iv p INPUT k

v p WRITE v q

Here are the p ossible transitions from programs p and cpsls p

iiii Neither of the programs has any transitions

n

iv The only transitions of p are of the form p k n for any n

Wehave cpsls p getSTc cpslskc

So using Lemma the only transitions of cpsls p are of the form

n

cpsls pcpslsk n for any n

n

v The only transition of p is If p q if v n

Wehave cpsls p putST v cpsls q

So using Lemma the only transition of cpsls p has the form

n

cpsls pcpsls q if v n

In each case the conditions for bisimulationupto are satised

Discussion

The main contribution of this chapter is to the semantics of functional IO Wesketched

a semantics of sideeecting IO but gave three ob jections to its use with lazy languages

We considered three mechanisms suitable for lazy languages and gave an op erational

semantics for each We showed how the notion of bisimilarity from CCS is a suitable

equivalence on programs engaged in IO We gave translations b etween the three styles

some of which are well known but gave the rst formal pro ofs that the translations are

correct

Our denition of bisimilarityisvery simple but for two reasons one might wish to develop

although each of the three IO mechanisms has a notion of program it further First

termination wehave not mo delled termination in the lab elled transition system Hence a

program that immediately terminates is bisimilar to one that diverges Second we have

assumed that teletyp e input is observable Consider two Landinstream programs f and

g

f xs

g xs case xs of

xs g xs

H

Given an input stream g unravels it forever whereas f lo ops immediately Wehave f g

HX

but f g and f g b ecause g forever inputs characters whereas f diverges One

might argue that they have indistinguishable behaviour b ecause neither ever pro duces

output On the other hand it seems reasonable to distinguish them on the ground that

teletyp e input is observable to the op erating system if not always to the end user These

DISCUSSION

points bring out the simplicity of our mo del but then it is meant to be simple so as to

emphasise the dierences b etween the IO mechanisms

We conclude the chapter with some remarks ab out the practical use of the three mech

anisms for IO in lazy languages As they stand the three mechanisms are rather to o

lowlevel for largescale programming As we discussed in Chapter various sets of com

binators have b een put forward as highlevel programming mo dels Wewill

investigate a set of combinators in the monadic style in Chapter

Landinstream IO is go o d for teletyp e IO but do es not scale to a practical IO scheme

construct elegant parsers that such as Haskell IO Several pap ers have shown how to

act on a lazy input stream of characters It is not clear that such parsing

techniques can be based so simply on synchronisedstream or continuationpassing IO

Landinstream programs can be written in either Haskell or Hop eC This is

b ecause the IO mechanisms of both provide op erations to obtain a lazy input stream

and hence Landinstream IO can b e simulated

A gulf separates the sourcelevel semantics of the streambased IO mechanisms given

here from their imp erative implementation The gulf is particularly wide b etween

of lazy input streams The only other semantics of the semantics and implementation

streambased IO is Thompsons tracebased work which is domaintheoretic and also

distant from practical implementations In contrast the op erational semantics we gave

for continuationpassing IO corresp onds fairly closely to an interpretive implementation

It is an op en question how to relate abstract sp ecications of IO to ecient

implementations using sideeects

The principal merit of synchronisedstream IO over continuationpassing IO is that the

former can eciently simulate the latter via a function such as cpsss Simulation of

the former by the latter using a function such as sscps is inevitably inecient

Synchronisedstream programs suer from problems relating to synchronisation b etween

input and output streams that do not arise with continuationpassing Highlevel

combinators such as for monadic IO can be implemented on top of either mechanism

If all user programs are to use suchcombinators there seems to b e no reason to

cho ose synchronisedstream IO as the underlying mechanism rather than continuation passing IO

CHAPTER FOUR MECHANISMS FOR TELETYPE IO

Chapter

Monadic IO

The three IO mechanisms of Chapter suitable for lazy languages express IO at a low

level This chapter explains how to program IO using higherlevel combinators in a style

which we call here monadic IO The combinators are higherlevel in two senses the

intended meaning of an executable program is a computation that p erforms IO and then

returns a value rather then just p erforming IO as in Chapter the combinators of

monadic IO can b e implemented using the lowerlevel op erations given in Chapter

This chapter discusses monadic IO in the context of a sp ecic application of functional

programming Over the last decade the Medical Research Council MRC at the Western

General Hospital in Edinburgh has built a prototyp e machine to assist the analysis of

cervical smears The prototyp e w as implemented using dedicated hardware C and

assemblyco de A team has started work on reimplementing the machine using a commer

cial workstation coupled with software partly written in a lazy functional language

The aim is to compare the gains of using a functional language such as reliabilityeaseof

programming and the p otential for pro ofs of program prop erties with the losses suchas

computational sp eed The system captures digitised images from a microscop e that sights

amovable stage holding a microscop e slide The intention is that the system will b e able

to lo cate automatically the ob jects on the slide that are most likely to be precancerous

cells and then display these to a cytologist for diagnosis If successful the computerised

system will allow a cytologist to pro cess slides faster than by simply using a microscop e

The MRC team have been prototyping the software for their system using a mixture of

C and a lazy functional language the prototyp e uses Gofer and the nal implementation

will use Haskell Basic image pro cessing will b e done byaserver running mature C co de

while the toplevel main program will b e new software written in the functional language

ts of the MRC computer This chapter addresses two questions How can the IO requiremen

b e programmed in a lazy functional language How can prop erties of programs engaged

in IO b e proved In the context of a simplied mo del of the MRC apparatus we suggest

howtheMRC computer could b e programmed in a monadic style We consider a simple

IO transformation suggested by Ian Poole of the MRC and prove it correct We show

how the monadic style applies to teletyp e IO because the simplied IO mo del for the

MRC computer includes teletyp e IO

x is a tutorial on the monadic style of functional IO It denes a simplied mo del of

CHAPTER MONADIC IO

MRC IO and p oses a verication problem x and x dene monadic typ es in H to

mo del teletyp e IO and the MRC IO mo del resp ectively the latter typ e is dened in

terms of the former Finallyinx weverify the motivating example

All the programs given in this chapter are written in Haskell notation They are to be

understo o d as H not HX programs as discussed in x All the programs have b een

typ echecked and executed in Mark Jones Gofer system Program equivalence written

is op erational equivalence as dened in Chapter

Programming IO in a monadic style

In the monadic style of functional IO there is a unary typ e constructor IO suchthat a

program closed term of typ e IO a is intended to mean

a computation which may p erform IO and may return an answer of typ e

a

Let a computation be a program of typ e IO a for some a To program teletyp e IO

as discussed in Chapter in a monadic style one might use the following op erations for

constructing computations

input IO Char

output Char IO

return a IO a

IO a a IO b IO b

Here is the intended meaning of computations constructed from these op erations

input means input a character v from the keyb oard and return v

output v means output character v to the printer and return

return v means immediately return v

p f means execute computation p call the answer x and then execute compu

tation fx

An example reading a line from the keyb oard

Let us consider the problem of writing a program to read from the keyb oard a string that

is a list of characters terminated by the newline character n We wish to derive a

function gettingLine with the following prop erties

gettingLine String IO String

gettingLine vs means having already read the string vs continue reading char

acters un til a newline app ears

We can dene gettingLine in terms of the basic computation constructors

gettingLine vs

input v

PROGRAMMING IO IN A MONADIC STYLE

if v n then return vs else gettingLine vvs

We can read o an interpretation of this denition from the intended meaning of the basic

constructors computation gettingLine vs means input a character call it v then if v

is a newline return the accumulated string vs or else add v to the accumulated string

and rep eat Here then is a solution to the problem

getLine IO String

getLine gettingLine return reverse

Function reverse a a is the list reversal function from the Haskell prelude The

string returned by gettingLine needs to b e reversed b ecause the characters are accu

mulated in reverse order to avoid rep eated list concatenations

Derived op erations

Sometimes two computations are to be comp osed in sequence but the answer from the

rst is to b e discarded This pattern can b e captured by the combinator

IO a IO b IO b

p q p const

where const is the Haskell version of the K combinator shown in Table Commonly

the rst argumentto is a task a program of typ e IO A task is a computation that

returns no informativeanswer up on termination The simplest task is skipgiven by

skip return

Sequential comp osition of a list of tasks is achieved bythesequence combinator

sequence IO IO

sequence foldr skip

The Haskell list combinator foldr is also shown in Table It is not hard to verify the

following equations

sequence skip

sequence p ps psequence ps

Here is a combinator that lifts a curried function f of two arguments to one that given

two computations runs them in sequence to obtain twoanswers x and y and then returns

the answer fxy

lift a b c IO a IO b IO c

lift f p q p x q y return f x y

write a generalisation of sequence that runs a list of computations We can use lift to

in sequence and then returns the list of their answers as its own answer

accumulate IO a IO a

accumulate foldr lift return

CHAPTER MONADIC IO

More examples printing a line and prompting

To illustrate how sequence can b e used here is a program to output a string of characters

followed by newline to the printer

putLine String IO

putLine vs sequence output v v vs output n

This is an example of a Haskell list comprehension which can be translated into H as

discussed in x In general we call functions such as putLine that have some typ e

a a IO b for m parametric computations

m

Finally here is a parametric computation to print a prompt and then read a line of input

String IO String askFor

askFor xs putLine xs getLine

Adding exceptions to the programming mo del

Exp erience of programming IO in imp erative languages suggests that an exception han

dling mechanism is a go o d way to represent and pro cess unexp ected events suchaserrors

For instance the implementors of DEC SRCs Taos op erating system argue that repre

senting op erating system errors by exceptions is b etter than returning an error co de as

in ATTs Unix for instance b ecause it is imp ossible for an exception to b e ignored

Exceptions can b e incorp orated into the monadic programming mo del bycho osing a typ e

Exn of exceptions and extending the intended meaning of a program of typ e IO a to b e

a computation which may p erform IO actions and may return an answer

of typ e aormay fail with an exception value of typ e Exn

For the purp ose of this chapter we take Exn to be String but in general it can be any

typ e There are two new primitiveways of constructing computations

raise Exn IO a

try IO a Exn IO a IO a

raise exn means immediately raise the exception exn

try p f means execute computation p if an answer v is returned then return v

and ignore f otherwise if p raises an exception exnexecutef exn

The intended meanings of the other primitive computation constructors are unchanged

except for sequen tial comp osition

p f means execute computation p if an exception exn is raised then immedi

ately raise exn and ignore f otherwise if an answer x is returned pro ceed to execute

computation fx

The exception mechanism in HX was obtained by extending the syntax and semantics of

H Here instead we obtain an exception mechanism byobject level programming with

no extension of H

PROGRAMMING IO IN A MONADIC STYLE

Biased choice is a derived form of exception handling where the exception value is

ignored

pq means execute computation pifananswer x is returned then return x and

ignore q otherwise if p raises an exception execute q

IO a IO a IO a

p q try p const q

An example using exceptions

Here is a parametric computation that if given a numerala list of digitsreturns the

corresp onding numb er Otherwise it raises the exception parseInt

parseInt String IO Int

parseInt cs

toDigit cs accumulate map

return foldr reverse

where

Int Int Int

toDigit Char IO Int

u t t u

toDigit v

if isDigit v then return ord v ord

else raise parseInt

This function can b e used to construct a computation that returns the number found by

reading a line from the keyb oard and treating it as a numeral and rep eating if necessary

until a legitimate numeral has b een typ ed

getInt IO Int

getInt

getLine vs parseInt vs

putLine Oops That wasnt a number getInt

A Simplied MRC Computer

We suggest a simplied version of a programming mo del suitable for the Edin burgh MRC

team We wish to program a computer equipp ed with a teletyp e interface but also an

external microscop e assembly consisting of a microscop e an image capture device and

a movable stage containing a microscop e slide Supp ose there is a typ e Image whose

values represent digitised images obtained from the external assembly Dene a typ e of

twodimensional co ordinates to represent p ositions on the slide

type Coord IntInt

We assume that the stage can be moved to a certain range of co ordinates and also to a

val of the slide distinguished parked p osition for insertion and remo

CHAPTER MONADIC IO

The functional program is to run on the main computer but we assume there is an in

dep endent IO controller resp onsible for stage motion We prop ose to use the teletyp e

programming mo del with exceptions extended with the following computation construc

tors

moveStage Coord IO

capture IO Image

parkStage IO

moveStage co means instruct the IO controller to move the stage to co ordinate

p osition co and immediately return

capture means wait for the IO controller to signal that the stage is stationary

capture an image im and return answer im but if the stage is parked raise the

exception capture

parkStage means instruct the IO controller to park the stage and immediately

return

This mo del motivated the development of this chapter The MRC team use an IO mo del

based on this simplied mo del but considerably extended

Here is a verication problem suggested by Ian Poole of the MRC We b egin with a

parametric computation called jobA Its parameters are a list of co ordinates cos and

an image analysis function f of typ e Image Char where for any image im character

fim represents some humanreadable result computed ab out im The purp ose of jobA is

to capture images from each of the co ordinates in cos apply the analysis function to each

of the images and output the results

jobA Image Char Coord IO

analyse Image Char IO

jobA f cos sequence moveStage co analyse f co cos

analyse f capture im output f im

Although the main computer together with the IO controller p ermit a limited amountof

concurrency one sees that the program ab ove do es not admit any concurrent activity In

pro cessing each co ordinate the main computer tells the IO controller to move the stage

but then immediately tries to capture an image and so undergo es a p erio d of idle waiting

Similarly the IO controller remains idle while the main computer is pro cessing the image

captured at each co ordinate

Clearly the program would run faster if immediately after capturing an image at one

ve the stage to the next co ordinate during the co ordinate the IO controller could mo

same time as the main computer pro cesses the current image Program jobB is intended

to do this

jobB f skip

jobB f cocos moveStage co jobC f cos

jobC f cos sequence each f co co cos analyse f

each f co capture im moveStage co output f im

PROGRAMMING IO IN A MONADIC STYLE

The problem is to verify that parametric computations jobA and jobB are equivalent The

remaining sections of the chapter develop a theory of the simplied MRC programming

mo del by mo delling its semantics within H We conclude in x by proving that jobA

and jobB give rise to equal computations

History

The rst serious use of the monadic style was in the Kent Applicative Op erating System

KAOS lines of Miranda written by John Cupitt KAOS has a typ e

constructor interacta typ e sysErr and the following op erations in Haskell notation

return a interact a

comp a interact b interact a interact b

raise sysErr interact a

catch sysErr interact a interact a interact a

Cupitts interact and sysErr corresp ond to IO and Exn resp ectively and his four op er

ations corresp ond to return raise and try resp ectively Cupitt based his combina

tors on a more complex set intro duced by Thompson for programming teletyp e IO

Cupitts combinators were programmed on top of a primitivesynchronisedstream mecha

nism He found that these combinators hid manylowlevel details in his implementation

Indep endently of Cupitt the author prop osed an extension of Holmstroms PFL called

PFL whic hwas based on a continuationpassing mechanism and prop osed the use

of a typ e constructor and op erations corresp onding to IO return and called Beh

Ret and resp ectively These highlevel op erations were dened in terms of the low

level continuationpassing mechanism Unlike Cupitts scheme PFL was never fully

implemented This dissertation grew out of the eort to make sense of the intuitive ideas

develop ed in PFL

This style of IO has come to b e called monadic b ecause the structure IO return

can b e mo delled categorically as a strong monad As discussed in Chapter Moggi

advo cated use of such structures to parameterise denotational descriptions of program

ming languages Inspired by Moggi Wadler has advo cated use of monads in the

functional language itself rather than the denotational metalanguage to express imp er

ative activity such as teletyp e IO or interaction with a mutable store The op erations

return and used here corresp ond to Wadlers op erations unit and bind resp ectively

A monadic approach to IO is b eing develop ed in the Glasgow Haskell compiler

Although many lines of co de in this style have been written and it has b een realised

that the combinators should ob ey the monadic laws there has been no previous work

on reasoning ab out programs engaged in monadic IO The remainder of this chapter

develops a theory of monadic IO

CHAPTER MONADIC IO

data TT a Read Char TT a

Write Char TT a

ReturnTT a

thenTT TT a a TT b TT b

p thenTT f

case p of

Read g Read v g v thenTT f

Write v q Write v q thenTT f

ReturnTT x f x

Table Aweak monad for teletyp e IO

A weak monad for teletyp e IO

Our theory of the MRC programming mo del is based on mo delling the semantics of compu

tations as functional programs within H This mo del admits pro ofs of program prop erties

but it is not b eing prop osed as the basis of an ecient implementation Peyton Jones and

Wadler discuss implementation techniques for monadic IO

We b egin by dening what we mean by a monad in the context of H

Denition For any structure M returnM thenM where M is a typ e constructor and

programs returnM and thenM havetyp es

returnM M

thenM M M M

dene three prop erties as follows

Mb eta returnM x thenM f fx for all suitably typ ed f and x

Masso c p thenM f thenM g p thenM x fx thenM g

for all suitably typ ed p f and g

Meta p thenM returnM p for all suitably typ ed p

A weak monad is such a structure M satisfying Mb eta and Masso c a monad is a

weak monad M satisfying Meta

Perhaps surprisingly the weak monad laws suce for the purp oses of this chapter In

fact the full eta law fails for the twoweak monads we consider We b egin with a weak

monad TTgiven in Table whichmodelsteletyp e IO It is a version of the constructor

mo del sideeecting IO Roughly sp eaking any program of typ e T prop osed in x to

TT a consists of a string of Reads and Writes terminated bya ReturnTT

Lemma Structure TT ReturnTT thenTTisaweak monad

Pro of TTb eta follows immediately by denition of thenTT For TTasso c it suces

A WEAK MONAD FOR TELETYPE IO

to show that relation S is a bisimulationupto where S is dened as follows

def

S fp thenTT f thenTT g p thenTT x fx thenTT g

j p TT a f aTTb gbTTcg

def

c

S S fc c j c S c g

To show that S is a bisimulationupto it suces to consider any pair e e in S

e or that there are canonical programs c and c such and show that either e

b

that e c for each iandthatc S c Accordingly let e pthenTTf thenTT g

i i

and e p thenTT x f x thenTT g By Stracheys prop erty one of the following

cases holds

p

p returnTT x

p Write v q or

p Read h

In each case we can establish the required conditions

e e

e ReturnTT x thenTT f thenTT g f x thenTT g

e ReturnTT x thenTT x f x thenTT g f x thenTT g

So e e By Stracheys prop erty either e e and we are done or there

b

b

is a canonical program c with e e c In the latter case wehave c c so c S c

as required

In this case wehave

e Write v q thenTT f thenTT g

Write v qthenTTf thenTT g

Write v qthenTTf thenTT g

e Write v q thenTT x f x thenTT g

Write v q thenTT x f x thenTT g

Dene programs e and c to b e

i

i

q thenTT f thenTT g e

e q thenTT v fv thenTT g

c Write v e

c Write v e

b

S c as required Wehave e c and since v v and e S e wehave c

i i

In this nal case case when p Read h either h equals or some canonical function

CHAPTER MONADIC IO

v e If h wehave

e Read thenTT f thenTT g

Readv vthenTTf thenTT g

Readv thenTT g

Readv thenTT g

Readv

e Read thenTT v fv thenTT g

Readv v thenTT v fv thenTT g

Readv

Since e e Readv and we are done Otherwise h v e and

ehave w

e Read v e thenTT f thenTT g

Readv e thenTT f thenTT g

Readv e thenTT f thenTT g

e Readv e thenTT v fv thenTT g

Readv e thenTT v fv thenTT g

Dene programs c and c to b e

i

i

c v e thenTT f thenTT g

c v e thenTT v fv thenTT g

c Read e

c Read e

c b

Wehave c S c so c S c But then e c for each i and c S c so the pro of for

i i

Read is complete

The TTeta lawisinvalid Here is a counterexample

Read thenTT ReturnTT Readv v thenTT ReturnTT

Readv

Read

If we had a theory of H which satised v theintention of the Haskell designers

we could in fact establish TTeta For the purp oses of this chapter the lackofTTeta is

unimp ortant all we need are the weak monad laws TTb eta and TTasso c

A weak monad for the MRC computer

The MRC programming mo del has three comp onents teletyp e IO interaction with the

external microscop e assembly and exception handling We mo del the state of the external

microscop e assembly as a pair consisting of the current p osition either parked or at a

particular co ordinate and a function representing the image that could b e captured from

any of the co ordinate p ositions

type State Maybe Coord Coord Image

data Maybe a Nothing Just a

A WEAK MONAD FOR THE MRC COMPUTER

either a c b c Either a b c

either f g ab case ab of

Left a f a

Right b g b

get IO State

put State IO

get s ReturnTT Left s s

put s s ReturnTT Left s

return x s ReturnTT Left x s

raise x s ReturnTT Right x s

raise p f s p s thenTT uncurry either f

try p f s p s thenTT uncurry either return f

output v s Write v ReturnTT Left s

input s Read v ReturnTT Left v s

parkStage get s put Nothing snd s

moveStage co get s put Just co snd s

capture get s case fst s of

Just co return snd s co

Nothing raise capture

Table Deriving the MRC programming mo del

Mo delling image capture from the loaded slide by a xed function is extremely crude as it

ignores the eects of random camera noise sloppy stage mechanics and the settings of the

fo cus lamp ob jective lens and lter It is a kind of idealised microscop e mo del whose

principle virtue is simplicity We use the typ e of strings to mo del exceptions

type Exn String

Intuitively we exp ect a computation of typ e IO a to act on some initial state by p erforming

some teletyp e IO b efore returning an answer either a value of typ e a or an exception of

typ e Exn We formalise this exp ectation in the following denition of IO

type IO a PrimIO Either a Exn

type PrimIO a State TT a State

data Either a b Left a Right b

Recall that programs of typ e TT a can b e thought of as a string of IO op erations ending

with a result of typ e a Therefore a program pIO a is a function that given an initial

state s returns psTT Either a Exn State which consists of a string of IO op erations

ending with a result of typ e Either a Exn State Such a result consists of a nal state

paired with either a normal result of typ e a or an exception of typ e Exn This denition of

IO allows us to mo del the semantics of computations within H and hence use our theory

of H to reason ab out programs In practice wewould almost certainly want to implement

the IO typ e more eciently

CHAPTER MONADIC IO

WeshowinTable how to program the various monadic op erations given our denition

of the IO typ e These programs should be understo o d as formal sp ecications of the

programming mo dels semantics rather than as realistic implementations There are three

auxiliary op erations either get and put Functions uncurry fst and snd are given in

Table

Wecanverify the laws IOb eta and IOasso c as follows

Lemma Structure IO return is a weak monad

Pro of The pro ofs are by routine equational reasoning IOb eta follows from the follow

ing calculations

return v fs return v s thenTT uncurryeither f raise

returnTTLeft v s thenTT uncurryeither f raise

either f raise Left v s

fvs

To establish IOasso c rst note the following facts

either f g v h either x f x hx g x h v

raise v f raise v

e thenTT x e x e thenTT e

The third of these a sp ecialised functional eta law can be established by a simple co

induction We can b egin the pro of of IOasso c as follows for any suitably typ ed programs

p f g and s

pf gs p s thenTT uncurryeither f raise

thenTT uncurryeither g raise

p s thenTT y uncurryeither f raise y

thenTT uncurryeither g raise

wsete to b e a sub expression of the righthand side No

e uncurryeither f raise y thenTT uncurryeither g raise

From the facts noted earlier we can calculate as follows

e either f raise fst ysnd y thenTT uncurryeither g raise

either f raise fst y gsnd y

either x f x gx raise x gfst ysnd y

either x f x g raise fst ysnd y

uncurryeither x f x g raise y

Hence via the sp ecialised functional eta law noted ab ovewehave

pf gs p s thenTT y e

p s thenTT uncurryeither x f x g raise

p x f x gs

as required for IOasso c

PROOF OF THE MOTIVATING EXAMPLE

analyse Image Char IO

jobA f cos sequence moveStage co analyse f co cos

analyse f capture im output f im

jobB f skip

jobB f cocos moveStage co jobC f cos

jobC f cos sequence each f co co cos analyse f

each f co capture im moveStage co output f im

Table The motivating example

Here is a counterexample to IOeta Let p be constReturnTT Wehaveforanystate

s

p return s ReturnTT thenTT uncurryeither return raise

uncurryeither return raise

either return raise

but ps ReturnTT which do es not equal in H nor in Haskell b ecause all datatyp e

constructors are lazy

From IOb eta and IOasso c we can verify the following facts ab out and skip

Lemma

pq r pqr

skip p p

It would help subsequent calculations if we had the right cancellation law

pskip p

whichwould make skip a monoid but this fails b ecause of the same counterexample

urthermore q return is another counterexample wehave qskip as IOeta F

skip return return The problem is that in H and in Haskell

Undesirable undened elements cause the failure of all these eta and right cancellation

laws TTeta fails in H b ecause x and IOeta fails in both H and Haskell

b ecause ReturnTT However for the purp ose of proving our motivating example

all we need is a sp ecic right cancellation law Lemma

Pro of of the motivating example

We conclude this c hapter byverifying the example set in x on page We b egin with

a lemma each part of which can easily proved by equational calculations from what we

have already proved ab out the IO typ e

Lemma

output v skip output v

CHAPTER MONADIC IO

capture fJust co gfgcoJust co g

moveStage co pmb gpJust co g

output v pmb gWrite v pmb g

analyse f pJust co gWrite fgco pJust co g

each f co pJust co gWrite fgco pJust co g

We need a lemma to relate jobA and the auxiliary function jobC

Lemma F or anynitecosCoord co Coord fImage Char gCoord Image

and mbMaybe Coord

jobA f co cosmb g jobC f cos Just co g

Pro of By app eal to the structural induction principle Prop osition as sp ecialised to

lists in x wemayprovethelemmaby induction on the structure of nite list cos We

have

lhs moveStage co analyse f jobA f cosmb g

analyse f jobA f cosJust co g

Write fgco jobA f cos Just co g

cos co cos In Nowwe consider the two p ossible forms of cos i cos and ii

case i it is not hard to check that b oth sides equal

Write fgco skip Just co g

In case ii wemay calculate as follows

rhs each f co jobC f cos Just co g

Write fgco jobC f cos Just co g

Write fgco jobA f co cos Just co g IH

lhs

In b oth cases then wehave the desired equivalence

The lemma would fail if we were to remove the constraint that cos be nite Consider

cos which is not nite in the sense of x Program jobA f co mb g

Write fgco but jobC f Just co g

Finallywe can verify our motivating example

Prop osition For any nite list cos function f and initial state mb g

jobA f cos mb gjobB f cos mb g

cos or cos co cos In the rst case Pro of If cos is nite then either

lhs skip mb g rhs In the second case we have rhs moveStage co

jobC f cos mb g jobC f cos Just co g jobA f co cos mb g by the

previous lemma and hence lhs rhs

Chapter

Conclusion

Summary

Wehaveshown how a theory of functional programming can b e develop ed from structural

op erational semantics and applicative bisimulation Wemight reckon this a CCSview of

calculus We develop ed parallel theories of a metalanguage for denotational semantics

M and a small functional language H essentially a fragmentofHaskell Coinduction in

the form of bisimulationuptoequivalence was found to b e useful in circumstances where

domaintheoretic Scott induction mighthave b een needed

We identied four basic mechanisms for teletyp e IO We gave a lab elled transition se

mantics for three of these mechanisms and dened a notion of bisimilarity We proved

that the three mechanisms are of equivalent expressivepower in the theoretical sense that

there are bisimulationpreserving translations b etween the three Actual implementation

of these translations would not necessarily b e ecient however

Weadvo cated a monadic style of functional IO in the context of an application of func

tional programming to medical electronics at the Edinburgh Medical Research Council

MRC Motivated by a verication example suggested by the MRC we develop ed a

theory of monadic IO as an extension of our theory of functional programming By mo d

elling the semantics of the MRC programming mo del within H wewere able to verify the

example via functional programming techniques

Ever since McCarthy referred to the IO op erations in LISP as pseudofunctions

functional IO has b een viewed with suspicion The work of this dissertation is imp ortant

b ecause it is the rst to show how a theory of functional programming can be smo othly

for functional IO and verication of extended to admit b oth an op erational semantics

programs engaged in IO

To nish o the dissertation for each chapter we sketch p ossible future work and oer

some further appraisal

CHAPTER CONCLUSION

A calculus of recursive typ es

It is a pity that the convergence theorem for M Theorem must dep end ultimately

on Mendlers theorem which this dissertation takes on trust It would b e interesting

to investigate whether the M convergence theorem could b e based on Wraiths enco ding

of recursivetyp es in the GirardReynolds calculus itself Another direction to pursue

would b e to see whether Mendlers original strong normalisation pro of could b e extended

to cop e with the new constants The author has recently obtained a result analogous to

Theorem for a variantof M via a form of Taits metho d up on which Mendlers pro of

for was based

A metalanguage for semantics

M is based on Plotkins domaintheoretic metalanguage for semantics in which

ts a particular domain construction Missing from b oth M and Plotkins eachtyp e represen

metalanguage is a typ e of nondeterministic computations corresp onding to a p owerdomain

construction Howes original pap er showed that applicative bisimulation can be

applied to a nondeterministic calculus It maybeworthwhile to investigate how to extend

M with a typ e representing nondeterministic computations

Op erational precongruence

Chapter studied how the context lemma and Howes metho d can b e used to provethat

an op erationallydened equivalence relation is a precongruence We exp ect that Howes

metho d will be extremely useful for constructing op erational theories of programming

languages The context lemma was simple and ingenious in the setting of combinatory

logic but much care app ears to b e needed to generalise it to a more complex calculus

suchasM

Theory of the metalanguage

elop ed an equational theory for M and proved results ab out certain typ es needed Wedev

for the denotational semantics of H Although not itself domaintheoretic the theory

of M can b e compared to axiomatisations of domain theory or functional programming

such as Edinburgh LCF Cambridge LCF and Thompsons logic for Miranda

There are two ma jor dierences First only M typ es of the form contain a



divergent term Hence the presentation of the theory in Table is simpler than in

LCF or Thompsons logic Second there is no principle of Scott induction for M Smith

h a principle in an op erational setting On the other has shown how to derive suc

hand wehave found in this dissertation that coinduction is sucienttoprove theorems

such as Lemma that in LCF would probably have required Scott induction

AN OPERATIONAL THEORY OF FUNCTIONAL PROGRAMMING

infix

abstype a Job JOB of unit a

with

fun exec JOB f f

fun unit x JOBfn x

fun JOB f q JOBfn exec q f

fun getStr n JOBfn inputstdinn

fun putStr s JOBfn outputstdouts

end

fun p q p fn u q

fun gettingLine s

getStr fn c

if c n then unit s else gettingLine sc

val getLine gettingLine

val main

putStr First name getLine fn first

putStr Second name getLine fn second

putStr Hello first secondn

Table A monadic style of IO for Standard ML

An op erational theory of functional programming

The most original asp ect of the theory of functional programming in Chapter is that

it is entirely grounded in op erational semantics Note that the theory of op erational

equivalence in H do es not dep end in any way on Mendlers normalisation theorem

although the theory of denotational equivalence do es

It is interesting to compare the op erational and denotational theories of H The denota

tional semantics is go o d for comparing dierent ob ject languages eg H HX andH with

sideeecting IO while the op erational semantics allows a simple derivation of program

equivalence

A useful future pro ject would b e to extend H to include eager algebraic typ e constructors

and a more realistic exception mechanism Such an extension would contain a nontrivial

fragment of core Standard ML

IO Four mechanisms for teletyp e

The main question left op en in this chapter is howtointegrate an op erational semantics of

sideeecting IO into a theory of functional programming Several authors have suggested

op erational semantics for ML extended with sideeecting op erators for concurrency

however there has been little work on equational theories for functional languages

extended with concurrency

CHAPTER CONCLUSION

Monadic IO

This chapter showed how to construct a crude mo del of the MRC computer within H and

use functional programming techniques to verify a simple prop erty Before this sort of

metho dology is to b e of any practical use there needs to b e a go o d deal more mechanised

supp ort for pro ofs of programs and exp erience of sp ecifying more realistic systems

One can view monadic IO as a controlled form of sideeecting IO the monad of side

eects has moved from the denotational semantics into the typ e system of the ob ject

language

This dissertation has not discussed practical implementation though of course the op er

ational semantics rules give some clues There is ongoing work at Glasgow on ecient

implementation of monadic IO Verication of an IO mechanism is a problem not

previously examined in work on veried functional implementations

The monadic style is not conned to lazy languages Table shows how monadic IO can

sideeecting IO In naive exp eriments b e implemented in core Standard ML on top of

this style of monadic IO was ab out six times slower than sideeecting IO It should

b e p ossible to improve this p erformance if the Job typ e were taken as primitive whichis

more or less what is prop osed in the Glasgow Haskell compiler If sideeects are

allowed only in the implementation of the Job typ e reasoning ab out monadic programs

in ML should not b e much harder than reasoning ab out monadic programs in Haskell

Bibliography

Each reference is followed by a parenthesised list of the pages on which it is cited

Harold Ab elson and Gerald J Sussman Structure and Interpretation of Com

puter Programs MIT Press Cambridge Mass p

Samson Abramsky The lazy lamb da calculus In Turner pages pp

Samson Abramsky and Richard Sykes SECDM a virtual machine for applicative

programming In Jouannaud pages pp

Peter Achten John van Groningen and Rinus Plasmeijer High level sp ecication

of IO in functional languages In Functional Programming Glasgow

Workshops in Computing SpringerVerlag p xi

Arvind and J Dean Bro ck Resource managers in functional programming Journal

of Parallel and Distributed Computing p

E A Ashcroft and W W Wadge Lucid a nonpro cedural language with iteration

Communications of the ACM July p

The Computer L Augustsson and T Johnsson The Chalmers LazyML compiler

Journal April p

John Backus Can programming be lib erated from the von Neumann style a

functional style and its algebra of programs Communications of the ACM

August p

John Backus John H Williams and Edward L Wimmers An intro duction to the

programming language FL In Turner pages pp

H P Barendregt The Lambda Calculus Its Syntax and Semanticsvolume

of Studies in logic and the foundations of mathematics NorthHolland

revised edition pp

Dave Berry Robin Milner and David N Turner Asemantics for ML concurrency

primitives In Pro ceedings of the Nineteenth A CM Symp osium on Princi

ples of Programming Languages pages pp

Gerard Berry Some syntactic and categorical constructions of lamb dacalculus mo d

els Technical Rep ort INRIA pp

Richard Bird and Philip Wadler Intro duction to Functional Programming

PrenticeHall International pp

BIBLIOGRAPHY

Bard Blo om Can LCF b e topp ed Flat lattice mo dels of typ ed lamb da calculus In

Pro ceedings of the rd IEEE Symp osium on Logic in Computer Science

pages IEEE Computer So ciety Press pp

Manfred Broy Nondeterministic data ow programs how to avoid the merge

anomaly Science of p

Georey Burn Lazy Functional Languages Interpretation and Compila

tion MIT Press Cambridge Mass p

R M Burstall and R Popplestone POP reference manual Machine Intelli

gence p

Luca Cardelli Stream inputoutput PolymorphismThe MLLCFHop e

Newsletter Decemb er p

Cardelli and Rob Pike Squeak a language for communicating with mice Luca

Computer Graphics July pp

Magnus Carlsson and Thomas Hallgren FUDGETS A graphical user interface in a

lazy functional language In FPCA Conference on Functional Program

ming Languages and Computer Architecture Cop enhagen pages

ACM Press p xi

Rob ert Cartwright and James Donahue The semantics of lazy and industrious

evaluation In LISP and Functional Programming pages ACM Press

p

P Caspi D Pilaud N Halbwachs and J A Plaice LUSTRE a declarative lan

guage for realtime programming In Fourteenth ACM Symp osium on Princi

y ples of Programming Languages pages Munic h West German

ACM Press p

Eric Co op er and J Gregory Morrisett Adding threads to Standard ML Technical

Rep ort CMUCS Computer Science Department CarnegieMellon Univer

sity Decemb er p

R L Crole and A M Pitts New foundations for xp oint computations FIX

hyp erdo ctrines and the FIXlogic Information and Computation

Earlier version in LICS p

Roy L Crole Programming Metalogics with a Fixp oint Typ e PhD the

sis University of Cambridge Computer Lab oratory February Available as

Technical Rep ort pp

Roy L Crole and Andrew D Gordon Factoring an adequacy pro of preliminary

rep ort In Functional Programming Glasgow Workshops in Computing

SpringerVerlag p xi

J Cupitt Another new scheme for writing functional op erating systems Technical

Rep ort Computing Lab oratory Universityof Kentat Canterbury March

pp

J Cupitt A brief walk through KAOS Technical Rep ort Computing Lab oratory

UniversityofKentatCanterburyFebruary pp

Roger B Dannenb erg Arctic A functional language for realtime control In LISP

BIBLIOGRAPHY

and Functional Programming pages ACM Press p

J Darlington P Henderson and D A Turner editors Functional Programming

and its ApplicationsCambridge University Press pp

B A Davey and H A Priestley Intro duction to Lattices and Order Cam

bridge University Press pp

Andrew Dwelly Synchronizing the IO b ehaviour of functional programs with feed

back Information Pro cessing LettersMay p

Andrew Dwelly Functions and dynamic user interfaces In Fourth International

Conference on Functional Programming Languages and Computer Ar

chitecture Imp erial College London pages ACM Press September

p

eter Dyb jer and Herb ert Sander A functional programming approach to the sp ec P

ication and verication of concurrent systems Formal Asp ects of Computing

Octob erDecemb er p

John R Ellis A LISP shell ACM SIGPLAN Notices May

p

Jon Fairbairn Making form follow function An exercise in functional programming

style SoftwarePractice and Exp erience p

Jo e Fasel and Paul Hudak A gentle intro duction to Haskell ACM SIGPLAN

Notices March Section T p

M Felleisen and D Friedman Control Op erators the SECDmachine and

the calculus pages NorthHolland p

M Felleisen and D PFriedman A syntactic theory of sequential state Theoretical

Computer Science p xi

Anthony J Field and Peter G Harrison Functional Programming Addison

WesleyWokingham England p

D P Friedman and D S Wise Cons should not evaluate its arguments In

S Michaelson and R Milner editors Third International Collo quium on Au

tomata Languages and Programming Edinburgh University Press July

p

JeanYvres Girard Paul Taylor and Yves Lafont Pro ofs and Typ es Cambridge

University Press pp

Andrew Gordon PFL Akernel scheme for functional IO Technical Rep ort

UniversityofCambridge Computer Lab oratoryFebruary pp

aredescription Andrew D Gordon The formal denition of a synchronous hardw

language in higher order logic In International Conference on Computer

Design Cambridge Massachusetts Octob er pages

IEEE Computer So ciety Press p

Andrew D Gordon An op erational semantics for IO in a lazy functional lan

guage In FPCA Conference on Functional Programming Languages

and Computer Architecture Cop enhagen pages ACM Press

BIBLIOGRAPHY

p xi

Michael J C Gordon Robin Milner and Christopher P Wadsworth Edinburgh

LCFvolume of Lecture Notes in Computer Science SpringerVerlag

pp

Brian T Graham The SECD Micropro cessor A Verication Case Study

Kluwer Academic Publishers pp

Carl A Gunter Semantics of Programming Languages Structures and

Techniques MIT Press Cambridge Mass p xi

Kevin Hammond Philip Wadler and Donald Brady Imp erate Be imp erativesum

mary Department of Computing Science University of Glasgow p

Dave Harrison Ruth A functional language for realtime programming In Parallel

Architectures and Languages Europ e Pro ceedings volume of Lecture

Notes in Computer Science pages SpringerVerlag pp

S Wise An implementation of an applicative le system In B C Heck and D

International Worshop on Memory ManagementStMaloFrance September

Also available as Technical Rep ort Computer Science Department

Indiana University June p

Peter Henderson Functional Programming Application and Implementa

tion PrenticeHall p

Peter Henderson Purely functional op erating systems In Darlington et al

pages pp

Peter Henderson and James H Morris Jr A lazy evaluator In Conference Record

of the Third ACM Symp osium on Principles of Programming Languages

pages ACM Press Atlanta Georgia pp

Matthew Hennessy The Semantics of Programming Languages John Wiley

and Sons Chichester p

J Roger Hindley and Jonathan P Seldin Intro duction to Combinators and

Calculus Cambridge University Press p

Soren Holmstrom PFL A functional language for parallel programming In Declar

ative Programming Workshop pages University College London

Extended version published as Rep ort Programming Metho dology Group

Chalmers University Septemb er pp

Douglas J Howe Equalityin lazy computation systems In Pro ceedings of the

th IEEE Symp osium on Logic in Computer Science pages

pp

Paul Hudak Simon L Peyton Jones Philip Wadler et al Rep ort on the functional

programming language Haskell A nonstrict purely functional language version

ACM SIGPLAN Notices March Section R pp

and Raman S Sundaresh On the expressiveness of purely functional Paul Hudak

IO systems Research Rep ort YALEUDCSRR Yale University Department

of Computer Science March pp

BIBLIOGRAPHY

John Hughes Why functional programming matters The Computer Journal

April pp

O A N Husain K C Watts F Lorriman B Butler J Tucker A Carothers

P Eason S Farrow D Rutovitz and M Stark Semiautomated cervical smear

prescreening system an evaluation of the Cytoscan Analytical and Cellular

Pathology p

Graham Hutton Higherorder functions for parsing Journal of Functional Pro

gramming July p

Evan Peter Ireland Writing interactive and lepro cessing functional programs A

metho d and implementation Masters thesis Victoria University of Wellington

March p

Alan Jerey A chemical abstract machine for graph reduction In Pro ceedings

Mathematical Foundations of Programming Semantics IX New Orleans

volume of Lecture Notes in Computer Science SpringerVerlag

p xi

Claire Jones Probabilistic Nondeterminism PhD thesis Universityof Edin

burgh Available as Technical Rep ort CST Computer Science Depart

ment UniversityofEdinburgh p

S B Jones and A F Sinclair Functional programming and op erating systems

The Computer Journal April pp

Simon B Jones Abstract machine supp ort for purely functional op erating systems

Technical Rep ort PRG Programming Research Group Oxford UniversityCom

puting Lab oratory August pp

yle Simon B Jones A range of op erating systems written in a purely functional st

Technical Rep ort PRG Programming Research Group Oxford UniversityCom

puting Lab oratory Septemb er pp

JeanPierre Jouannaud editor Functional Programming Languages and

Computer Architecture Nancy France volume of Lecture Notes in

Computer Science SpringerVerlag Septemb er pp

Gilles Kahn A preliminary theory for parallel programs Rapp ort de Recherche

IRIA January p

Gilles Kahn and David B MacQueen Coroutines and networks of parallel pro

cesses In B Gilchrist editor Information Pro cessing pages North

Holland pp

Y H Kamath and M M Matthews Implementation of an FPshell IEEE Trans

actions on Software Engineering SE May p

Kent Karlsson Nebula A functional op erating system Programming Metho dology

ersity of Technology and University of Gothenburg Group Chalmers Univ

pp

Paul Kelly Functional Programming for Lo oselycoupled Multipro cessors

MIT Press Cambridge Mass p

P J Landin A corresp ondence b etween ALGOL and Churchs lamb danotation

BIBLIOGRAPHY

Parts I and I I Communications of the ACM February

and March pp

P J Landin The next programming languages Communications of the

ACM March p

John Launchbury A natural semantics for lazy evaluation In Pro ceedings of the

Twentieth ACM Symp osium on Principles of Programming Languages

p xi

John Launchbury and Simon L Peyton Jones Lazy functional state threads In

Pro ceedings of the ACM Conference on Programming Languages Design

and Implementation PLDI Orlando June p xi

David R Lester Combinator Graph Reduction A Congruence and its Ap

Oxford University Com plications DPhil thesis Programming Research Group

puting Lab oratory April Available as Technical Rep ort PRG pp

I A Mason and C L Talcott Equivalence in functional languages with eects

Journal of Functional Programming p xi

David Matthews A distributed concurrent implementation of Standard ML In

EurOp en Autumn Conference App ears as LFCS Rep ort ECS

LFCS p

John McCarthy Paul W Abrahams Daniel J Edwards Timothy P Hart and

Michael I Levin LISP Programmers Manual MIT Press Cambridge

Mass pp

Chris S McDonald fsha functional unix command interpreter Software

Practice and Exp erience Octob er p

Paul R McJones and Garret F Swart Evolving the UNIX system interface to sup

p ort multithreaded programs Technical Rep ort DEC Systems ResearchCenter

Palo Alto Septemb er p

Lee M McLoughlin and Sean Hayes Imp erative eects from a pure functional

language In Kei Davis and John Hughes editors Functional Programming

Glasgow Workshops in Computing pages SpringerVerlag

p

Silvio Lemos Meira Pro cesses and functions In TAPSOFTCCIPL volume

Barcelona SpringerVerlag of Lecture Notes in Computer Science

p

Nax Paul Mendler Inductivetyp es and typ e constraints in the secondorder lambda

calculus Annals of Pure and Applied Logic Earlier

version in LICS pp

Paul Francis Mendler Inductive Denition in Typ e Theory PhD thesis

Department of Computer Science Cornell University Septemb er Available as

Technical Rep ort p

Alb ert R Meyer and Stavros S Cosmadakis Semantical paradigms Notes for an

invited lecture In Pro ceedings of the rd IEEE Symp osium on Logic in

BIBLIOGRAPHY

Computer Science pages July p

Robin Milner Fully abstract mo dels of typ ed lamb dacalculi Theoretical Com

puter Science pp

Robin Milner A theory of typ e p olymorphism in programming Journal of Com

puter and System Sciences Decemb er p

Robin Milner A Calculus of Communicating Systemsvolume of Lecture

Notes in Computer Science SpringerVerlag pp

Robin Milner Communication and Concurrency PrenticeHall International

pp

Robin Milner Functions as pro cesses Mathematical Structures in Computer

Science p

Robin Milner Joachim Parrow and David Walker A calculus of mobile pro cesses

parts i and ii Information and Computation and pp

Robin Milner and Mads Tofte Coinduction in relational semantics Theoretical

Computer Science pp

Robin Milner Mads Tofte and Rob ert Harp er The Denition of Standard ML

MIT Press Cambridge Mass pp

Eugenio Moggi The Partial Lamb daCalculus PhD thesis Department of

Computer Science University of Edinburgh August Available as Technical

rep ort CST p

Eugenio Moggi Notions of computations and monads Theoretical Computer

Science Earlier version in LICS pp

Eugenio Moggi An abstract view of programming languages Technical Rep ort

ECSLFCS Lab oratory for Foundations of Computer Science Department

of Computer Science University of Edinburgh April pp

Peter D Mosses Denotational semantics In Jan Van Leeuven editor Handb o ok

of Theoretical Computer Science chapter pages Elsevier Science

Publishers B V Volume B p

Rob Noble and Colin Runciman Functional languages and graphical user

interfacesa review and case study Technical Rep ort YCS Department

of Computer Science UniversityofYork February p xi

John T ODonnell Dialogues A basis for constructing programming environments

In ACM Symp osium on Language Issues in Programming Environments

pages SIGPLAN Notices pp

Lawrence C Paulson Logic and Computation Interactive Pro of with Cam

bridge LCF Cambridge University Press pp

Lawrence C Paulson ML for the Working ProgrammerCambridge University

Press p

tinuation extension for Hop e Technical Rep ort Nigel Perry Hop eC A con

ICFPRLANG Department of Computing Imp erial College London

BIBLIOGRAPHY

Novemb er p

Nigel Perry The Implementation of Practical Functional Programming

Languages PhD thesis Department of Computing Imp erial College London

June pp

Simon L Peyton Jones The implementation of functional programming

languages PrenticeHall International April pp

Simon L Peyton Jones and Philip Wadler Imp erative functional programming In

Pro ceedings th ACM Symp osium on Principles of Programming Lan

guages Charleston South Carolina January pages ACM Press

pp xi

Andrew Pitts and Ian Stark On the observable prop erties of higher order functions

that dynamically create lo cal names preliminary rep ort In SIPL ACM

SIGPLAN Workshop on State in Programming Languages pages

p xi June

Andrew M Pitts Evaluation logic In G Birtwistle editor IVth Higher Or

der Workshop Ban Workshops in Computing pages Springer

Verlag Available as UniversityofCambridge Computer Lab oratory Technical

Rep ort August pp

Andrew M Pitts Relational prop erties of domains Technical Rep ort University

of Cambridge Computer Lab oratory Decemb er p xi

Andrew M Pitts A coinduction principle for recursively dened domains Theo

retical Computer Science pp

Andrew M Pitts Computational adequacy via mixed inductive denitions In

Pro ceedings Mathematical Foundations of Programming Semantics IX

New Orleans volume of Lecture Notes in Computer Science pages

SpringerVerlag p xi

Gordon D Plotkin Callbyname callbyvalue and the calculus Theoretical

Computer Science pp

Gordon D Plotkin LCF considered as a programming language Theoretical

Computer Science pp

Gordon D Plotkin The category of complete partial orders a to ol for making

meanings Unpublished lecture notes for the Summer Scho ol on Foundations of

Articial Intelligence and Computer Science Pisa June pp

Gordon D Plotkin A structural approach to op erational semantics Technical

Rep ort FN DAIMI Aarhus UniversitySeptemb er p

Gordon D Plotkin Denotational semantics with partial functions Unpublished

lecture notes CSLI Stanford University July pp

Ian Poole A functional programming environment for image analysis In Pro ceed

the th International Conference on Pattern Recognition The ings of

Haguevolume IV pages August p

S Purushothaman and J Seaman An adequate op erational semantics of sharing

in lazy evaluation In B KriegBruc kner editor Pro ceedings of th Europ ean

BIBLIOGRAPHY

Symp osium on Programming ESOP Rennes France volume of

Lecture Notes in Computer Science SpringerVerlag Available as Rep ort

CS Department of Computer Science Pennsylvania State University p

S A Reb elsky IO trees and interactive lazy functional programming In Fourth

International Symp osium on Programming Language Implementation

and Logic Programming volume of Lecture Notes in Computer Sci

ence pages Leuven August SpringerVerlag p

D Hugh Redelmeier Towards Practical Functional Programming PhD thesis

Computer Systems Research Group UniversityofToronto May Available as

Technical Rep ort CSRG p

A Reid and S Singh Budgets Cheap and cheerful widget combinators In Func

tional Programming GlasgowWorkshops in Computing SpringerVerlag

p xi

John H Reppy An op erational semantics of rstclass synchronous op erations

Technical Rep ort TR Department of Computer Science Cornell University

August pp

J C Reynolds Denitional interpreters for higherorder programming languages

In Pro c th ACM National Conference pages ACM New York

p

David Sands Op erational theories of improvement in functional languages ex

tended abstract In Functional Programming Glasgow Workshops in

Computing pages SpringerVerlag p xi

Davide Sangiorgi The lazy lamb da calculus in a concurrency scenario Information

and Computation To app ear p

David A Schmidt Denotational Semantics A Metho dology for Language

Development Allyn and Bacon Inc p

Jon Shultis A functional shell In Pro c Symp on Programming Language

Issues in Software Systems SIGPLAN Notices p

To dd G Simpson Design and verication of IFL a widesp ectrum interme

diate functional language PhD thesis Department of Computer Science The

University of Calgary July Available as Research Rep ort pp

Scott F Smith From op erational to denotational semantics In MFPS VI I

Pittsburgh volume of Lecture Notes in Computer Science pages

SpringerVerlag pp

Denotational semantics the ScottStrachey approach to Joseph E Stoy

programming language theory MIT Press Cambridge Mass pp

Joseph E Stoy Mathematical asp ects of functional programming In Darlington

et al pages p

William Stoye Messagebased functional op erating systems Science of Com

puter Programming Originally app eared as ANewScheme

BIBLIOGRAPHY

for Writing Functional Op erating Systems Technical Rep ort UniversityofCam

bridge Computer Lab oratory pp

Christopher Strachey A general purp ose macrogenerator Technical Memorandum

University Mathematical Lab oratory Cambridge March p

Christopher Strachey Fundamental concepts in programming languages Unpub

lished lectures given at the International Summer Scho ol in Computer Programming

Cop enhagen August p

R D Tennent Semantics of Programming Languages PrenticeHall Interna

tional p

Simon Thompson A logic for Miranda Formal Asp ects of Computing

Octob erDecember p

Simon Thompson Interactive functional programs A metho d and a formal seman

tics In Turner pages Originally app eared as Technical Rep ort

pp Computing Lab oratory UniversityofKentatCanterburyNovemb er

Mark Tillotson Intro duction to the functional programming language Ponder

Technical Rep ort University of Cambridge Computer Lab oratory May

pp

David Turner A new implementation technique for applicative languages

SoftwarePractice and Exp erience pp

David Turner Functional programming and communicating pro cesses some design

considerations for a functional op erating system In Parallel Architectures and

Languages Europ e Pro ceedingsvolume of Lecture Notes in Computer

Science pages SpringerVerlag pp

David Turner An approach to functional op erating systems In Turner pages

pp

Addison David Turner editor ResearchTopics in Functional Programming

Wesley pp

W Wadge and E Ashcroft Lucid the dataow programming language

Academic Press New York pp

Philip Wadler How to replace failure by a list of successes In Jouannaud pages

p

Philip Wadler Comprehending monads Mathematical Structures in Com

puter Science pp

Philip Wadler The essence of functional programming In Pro ceedings of the

Nineteenth ACM Symp osium on Principles of Programming Languages

pp

John H Williams and Edward L Wimmers Sacricing simplicityfor convenience

Where do you draw the line In Conference Record of the Fifteenth ACM

Symp osium on Principles of Programming Languages pages Jan

uary pp

BIBLIOGRAPHY

Glynn Winskel The Formal Semantics of Programming Languages MIT

Press Cambridge Mass p xi

GC Wraith A note on categorical datatyp es In D H Pitt D E Rydheheard

P Dyb jer A M Pitts and A Poigne editors Category Theory and Computer

Science volume of Lecture Notes in Computer Science SpringerVerlag

pp

Stuart C Wray Implementation and programming techniques for func

tional languages PhD thesis University of Cambridge Computer Lab oratory

June Available as Technical Rep ort pp

BIBLIOGRAPHY

Notation Index

Chapter One

X Y Z typ e variable

f g u v w x y z term variable

t phrase of abstract syntax

ftv t typ e variables free in t

fv t term variables free in t

t

t X substitution for a typ e variable

t

t x substitution for a term variable

t t syntactic identity up to alphaconversion

bv t term variables b ound in t

C D context

C t instantiation of a context

R R comp osition of relations R and R

R transitive closure of relation R



R reexiv e transitive closure of relation R

Id identity relation



R inverse of relation R

Chapter Two

typ e in

recursivetyp e in

L M N term in

M typ e assignmentin

M N reduction in

M N simulation in

Chapter Three

typ e in M

recursivetyp e in M

U V canonical term in M

NOTATION INDEX

L M N term in M

environmentinM

M typ e assignmentinM

E exp erimentinM

M N reduction in M

M V evaluation in M

M convergence in M

M divergence in M

translation of M into

Chapter Four

V canonical context

D

C x substitution for contexts

E contextualexp eriment

adivergent program in M

b

R a conned relation generated from R

C

contextual order in M

E

exp erimental order in M

functional inducing applicative similarityin M

A

applicative similarityin M

A

applicative bisimilarityin M



compatible similarityin M

Chapter Five

op erational equivalence in M

op erational order in M

bbc representation of b o olean b ftt g in M

bnc representation of number n N in M

bc representation of op erator in M

Chapter Six

typ e in H

algebraic t yp e in H

dc dataclause in H

c canonical term in H

e term in H

cc caseclause in H

representation of literal in H

environmentinH

e typ e assignmentinH

NOTATION INDEX

adivergent program in H

LE left exp erimentinH

RE right exp erimentinH

e e reduction in H

e c evaluation in H

e convergence in H

e divergence in H

M N translation of H function application into M

translation of H into M

b

R a conned relation generated from R

functional inducing applicative similarityin H

op erational equivalence in H

v op erational order in H



v compatible similarityin H

denotational equivalence in H induced from M

Chapter Seven

teletyp e action

hi functional inducing bisimilarity

lab elled transition

bisimilarity

Chapter Eight

IO typ e of computations

input input a character

output output a character

return return a value

sequential comp osition

restricted sequential comp osition

sequence iterated sequential comp osition

lift lift a curried function

accumulate accumulating sequential comp osition

raise raise an exception

try handle an exception

biased choice

Coord typ e of co ordinates

moveStage move microscop e stage

capture capture an image

parkStage park microscop e stage

TT typ e of teletyp e IO

State typ e of state

Maybe optional typ e

NOTATION INDEX

Exn typ e of exceptions

PrimIO primitivetyp e of computations

Either sum typ e

either sum discrimination

get return the current state

put replace the current state

Index

concurrency actions

constructor for adequacy xi

in MRCmodel op erational

Kahn networks advice to the reader

Concurrent Clean xi ALGOL

conned extension alphaconversion

conned relation in

conned term

bisimilarity

congruence

applicative

context

compared with applicative bisimilar

canonical

ity

instantiation

ground applicative

context lemma

simplicity of

contextual equivalence

bisimulation

con textual order

applicative xi

convergence

and nondeterminism

denotational equivalence

bisimulationupto

divergence

Bo oleans in M

Budgets xi

eager language

emptytyp e in M

callbyname

environment

callbyneed xi

evaluation

callbyvalue

evaluation contexts

canonical freeness

exceptions

CCS

in HX

in monadic IO

certainlyconvergenttyp es

execution typ e

coinduction

exp erimental order

and bisimulation

exp eriments

and Scott induction

left and right

principle of

combinators for IO

FL

history

FP

monadic programming

Fudgets xi

stream transformers

functional extensionality

compatible relation

computation

closure

computational monad

Gofer x

INDEX

lamb dacalculus GPM

computational grammar of

lazy H terms

p olymorphic H typ es

simplytyp ed M contextualexp eriments

untyp ed M exp eriments

lazy language M terms

LCF vii M typ es

LISP terms

typ es Lucid

ground relation

Miranda

ground restriction

ML x

Haskell vii ix x

Lazy

Standard

Mo dula

committee

monad T of

interpreted in H

exceptions

Hop e

lifting

Howes metho d

sideeects

hyp othesis of this dissertation

MRC computer

IO mechanism

name convention in

continuationpassing

Nebula

semantics

numb ers in M

FL histories

onep ointtyp e in M

semantics

op erational equivalence

hiatons

op erational extensionality

IO trees

op erational order

Landinstream

semantics

PCF

monadic

PFL

for Standard ML

p olymorphic denitions

sideeecting

POP

semantics

p ositivetyp es

streams for eager languages

p ossiblydivergenttyp es

synchronisedstream

precongruence

Dialogue typ e

preorder

semantics

program

systems

initial typ es

reduction

input p olling

relational op erations

ionotgettingLine

iterated pro ducts and sums in M

Scheme

Silage

KAOS

similarity

applicative lab elled transition system

INDEX

compatible

ground applicative

simulation

applicative

SKIMI I

Stracheys prop erty

stream

implementation of input

recursivetyp e for

strong normalisation

for Mendlers calculus

for

structural induction

substitution

syntax conventions

task

terminal typ es

trace

equivalence and bisimilarity

semantics

typ e assignment

variable convention