The RPAL Functional Language Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 12.

Slides:



Advertisements
Similar presentations
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Advertisements

Programming Languages and Paradigms The C Programming Language.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Functional Programming Element of Functional Programming.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Programming Language Principles Lecture 26 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Denotational Semantics.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
Standardizing RPAL AST’s Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 10.
Writing RPAL Programs Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 13.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CMSC 330: Organization of Programming Languages
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
CMSC 330: Organization of Programming Languages Operational Semantics.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Tuples Module 11.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
© 2016 Pearson Education, Ltd. All rights reserved.
Java Primer 1: Types, Classes and Operators
Introduction to Scheme
ML Again ( Chapter 7) Patterns Local variable definitions
CS 326 Programming Languages, Concepts and Implementation
A lightening tour in 45 minutes
Introduction to RPAL Module 10.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
RPAL Function definitions
Programming Language Principles
CS190/295 Programming in Python for Life Sciences: Lecture 6
The Metacircular Evaluator
CSE 341 Section 5 Winter 2018.
CSCE 314: Programming Languages Dr. Dylan Shell
6.001 SICP Variations on a Scheme
The RPAL Functional Language
Announcements Quiz 5 HW6 due October 23
Recursion and Rpal’s synTax
Recursion and Fixed-Point Theory
Programming Language Principles
Javascript Chapter 19 and 20 5/3/2019.
Presentation transcript:

The RPAL Functional Language Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 12

RPAL is a subset of PAL PAL: ‘Pedagogic Algorithmic Language. Developed by J. Wozencraft and A. Evans at MIT, early 70's. Intellectual ancestor of Scheme, designed by Guy Steele, mid-70's. Steele (Fellow at Sun Microsystems) is also one of principal contributors to Java. Seach Google: 'Guy Steele Scheme' for some background and perspective.

Why study RPAL? Unknown language PARADIGM SHIFT ! Generic, “plain vanilla” functional language. Intellectual ancestor of Scheme, ML, etc.

Three Versions of PAL: RPAL, LPAL, and JPAL We will only cover RPAL. R in RPAL stands for right-reference (as in C). An RPAL program is simply an expression. No notion of assignment, or even memory. No loops, only recursion.

Two Notions: Function Definition and Function Application RPAL is a functional language. Every RPAL program is an expression. Running an RPAL program consists of evaluating the expression. The most important construct in RPAL is the function.

Two Notions: Function Definition and Function Application (cont’d) Functions in RPAL are first-class objects. Programmer can do anything with a function: Send a function as a parameter to a function Return a function from a function.

Sample RPAL Programs: 1)let X=3 in Print(X,X**2) // Prints (3,9) 2)let Abs N = N ls 0 -> -N | N in Print(Abs -3) // Prints 3 The actual value of each program is dummy (the value of Print).

Preview of Lambda Calculus Program 1 is equivalent to: (fn X. Print(X,X**2)) 3 Program 2 is equivalent to: let Abs = fn N. N ls 0 -> -N | N in Print(Abs -3) which is equivalent to: (fn Abs. Print(Abs 3)) (fn N. N ls 0 -> -N | N)

RPAL constructs Operators Function definitions Constant definitions Conditional expressions Function application Recursion

RPAL Is Dynamically Typed The type of an expression is determined at run - time. Example: let Funny = (B -> 1 | 'January')

RPAL Has Six Data Types: Integer Truthvalue (boolean) String Tuple Function Dummy

Type Identification Functions All are intrinsic functions. Applied to a value, return true or false: Isinteger x Istruthvalue x Isstring x Istuple x Isfunction x Isdummy x

Other Operations Truthvalue operations: or, &, not, eq, ne Integer operations: +, -, *, /, **, eq, ne, ls,, le, = String operations: eq, ne, Stem S, Stern S, Conc S T

Examples let Name = 'Dolly' in Print ('Hello', Name) let Inc x = x + 1 in Print (Inc x) let Inc = fn x. x + 1 in Print (Inc x) Print (Inc 7) where Inc x = x + 1

Nesting Definitions Nested scopes are as expected. let X = 3 in let Sqr X = X**2 in Print (X, Sqr X, X * Sqr X, Sqr X ** 2) Scope of X=3 starts here Scope of Sqr starts here

Nesting Definitions (cont’d) ( Print (X, Sqr X, X * Sqr X, Sqr X ** 2) where Sqr X = X**2 ) where X = 3 Parentheses required ! Otherwise Sqr X = X**2 where X=3

Simultaneous Definitions let X=3 and Y=5 in Print(X+Y) Note the and keyword: not a boolean operator (for that we have & ). Both definitions come into scope in the Expression Print(X+Y). Different from let X=3 in let Y=5 in Print(X+Y)

Function Definitions Within One Another The scope of a 'within' definition is another definition, not an expression. Example: let c=3 within f x = x + c in Print(f 3)

Functions In RPAL, functions are first-class objects. Functions can be named, passed as parameters, returned from functions, selected using conditional, stored in tuples, etc. Treated like 'values' (which they are, as we shall see).

Every function in RPAL has: A bound variable (its parameter) A body (an expression) An environment (later) For example: fn X. X -X | X

Functions (cont’d) Naming a Function let Abs = fn X. X ls 0 -> -X | X in Print (Abs(3)) Passing a function as a parameter: let f g = g 3 in let h x = x + 1 in Print(f h) Returning a function from a function: let f x = fn y. x+y in Print (f 3 2)

Functions (cont’d) Selecting a function using conditional: let B=true in let f = B -> (fn y.y+1) | (fn y.y+2) in Print (f 3) Storing a function in a tuple: let T=((fn x.x+1),(fn x.x+2)) in Print (T 1 3, T 2 3)

Functions (cont’d) N-ary functions are legal, using tuples: let Add (x,y) = x+y in Print (Add (3,4) )

Function Application By juxtaposition, i.e. (fn x.B) A. Two orders of evaluation: 1.PL order: evaluate A first, then B with x replaced with the value of A. 2.Normal order, postpone evaluating A. Evaluate B with x literally replaced with A. RPAL uses PL order.

Example: Normal order vs. PL order let f x y = x in Print(f 3 (1/0)) Normal Order: output is 3. PL Order: division by zero error.

Recursion Only way to achieve repetition. No loops in RPAL. Use the rec keyword. Without rec, the function is not recursive.

Factorial let rec Fact n = n eq 1 -> 1 | n * Fact (n-1) in Print (Fact 3) Without rec, the scope of Fact would be the last line ONLY.

Example: let rec length S = S eq '' -> 0 | 1 + length (Stern S) in Print ( length('1,2,3'), length (''), length('abc') ) Typical layout: define functions, and print test cases.

Example: let Is_perfect_Square N = Has_sqrt_ge (N,1) where rec Has_sqrt_ge (N,R) = R**2 gr N -> false | R**2 eq N -> true | Has_sqrt_ge (N,R+1) in Print (Is_perfect_Square 4, Is_perfect_Square 64, Is_perfect_Square 3)

Tuples The only data structure in RPAL. Any length, any nesting depth. Empty tuple (length zero) is nil. Example: let Bdate = ('June', 21, '19XX') in let Me = ('Bermudez','Manuel', Bdate, 42) in Print (Me)

Arrays Tuples in general are heterogeneous. Array is special case of tuple: a homogeneous tuple (all elements of the same type). Example: let I=2 in let A=(1,I,I**2,I**3,I**4,I**5) in Print (A)

Multi-Dimensional Arrays: Tuples of Tuples let A=(1,2) and B=(3,4) and C=(5,6) in let T=(A,B,C) in Print(T) Triangular Array: let A = nil aug 1 and B=(2,3) and C=(4,5,6) in let T=(A,B,C) in Print(T)

Notes on Tuples () is NOT the empty tuple. (3) is NOT a singleton tuple. nil is the empty tuple. The singleton tuple is built using aug : nil aug 3. Build tuples using the comma, e.g. (1,2,3)

Selecting an Element From a Tuple Apply the tuple to an integer, as if it were a function. Example: let T = ( 1, (2,3), ('a', 4)) in Print (T 2) Output: (2,3) Example: let T=('a','b',true,3) in Print(T 3,T 2) Output: (true, b)

Extending Tuples Use aug (augment) operation. Additional element added to RIGHT side of tuple. NEW tuple is built. NOT an assignment to a tuple. In general, ALL objects in RPAL are IMMUTABLE. Example: let T = (2,3) in let A = T aug 4 in Print (A) // Output: (2,3,4)

Summary of Tuple Operations E1,E2,...,En tuple construction (tau) T aug E tuple extension (augmentation) Order T number of elements in T Null T true if T is nil, false otherwise

Operator Allows infix use of a function. Example: let Add x y = x + y in Print 4) Equivalent to: let Add x y = x + y in Print (Add (Add 2 3) 4)

Operator Precedence in RPAL, from lowest to highest let fn where tau aug -> or & not gr ge le ls eq ne + - * / function application ()

Sample RPAL Programs Example 1: let Sum_list L = Partial_sum (L, Order L) where rec Partial_sum (L,N) = N eq 0 -> 0 | L N + Partial_sum(L,N-1) in Print ( Sum_list (2,3,4,5) )

Sample RPAL Programs (cont’d) Example 2: let Vector_sum(A,B) = Partial_sum (A,B,Order A) where rec Partial_sum (A,B,N) = N eq 0 -> nil | ( Partial_sum(A,B,N-1) aug (A N + B N) ) // parentheses required in Print (Vector_sum((1,2,3),(4,5,6)))

Error Conditions Error Location of error A is not a tuple Evaluation of Order A B is not a tuple Indexing of B N A shorter than B Last part of B is ignored B shorter than A Indexing B N Elements not integers Addition

Data Verification let Vector_sum(A,B) = not (Istuple A) -> 'Error' | not (Istuple B) -> 'Error' | Order A ne Order B -> 'Error' | Partial_sum (A,B,Order A) where... in Print(Vector_sum((1,2),(4,5,6))) To check tuple elements, need a separate recursive function

RPAL’s SYNTAX RPAL’s lexical grammar. RPAL’s phrase-structure grammar.

The RPAL Functional Language Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 12