THE CALCULATOR SYNTAX

Similar documents
The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

Basic Structure of Denotational Definitions

CIS 500 Software Foundations Fall September 25

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

This book is licensed under a Creative Commons Attribution 3.0 License

Principles of Programming Languages 2017W, Functional Programming

Type Systems Winter Semester 2006

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Programming Languages Third Edition

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

6.001 Notes: Section 6.1

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

CS 6110 S14 Lecture 1 Introduction 24 January 2014

CS 11 Haskell track: lecture 1

Contents. Chapter 1 SPECIFYING SYNTAX 1

Introduction to lambda calculus Part 3

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Intro. Scheme Basics. scm> 5 5. scm>

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Introduction to Scheme

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Functional programming in LISP

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

Elixir, functional programming and the Lambda calculus.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

A SIMPLE IMPERATIVE LANGUAGE THE STORE FUNCTION NON-TERMINATING COMMANDS

Denotational semantics

A Small Interpreted Language

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Lecture 19: Functions, Types and Data Structures in Haskell

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

From the λ-calculus to Functional Programming Drew McDermott Posted

CS59-F16 Make-up Midterm

MetacircularScheme! (a variant of lisp)

Shell CSCE 314 TAMU. Haskell Functions

(Refer Slide Time: 01:01)

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

1 Introduction. 3 Syntax

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Binary Adders: Half Adders and Full Adders

Chapter 15. Functional Programming Languages

Lambda Calculus. Variables and Functions. cs3723 1

Local Specialization in Open-Axiom

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Introduction. chapter Functions

JME Language Reference Manual

Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Functional Programming. Pure Functional Languages

Lambda Calculus see notes on Lambda Calculus

CMSC 330: Organization of Programming Languages. Operational Semantics

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

An introduction introduction to functional functional programming programming using usin Haskell

COMP 382: Reasoning about algorithms

Functional programming with Common Lisp

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

Com S 541. Programming Languages I

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Fundamentals and lambda calculus

Scheme: Expressions & Procedures

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Functional Programming. Big Picture. Design of Programming Languages

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Functional Programming. Pure Functional Programming

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

10.6 Theoretical Foundations

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Lambda Calculus LC-1

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

CS164: Midterm I. Fall 2003

CS 314 Principles of Programming Languages

Standard Boolean Forms

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Lecture 4: Higher Order Functions

Documentation for LISP in BASIC

SMURF Language Reference Manual Serial MUsic Represented as Functions

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Lambda Calculus. Gunnar Gotshalks LC-1

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Transcription:

TH CALCULATOR After looking at the basics of the denotational method with the binary numerals we will take chmidt s example of a calculator to bring us one step closer to looking at the semantics of a real language. The calculator we shall study is essentially a two-function calculator that operates on natural numbers only (no negatives). It is unusual in that it has a conditional built into it, and it can handle numbers of any size. How it does this is a mystery, but we are only interested in semantics, not in engineering. It looks like this: < > O OFF 7 8 9 4 5 6 1 2 3 0 ( ) LATAWR + IF, TOTAL At the top is a display with two scroll buttons (for those long numbers). There are O and OFF buttons, the usual digit buttons, 0-9, and two operator buttons for addition and multiplication. The IF and the comma buttons work together as we shall see. TOTAL makes the calculator compute the result of the previously input expression. The LATAWR button recalls the value of the last calculation and inserts the value into the current one. YTAX A real calculator has all kinds of problems to solve when the user pushes the wrong buttons. For instance what happens the + button is pushed twice in a row? What happens if the parentheses are not matched? What happens if the IF form is not used correctly? We are going to ignore all of these complications, and instead only allow correctly formed calculations. We can do this because our first step in syntax is to describe correct forms, not the incorrect ones. The abstract syntax is then: P Program xpression-sequence xpression umeral igit P :: O :: TOTAL TOTAL OFF :: 1 + 2 1 2 ( ) IF 1, 2, 3 LATAWR :: :: 0 1 2 3 4 5 6 7 8 9 0

Clearly in this grammar the terminal symbols are given by what is on the button (O, LATAWR, etc.); P,, and are the main non-terminals. A typical program would be: O 1 + 2 TOTAL 3 LATAWR TOTAL OFF, which would show 3 and then 9 in the display. A program can thus contain many calculations; something we will have to show in the semantics. MATIC ALGBRA This calculator only deals with natural numbers, so clearly the domain at used in the binary numeral example is also used here. In addition to arithmetic, we will also need an equality operation for the IF test. The domain is then: omain: at zero, one, two, : at plus : at at at times : at at at equals : at at Tr We will need the Boolean domain for the last operation: omain: Tr true, false : Tr :Tr A A A, for any domain A. This last operation is the conditional form that we used to extend the lambda calculus. otice that we have written all the functions out in curried form, without the cross product. VALUATIO FUCTIO Following the method used for the binary numerals, each non-terminal in the grammar has a corresponding function, overloaded with each of the alternatives that define the non-terminal. We need to define the functionality of each before we start, so that each alternative equation is correctly defined. tarting with the syntactic category, Program, we need to decide what its function will return. We have three choices. We could have it return nothing, since the calculator has done its job when the OFF button is pushed, or we could have it return the last value calculated. either of these choices capture the whole story, so we will choose to have it return a list of all the values calculated. The denotation of our program will then be at, or a list of all the values calculated. o, we have: P: Program at The list is actually a domain with the following definition: omain: List A, where A is any domain nil : List hd : List A hd( a cons a nil) a tl : List List 1 2 1 tl( a cons a nil) a nil 1 2 2 cons : A List List cons( a, a cons a nil) a cons a nil 0 1 2 0 1

This is reminiscent of the language Lisp, or cheme, which, of course, have lambda calculus as their origin. We can define these operations in pure lambda calculus, but the expressions then become overly complicated, so we will omit them, just as we omit the definitions of arithmetic. hd returns the first item of the list, tl returns the list without the first item, and cons adds an item to the front of an existing list. nil is the empty list. We will only need cons and nil for our example, in order to form the list of values to be returned. The expression sequence,, actually produces this list of values, but it has a hidden parameter, since any one of the expression in the sequence can use the value from LATAWR. Thus we have: : xpression-sequence at at or, : xpression-sequence at at in curried form. The parentheses surround the output, while the rest are inputs. The expression function is easy now, since it only produces one value: : xpression at at Again, the extra parameters are for LATAWR. umerals are easy, and the function for digits is trivial: : umeral at : igit at Let us finish up the equations for the constants first: 0 zero 9 nine ( ten times ) plus xpressions can be simple: ( n) n ( ) ( ) ( n) The argument n is the value of the calculator s memory that can be used by pressing LATAWR: LATAWR ( n) n Arithmetic is now easy: 1+ 2 1 2 1 2 n 1 n times 2 ( n) ( n) plus ( n) ( ) ( ) ( n) The conditional form IF 1, 2, 3 will be given the semantics that if the first expression evaluates to zero, then the value of the expression will be the second expression, otherwise the value will be the third one. This is where the equals operations comes in: 1 2 3 ( 1 ) 2 3 IF,, ( n) ( n) equals zero ( n) ( n) We finish up with the expression sequence: n n n n cons TOTAL OFF ( n) ( n) TOTAL ( ) let ( ) in ( n )

otice how the result of calculating the first expression in the sequence is passed to the rest of the sequence, but is remembered as the head of the list. The final one in the sequence need not be passed on since the OFF button has been pushed. Finally the program itself: P O ( zero) where the memory of the calculator is initialized to zero for the first expression in the sequence. UMMARY OF MATIC FIITIO It is instructive to collect all the definitions together in one place to see the whole picture. We do this below: 1. yntax P Program xpression-sequence xpression umeral igit P :: O :: TOTAL TOTAL OFF :: 1 + 2 1 2 ( ) IF 1, 2, 3 LATAWR :: :: 0 1 2 3 4 5 6 7 8 9 0 2. emantic algebras omain: at zero, one, two, : at plus : at at at times : at at at equals : at at Tr omain: Tr true, false : Tr :Tr A A A,for any domain A. omain: List A, where A is any domain nil : List hd : List A tl : List List cons : A List List 3. Valuation functions

P: Program at P O ( zero) : xpression-sequence at at n n n n cons TOTAL OFF ( n) ( n) TOTAL ( ) let ( ) in ( n ) : xpression at at 1+ 2 1 2 1 2 n 1 n times 2 ( n) ( n) plus ( n) ( ) ( ) ( n) ( n) n ( ) ( ) ( n) 1 2 3 ( 1 ) 2 3 IF,, ( n) ( n) equals zero ( n) ( n) LATAWR ( n) n : umeral at : igit at ( ten times ) plus 0 9 XAMPL RIVATIO zero nine In order to show how the functions work, let us take the example program O 1 + 2 TOTAL 3 LATAWR TOTAL OFF and derive its denotation: P zero O 1 + 2 TOTAL 3 LATAWR TOTAL OFF 1 + 2 TOTAL 3 LATAWR TOTAL OFF ( ) let n 1 + 2 ( zero) in n cons 3 LATAWR TOTAL OFF ( n ) At this point we can work on the first expression to find what 1 + 2 ( zero) 1( zero) plus 2( zero) 1 plus 1 1 plus 1 one plus two We could reduce this to three at this point or leave it unevaluated. This is the difference between the normal order of reduction and the applicative order. Here we will use the applicative order, and pass three to the rest of the sequence: n is:

three cons ( 3 LATAWR ( )) 3 LATAWR TOTAL OFF ( three) three cons three Again, we work on the expression separately: 3 LATAWR ( three) three times 3 times three 3 times three 3( ) LATAWR( three) three times three nine The whole derivation thus reduces to: three cons nine which is the program s static denotation. If we had left the domain algebra expression unevaluated, we would have had: ( ) ( one plus two) cons three times ( one plus two) In a sense this is like the compiled version of the program. The syntax has disappeared and the result is a more primitive program running on the calculator s virtual machine, which is purely abstract in this case. otice that, denotationally speaking, three and one plus two cons three times one plus two are ( ) cons nine ( ) ( ) identical, since they both denote the same list. The difference is purely in the level of representation. LAMBA FORM FOR VALUATIO FUCTIO very valuation function has an alternative expansion of a syntactic category as its first argument. Where there are other arguments necessary, as in the functions for and, then we can write these using the lambda notation: λ TOTAL OFF λn. ( n) and TOTAL n.let n ( n) in n cons ( n ) 1+ 2 λn 1 n plus 2 1 2 λ 1 2. ( ) ( n) n. ( n) times ( n) ometimes the lambda version is easier to read than the other, but it is a choice we can make if you we want to. Where the argument is present on both sides, it is technically possible to omit it. e.g. ( ) This is possible because of the extensionality of functions. still has functionality xpression at at, so both side of the equation have functionality at at. Thus they both need at to produce their final result.