Unboxed Values As Rst Class Citizens in a Non-Strict Functional Language 1

Total Page:16

File Type:pdf, Size:1020Kb

Unboxed Values As Rst Class Citizens in a Non-Strict Functional Language 1 Unboxed values as rst class citizens in a nonstrict functional language Simon L Peyton Jones and John Launchbury Department of Computing Science University of Glasgow G QQ fsimonp j jlgdcsglasgowacuk July Abstract The co de compiled from a nonstrict functional program usually manipulates heap allo cated boxed numbers Compilers for such languages often go to considerable trouble to optimise op erations on b oxed numbers into simpler op erations on their unboxed forms These optimisations are usually handled in an ad hoc manner in the co de generator b ecause earlier phases of the compiler have no way to talk ab out unboxed values We present a new approach which makes unboxed values into nearly rstclass citizens The language including its type system is extended to handle unboxed values The optimisation of b oxing and unboxing op erations can now b e reinterpreted as a set of correctnesspre se rving program transformations Indeed the particular transformations required are ones which a compiler would want to implement anyway The compiler b ecomes b oth simpler and more mo dular Two other b enets accrue Firstly the results of strictness analysis can b e exploited within the same uniform transformational framework Secondly new algebraic data types with unboxed comp onents can b e declared Values of these types can b e manipulated much more eciently than the corresp onding b oxed versions Both a static and a dynamic semantics are given for the augmented language The denotational dynamic semantics is notable for its use of unpointed domains Introduction Most compilers have a phase during which they attempt to optimise the program by applying correctnesspreserving transformations to it Constant folding is a particular example of this sort of transformation pro cedure inlining is another Functional languages are esp ecially amenable to such transformation b ecause of their simple semantics Nonstrict functional languages are nicest of all b ecause reduction sometimes called unfolding in a transformational context is always valid in a strict language it is only valid if the b o dy of the function b eing unfolded is guaranteed to evaluate the argument This pap er app ears in the Pro ceedings of the Conference on Functional Programming Languages and Computer Architecture Cambridge Sept Several researchers have b egun to express more and more of the work of the compiler in the form of correctnesspreserving transformations see Section Such an approach has obvious advantages Firstly each transformation can b e proved to b e correct indep endent of the others When more of the compiler takes the form of such transformations it is easier to prove the compiler correct Secondly each transformation exp oses opp ortunities for other transformations The more that is done within a transformation phase the more chance there is for such b enecial interactions There is an imp ortant class of optimisations for nonstrict languages which has so far b een b eyond the scop e of program transformation These all relate to the treatment of socalled unboxed values which we introduce in the next section Instead this family of optimisations is generally implemented in an ad hoc manner in the co de generator Following some preliminaries Sections and this pap er makes four main contributions Most imp ortant we show how the class of unboxedvalue optimisations can formulated as correctnesspreserving transformations Section Unboxed values are made rst class citizens distinguished from their b oxed counterparts by the type system These transformations do not generate much b etter co de than current compilers do our in tention is only to present the optimisations in a new and elegant way We show how to apply the same idea to express and exploit the results of strictness analysis in a uniform way Section We show how the approach can b e generalised to other algebraic data types Section Declaring and using such unboxed data types gives a substantial p erformance improve ment We provide a formal underpinning for the approach by giving b oth a static semantics and a denotational dynamic semantics for the language extended with unboxed values Sections and The dynamic semantics has the desirable prop erty that the semantic equations are unchanged from those for the language without unboxed values despite the extra strictness of the extended language This eect is achieved by using unpointed domains We conclude by discussing some languagedesign issues reviewing related work and mention ing some areas for further work The problem Consider the following function denition double x x x In a nonstrict language x may b e unevaluated when double is called in which case a p ointer to a heapallo cated closure or susp ension for x is passed to double When double needs xs value in this case right away it evaluates the closure As a side eect of this evaluation the closure of x is overwritten with its value Any further attempts to evaluate x will succeed immediately returning its value This way of ensuring that unevaluated closures are evaluated at most once is called lazy evaluation Unfortunately lazy evaluation tends to make arithmetic horribly inecient if some care is not taken In particular in a nave implementation numbers are always represented by a p ointer to a heapallo cated ob ject which is either an unevaluated closure or is a b ox containing the numbers actual value which has now overwritten the closure This means that a simple arithmetic op eration which would take a single machine instruction in a strict language requires quite a long sequence of instructions the two op erands are fetched from their b oxes the op eration p erformed a new b ox allo cated to contain the result and the result placed in it The bitpattern representing the value itself on which the builtin machine instructions op er ate is called an unboxed value Unboxed values come in a variety of shap es and sizes bit integers bit integers single and doubleprecision oating p oint numbers and so on are all unboxed values A p ointer to a heapallo cated b ox containing an unboxed value is called a boxed value Clearly it is vastly more ecient to manipulate unboxed values than b oxed ones Quite a lot can b e done For example consider again the denition of double given ab ove A nave compiler would compile co de for double which would evaluate x extract its unboxed value then evaluate x again and extract the value again then add the two and b ox the result A slightly cleverer compiler would realise that it already had xs value in hand and refrain from the second evaluation As another example consider the following denition f x y z x y z A nave compiler might generate co de to b ox the value of yz only to unbox it again right away A cleverer compiler can elide these unnecessary op erations As a nal example consider the following call to double double pq The straightforward approach is to build a closure for pq and pass it to double which will evaluate it But double is clearly going to evaluate its argument so it is a waste to allo cate the closure only for double to evaluate it and discard it for the garbage collector to recover later It would b e much b etter for the caller to evaluate p and q add their values and pass pq to double in unboxed form The Core language We b egin with a few preliminaries to set the scene for our new prop osals Our compilation route involves the following steps The source language is Haskell Hudak et al a stronglytyped nonstrict purelyfunctional language Haskells main innovative feature is its supp ort for sys tematic overloading but we do not discuss this asp ect at all here Haskell is compiled to the Core language All Haskells syntactic sugar has b een compiled out type checking p erformed and overloading resolved Patternmatching has b een compiled into case expressions each of which p erforms only a single level of matching Boxing and unboxing are explicit as describ ed b elow Program analyses and transformations are applied to the Core language In particular the b oxingunboxing optimisations are carried out here The Core language is translated to the STG language the abstract machine co de for the Spineless Tagless Gmachine our evaluation mo del The Spineless Tagless Gmachine was initially presented in Peyton Jones Salkild but the latter has b een com pletely rewritten as a companion to this pap er Peyton Jones The STG language is translated to Abstract C This is just an internal data type which can b e simply printed out as C co de and thence compiled to native co de with standard C compilers Abstract C can also serve serve as an input to a co de generator thereby generating native co de directly but we have not yet implemented this route In this pap er we only concern ourselves with the Core language which is introduced in the next section However since the Core language do es not have explicit algebraic type declarations we b orrow Haskells syntax for this purp ose when required The syntax of the Core language The abstract syntax of the Core language is given in Figure The binds constituting a prog should dene main which is taken to b e the value of the program The concrete syntax we use is conventional parentheses are used to disambiguate application asso ciates to the left and binds more tightly than any other op erator the b o dy of a lambda abstraction extends as far to the right as p ossible the usual inx arithmetic op erators are p ermitted the usual syntax for lists is allowed with inx constructor and empty list and where the layout
Recommended publications
  • Programming the Capabilities of the PC Have Changed Greatly Since the Introduction of Electronic Computers
    1 www.onlineeducation.bharatsevaksamaj.net www.bssskillmission.in INTRODUCTION TO PROGRAMMING LANGUAGE Topic Objective: At the end of this topic the student will be able to understand: History of Computer Programming C++ Definition/Overview: Overview: A personal computer (PC) is any general-purpose computer whose original sales price, size, and capabilities make it useful for individuals, and which is intended to be operated directly by an end user, with no intervening computer operator. Today a PC may be a desktop computer, a laptop computer or a tablet computer. The most common operating systems are Microsoft Windows, Mac OS X and Linux, while the most common microprocessors are x86-compatible CPUs, ARM architecture CPUs and PowerPC CPUs. Software applications for personal computers include word processing, spreadsheets, databases, games, and myriad of personal productivity and special-purpose software. Modern personal computers often have high-speed or dial-up connections to the Internet, allowing access to the World Wide Web and a wide range of other resources. Key Points: 1. History of ComputeWWW.BSSVE.INr Programming The capabilities of the PC have changed greatly since the introduction of electronic computers. By the early 1970s, people in academic or research institutions had the opportunity for single-person use of a computer system in interactive mode for extended durations, although these systems would still have been too expensive to be owned by a single person. The introduction of the microprocessor, a single chip with all the circuitry that formerly occupied large cabinets, led to the proliferation of personal computers after about 1975. Early personal computers - generally called microcomputers - were sold often in Electronic kit form and in limited volumes, and were of interest mostly to hobbyists and technicians.
    [Show full text]
  • Certification of a Tool Chain for Deductive Program Verification Paolo Herms
    Certification of a Tool Chain for Deductive Program Verification Paolo Herms To cite this version: Paolo Herms. Certification of a Tool Chain for Deductive Program Verification. Other [cs.OH]. Université Paris Sud - Paris XI, 2013. English. NNT : 2013PA112006. tel-00789543 HAL Id: tel-00789543 https://tel.archives-ouvertes.fr/tel-00789543 Submitted on 18 Feb 2013 HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements d’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. UNIVERSITÉ DE PARIS-SUD École doctorale d’Informatique THÈSE présentée pour obtenir le Grade de Docteur en Sciences de l’Université Paris-Sud Discipline : Informatique PAR Paolo HERMS −! − SUJET : Certification of a Tool Chain for Deductive Program Verification soutenue le 14 janvier 2013 devant la commission d’examen MM. Roberto Di Cosmo Président du Jury Xavier Leroy Rapporteur Gilles Barthe Rapporteur Emmanuel Ledinot Examinateur Burkhart Wolff Examinateur Claude Marché Directeur de Thèse Benjamin Monate Co-directeur de Thèse Jean-François Monin Invité Résumé Cette thèse s’inscrit dans le domaine de la vérification du logiciel. Le but de la vérification du logiciel est d’assurer qu’une implémentation, un programme, répond aux exigences, satis- fait sa spécification. Cela est particulièrement important pour le logiciel critique, tel que des systèmes de contrôle d’avions, trains ou centrales électriques, où un mauvais fonctionnement pendant l’opération aurait des conséquences catastrophiques.
    [Show full text]
  • Presentation on Ocaml Internals
    OCaml Internals Implementation of an ML descendant Theophile Ranquet Ecole Pour l’Informatique et les Techniques Avancées SRS 2014 [email protected] November 14, 2013 2 of 113 Table of Contents Variants and subtyping System F Variants Type oddities worth noting Polymorphic variants Cyclic types Subtyping Weak types Implementation details α ! β Compilers Functional programming Values Why functional programming ? Allocation and garbage Combinatory logic : SKI collection The Curry-Howard Compiling correspondence Type inference OCaml and recursion 3 of 113 Variants A tagged union (also called variant, disjoint union, sum type, or algebraic data type) holds a value which may be one of several types, but only one at a time. This is very similar to the logical disjunction, in intuitionistic logic (by the Curry-Howard correspondance). 4 of 113 Variants are very convenient to represent data structures, and implement algorithms on these : 1 d a t a t y p e tree= Leaf 2 | Node of(int ∗ t r e e ∗ t r e e) 3 4 Node(5, Node(1,Leaf,Leaf), Node(3, Leaf, Node(4, Leaf, Leaf))) 5 1 3 4 1 fun countNodes(Leaf)=0 2 | countNodes(Node(int,left,right)) = 3 1 + countNodes(left)+ countNodes(right) 5 of 113 1 t y p e basic_color= 2 | Black| Red| Green| Yellow 3 | Blue| Magenta| Cyan| White 4 t y p e weight= Regular| Bold 5 t y p e color= 6 | Basic of basic_color ∗ w e i g h t 7 | RGB of int ∗ i n t ∗ i n t 8 | Gray of int 9 1 l e t color_to_int= function 2 | Basic(basic_color,weight) −> 3 l e t base= match weight with Bold −> 8 | Regular −> 0 in 4 base+ basic_color_to_int basic_color 5 | RGB(r,g,b) −> 16 +b+g ∗ 6 +r ∗ 36 6 | Grayi −> 232 +i 7 6 of 113 The limit of variants Say we want to handle a color representation with an alpha channel, but just for color_to_int (this implies we do not want to redefine our color type, this would be a hassle elsewhere).
    [Show full text]
  • Comparative Studies of Programming Languages; Course Lecture Notes
    Comparative Studies of Programming Languages, COMP6411 Lecture Notes, Revision 1.9 Joey Paquet Serguei A. Mokhov (Eds.) August 5, 2010 arXiv:1007.2123v6 [cs.PL] 4 Aug 2010 2 Preface Lecture notes for the Comparative Studies of Programming Languages course, COMP6411, taught at the Department of Computer Science and Software Engineering, Faculty of Engineering and Computer Science, Concordia University, Montreal, QC, Canada. These notes include a compiled book of primarily related articles from the Wikipedia, the Free Encyclopedia [24], as well as Comparative Programming Languages book [7] and other resources, including our own. The original notes were compiled by Dr. Paquet [14] 3 4 Contents 1 Brief History and Genealogy of Programming Languages 7 1.1 Introduction . 7 1.1.1 Subreferences . 7 1.2 History . 7 1.2.1 Pre-computer era . 7 1.2.2 Subreferences . 8 1.2.3 Early computer era . 8 1.2.4 Subreferences . 8 1.2.5 Modern/Structured programming languages . 9 1.3 References . 19 2 Programming Paradigms 21 2.1 Introduction . 21 2.2 History . 21 2.2.1 Low-level: binary, assembly . 21 2.2.2 Procedural programming . 22 2.2.3 Object-oriented programming . 23 2.2.4 Declarative programming . 27 3 Program Evaluation 33 3.1 Program analysis and translation phases . 33 3.1.1 Front end . 33 3.1.2 Back end . 34 3.2 Compilation vs. interpretation . 34 3.2.1 Compilation . 34 3.2.2 Interpretation . 36 3.2.3 Subreferences . 37 3.3 Type System . 38 3.3.1 Type checking . 38 3.4 Memory management .
    [Show full text]
  • “Scrap Your Boilerplate” Reloaded
    “Scrap Your Boilerplate” Reloaded Ralf Hinze1, Andres L¨oh1, and Bruno C. d. S. Oliveira2 1 Institut f¨urInformatik III, Universit¨atBonn R¨omerstraße164, 53117 Bonn, Germany {ralf,loeh}@informatik.uni-bonn.de 2 Oxford University Computing Laboratory Wolfson Building, Parks Road, Oxford OX1 3QD, UK [email protected] Abstract. The paper “Scrap your boilerplate” (SYB) introduces a com- binator library for generic programming that offers generic traversals and queries. Classically, support for generic programming consists of two es- sential ingredients: a way to write (type-)overloaded functions, and in- dependently, a way to access the structure of data types. SYB seems to lack the second. As a consequence, it is difficult to compare with other approaches such as PolyP or Generic Haskell. In this paper we reveal the structural view that SYB builds upon. This allows us to define the combinators as generic functions in the classical sense. We explain the SYB approach in this changed setting from ground up, and use the un- derstanding gained to relate it to other generic programming approaches. Furthermore, we show that the SYB view is applicable to a very large class of data types, including generalized algebraic data types. 1 Introduction The paper “Scrap your boilerplate” (SYB) [1] introduces a combinator library for generic programming that offers generic traversals and queries. Classically, support for generic programming consists of two essential ingredients: a way to write (type-)overloaded functions, and independently, a way to access the structure of data types. SYB seems to lacks the second, because it is entirely based on combinators.
    [Show full text]
  • Csci 555: Functional Programming Functional Programming in Scala Functional Data Structures
    CSci 555: Functional Programming Functional Programming in Scala Functional Data Structures H. Conrad Cunningham 6 March 2019 Contents 3 Functional Data Structures 2 3.1 Introduction . .2 3.2 A List algebraic data type . .2 3.2.1 Algebraic data types . .2 3.2.2 ADT confusion . .3 3.2.3 Using a Scala trait . .3 3.2.3.1 Aside on Haskell . .4 3.2.4 Polymorphism . .4 3.2.5 Variance . .5 3.2.5.1 Covariance and contravariance . .5 3.2.5.2 Function subtypes . .6 3.2.6 Defining functions in companion object . .7 3.2.7 Function to sum a list . .7 3.2.8 Function to multiply a list . .9 3.2.9 Function to remove adjacent duplicates . .9 3.2.10 Variadic function apply ................... 11 3.3 Data sharing . 11 3.3.1 Function to take tail of list . 12 3.3.2 Function to drop from beginning of list . 13 3.3.3 Function to append lists . 14 3.4 Other list functions . 15 3.4.1 Tail recursive function reverse . 15 3.4.2 Higher-order function dropWhile . 17 3.4.3 Curried function dropWhile . 17 3.5 Generalizing to Higher Order Functions . 18 3.5.1 Fold Right . 18 3.5.2 Fold Left . 22 3.5.3 Map . 23 1 3.5.4 Filter . 25 3.5.5 Flat Map . 26 3.6 Classic algorithms on lists . 27 3.6.1 Insertion sort and bounded generics . 27 3.6.2 Merge sort . 29 3.7 Lists in the Scala standard library .
    [Show full text]
  • Open Pattern Matching for C++
    Open Pattern Matching for C++ Yuriy Solodkyy Gabriel Dos Reis Bjarne Stroustrup Texas A&M University College Station, Texas, USA fyuriys,gdr,[email protected] Abstract 1.1 Summary Pattern matching is an abstraction mechanism that can greatly sim- We present functional-style pattern matching for C++ built as an plify source code. We present functional-style pattern matching for ISO C++11 library. Our solution: C++ implemented as a library, called Mach71. All the patterns are • is open to the introduction of new patterns into the library, while user-definable, can be stored in variables, passed among functions, not making any assumptions about existing ones. and allow the use of class hierarchies. As an example, we imple- • is type safe: inappropriate applications of patterns to subjects ment common patterns used in functional languages. are compile-time errors. Our approach to pattern matching is based on compile-time • Makes patterns first-class citizens in the language (§3.1). composition of pattern objects through concepts. This is superior • is non-intrusive, so that it can be retroactively applied to exist- (in terms of performance and expressiveness) to approaches based ing types (§3.2). on run-time composition of polymorphic pattern objects. In partic- • provides a unified syntax for various encodings of extensible ular, our solution allows mapping functional code based on pattern hierarchical datatypes in C++. matching directly into C++ and produces code that is only a few • provides an alternative interpretation of the controversial n+k percent slower than hand-optimized C++ code. patterns (in line with that of constructor patterns), leaving the The library uses an efficient type switch construct, further ex- choice of exact semantics to the user (§3.3).
    [Show full text]
  • Codata in Action
    Codata in Action Paul Downen1, Zachary Sullivan1, Zena M. Ariola1, and Simon Peyton Jones2 1 University of Oregon, Eugene, USA?? [email protected], [email protected], [email protected] 2 Microsoft Research, Cambridge, UK [email protected] Abstract. Computer scientists are well-versed in dealing with data struc- tures. The same cannot be said about their dual: codata. Even though codata is pervasive in category theory, universal algebra, and logic, the use of codata for programming has been mainly relegated to represent- ing infinite objects and processes. Our goal is to demonstrate the ben- efits of codata as a general-purpose programming abstraction indepen- dent of any specific language: eager or lazy, statically or dynamically typed, and functional or object-oriented. While codata is not featured in many programming languages today, we show how codata can be eas- ily adopted and implemented by offering simple inter-compilation tech- niques between data and codata. We believe codata is a common ground between the functional and object-oriented paradigms; ultimately, we hope to utilize the Curry-Howard isomorphism to further bridge the gap. Keywords: codata · lambda-calculi · encodings · Curry-Howard · func- tion programming · object-oriented programming 1 Introduction Functional programming enjoys a beautiful connection to logic, known as the Curry-Howard correspondence, or proofs as programs principle [22]; results and notions about a language are translated to those about proofs, and vice-versa [17]. In addition to expressing computation as proof transformations, this con- nection is also fruitful for education: everybody would understand that the as- sumption \an x is zero" does not mean \every x is zero," which in turn explains the subtle typing rules for polymorphism in programs.
    [Show full text]
  • Generalized Abstract GHC.Generics
    Generalized Abstract GHC.Generics Ryan Scott Indiana University Haskell Implementors Workshop 2018 St. Louis, MO GHC.Generics GHC’s most popular datatype-generic programming library. GHC.Generics GHC’s most popular datatype-generic programming library. data ADT a = MkADT1 a | MkADT2 a deriving Generic GHC.Generics GHC’s most popular datatype-generic programming library. data ADT a = MkADT1 a | MkADT2 a deriving Generic GHC.Generics GHC’s most popular datatype-generic programming library. data ADT a data GADT a where = MkADT1 a MkGADT1 :: GADT Int | MkADT2 a MkGADT2 :: GADT Bool deriving Generic deriving Generic GHC.Generics GHC’s most popular datatype-generic programming library. data ADT a data GADT a where = MkADT1 a MkGADT1 :: GADT Int | MkADT2 a MkGADT2 :: GADT Bool deriving Generic deriving Generic GHC.Generics GHC’s most popular datatype-generic programming library. class Generic a where -- Can be derived type Rep a from :: a -> Rep a to :: Rep a -> a GHC.Generics GHC’s most popular datatype-generic programming library. class NFData a where rnf :: a -> () instance NFData a => NFData [a] where rnf [] = () rnf (x:xs) = rnf x `seq` rnf xs We’ll continue to use NFData as a running example. What can GHC.Generics do? Generically represent any (simple) algebraic data type as a composition of representation types. data U1 = U1 -- No fields newtype K1 c = K1 c -- One field data a :*: b = a :*: b -- Products data a :+: b = L1 a | R1 b -- Sums What can GHC.Generics do? Generically represent any (simple) algebraic data type as a composition of representation types. -- Example instance instance Generic [a] where type Rep [a] = U1 -- [] constructor :+: (K1 a :*: K1 [a]) -- (:) constructor from [] = L1 U1 from (x:xs) = R1 (K1 x :*: K1 xs) to (L1 U1) = [] to (R1 (K1 x :*: K1 xs)) = x:xs What can GHC.Generics do? Generically represent any (simple) algebraic data type as a composition of representation types.
    [Show full text]
  • Parametric Polymorphism and Operational Improvement
    Parametric Polymorphism and Operational Improvement JENNIFER HACKETT, University of Nottingham, UK GRAHAM HUTTON, University of Nottingham, UK Parametricity, in both operational and denotational forms, has long been a useful tool for reasoning about program correctness. However, there is as yet no comparable technique for reasoning about program improve- ment, that is, when one program uses fewer resources than another. Existing theories of parametricity cannot be used to address this problem as they are agnostic with regard to resource usage. This article addresses this problem by presenting a new operational theory of parametricity that is sensitive to time costs, which can be used to reason about time improvement properties. We demonstrate the applicability of our theory by showing how it can be used to prove that a number of well-known program fusion techniques are time improvements, including fixed point fusion, map fusion and short cut fusion. CCS Concepts: • Software and its engineering → Functional languages; Polymorphism; • Theory of computation → Operational semantics; Additional Key Words and Phrases: parametricity, improvement theory ACM Reference Format: Jennifer Hackett and Graham Hutton. 2018. Parametric Polymorphism and Operational Improvement. Proc. ACM Program. Lang. 2, ICFP, Article 68 (September 2018), 24 pages. https://doi.org/10.1145/3236763 68 1 INTRODUCTION Parametric polymorphism is everywhere. In typed functional languages, many if not most of the built-in and user-defined functions are parametric. Because of this ubiquity, we must carefully study the properties of parametric functions. Chief among these properties is the abstraction theorem [Reynolds 1983], which shows that any well-typed term must satisfy a property that can be derived uniformly from its type.
    [Show full text]
  • Refining Algebraic Data Types
    Refining algebraic data types Andrei Lapets ∗ December 29, 2006 Abstract Our purpose is to formalize two potential refinements of single-sorted algebraic data types – subalgebras and algebras which satisfy equivalence relations – by considering their categor- ical interpretation. We review the usual categorical formalization of single- and multi-sorted algebraic data types as initial algebras, and the correspondence between algebraic data types and endofunctors. We introduce a relation on endofunctors which corresponds to a subtyping relation on algebraic data types, and partially extend this relation to multi-sorted algebras. Finally, we explore how this relation can help us understand single-sorted algebraic data types which satisfy equivalence relations. 1 Algebraic Data Types as Initial Algebras In programming languages with type systems, a type usually represents a family of values, such as ∼ 32 the space of 32-bit integers: Int32 = Z/2 Z. Some programming languages (popular examples include Haskell [Je99] and ML [MTHM97]) have type systems which allow a programmer to define her own families of values by means of a recursion equation. For example, the data type representing inductively constructed binary trees with integer values at the leaves can be defined by the equation IntTree = Int32 ] (IntTree × IntTree). Typically, names must be assigned to the functions which allow the programmer to construct values of a data type. The diagram below illustrates this particular example: Int32 IntTree × IntTree MM kk MM kkk MM kkk Leaf MMM kkkNode M& ukkk IntTree The two functions Leaf : Int32 → IntTree and Node : IntTree × IntTree → IntTree can effectively be viewed as operators in an algebra which has no axioms.
    [Show full text]
  • Projections for Strictness Analysis
    Projections for Strictness Analysis Philip Wadler Programming Research Group, Oxford University and Programming Methodology Group, Chalmers University, GSteborg R.J.M. Hughes Department of Computer Science, University of Glasgow Abstract Contexts have been proposed as a means of performing strictness analysis on non-flat do- mains. Roughly speaking, a eontezt describes how much a sub-expression will be evaluated by the surrounding program. This paper shows how contexts can be represented using the notion of projection from domain theory. This is clearer than the previous explanation of contexts in terms of continuations. In addition, this paper describes finite dornaine of contexts over the non-flat list domain. This means that recursive context equations can be solved using standard fixpoint techniques, instead of the algebraic manipulation previously used. Praises of lazy functional languages have been widely sung, and so have some curses. One reason for praise is that laziness supports programming styles that are inconvenient or impossible otherwise [Joh87,Hug84,Wad85a]. One reason for cursing is that laziness hinders efficient implementation. Still, acceptable efficiency for lazy languages is at last being achieved. This is done by means of graph reduction [Pey87], as found in the G-machine [Aug84,Joh84] and the Ponder implementation [FW86], among others. The essential trick is to evaluate an expression immediately, when this is safe, rather than to construct a graph. Strictness analysis can reveal more places where this optimisation is safe. In the Ponder implementation, strictness analysis speeds up some programs by a factor of two or more. In addition, strictness analysis may enable other optimisations, such as destructive updating of arrays [HB85].
    [Show full text]