Functional Programming and Input/Output
Total Page:16
File Type:pdf, Size:1020Kb
Functional Programming and InputOutput Andrew Donald Gordon Kings College A dissertation submitted to the University of Cambridge towards the degree of Do ctor of Philosophy August Contents Summary vii Preface ix Preface to the Bo ok Edition xi Intro duction Functional programming 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.