TYPES OF PROGRAMMING LANGUAGES PRINCIPLES OF PROGRAMMING LANGUAGES - - PowerPoint PPT Presentation

types of programming languages
SMART_READER_LITE
LIVE PREVIEW

TYPES OF PROGRAMMING LANGUAGES PRINCIPLES OF PROGRAMMING LANGUAGES - - PowerPoint PPT Presentation

TYPES OF PROGRAMMING LANGUAGES PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2019 Dalhousie University 1/30 REASONS TO CHOOSE A PARTICULAR PROGRAMMING LANGUAGE Easy to express complex ideas Easy to control exactly how the


slide-1
SLIDE 1

TYPES OF PROGRAMMING LANGUAGES

PRINCIPLES OF PROGRAMMING LANGUAGES

Norbert Zeh Winter 2019

Dalhousie University 1/30

slide-2
SLIDE 2

REASONS TO CHOOSE A PARTICULAR PROGRAMMING LANGUAGE

  • Easy to express complex ideas
  • Easy to control exactly how the computation is carried out
  • Rich set of data types
  • Extensive (standard) library
  • Active, friendly community
  • Was used for this project before I joined
  • Good compiler support
  • Open-source

2/30

slide-3
SLIDE 3

REASONS TO CHOOSE A PARTICULAR PROGRAMMING LANGUAGE

  • Easy to express complex ideas
  • Easy to control exactly how the computation is carried out
  • Rich set of data types
  • Extensive (standard) library
  • Active, friendly community
  • Was used for this project before I joined
  • Good compiler support
  • Open-source

2/30

slide-4
SLIDE 4

WHY ARE THERE SO MANY PROGRAMMING LANGUAGES?

  • Trade-off between features
  • Speed vs expressiveness/elegance
  • Speed, type safety vs rapid development
  • Tinkering with programming languages is fun

… and then they sometimes get adopted.

  • Corporate pressure

3/30

slide-5
SLIDE 5

WHY ARE THERE SO MANY PROGRAMMING LANGUAGES?

  • Trade-off between features
  • Speed vs expressiveness/elegance
  • Speed, type safety vs rapid development
  • Tinkering with programming languages is fun

… and then they sometimes get adopted.

  • Corporate pressure

3/30

slide-6
SLIDE 6

WHY ARE THERE SO MANY PROGRAMMING LANGUAGES?

  • Trade-off between features
  • Speed vs expressiveness/elegance
  • Speed, type safety vs rapid development
  • Tinkering with programming languages is fun

… and then they sometimes get adopted.

  • Corporate pressure

3/30

slide-7
SLIDE 7

WHY ARE THERE SO MANY PROGRAMMING LANGUAGES?

  • Trade-off between features
  • Speed vs expressiveness/elegance
  • Speed, type safety vs rapid development
  • Tinkering with programming languages is fun

… and then they sometimes get adopted.

  • Corporate pressure

3/30

slide-8
SLIDE 8

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-9
SLIDE 9

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-10
SLIDE 10

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-11
SLIDE 11

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-12
SLIDE 12

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-13
SLIDE 13

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-14
SLIDE 14

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-15
SLIDE 15

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-16
SLIDE 16

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-17
SLIDE 17

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-18
SLIDE 18

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-19
SLIDE 19

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-20
SLIDE 20

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-21
SLIDE 21

GENERAL PURPOSE VS SPECIAL PURPOSE

General-purpose programming language:

  • Designed to express arbitrary computations
  • Turing-complete

Examples:

  • C, C++, Java, Python, Ruby
  • Haskell, Scheme, Prolog
  • Lua, Tcl/Tk

Special-purpose programming language:

  • Designed to make it easy to express certain

types of programs

  • May not be Turing-complete

Examples:

  • (La)TeX, HTML/XML, XSLT
  • R, Matlab
  • sed, awk

4/30

slide-22
SLIDE 22

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-23
SLIDE 23

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-24
SLIDE 24

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-25
SLIDE 25

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-26
SLIDE 26

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-27
SLIDE 27

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-28
SLIDE 28

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-29
SLIDE 29

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-30
SLIDE 30

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-31
SLIDE 31

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-32
SLIDE 32

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-33
SLIDE 33

TYPES OF PROGRAMMING LANGUAGES

C Pascal Modula Ada Fortran C++ Python Ruby Java Objective C Swift Lisp ML Scala Scheme Haskell Prolog Hilog Postscript Forth Factor Concatenative Logic Imperative Object-oriented Functional Declarative

5/30

slide-34
SLIDE 34

THE LANGUAGES I USE

Rust or C++

  • When I need performance

C

  • When I feel nostalgic

Haskell

  • When I want to have fun and

write elegant code that I trust Prolog

  • When I want to solve puzzles

Python

  • When I need to write a

prototype quickly Scala

  • When I’m told to use Java

Java

  • Never

Scheme

  • When I’d rather teach you

Haskell

6/30

slide-35
SLIDE 35

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-36
SLIDE 36

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-37
SLIDE 37

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-38
SLIDE 38

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-39
SLIDE 39

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-40
SLIDE 40

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-41
SLIDE 41

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-42
SLIDE 42

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-43
SLIDE 43

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-44
SLIDE 44

IMPERATIVE VS DECLARATIVE PROGRAMMING

Imperative programming:

  • Focus on telling the

computer exactly which steps to execute

  • Close to the machine
  • Difficult to analyze/

automatically optimize

  • Functions called for
  • Return values
  • Side effects

Declarative programming:

  • Focus on telling the computer what

to do, not how to do it

  • More high-level, elegant, expressive
  • Need to include evaluation engine

in run-time system

  • Can come with performance

penalties

  • Easier to analyze/automatically
  • ptimize/parallelize

7/30

slide-45
SLIDE 45

SIDE EFFECTS

  • Variable updates, I/O (disk, screen, keyboard, network, …)

… are evil:

  • Source of 90% of all software bugs

… are the only reason we compute at all:

  • Taking input and communicating results requires side effects.

8/30

slide-46
SLIDE 46

SIDE EFFECTS

  • Variable updates, I/O (disk, screen, keyboard, network, …)

… are evil:

  • Source of 90% of all software bugs

… are the only reason we compute at all:

  • Taking input and communicating results requires side effects.

8/30

slide-47
SLIDE 47

SIDE EFFECTS

  • Variable updates, I/O (disk, screen, keyboard, network, …)

… are evil:

  • Source of 90% of all software bugs

… are the only reason we compute at all:

  • Taking input and communicating results requires side effects.

8/30

slide-48
SLIDE 48

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-49
SLIDE 49

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-50
SLIDE 50

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-51
SLIDE 51

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-52
SLIDE 52

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-53
SLIDE 53

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-54
SLIDE 54

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-55
SLIDE 55

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

→ easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-56
SLIDE 56

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

→ easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-57
SLIDE 57

FUNCTIONAL PROGRAMMING

  • Functions have no side effects
  • Variables are immutable once defined
  • Functions are first-class objects
  • Can express imperative computations elegantly!

Pros:

  • Easier to analyze/optimize
  • No specified execution order

→ easy to parallelize Cons:

  • Can be less efficient than

well-designed imperative code

  • Some imperative data

structures are inherently more efficient than their purely functional counterparts

9/30

slide-58
SLIDE 58

LOGIC PROGRAMMING

General program structure:

  • Database of facts, represented as logical predicates
  • Rules for deducing new facts from known facts
  • Execution driven by queries whether certain facts are true

Runtime system:

  • Engine to perform the deduction process efficiently

Pros:

  • Even higher abstraction than

functional programming

  • In theory, no need to worry

about execution details at all Cons:

  • In practice, need to understand

execution details enough to

  • Avoid infinite loops in deduction
  • Obtain efficient programs

10/30

slide-59
SLIDE 59

LOGIC PROGRAMMING

General program structure:

  • Database of facts, represented as logical predicates
  • Rules for deducing new facts from known facts
  • Execution driven by queries whether certain facts are true

Runtime system:

  • Engine to perform the deduction process efficiently

Pros:

  • Even higher abstraction than

functional programming

  • In theory, no need to worry

about execution details at all Cons:

  • In practice, need to understand

execution details enough to

  • Avoid infinite loops in deduction
  • Obtain efficient programs

10/30

slide-60
SLIDE 60

LOGIC PROGRAMMING

General program structure:

  • Database of facts, represented as logical predicates
  • Rules for deducing new facts from known facts
  • Execution driven by queries whether certain facts are true

Runtime system:

  • Engine to perform the deduction process efficiently

Pros:

  • Even higher abstraction than

functional programming

  • In theory, no need to worry

about execution details at all Cons:

  • In practice, need to understand

execution details enough to

  • Avoid infinite loops in deduction
  • Obtain efficient programs

10/30

slide-61
SLIDE 61

LOGIC PROGRAMMING

General program structure:

  • Database of facts, represented as logical predicates
  • Rules for deducing new facts from known facts
  • Execution driven by queries whether certain facts are true

Runtime system:

  • Engine to perform the deduction process efficiently

Pros:

  • Even higher abstraction than

functional programming

  • In theory, no need to worry

about execution details at all Cons:

  • In practice, need to understand

execution details enough to

  • Avoid infinite loops in deduction
  • Obtain efficient programs

10/30

slide-62
SLIDE 62

LOGIC PROGRAMMING

General program structure:

  • Database of facts, represented as logical predicates
  • Rules for deducing new facts from known facts
  • Execution driven by queries whether certain facts are true

Runtime system:

  • Engine to perform the deduction process efficiently

Pros:

  • Even higher abstraction than

functional programming

  • In theory, no need to worry

about execution details at all Cons:

  • In practice, need to understand

execution details enough to

  • Avoid infinite loops in deduction
  • Obtain efficient programs

10/30

slide-63
SLIDE 63

LOGIC PROGRAMMING

General program structure:

  • Database of facts, represented as logical predicates
  • Rules for deducing new facts from known facts
  • Execution driven by queries whether certain facts are true

Runtime system:

  • Engine to perform the deduction process efficiently

Pros:

  • Even higher abstraction than

functional programming

  • In theory, no need to worry

about execution details at all Cons:

  • In practice, need to understand

execution details enough to

  • Avoid infinite loops in deduction
  • Obtain efficient programs

10/30

slide-64
SLIDE 64

LOGIC PROGRAMMING

General program structure:

  • Database of facts, represented as logical predicates
  • Rules for deducing new facts from known facts
  • Execution driven by queries whether certain facts are true

Runtime system:

  • Engine to perform the deduction process efficiently

Pros:

  • Even higher abstraction than

functional programming

  • In theory, no need to worry

about execution details at all Cons:

  • In practice, need to understand

execution details enough to

  • Avoid infinite loops in deduction
  • Obtain efficient programs

10/30

slide-65
SLIDE 65

INTERMISSION: SCHEME AND PROLOG TUTORIALS

11/30

slide-66
SLIDE 66

AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (1)

C++:

template <typename It> void merge_sort(const It &begin, const It &end) { auto n = end - begin; if (n < 2) return; auto mid = begin + n / 2; merge_sort(begin, mid); merge_sort(mid, end); std::vector<std::iterator_traits<It>::value_type> left(begin, mid); std::vector<std::iterator_traits<It>::value_type> right(mid, end); merge(left, right, begin); }

12/30

slide-67
SLIDE 67

AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (2)

template <typename It> void merge( const std::vector<std::iterator_traits<It>::value_type> &left, const std::vector<std::iterator_traits<It>::value_type> &right, It out) { auto l = left.begin(), r = right.begin(); while (l != left.end() && r != right.end()) { if (*r < *l) *out++ = *r++; else *out++ = *l++; } while (l != left.end()) *out++ = *l++; while (r != right.end()) *out++ = *r++; }

13/30

slide-68
SLIDE 68

AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (3)

Haskell:

mergeSort :: Ord t => [t] -> [t] mergeSort [] = [] mergeSort [x] = [x] mergeSort xs = merge (mergeSort ls) (mergeSort rs) where n = length xs (ls, rs) = splitAt (n `div` 2) xs merge :: Ord t => [t] -> [t] -> [t] merge [] rs = rs merge ls [] = ls merge ls@(l:ls') rs@(r:rs') | r < l = r : merge ls rs' | otherwise = l : merge ls' rs

14/30

slide-69
SLIDE 69

AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (4)

Prolog:

list_sorted([], []). list_sorted([X], [X]). list_sorted(List, Sorted) :- list_left_right(List, Left, Right), list_sorted(Left, LeftSorted), list_sorted(Right, RightSorted), merged_left_right(Sorted, LeftSorted, RightSorted). merged_left_right(Left, Left, []). merged_left_right([R|Right], [], [R|Right]). merged_left_right([L|Merged], [L|Left], [R|Right]) :- L #=< R, merged_left_right(Merged, Left, [R|Right]). merged_left_right([R|Merged], [L|Left], [R|Right]) :- R #< L, merged_left_right(Merged, [L|Left], Right).

15/30

slide-70
SLIDE 70

AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (5)

list_left_right(List, Left, Right) :- phrase(parse_half(List, Left), List, Right). parse_half([], []) --> []. parse_half([_], []) --> []. parse_half([_,_|List], [L|Left]) --> [L], parse_half(List, Left).

16/30

slide-71
SLIDE 71

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-72
SLIDE 72

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-73
SLIDE 73

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-74
SLIDE 74

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-75
SLIDE 75

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-76
SLIDE 76

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-77
SLIDE 77

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-78
SLIDE 78

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-79
SLIDE 79

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-80
SLIDE 80

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-81
SLIDE 81

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-82
SLIDE 82

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-83
SLIDE 83

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-84
SLIDE 84

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (1)

Problem: Build a permutation of the integers {0, 1, . . . , n − 1} specified by indicating, for each element, after which element it is to be inserted. Example: 1 3 2

1 2 3 4 5 6

4 2 6 1 3 5 1 2 1 2 1 3 4 2 1 3 4 2 1 3 5 1 2 3 4 5 6 Input: Output:

17/30

slide-85
SLIDE 85

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-86
SLIDE 86

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-87
SLIDE 87

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-88
SLIDE 88

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-89
SLIDE 89

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-90
SLIDE 90

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-91
SLIDE 91

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-92
SLIDE 92

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-93
SLIDE 93

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-94
SLIDE 94

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-95
SLIDE 95

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (2)

C++: Linear time

std::vector<int> dynamic_permute(const std::vector<int> &refs) { int n = ref.size() + 1; std::list<int> seq; std::vector<std::list<int>::iterator> list_nodes(n); list_nodes[0] = seq.insert(seq.end(), 0); for (int i = 1; i < n; ++i) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); return std::vector<int>(seq.begin(), seq.end()); }

4 2 6 1 3 5

18/30

slide-96
SLIDE 96

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3)

Doing this without mutation: 3 1 2 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. This makes standard pointer-based data structures difficult/impossible to implement functionally.

19/30

slide-97
SLIDE 97

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3)

Doing this without mutation: 3 1 2 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. This makes standard pointer-based data structures difficult/impossible to implement functionally.

19/30

slide-98
SLIDE 98

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3)

Doing this without mutation: 3 1 2 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. This makes standard pointer-based data structures difficult/impossible to implement functionally.

19/30

slide-99
SLIDE 99

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3)

Doing this without mutation: 3 1 2 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. This makes standard pointer-based data structures difficult/impossible to implement functionally.

19/30

slide-100
SLIDE 100

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3)

Doing this without mutation: 3 1 2 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. This makes standard pointer-based data structures difficult/impossible to implement functionally.

19/30

slide-101
SLIDE 101

AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3)

Doing this without mutation: 3 1 2 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. This makes standard pointer-based data structures difficult/impossible to implement functionally.

19/30

slide-102
SLIDE 102

WHAT ABOUT TRADITIONAL PERMUTING? (1)

Problem: Given a list of elements, each annotated with its desired position in the

  • utput list, build an array storing each element in the desired position.

Example: (2, a) (0, b) (3, c) (1, e) (4, d) b e a c d Input: Output:

20/30

slide-103
SLIDE 103

WHAT ABOUT TRADITIONAL PERMUTING? (2)

C++:

template <typename T> std::vector<T> permute( const std::vector<std::pair<int, T>> &input) { std::vector<T> output(input.size()); for (auto &item : input)

  • utput[item.first] = item.second;

return output; }

Haskell:

permute :: [(Int, t)] -> [t] permute xs = elems (array (0, len xs - 1) xs)

21/30

slide-104
SLIDE 104

WHAT ABOUT TRADITIONAL PERMUTING? (2)

C++:

template <typename T> std::vector<T> permute( const std::vector<std::pair<int, T>> &input) { std::vector<T> output(input.size()); for (auto &item : input)

  • utput[item.first] = item.second;

return output; }

Haskell:

permute :: [(Int, t)] -> [t] permute xs = elems (array (0, len xs - 1) xs)

21/30

slide-105
SLIDE 105

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (1)

4 2 6 1 2 3 6 1 7 6 9 5 4 3 9 4 4 2 8 3 8 9 9 8 2 3 7 4 1 3 6 8 1 6 7 2 4 8 7 3 2 9 6 1 5 2 3 5 6 4 1 7 8 9 1 6 9 5 8 7 4 2 3 6 9 1 7 3 8 2 5 4 5 4 2 9 1 6 8 3 7 8 7 3 2 5 4 1 9 6 9 5 8 4 6 2 3 7 1 7 2 4 1 9 3 5 6 8 3 1 6 8 7 5 9 4 2 Sudoku

22/30

slide-106
SLIDE 106

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (2)

Prolog: Elegant but (ridiculously) slow

sudoku(Rows) :- transpose(Rows, Columns), rows_blocks(Rows, Blocks), append([Rows, Columns, Blocks], Sets), maplist(permutation([1, 2, 3, 4, 5, 6, 7, 8, 9]), Sets).

rows_blocks([], []). rows_blocks([R1,R2,R3|Rows], [B1,B2,B3|Blocks]) :- rows3_blocks3([R1,R2,R3], [B1,B2,B3]). rows3_blocks3([[R11,R12,R13|R1], [R21,R22,R23|R2], [R31,R32,R33|R3]], [[R11,R12,R13,R21,R22,R23,R31,R32,R33|Bs]) :- rows3_blocks3([R1,R2,R3], Bs).

23/30

slide-107
SLIDE 107

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (2)

Prolog: Elegant but (ridiculously) slow

sudoku(Rows) :- transpose(Rows, Columns), rows_blocks(Rows, Blocks), append([Rows, Columns, Blocks], Sets), maplist(permutation([1, 2, 3, 4, 5, 6, 7, 8, 9]), Sets).

rows_blocks([], []). rows_blocks([R1,R2,R3|Rows], [B1,B2,B3|Blocks]) :- rows3_blocks3([R1,R2,R3], [B1,B2,B3]). rows3_blocks3([[R11,R12,R13|R1], [R21,R22,R23|R2], [R31,R32,R33|R3]], [[R11,R12,R13,R21,R22,R23,R31,R32,R33|Bs]) :- rows3_blocks3([R1,R2,R3], Bs).

23/30

slide-108
SLIDE 108

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (2)

Prolog: Elegant but (ridiculously) slow

sudoku(Rows) :- transpose(Rows, Columns), rows_blocks(Rows, Blocks), append([Rows, Columns, Blocks], Sets), maplist(permutation([1, 2, 3, 4, 5, 6, 7, 8, 9]), Sets).

rows_blocks([], []). rows_blocks([R1,R2,R3|Rows], [B1,B2,B3|Blocks]) :- rows3_blocks3([R1,R2,R3], [B1,B2,B3]). rows3_blocks3([[R11,R12,R13|R1], [R21,R22,R23|R2], [R31,R32,R33|R3]], [[R11,R12,R13,R21,R22,R23,R31,R32,R33|Bs]) :- rows3_blocks3([R1,R2,R3], Bs).

23/30

slide-109
SLIDE 109

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (2)

Prolog: Elegant but (ridiculously) slow

sudoku(Rows) :- transpose(Rows, Columns), rows_blocks(Rows, Blocks), append([Rows, Columns, Blocks], Sets), maplist(permutation([1, 2, 3, 4, 5, 6, 7, 8, 9]), Sets).

rows_blocks([], []). rows_blocks([R1,R2,R3|Rows], [B1,B2,B3|Blocks]) :- rows3_blocks3([R1,R2,R3], [B1,B2,B3]). rows3_blocks3([[R11,R12,R13|R1], [R21,R22,R23|R2], [R31,R32,R33|R3]], [[R11,R12,R13,R21,R22,R23,R31,R32,R33|Bs]) :- rows3_blocks3([R1,R2,R3], Bs).

23/30

slide-110
SLIDE 110

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (3)

Prolog: Elegant and fast

sudoku(Rows) :- transpose(Rows, Columns), append(Rows, Vs), Vs ins 1..9, maplist(all_distinct, Rows), maplist(all_distinct, Columns), Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is], blocks(As,Bs,Cs), blocks(Ds,Es,Fs), blocks(Gs,Hs,Is), label(Vs).

blocks([], [], []). blocks([A1,A2,A3|As], [B1,B2,B3|Bs], [C1,C2,C3|Cs]) :- all_distinct([A1,A2,A3,B1,B2,B3,C1,C2,C3]), blocks(As,Bs,Cs).

What’s different? This uses efficient constraint propagation.

24/30

slide-111
SLIDE 111

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (3)

Prolog: Elegant and fast

sudoku(Rows) :- transpose(Rows, Columns), append(Rows, Vs), Vs ins 1..9, maplist(all_distinct, Rows), maplist(all_distinct, Columns), Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is], blocks(As,Bs,Cs), blocks(Ds,Es,Fs), blocks(Gs,Hs,Is), label(Vs).

blocks([], [], []). blocks([A1,A2,A3|As], [B1,B2,B3|Bs], [C1,C2,C3|Cs]) :- all_distinct([A1,A2,A3,B1,B2,B3,C1,C2,C3]), blocks(As,Bs,Cs).

What’s different? This uses efficient constraint propagation.

24/30

slide-112
SLIDE 112

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (3)

Prolog: Elegant and fast

sudoku(Rows) :- transpose(Rows, Columns), append(Rows, Vs), Vs ins 1..9, maplist(all_distinct, Rows), maplist(all_distinct, Columns), Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is], blocks(As,Bs,Cs), blocks(Ds,Es,Fs), blocks(Gs,Hs,Is), label(Vs).

blocks([], [], []). blocks([A1,A2,A3|As], [B1,B2,B3|Bs], [C1,C2,C3|Cs]) :- all_distinct([A1,A2,A3,B1,B2,B3,C1,C2,C3]), blocks(As,Bs,Cs).

What’s different? This uses efficient constraint propagation.

24/30

slide-113
SLIDE 113

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (3)

Prolog: Elegant and fast

sudoku(Rows) :- transpose(Rows, Columns), append(Rows, Vs), Vs ins 1..9, maplist(all_distinct, Rows), maplist(all_distinct, Columns), Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is], blocks(As,Bs,Cs), blocks(Ds,Es,Fs), blocks(Gs,Hs,Is), label(Vs).

blocks([], [], []). blocks([A1,A2,A3|As], [B1,B2,B3|Bs], [C1,C2,C3|Cs]) :- all_distinct([A1,A2,A3,B1,B2,B3,C1,C2,C3]), blocks(As,Bs,Cs).

What’s different? This uses efficient constraint propagation.

24/30

slide-114
SLIDE 114

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (4)

Prolog:

  • 12 LOC
  • Instantaneous answer
  • SWI Prolog
  • Free, well maintained,

feature-rich, ISO compliant

  • Much slower than SICSTUS

Prolog (commercial)

Python:

  • SAT solver (250 LOC)
  • Encode puzzle as CNF (100 LOC)
  • Instantaneous answer
  • Could get faster if
  • Implemented in C++
  • Using state-of-the-art SAT solver

25/30

slide-115
SLIDE 115

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (4)

Prolog:

  • 12 LOC
  • Instantaneous answer
  • SWI Prolog
  • Free, well maintained,

feature-rich, ISO compliant

  • Much slower than SICSTUS

Prolog (commercial)

Python:

  • SAT solver (250 LOC)
  • Encode puzzle as CNF (100 LOC)
  • Instantaneous answer
  • Could get faster if
  • Implemented in C++
  • Using state-of-the-art SAT solver

25/30

slide-116
SLIDE 116

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (5)

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Binary Puzzle · No two identical rows/columns · #0s = #1s in each row/column · No three consecutive 0s or 1s in any row or column

26/30

slide-117
SLIDE 117

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (6)

binary(Rows) :- append(Rows, Vs), Vs ins 0..1, transpose(Rows, Columns), maplist(no_triplets, Rows), maplist(no_triplets, Columns), maplist(zero_one_balance, Rows), maplist(zero_one_balance, Columns), phrase(pairs(Rows), Row_Pairs), phrase(pairs(Columns), Column_Pairs), maplist(not_same, Row_Pairs), maplist(not_same, Column_Pairs), label(Vs). no_triplets(List) :- length(List,L), L < 3. no_triplets([A,B,C|List]) :- A+B+C #> 0, A+B+C #< 3, no_triplets([B,C|List]). zero_one_balance(List) :- length(List,L), Half is L // 2, sum(List, #= Half). not_same((List1,List2) :- maplist(diff, List1, List2, Diffs), sum(Diffs, #>, 0). diff(A, B, Diff) :- Diff #<==> A #\= B. pairs([_]) --> []. pairs([X|List]) --> pairs_(X, List), pairs(List). pairs_(_, []) --> []. pairs_(X, [Y|List]) --> [(X,Y)], pairs_(X,List). 27/30

slide-118
SLIDE 118

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (6)

binary(Rows) :- append(Rows, Vs), Vs ins 0..1, transpose(Rows, Columns), maplist(no_triplets, Rows), maplist(no_triplets, Columns), maplist(zero_one_balance, Rows), maplist(zero_one_balance, Columns), phrase(pairs(Rows), Row_Pairs), phrase(pairs(Columns), Column_Pairs), maplist(not_same, Row_Pairs), maplist(not_same, Column_Pairs), label(Vs). no_triplets(List) :- length(List,L), L < 3. no_triplets([A,B,C|List]) :- A+B+C #> 0, A+B+C #< 3, no_triplets([B,C|List]). zero_one_balance(List) :- length(List,L), Half is L // 2, sum(List, #= Half). not_same((List1,List2) :- maplist(diff, List1, List2, Diffs), sum(Diffs, #>, 0). diff(A, B, Diff) :- Diff #<==> A #\= B. pairs([_]) --> []. pairs([X|List]) --> pairs_(X, List), pairs(List). pairs_(_, []) --> []. pairs_(X, [Y|List]) --> [(X,Y)], pairs_(X,List). 27/30

slide-119
SLIDE 119

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (6)

binary(Rows) :- append(Rows, Vs), Vs ins 0..1, transpose(Rows, Columns), maplist(no_triplets, Rows), maplist(no_triplets, Columns), maplist(zero_one_balance, Rows), maplist(zero_one_balance, Columns), phrase(pairs(Rows), Row_Pairs), phrase(pairs(Columns), Column_Pairs), maplist(not_same, Row_Pairs), maplist(not_same, Column_Pairs), label(Vs). no_triplets(List) :- length(List,L), L < 3. no_triplets([A,B,C|List]) :- A+B+C #> 0, A+B+C #< 3, no_triplets([B,C|List]). zero_one_balance(List) :- length(List,L), Half is L // 2, sum(List, #= Half). not_same((List1,List2) :- maplist(diff, List1, List2, Diffs), sum(Diffs, #>, 0). diff(A, B, Diff) :- Diff #<==> A #\= B. pairs([_]) --> []. pairs([X|List]) --> pairs_(X, List), pairs(List). pairs_(_, []) --> []. pairs_(X, [Y|List]) --> [(X,Y)], pairs_(X,List). 27/30

slide-120
SLIDE 120

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (6)

binary(Rows) :- append(Rows, Vs), Vs ins 0..1, transpose(Rows, Columns), maplist(no_triplets, Rows), maplist(no_triplets, Columns), maplist(zero_one_balance, Rows), maplist(zero_one_balance, Columns), phrase(pairs(Rows), Row_Pairs), phrase(pairs(Columns), Column_Pairs), maplist(not_same, Row_Pairs), maplist(not_same, Column_Pairs), label(Vs). no_triplets(List) :- length(List,L), L < 3. no_triplets([A,B,C|List]) :- A+B+C #> 0, A+B+C #< 3, no_triplets([B,C|List]). zero_one_balance(List) :- length(List,L), Half is L // 2, sum(List, #= Half). not_same((List1,List2) :- maplist(diff, List1, List2, Diffs), sum(Diffs, #>, 0). diff(A, B, Diff) :- Diff #<==> A #\= B. pairs([_]) --> []. pairs([X|List]) --> pairs_(X, List), pairs(List). pairs_(_, []) --> []. pairs_(X, [Y|List]) --> [(X,Y)], pairs_(X,List). 27/30

slide-121
SLIDE 121

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (6)

binary(Rows) :- append(Rows, Vs), Vs ins 0..1, transpose(Rows, Columns), maplist(no_triplets, Rows), maplist(no_triplets, Columns), maplist(zero_one_balance, Rows), maplist(zero_one_balance, Columns), phrase(pairs(Rows), Row_Pairs), phrase(pairs(Columns), Column_Pairs), maplist(not_same, Row_Pairs), maplist(not_same, Column_Pairs), label(Vs). no_triplets(List) :- length(List,L), L < 3. no_triplets([A,B,C|List]) :- A+B+C #> 0, A+B+C #< 3, no_triplets([B,C|List]). zero_one_balance(List) :- length(List,L), Half is L // 2, sum(List, #= Half). not_same((List1,List2) :- maplist(diff, List1, List2, Diffs), sum(Diffs, #>, 0). diff(A, B, Diff) :- Diff #<==> A #\= B. pairs([_]) --> []. pairs([X|List]) --> pairs_(X, List), pairs(List). pairs_(_, []) --> []. pairs_(X, [Y|List]) --> [(X,Y)], pairs_(X,List). 27/30

slide-122
SLIDE 122

AN EXAMPLE WHERE LOGIC PROGRAMMING SHINES: CONSTRAINT SATISFACTION (7)

Prolog:

  • 31 LOC
  • Around 7 secs to solve
  • SWI Prolog
  • Free, well maintained,

feature-rich, ISO compliant

  • Much slower than SICSTUS

Prolog (commercial)

Python:

  • SAT solver (250 LOC)
  • Encode puzzle as CNF (150 LOC)
  • Around 70 secs to solve
  • Could get faster if
  • Implemented in C++
  • Using state-of-the-art SAT solver

28/30

slide-123
SLIDE 123

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-124
SLIDE 124

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-125
SLIDE 125

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-126
SLIDE 126

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-127
SLIDE 127

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-128
SLIDE 128

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-129
SLIDE 129

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-130
SLIDE 130

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-131
SLIDE 131

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-132
SLIDE 132

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-133
SLIDE 133

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-134
SLIDE 134

EXPRESSIVENESS OF PROGRAMMING LANGUAGES

We want expressive programming languages that make programming easy:

  • Concise
  • Help to avoid common bugs
  • Help to express exactly what we want

What do we want exactly? Fine-grained control High-level abstractions C++ Python, Ruby, Java, Scala Haskell Prolog “Trust me, I know what I’m doing” Strict compile-time checks Python, Ruby C++, Java Scala Haskell

29/30

slide-135
SLIDE 135

SUMMARY

Programming languages are the tools we use to express computations. Different programming languages may be better for different jobs. Be eager to explore new programming languages!

  • Outside your comfort zone!
  • It’s fun.
  • It makes you a better programmer, even in your favourite language.
  • Your favourite language may change.

30/30

slide-136
SLIDE 136

SUMMARY

Programming languages are the tools we use to express computations. Different programming languages may be better for different jobs. Be eager to explore new programming languages!

  • Outside your comfort zone!
  • It’s fun.
  • It makes you a better programmer, even in your favourite language.
  • Your favourite language may change.

30/30

slide-137
SLIDE 137

SUMMARY

Programming languages are the tools we use to express computations. Different programming languages may be better for different jobs. Be eager to explore new programming languages!

  • Outside your comfort zone!
  • It’s fun.
  • It makes you a better programmer, even in your favourite language.
  • Your favourite language may change.

30/30