functional programming languages liam o connor
play

Functional Programming Languages Liam OConnor CSE, UNSW (and - PowerPoint PPT Presentation

The Functional Paradigm MinHS Functional Programming Languages Liam OConnor CSE, UNSW (and data61) Term3 2019 1 The Functional Paradigm MinHS Functional Programming Many languages have been called functional over the years: Lisp (


  1. The Functional Paradigm MinHS Functional Programming Languages Liam O’Connor CSE, UNSW (and data61) Term3 2019 1

  2. The Functional Paradigm MinHS Functional Programming Many languages have been called functional over the years: Lisp ( define (max-of lst ) (cond [(= (length lst ) 1) (first lst )] [else (max (first lst ) (max-of (rest lst )))])) 2

  3. The Functional Paradigm MinHS Functional Programming Many languages have been called functional over the years: Haskell maxOf :: [ Int ] → Int maxOf = foldr1 max Lisp ( define (max-of lst ) (cond [(= (length lst ) 1) (first lst )] [else (max (first lst ) (max-of (rest lst )))])) 3

  4. The Functional Paradigm MinHS Functional Programming Many languages have been called functional over the years: Haskell maxOf :: [ Int ] → Int maxOf = foldr1 max Lisp ( define (max-of lst ) (cond [(= (length lst ) 1) (first lst )] [else (max (first lst ) (max-of (rest lst )))])) JavaScript? function maxOf( arr ) { var max = arr . reduce( function ( a , b ) { return Math . max( a , b ); } ); } 4

  5. The Functional Paradigm MinHS Functional Programming Many languages have been called functional over the years: Haskell maxOf :: [ Int ] → Int maxOf = foldr1 max Lisp ( define (max-of lst ) (cond [(= (length lst ) 1) (first lst )] [else (max (first lst ) (max-of (rest lst )))])) JavaScript? function maxOf( arr ) { var max = arr . reduce( function ( a , b ) { What do they return Math . max( a , b ); have in common? } ); } 5

  6. The Functional Paradigm MinHS Definitions Unlike imperative languages, functional programming languages are not very crisply defined. Attempt at a Definition A functional programming language is a programming language derived from or inspired by the λ -calculus, or derived from or inspired by another functional programming language. The result? If it has λ in it, you can call it functional. 6

  7. The Functional Paradigm MinHS Definitions Unlike imperative languages, functional programming languages are not very crisply defined. Attempt at a Definition A functional programming language is a programming language derived from or inspired by the λ -calculus, or derived from or inspired by another functional programming language. The result? If it has λ in it, you can call it functional. In this course, we’ll consider purely functional languages, which have a much better definition. 7

  8. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Garbage Collection?

  9. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Garbage Collection? Lisp, 1958

  10. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Garbage Collection? Lisp, 1958 Functions as Values?

  11. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Garbage Collection? Lisp, 1958 Functions as Values? Lisp, 1958

  12. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Garbage Collection? Lisp, 1958 Polymorphism? Functions as Values? Lisp, 1958

  13. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Garbage Collection? Lisp, 1958 Polymorphism? ML, 1973 Functions as Values? Lisp, 1958

  14. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? Garbage Collection? Lisp, 1958 Polymorphism? ML, 1973 Functions as Values? Lisp, 1958

  15. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? ML, 1973 Garbage Collection? Lisp, 1958 Polymorphism? ML, 1973 Functions as Values? Lisp, 1958

  16. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? ML, 1973 Garbage Collection? Lisp, 1958 Metaprogramming? Polymorphism? ML, 1973 Functions as Values? Lisp, 1958

  17. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? ML, 1973 Garbage Collection? Lisp, 1958 Metaprogramming? Lisp, 1958 Polymorphism? ML, 1973 Functions as Values? Lisp, 1958

  18. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? ML, 1973 Garbage Collection? Lisp, 1958 Metaprogramming? Lisp, 1958 Polymorphism? ML, 1973 Lazy Evaluation? Functions as Values? Lisp, 1958

  19. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? ML, 1973 Garbage Collection? Lisp, 1958 Metaprogramming? Lisp, 1958 Polymorphism? ML, 1973 Lazy Evaluation? Functions as Values? Miranda, 1985 Lisp, 1958

  20. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? Monads? ML, 1973 Garbage Collection? Lisp, 1958 Metaprogramming? Lisp, 1958 Polymorphism? ML, 1973 Lazy Evaluation? Functions as Values? Miranda, 1985 Lisp, 1958

  21. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? Monads? ML, 1973 Haskell, 1991 Garbage Collection? Lisp, 1958 Metaprogramming? Lisp, 1958 Polymorphism? ML, 1973 Lazy Evaluation? Functions as Values? Miranda, 1985 Lisp, 1958

  22. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? Monads? ML, 1973 Haskell, 1991 Garbage Collection? Lisp, 1958 Software Transactional Memory? Metaprogramming? Lisp, 1958 Polymorphism? ML, 1973 Lazy Evaluation? Functions as Values? Miranda, 1985 Lisp, 1958

  23. The Functional Paradigm MinHS Why Study FP Languages? Think of a major innovation in the area of programming languages. Type Inference? Monads? ML, 1973 Haskell, 1991 Garbage Collection? Lisp, 1958 Software Transactional Memory? Metaprogramming? GHC Haskell, 2005 Lisp, 1958 Polymorphism? ML, 1973 Lazy Evaluation? Functions as Values? Miranda, 1985 Lisp, 1958 23

  24. The Functional Paradigm MinHS Purely Functional Programming Languages The term purely functional has a very crisp definition. Definition A programming language is purely functional if β -reduction (or evaluation in general) is actually a confluence. In other words, functions have to be mathematical functions, and free of side effects . 24

  25. The Functional Paradigm MinHS Purely Functional Programming Languages The term purely functional has a very crisp definition. Definition A programming language is purely functional if β -reduction (or evaluation in general) is actually a confluence. In other words, functions have to be mathematical functions, and free of side effects . Consider what would happen if we allowed effects in a functional language: count = 0; f x = { count := count + x ; return count } ; m = ( λ y . y + y ) ( f 3) If we evaluate f 3 first, we will get m = 6, but if we β -reduce m first, we will get m = 9. ⇒ not confluent. 25

  26. The Functional Paradigm MinHS Making a Functional Language We’re going to make a language called MinHS . Three types of values: integers, booleans, and functions. 1 26

  27. The Functional Paradigm MinHS Making a Functional Language We’re going to make a language called MinHS . Three types of values: integers, booleans, and functions. 1 Static type checking (not inference) 2 27

  28. The Functional Paradigm MinHS Making a Functional Language We’re going to make a language called MinHS . Three types of values: integers, booleans, and functions. 1 Static type checking (not inference) 2 Purely functional (no effects) 3 28

  29. The Functional Paradigm MinHS Making a Functional Language We’re going to make a language called MinHS . Three types of values: integers, booleans, and functions. 1 Static type checking (not inference) 2 Purely functional (no effects) 3 Call-by-value (strict evaluation) 4 29

  30. The Functional Paradigm MinHS Making a Functional Language We’re going to make a language called MinHS . Three types of values: integers, booleans, and functions. 1 Static type checking (not inference) 2 Purely functional (no effects) 3 Call-by-value (strict evaluation) 4 In your Assignment 1, you will be implementing an evaluator for a slightly less minimal dialect of MinHS. 30

  31. The Functional Paradigm MinHS Syntax Integers n ::= · · · Identifiers f , x ::= · · · Literals b ::= True | False Types τ ::= Bool | Int | τ 1 → τ 2 Infix Operators ::= * | + | == | · · · ⊛ Expressions e ::= x | n | b | ( e ) | e 1 ⊛ e 2 | if e 1 then e 2 else e 3 31

  32. The Functional Paradigm MinHS Syntax Integers n ::= · · · Identifiers f , x ::= · · · Literals b ::= True | False Types τ ::= Bool | Int | τ 1 → τ 2 Infix Operators ::= * | + | == | · · · ⊛ Expressions e ::= x | n | b | ( e ) | e 1 ⊛ e 2 | if e 1 then e 2 else e 3 | e 1 e 2 32

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend