A Study of Syntactic and Semantic Artifacts and Its Application to Lambda Definability, Strong Normalization, and Weak Normalization in the Presence of State
Total Page:16
File Type:pdf, Size:1020Kb
BRICS RS-08-3 J. Munk: A Study of Syntactic and Semantic Artifacts and its Application to Lambda Definability, Strong Normalization BRICS Basic Research in Computer Science A Study of Syntactic and Semantic Artifacts and its Application to Lambda Definability, Strong Normalization, and Weak Normalization in the Presence of State Johan Munk BRICS Report Series RS-08-3 ISSN 0909-0878 April 2008 , Copyright c 2008, Johan Munk. BRICS, Department of Computer Science University of Aarhus. All rights reserved. Reproduction of all or part of this work is permitted for educational or research use on condition that this copyright notice is included in any copy. See back inner page for a list of recent BRICS Report Series publications. Copies may be obtained by contacting: BRICS Department of Computer Science University of Aarhus IT-parken, Aabogade 34 DK–8200 Aarhus N Denmark Telephone: +45 8942 9300 Telefax: +45 8942 5601 Internet: [email protected] BRICS publications are in general accessible through the World Wide Web and anonymous FTP through these URLs: http://www.brics.dk ftp://ftp.brics.dk This document in subdirectory RS/08/3/ A Study of Syntactic and Semantic Artifacts and its Application to Lambda Definability, Strong Normalization, and Weak Normalization in the Presence of State Johan Munk1 (Advisor: Olivier Danvy) May 4, 2007. Revised August 22, 2007 1IT-parken, Aabogade 34, DK-8200 Aarhus N, Denmark. Email: <[email protected]> Student id: 20000345 Abstract Church’s lambda-calculus underlies the syntax (i.e., the form) and the semantics (i.e., the meaning) of functional programs. This thesis is dedicated to studying man-made constructs (i.e., artifacts) in the lambda calculus. For example, one puts the expressive power of the lambda calculus to the test in the area of lambda definability. In this area, we present a course-of-value representation bridging Church numerals and Scott numerals. We then turn to weak and strong normalization using Danvy et al.’s syntactic and functional correspon- dences. We give a new account of Felleisen and Hieb’s syntactic theory of state, and of abstract machines for strong normalization due to Curien, Crégut, Lescanne, and Kluge. Contents I λ-calculi and programming languages 4 1 The λ-calculus 5 1.1 λ-terms ......................................... 5 1.1.1 Conventions .................................. 6 1.1.2 Free variables and bound variables . ................... 7 1.1.3 λ-terms modulo bound variable names .................. 7 1.2 Reductions and normal forms ............................ 8 1.3 One-step reduction and equality . ........................ 8 1.3.1 β-equivalence and convertibility . ................... 10 1.4 Uniqueness of normal forms ............................. 10 1.5 Reduction strategies . .............................. 11 1.5.1 Normal-order reduction . ........................ 11 1.5.2 Standardization and normalization . ................... 12 1.5.3 Reducing to weak head normal forms ................... 13 1.6 The λ-calculus with de Bruijn indices ........................ 14 1.6.1 Correspondence with terms using named variables ........... 15 1.6.2 β-contraction on de Bruijn-indexed λ-terms . ............. 16 1.6.3 Defining the λ-calculus for de Bruijn-indexed λ-terms . ....... 17 1.7 The λ-calculus defined as a proof system . ................... 17 1.8 Summary ........................................ 18 2 Definability in the λ-calculus 19 2.1 Church numerals ................................... 20 2.2 Representing structured elements . ........................ 21 2.2.1 Ordered pairs . .............................. 21 2.2.2 Kleene’s predecessor function and the corresponding subtraction func- tion ....................................... 22 2.2.3 Boolean values and functions on booleans . ............. 23 2.2.4 The factorial function ............................ 23 2.2.5 Representing lists . .............................. 24 2.3 Dynamic programming . .............................. 25 2.3.1 A longest common subsequence . ................... 26 2.3.2 Length of a longest common subsequence ................. 27 2.4 Scott numerals . ................................... 29 2.4.1 Scott numerals are selectors in pair-represented lists ........... 29 2.4.2 Lists and streams . .............................. 30 i 2.4.3 Functions on Scott numerals ........................ 30 2.5 A correspondence between Church numerals and Scott numerals ....... 31 2.6 An alternative definition of the subtraction function for Church numerals . 31 2.6.1 Generalizing Kleene’s predecessor function . ............. 31 2.6.2 A course-of-value representation of Church numerals . ....... 32 2.6.3 Lists as generalized pairs . ........................ 32 2.6.4 The alternative definition of the subtraction function . ....... 33 2.7 The quotient function and the remainder function . ............. 33 2.8 Extending the λ-calculus with literals and a corresponding primitive succes- sor function . ................................... 35 2.9 Summary ........................................ 36 3 Other λ-calculi 38 3.1 The λβη-calculus ................................... 38 3.2 Explicit substitutions . .............................. 39 3.3 The λv-calculus . ................................... 39 3.3.1 Standard reduction .............................. 41 3.3.2 Normalization . .............................. 42 3.3.3 Reducing to weak head normal forms ................... 42 3.4 Summary ........................................ 43 4 Programming languages 44 4.1 Machine-level and high-level programming languages ............. 44 4.2 Paradigms ....................................... 45 4.3 Defining a programming language . ........................ 45 4.4 Syntax ......................................... 46 4.5 Semantics ....................................... 46 4.5.1 Denotational semantics . ........................ 47 4.5.2 Abstract machines .............................. 49 4.5.3 Reduction semantics ............................. 53 4.6 Summary ........................................ 55 5 λ-calculi, programming languages, and semantic artifacts 57 5.1 Call by value, call by name, and the λ-calculus .................. 57 5.1.1 Call by value . .............................. 58 5.1.2 Call by name . .............................. 59 5.2 A syntactic correspondence ............................. 59 5.2.1 The λρ^-calculus . .............................. 60 5.2.2 Correspondence with the λ-calculus . ................... 61 5.2.3 A normal-order reduction semantics for the λρ^-calculus . ....... 63 5.2.4 The reduction semantics with explicit decomposition . ....... 63 5.2.5 Refocusing ................................... 65 5.2.6 Obtaining an abstract machine ....................... 65 5.3 A functional correspondence ............................ 67 5.3.1 A definitional interpreter . ........................ 68 5.3.2 Closure conversion .............................. 68 5.3.3 Continuation-passing-style/direct-style transformations . ....... 69 ii 5.3.4 Defunctionalization/refunctionalization ................. 69 5.3.5 Relating interpreters and abstract machines . ............. 69 5.4 Summary ........................................ 70 6 Including imperative constructs 71 6.1 State variables and assignments . ........................ 71 6.2 Felleisen and Hieb’s revised calculus of state ................... 72 6.2.1 The term language .............................. 72 6.2.2 Conventions .................................. 72 6.2.3 The ρ-application . .............................. 72 6.2.4 Notions of reduction ............................. 73 6.2.5 One-step reduction and equality . ................... 73 6.2.6 The Church-Rosser property ........................ 74 6.2.7 Standard reduction .............................. 74 6.3 An applicative-order reduction semantics with explicit substitution including the imperative constructs .............................. 75 6.3.1 The term language .............................. 75 6.3.2 The notion of reduction . ........................ 75 6.3.3 A one-step reduction function ....................... 76 6.3.4 Evaluation ................................... 77 6.4 Derivation of a corresponding abstract machine ................. 78 6.4.1 Introduction of explicit decomposition and plugging . ....... 78 6.4.2 Obtaining a syntactically corresponding abstract machine ....... 78 6.5 Summary ........................................ 79 II Strong normalization 81 7 Strong normalization with actual substitution 82 7.1 Obtaining a reduction semantics . ........................ 82 7.2 Deriving a corresponding abstract machine . ................... 83 7.3 Summary ........................................ 84 8 Strong normalization with explicit substitutions 85 8.1 Strong normalization via the λs^-calculus . ................... 85 8.1.1 Motivation ................................... 85 8.1.2 The λs^-calculus . .............................. 86 8.1.3 A normal-order reduction semantics for the λs^-calculus . ....... 89 8.1.4 Reduction-free strong normalization in the λs^-calculus . ....... 91 ^ 8.2 Strong normalization via the λs^-calculus . ................... 92 ^ 8.2.1 The λs^-calculus . .............................. 93 8.2.2 Obtaining an efficient abstract machine .................. 93 8.3 Summary ........................................ 97 iii 9 Strong normalization starting from Lescanne’s normalizer 98 9.1 Lescanne’s specification made deterministic . ................... 98 9.2 Obtaining a corresponding abstract machine ..................