Functional Languages. Hwansoo Han

Similar documents
Chapter 11 :: Functional Languages

Programming Language Pragmatics

4/19/2018. Chapter 11 :: Functional Languages

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

Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

LECTURE 16. Functional Programming

Organization of Programming Languages CS3200/5200N. Lecture 11

Functional Programming. Big Picture. Design of Programming Languages

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Functional Programming. Pure Functional Languages

Functional Programming

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functional Programming. Pure Functional Languages

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Functional Programming. Pure Functional Programming

FP Foundations, Scheme

Programming Languages

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

CSCC24 Functional Programming Scheme Part 2

Functional Programming Languages (FPL)

Functional Programming

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Functional Languages. Programming Language Pragmatics. DOI: /B Copyright 2009 by Elsevier Inc. All rights reserved.

CS 360 Programming Languages Interpreters

CS 242. Fundamentals. Reading: See last slide

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CSC 533: Programming Languages. Spring 2015

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

Haskell Review COMP360

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

1.3. Conditional expressions To express case distinctions like

CSc 520 Principles of Programming Languages

Chapter 15. Functional Programming Languages

An Introduction to Scheme

CS 11 Haskell track: lecture 1

CS 314 Principles of Programming Languages

Imperative languages

Programming Languages Third Edition

Functional Programming

Urmas Tamm Tartu 2013

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Imperative, OO and Functional Languages A C program is

Lambda Calculus see notes on Lambda Calculus

Functional Programming Lecture 1: Introduction

Documentation for LISP in BASIC

SOFTWARE ARCHITECTURE 6. LISP

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

A Brief Introduction to Scheme (II)

Lambda Calculus and Computation

Lecture 5: The Halting Problem. Michael Beeson

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

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Introduction to the Lambda Calculus. Chris Lomont

CSCE 314 Programming Languages

Principles of Programming Languages 2017W, Functional Programming

Introduction to Scheme

An Explicit-Continuation Metacircular Evaluator

CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages

Programming Systems in Artificial Intelligence Functional Programming

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

Introduction to Functional Programming in Haskell 1 / 56

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Chapter 15. Functional Programming Languages

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Chapter 1. Fundamentals of Higher Order Programming

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

Lambda Calculus.

Lambda the Ultimate. Corky Cartwright Vivek Sarkar Department of Computer Science Rice University

VU Semantik von Programmiersprachen

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

From the λ-calculus to Functional Programming Drew McDermott Posted

CSC324 Functional Programming Efficiency Issues, Parameter Lists

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Introduction. chapter Functions

CMSC330. Objects, Functional Programming, and lambda calculus

A Small Interpreted Language

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

An introduction to Scheme

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Scheme Quick Reference

CSCI337 Organisation of Programming Languages LISP

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Project 2: Scheme Interpreter

Functional Programming Languages (FPL)

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Architecture of LISP Machines. Kshitij Sudan March 6, 2008

User-defined Functions. Conditional Expressions in Scheme

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

John McCarthy IBM 704

CS 314 Principles of Programming Languages

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

Lambda Calculus. Gunnar Gotshalks LC-1

Programming with Math and Logic

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Transcription:

Functional Languages Hwansoo Han

Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm or effective procedure, Based on automata, symbolic manipulation, recursive function definitions, and combinatorics A conjecture known as Church-Turing thesis If an algorithm (a process that terminates) exists, there is an equivalent Turing machine, -calculus, and recursion Equivalent computability among models 2

Turing Machine Turing s model of computing Pushdown automaton + an unbounded storage tape Turing machine computes in an imperative way By changing the values in cells of its tape like variables just as a high level imperative program computes by changing the values of variables 3 by Tom Dunne

Lambda Calculus Church s model of computing Notion of parameterized expressions With each parameter introduced by an occurrence of the letter - hence the notation s name. -calculus was the inspiration for functional programming Compute by substituting parameters into expressions, just as one computes in a high level functional program by passing arguments to functions Functional languages such as Lisp, Scheme, FP, ML, Miranda, and Haskell are attempts to realize Church's -calculus in practical form as a programming language 4

Programs as Mathematical Proofs To prove a proposition, mathematicians use a proof A program is a constructive proof for any given inputs Constructive proof one that shows how to obtain a mathematical object with some desired property Non-constructive proof one that merely shows that such an object must exist by contradiction, counting, reduction to known theorem whose proof is also non-constructive Logic programming is also tied to the notion of constructive proofs, but at a more abstract level: Logic programmer writes a set of axioms that allow the computer to discover a constructive proof for each particular set of inputs 5

Functional Programming Concepts Do everything by composing functions, which have No notion of internal state, and thus no side effects Features, many of which are often missing in imperative languages First-class function values and high-order functions First-class function can be a parameter, a return value, assigned to a variable High-order function takes a function as a parameter, and return a function as a result Extensive polymorphism List types and operators Structured function returns Fully general aggregates for structured objects Garbage collection 6

Recursion in Functional Languages How get anything done in a functional language? Recursion (especially tail recursion) takes the place of iteration In general, you can get the effect of a series of assignments x := 0... x := expr1... x := expr2... from f3(f2(f1(0))), where each f expects the value of x as an argument, f1 returns expr1, and f2 returns expr2 7

Recursion in FL (cont d) Recursion even does a nifty job of replacing a looping 8 x = 0; i = 1; j = 100; while (i < j) { x = x + i*j; i = i + 1; j = j - 1 } return x becomes f(0,1,100), where f(x, i, j) == if i < j then f (x+i*j, i+1, j-1) else x

Scheme a Lisp descendant Scheme is a particularly elegant Lisp Interpreter runs a read-eval-print loop Things typed are evaluated recursively once Anything in parentheses is a function call (unless quoted) Parentheses are not just grouping Adding a level of parentheses changes meaning (+ 3 4) 7 7 7 (load my_scheme_prog ) evaluate from the loaded file ((+ 3 4)) ERROR eval: 7 is not a procedure 9

Scheme Values Boolean values: #t and #f Numbers Quoting (+ 3 4) 7 ; function (quote (+ 3 4)) (+ 3 4) ; quoted ( ) is not function (+ 3 4) (+ 3 4) ; is the same as quote Conditional Expression (if (< 2 3) 4 5) 4 (cond ((< 3 2) 1) ((< 4 3) 2) (else 3)) 3 10

Scheme (cont d) Lambda expressions - function (lambda (a b) (if (< a b) a b)) Nested scopes with let and letrec (for recursion) (let ( list_of_pairs ) the_rest ) ; list_of_pairs: var for the_rest (let ((a 3) (b 4) (square (lambda (x) (* x x))) (plus +)) (sqrt (plus (square a) (square b)))) 5.0 ; sqrt is a built-in func (let ((a 3)) ; scope of binding produced by let is for the 2 nd arg only (let ((a 4) (b a)) ; a takes the value of outer a names are visible at the end (+ a b))) 7 11

Scheme (cont d) Equivalence (if (= n 1) 1 3) (eq? a b) (eqv? a b) ; a and b refer to the same object? ; a and b are semantically equivalent? (in different locations) (equal? a b) ; a and b are the same recursive structure? List operators (car (2 3 4)) 2 (cdr (2 3 4)) (3 4) (cdr (2)) () (cons 2 (3 4)) (2 3 4) (cons 2 3) (2. 3) ; an improper list Type predicates (boolean? x) (char? x) (string? x) (symbol? x) (number? x) (pair? x) (list? x) (complex? x) (rational? x) (real? x) (integer? x)... 12

Scheme (cont d) Imperative stuff vector type To access an arbitrary element in a sequence Indexed by an integer, like an array, but elements can be heterogeneous types Assignments: (set! x 3) (set-car! l (c d)) (set-cdr! l (e)) Sequencing: (begin (display hi ) (display mom )) Iteration (for-each) I/O (read, display) 13

Evaluation Order Revisited Applicative order (as in Scheme) What you're used to in imperative languages Usually faster Normal order Like call-by-name don't evaluate arg until you need it Sometimes faster applicative order normal order (double (* 3 4)) (double (* 3 4)) (double 12) (+ (* 3 4) (* 3 4)) (+ 12 12) (+ 12 (* 3 4)) 24 (+ 12 12) 24 14

Strict vs. Non-strict Functions Strict function vs. non-strict function A strict function requires all arguments to be well-defined (successfully evaluated), so applicative order can be used Scheme, ML A non-strict function does not require all arguments to be well-defined (some may fail to be evaluated at run-time); it requires normal-order evaluation (lazy evaluation) Miranda, Haskel 15

Higher-Order Functions Higher-order functions Map Takes a function as argument or return a function as a result (map * (2 4 6) (3 5 7)) (6 20 42) Reduce (folding) 16 (define fold (lambda (f i l) (if (null? l) i ; i is commonly the identity element for f (f (car l) (fold f i (cdr l)))))) (define total (lambda (l) (fold + 0 l))) (total ( 1 2 3 4 5)) 15 (define total-all (lambda (l) (map total l))) (total-all ((1 2 3 4 5) (2 4 6 8 10) (3 6 9 12 15))) (15 30 45)

Functional Programming: Pros Advantages Lack of side effects makes programs easier to understand Lack of explicit evaluation order (in some languages) offers possibility of parallel evaluation (e.g. MultiLisp) Lack of side effects and explicit evaluation order simplifies some things for a compiler Programs are often surprisingly short Language can be extremely small and yet powerful 17

Functional Programming: Cons Problems Difficult but not impossible! to implement efficiently on von Neumann machines Lots of copying of data through parameters Apparent need to create a whole new array in order to change one element Heavy use of pointers (space/time and locality problem) Frequent procedure calls Heavy space use for recursion Requires garbage collection Requires a different mode of thinking by the programmer Difficult to integrate I/O into purely functional model 18

Guile Scheme interpreter in Linux Install guile from the Interpreters 19 $ guile guile> (+ 3 4) 7 guile> (map * (1 2 3 4) (1 2 3 4)) (1 4 9 16) guile> (load my_prog.scm ) ; load insert func. from file guile> (insert a (b c d)) guile> (quit) $