SlideShare uma empresa Scribd logo
1 de 76
Programs as Strategies
Programs as Strategies
Computers and
     software are...

• Important
• Ubiquitous
• Complicated
Layers of complexity
•   Programs

•   Compilers

•   Operating System

•   Hardware

•   ... and that’s just on one
    computer.
The usual answer:
            models
•   The “layers” are already
    abstractions.

•   They encapsulate parts
    of the system and
    interact with the rest via
    reduced interfaces.

•   We don’t understand
    the whole truth - just a
    model.
Two models of drinking
     from a tap

•   I turn on the tap and I
    get a drink
Two models of drinking
     from a tap

•   Turning the tap opens a
    valve which releases
    water supplied at a
    pressure of 2 bar...
Models and
          abstractions
• Our conceptual models and their
  corresponding abstractions make complex
  systems tractable.
• Really good models (e.g. scientific theories)
  help us predict, design, and discover.
• What models will help us with
  programming?
Turing machines

•   Alan Turing devised an
    early model of
    computation: the Turing
    Machine.

•   A TM manipulates data
    stored on a tape
    according to a table of
    rules - a fixed program.
Turing machines
• Turing machines expose powerful and
  central concepts:
  • computability
  • time usage
  • space usage
• But they are monolithic, and too low-level
  to be of use in analysing modern software.
Goal: compositionality

• Software is built by assembling small pieces
  of code into large programs.
• Our models should reflect and empower
  this compositionality.
• We need abstractions that support
  composition.
Functions
•   Possibly the most widely used (mathematical) abstraction.
•   We can happily plug functions together:
    •   2x
    •   sin(x)
    •   sin(2x)
    •   x2
    •   sin(2x) + x2

•   These are all functions of type (R→R)
Understanding +

• The + in the last example operates on two
  functions to give another function
                  sin(2x) +   x 2


• So + has the type
         (R→R) ⨉ (R→R) → (R→R)
Higher-order functions
  and programming
• These higher order functions provide both a
  mathematical model of programming and
  an approach to programming itself.
  • higher-order functional programming
    began with LISP in the 1950s.
Programs as functions

function addOne(x:int):int {
  return (x+1);
}
            x↦x+1
Programs as functions

function fact(x:int):int {
  if (x == 0) return 1
  else return (x * fact(x-1));
}

             x ↦ x!
        3↦3⨉2 ⨉1=6
            but why?
Programs as functions

function fact(x:int):int {
  if (x == 0) return 1
  else return (x * fact(x-1));
}

             x ↦ x!
        3↦3⨉2 ⨉1=6
            but why?
Infinity
• Recursion introduces infinity to the picture.
• For programmers, infinity is where the power
  lies.
• For models, infinity is where the problems lie.
• We need some topology to interpret
  recursion: a recursive function is modelled as
  the limit of an infinite sequence of
  approximants.
Imperative programs?
function fact(x:int):int {
  y := x;
  z := 1;
  while (y ≠ 0) {
    z := z ⨉ y;
    y := y-1;
  }
  return z;
}
Modelling memory
• Code like
                  y := y-1
  manipulates (an abstraction of) the memory
  of the computer.
• To model these with functions, we need to
  model the memory itself as a function
              identifiers → values.
Mathematical semantics
• These ideas were the basis of mathematical
  semantics developed by Strachey, Scott and
  others from around 1970.
• The theory provides:
 • an understanding of recursion and loops
 • ways of proving certain correctness
    properties of programs (e.g. Hoare logic)
But

• For imperative programs, the theory breaks
  abstractions: functions can manipulate the
  memory in arbitrary ways.
• There is no way to see the two factorial
  programs as “the same.”
A different abstraction:
       interaction
• Replace functions with interacting agents.
• An agent is a “black box” that receives
  messages from its environment and sends
  out responses.
• Ideas like these were first used in modelling
  concurrent systems (Hoare’s CSP, Milner’s
  CCS) around 1980.
Functions as agents
    input   output
Functions as agents
      input       output




•That was easy!
Functions as agents
      input          output




•That was easy!
•It turns out to be more convenient to add a
“request” protocol at the beginning.
Functions as agents
    request          request
      input          output


•That was easy!
•It turns out to be more convenient to add a
“request” protocol at the beginning.
Plugging together
request         request   request         request
                                                    q
           x2                       x+3
 input          output     input          output
Plugging together
request         request       request         request
                          q
           x2                           x+3
 input          output         input          output
Plugging together
    request         request   request         request
q
               x2                       x+3
     input          output     input          output
Plugging together
request           request   request         request
             x2                       x+3
    input         output     input          output
2
Plugging together
request            request   request         request
              x2                       x+3
 input             output     input          output
          2
Plugging together
request             request   request         request
           x2                           x+3
 input              output     input          output
                4
Plugging together
request         request   request           request
           x2                         x+3
 input          output        input         output
                          4
Plugging together
request         request   request             request
           x2                           x+3
 input          output     input              output
                                    4
Plugging together
request         request   request             request
           x2                       x+3
 input          output     input              output
                                          7
Plugging together
request         request   request         request
           x2                       x+3
 input          output     input          output
                                                    7
Plugging together
request                       request
                                        q
                 x2+3
 input                        output
Plugging together
    request                       request
q
                     x2+3
     input                        output
Plugging together
request                         request
                   x2+3
    input                       output
2
Plugging together
request                       request
                 x2+3
 input                        output
          2
Plugging together
request                       request
                 x2+3
 input                        output
                          7
Plugging together
request                       request
                 x2+3
 input                        output
                                        7
A formalisation: game
      semantics
• This idea can be made precise in lots of
  ways. Here’s one.
• Interaction is the playing of a game between
  the agent and its environment.
• The game specifies what moves are available
  and when they can be played.
• Agent and environment take turns.
The integer game

          request         environment


      0    1   2    ...      agent

A game is a tree of moves. Child moves can
only be played once their parents have been
played.
and its dual

    request            agent


0   1    2    ...   environment
int → int
    request             request


0   1    2    ...   0   1    2    ...
Strategies


• Our “agents” are strategies: plans telling the
  agent what to do in any given situation.
The identity function
     request             request


 0   1    2    ...   0   1    2    ...
The identity function
     request             request


 0   1    2    ...   0   1    2    ...
The identity function
     request             request


 0   1    2    ...   0   1    2    ...
The identity function
     request             request


 0   1    2    ...   0   1    2    ...
The identity function
     request             request


 0   1    2    ...   0   1    2    ...
The identity function
         request              request


     0    1   2    ...    0    1   2    ...

•This strategy implements the function x ↦ x by
copying information.
•It works for any game, with no knowledge of
the moves: pure logic.
History-free strategies

• Every strategy we have seen so far chooses
  its move based only on the last move by
  the environment.
• Notice: history-freeness is preserved by
  plugging.
A new theory
• These ideas form game semantics.
• Games models possess a lot of the
  mathematical structure from the function-
  based semantics:
  • compositionality,
  • recursion as a limit of approximants, etc.
• But also...
A theorem
Theorem [Abramsky-Jagadeesan-
Malacaria 1993/2000; cf. Hyland-Ong,
Nickau]
Every pure functional program denotes a
history-free strategy.
Every computable history-free strategy
can be programmed.
Imperative programs
• Imperative programs work by interacting
  with the computer’s memory.
• Idea: model a memory location as an agent.
 write(true)    write(false)          read

     ok             ok         true          false
Interacting with the
      memory
                write(true)
         cell
Interacting with the
      memory
       write(true)
         cell
Interacting with the
      memory

         cell
           ok
Interacting with the
      memory

         cell
                ok
Interacting with the
      memory
                 read
         cell
Interacting with the
      memory
         read
         cell
Interacting with the
       memory
                      read
                      cell



write(true) ok      read ...then what?
A good memory location cannot be history
free. But the right history-sensitive strategy is
clear.
Extending the theory

• The theory of game semantics extends to
  arbitrary history-sensitive strategies.
• Then the cell can respond appropriately to
  read moves, so we can model the memory.
• Hence...
Another theorem

Theorem [Abramsky-M]
Every program of a certain imperative
language denotes a strategy.
Every computable strategy can be
programmed in this language.
Infinity
• Infinity enters the games models because
  we have to allow moves to be repeated.
Infinity
• Infinity enters the games models because
  we have to allow moves to be repeated.
Infinity
• Infinity enters the games models because
  we have to allow moves to be repeated.
Infinity
• Infinity enters the games models because
  we have to allow moves to be repeated.




• As usual, this is where all the technical
  problems are.
Taming infinity
• We need infinity to interpret recursion and
  loops.
• But we can tame it too:
 • restrict to finite data sets
 • limit the ways moves can be repeated
 • find out what programs are left
Towards automated
correctness checking
Theorem [Ghica-M]
For a certain constrained class of
programs, strategies are finite state
machines.
Therefore, we can automatically check
whether two such programs are equal.
More
•   Lots of other kinds of games to model other
    kinds of programs, logic, ... [numerous authors]
•   Implementation of strategies as circuits [Ghica]
•   Logical counterparts of history-sensitive
    strategies [Churchill, Laird, M]
•   Relational and geometrical foundations of
    game semantics [Calderon, M, Power,
    Wingfield].
Further

•   Modelling the whole
    system - all layers,
    multiple computers,
    people?

•   Quantitative models?
    Power consumption,
    transmission and storage
    costs, ...
Legal bits
•   Photographs used under Creative Commons Attribution-
    NonCommercial-ShareAlike licence:
    •   Circuit Board http://www.flickr.com/photos/loganwilliams/
    •   Tic Tac Toe http://www.flickr.com/photos/anonymonk/
    •   Code http://www.flickr.com/photos/circle_hk
    •   Go http://www.flickr.com/photos/spwelton/
    •   Lego http://www.flickr.com/photos/kaptainkobold/
    •   Cat drinking http://www.flickr.com/photos/meantux/
    •   Alan Turing http://www.flickr.com/photos/sbisson/
    •   Blue Sky http://www.flickr.com/photos/11247304@N06

Mais conteúdo relacionado

Mais procurados

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
Mohamed Ahmed
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
Hariz Mustafa
 

Mais procurados (20)

Max Koretskyi "Why are Angular and React so fast?"
Max Koretskyi "Why are Angular and React so fast?"Max Koretskyi "Why are Angular and React so fast?"
Max Koretskyi "Why are Angular and React so fast?"
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Functional solid
Functional solidFunctional solid
Functional solid
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
 
Python lecture 03
Python lecture 03Python lecture 03
Python lecture 03
 
C# - What's next
C# - What's nextC# - What's next
C# - What's next
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
Learning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysisLearning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysis
 
Objective-Cひとめぐり
Objective-CひとめぐりObjective-Cひとめぐり
Objective-Cひとめぐり
 
Chapter 4 - Classes in Java
Chapter 4 - Classes in JavaChapter 4 - Classes in Java
Chapter 4 - Classes in Java
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 

Semelhante a Programs as Strategies

Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)
Peter Kofler
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
eShikshak
 
Gae icc fall2011
Gae icc fall2011Gae icc fall2011
Gae icc fall2011
Juan Gomez
 
Concurrency for dummies
Concurrency for dummiesConcurrency for dummies
Concurrency for dummies
Azrul MADISA
 
vhdl.ppt Verilog Hardware Description Language
vhdl.ppt Verilog Hardware Description Languagevhdl.ppt Verilog Hardware Description Language
vhdl.ppt Verilog Hardware Description Language
shreenathji26
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
butest
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
butest
 

Semelhante a Programs as Strategies (20)

Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
 
Computing with P systems
Computing with P systemsComputing with P systems
Computing with P systems
 
Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Go Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
Go Reactive: Event-Driven, Scalable, Resilient & Responsive SystemsGo Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
Go Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
 
Overview of Python - Bsides Detroit 2012
Overview of Python - Bsides Detroit 2012Overview of Python - Bsides Detroit 2012
Overview of Python - Bsides Detroit 2012
 
python ppt.pptx
python ppt.pptxpython ppt.pptx
python ppt.pptx
 
Gae icc fall2011
Gae icc fall2011Gae icc fall2011
Gae icc fall2011
 
Concurrency for dummies
Concurrency for dummiesConcurrency for dummies
Concurrency for dummies
 
vhdl.ppt Verilog Hardware Description Language
vhdl.ppt Verilog Hardware Description Languagevhdl.ppt Verilog Hardware Description Language
vhdl.ppt Verilog Hardware Description Language
 
[系列活動] 一日搞懂生成式對抗網路
[系列活動] 一日搞懂生成式對抗網路[系列活動] 一日搞懂生成式對抗網路
[系列活動] 一日搞懂生成式對抗網路
 
Functional Programming and Composing Actors
Functional Programming and Composing ActorsFunctional Programming and Composing Actors
Functional Programming and Composing Actors
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Último (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Programs as Strategies

  • 3. Computers and software are... • Important • Ubiquitous • Complicated
  • 4. Layers of complexity • Programs • Compilers • Operating System • Hardware • ... and that’s just on one computer.
  • 5. The usual answer: models • The “layers” are already abstractions. • They encapsulate parts of the system and interact with the rest via reduced interfaces. • We don’t understand the whole truth - just a model.
  • 6. Two models of drinking from a tap • I turn on the tap and I get a drink
  • 7. Two models of drinking from a tap • Turning the tap opens a valve which releases water supplied at a pressure of 2 bar...
  • 8. Models and abstractions • Our conceptual models and their corresponding abstractions make complex systems tractable. • Really good models (e.g. scientific theories) help us predict, design, and discover. • What models will help us with programming?
  • 9. Turing machines • Alan Turing devised an early model of computation: the Turing Machine. • A TM manipulates data stored on a tape according to a table of rules - a fixed program.
  • 10. Turing machines • Turing machines expose powerful and central concepts: • computability • time usage • space usage • But they are monolithic, and too low-level to be of use in analysing modern software.
  • 11. Goal: compositionality • Software is built by assembling small pieces of code into large programs. • Our models should reflect and empower this compositionality. • We need abstractions that support composition.
  • 12. Functions • Possibly the most widely used (mathematical) abstraction. • We can happily plug functions together: • 2x • sin(x) • sin(2x) • x2 • sin(2x) + x2 • These are all functions of type (R→R)
  • 13. Understanding + • The + in the last example operates on two functions to give another function sin(2x) + x 2 • So + has the type (R→R) ⨉ (R→R) → (R→R)
  • 14. Higher-order functions and programming • These higher order functions provide both a mathematical model of programming and an approach to programming itself. • higher-order functional programming began with LISP in the 1950s.
  • 15. Programs as functions function addOne(x:int):int { return (x+1); } x↦x+1
  • 16. Programs as functions function fact(x:int):int { if (x == 0) return 1 else return (x * fact(x-1)); } x ↦ x! 3↦3⨉2 ⨉1=6 but why?
  • 17. Programs as functions function fact(x:int):int { if (x == 0) return 1 else return (x * fact(x-1)); } x ↦ x! 3↦3⨉2 ⨉1=6 but why?
  • 18. Infinity • Recursion introduces infinity to the picture. • For programmers, infinity is where the power lies. • For models, infinity is where the problems lie. • We need some topology to interpret recursion: a recursive function is modelled as the limit of an infinite sequence of approximants.
  • 19. Imperative programs? function fact(x:int):int { y := x; z := 1; while (y ≠ 0) { z := z ⨉ y; y := y-1; } return z; }
  • 20. Modelling memory • Code like y := y-1 manipulates (an abstraction of) the memory of the computer. • To model these with functions, we need to model the memory itself as a function identifiers → values.
  • 21. Mathematical semantics • These ideas were the basis of mathematical semantics developed by Strachey, Scott and others from around 1970. • The theory provides: • an understanding of recursion and loops • ways of proving certain correctness properties of programs (e.g. Hoare logic)
  • 22. But • For imperative programs, the theory breaks abstractions: functions can manipulate the memory in arbitrary ways. • There is no way to see the two factorial programs as “the same.”
  • 23. A different abstraction: interaction • Replace functions with interacting agents. • An agent is a “black box” that receives messages from its environment and sends out responses. • Ideas like these were first used in modelling concurrent systems (Hoare’s CSP, Milner’s CCS) around 1980.
  • 24. Functions as agents input output
  • 25. Functions as agents input output •That was easy!
  • 26. Functions as agents input output •That was easy! •It turns out to be more convenient to add a “request” protocol at the beginning.
  • 27. Functions as agents request request input output •That was easy! •It turns out to be more convenient to add a “request” protocol at the beginning.
  • 28. Plugging together request request request request q x2 x+3 input output input output
  • 29. Plugging together request request request request q x2 x+3 input output input output
  • 30. Plugging together request request request request q x2 x+3 input output input output
  • 31. Plugging together request request request request x2 x+3 input output input output 2
  • 32. Plugging together request request request request x2 x+3 input output input output 2
  • 33. Plugging together request request request request x2 x+3 input output input output 4
  • 34. Plugging together request request request request x2 x+3 input output input output 4
  • 35. Plugging together request request request request x2 x+3 input output input output 4
  • 36. Plugging together request request request request x2 x+3 input output input output 7
  • 37. Plugging together request request request request x2 x+3 input output input output 7
  • 38. Plugging together request request q x2+3 input output
  • 39. Plugging together request request q x2+3 input output
  • 40. Plugging together request request x2+3 input output 2
  • 41. Plugging together request request x2+3 input output 2
  • 42. Plugging together request request x2+3 input output 7
  • 43. Plugging together request request x2+3 input output 7
  • 44. A formalisation: game semantics • This idea can be made precise in lots of ways. Here’s one. • Interaction is the playing of a game between the agent and its environment. • The game specifies what moves are available and when they can be played. • Agent and environment take turns.
  • 45. The integer game request environment 0 1 2 ... agent A game is a tree of moves. Child moves can only be played once their parents have been played.
  • 46. and its dual request agent 0 1 2 ... environment
  • 47. int → int request request 0 1 2 ... 0 1 2 ...
  • 48. Strategies • Our “agents” are strategies: plans telling the agent what to do in any given situation.
  • 49. The identity function request request 0 1 2 ... 0 1 2 ...
  • 50. The identity function request request 0 1 2 ... 0 1 2 ...
  • 51. The identity function request request 0 1 2 ... 0 1 2 ...
  • 52. The identity function request request 0 1 2 ... 0 1 2 ...
  • 53. The identity function request request 0 1 2 ... 0 1 2 ...
  • 54. The identity function request request 0 1 2 ... 0 1 2 ... •This strategy implements the function x ↦ x by copying information. •It works for any game, with no knowledge of the moves: pure logic.
  • 55. History-free strategies • Every strategy we have seen so far chooses its move based only on the last move by the environment. • Notice: history-freeness is preserved by plugging.
  • 56. A new theory • These ideas form game semantics. • Games models possess a lot of the mathematical structure from the function- based semantics: • compositionality, • recursion as a limit of approximants, etc. • But also...
  • 57. A theorem Theorem [Abramsky-Jagadeesan- Malacaria 1993/2000; cf. Hyland-Ong, Nickau] Every pure functional program denotes a history-free strategy. Every computable history-free strategy can be programmed.
  • 58. Imperative programs • Imperative programs work by interacting with the computer’s memory. • Idea: model a memory location as an agent. write(true) write(false) read ok ok true false
  • 59. Interacting with the memory write(true) cell
  • 60. Interacting with the memory write(true) cell
  • 61. Interacting with the memory cell ok
  • 62. Interacting with the memory cell ok
  • 63. Interacting with the memory read cell
  • 64. Interacting with the memory read cell
  • 65. Interacting with the memory read cell write(true) ok read ...then what? A good memory location cannot be history free. But the right history-sensitive strategy is clear.
  • 66. Extending the theory • The theory of game semantics extends to arbitrary history-sensitive strategies. • Then the cell can respond appropriately to read moves, so we can model the memory. • Hence...
  • 67. Another theorem Theorem [Abramsky-M] Every program of a certain imperative language denotes a strategy. Every computable strategy can be programmed in this language.
  • 68. Infinity • Infinity enters the games models because we have to allow moves to be repeated.
  • 69. Infinity • Infinity enters the games models because we have to allow moves to be repeated.
  • 70. Infinity • Infinity enters the games models because we have to allow moves to be repeated.
  • 71. Infinity • Infinity enters the games models because we have to allow moves to be repeated. • As usual, this is where all the technical problems are.
  • 72. Taming infinity • We need infinity to interpret recursion and loops. • But we can tame it too: • restrict to finite data sets • limit the ways moves can be repeated • find out what programs are left
  • 73. Towards automated correctness checking Theorem [Ghica-M] For a certain constrained class of programs, strategies are finite state machines. Therefore, we can automatically check whether two such programs are equal.
  • 74. More • Lots of other kinds of games to model other kinds of programs, logic, ... [numerous authors] • Implementation of strategies as circuits [Ghica] • Logical counterparts of history-sensitive strategies [Churchill, Laird, M] • Relational and geometrical foundations of game semantics [Calderon, M, Power, Wingfield].
  • 75. Further • Modelling the whole system - all layers, multiple computers, people? • Quantitative models? Power consumption, transmission and storage costs, ...
  • 76. Legal bits • Photographs used under Creative Commons Attribution- NonCommercial-ShareAlike licence: • Circuit Board http://www.flickr.com/photos/loganwilliams/ • Tic Tac Toe http://www.flickr.com/photos/anonymonk/ • Code http://www.flickr.com/photos/circle_hk • Go http://www.flickr.com/photos/spwelton/ • Lego http://www.flickr.com/photos/kaptainkobold/ • Cat drinking http://www.flickr.com/photos/meantux/ • Alan Turing http://www.flickr.com/photos/sbisson/ • Blue Sky http://www.flickr.com/photos/11247304@N06

Notas do Editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n