42
1 INF 3110/4110 - 2005 Inf3110/4110 Something on non-Java-like languages, sets as functions and foldr Gerardo Schneider Department of Informatics – University of Oslo

Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

1

INF 3110/4110 -2005

Inf3110/4110

Something on non-Java-like languages, sets as functions and foldr

Gerardo SchneiderDepartment of Informatics – University of Oslo

Page 2: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

2

INF 3110/4110 -2005

Outline

Why (not) Java?

Some successful stories of non-Java-like languages

Solution to the implementation of set as functions (Exercise 3-(b))

A bit more on higher-order functions

Page 3: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

3

INF 3110/4110 -2005

Object-oriented programming: a small part of a big world

Object-oriented programming is just one tool in a vastly bigger worldFor example, consider the task of building robust telecommunications systems• Ericsson has developed a highly available ATM switch, the AXD

301, using a message-passing architecture (more than one million lines of Erlang code)

• The important concepts are isolation, concurrency, and higher-order programming

• Not used are inheritance, classes and methods, UML diagrams, and monitors

Source: Peter Van Roy’s slide of an invited talk at CLEI’05 – Cali, Colombia

Page 4: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

4

INF 3110/4110 -2005

Something on concurrent prog.

There are three main paradigms of concurrent programming• Declarative (dataflow; deterministic) concurrency• Message-passing concurrency (active entities that send asynchronous

messages; Actor model, Erlang style)• Shared-state concurrency (active entities that share common data

using locks and monitors; Java style)

Declarative concurrency is very useful, yet is little known• No race conditions; allows declarative reasoning techniques• Large parts of programs can be written with it

Shared-state concurrency is the most complicated (the worst to program in), yet it is the most widespread (e.g. Java)!• Multiple threads accessing shared variables• Interleaving semantics: huge number of cases and complicated

reasoningSource: Based on Peter Van Roy’s slides of an invited talk at CLEI’05 – Cali, Colombia

Page 5: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

5

INF 3110/4110 -2005

Some problems with Java

Java is based on the shared-state concurrency modelShared-state and message-passing models are equaly expressive (theoretically) but not in practice: The shared-state model is harder to program than the message-passing model!

Better model: objects communicating asynchronously through message-passing

Page 6: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

6

INF 3110/4110 -2005

Limitations of Java

Not good for• Internet programming (e.g. web services)• Safety critical systems (e.g. aeoronautics)• Dynamic upgrading (e.g. telecommunication)• Real-time systems (e.g. robotics)• Embedded safe systems (e.g. Smart cards)• Component-based applications • Systems involving cross-cutting concerns like history

information and synchronization (problems with the aggregation and inheritance mechanism)

• ...

Page 7: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

7

INF 3110/4110 -2005

Outline

Why (not) Java?

Some successful stories of non-Java-like languages

Solution to the implementation of set as functions (Exercise 3-(b))

A bit more on higher-order functions

Page 8: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

8

INF 3110/4110 -2005

Text editing: Lisp

Emacs is an extensible, customizable, self-documenting real-time display editorIt is a text editor and more. At its core is an interpreter for Emacs Lisp (”elisp'', for short), a dialect of the Lisp PL with extensions to support text editing Lisp is a family of functional languages. The two major dialects in use today are Common Lispand Scheme (see http://www.lisp.org/table/lisp.htm)See http://www.lisp.org/table/good.htm for more applications of Lisp

Source:http://www.gnu.org/software/emacs/emacs.html#Whatis

Page 9: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

9

INF 3110/4110 -2005

Aeronautics: Scade

Scade is the commercial version of Lustre, a synchronous language dedicated to reactive systems developed at VERIMAG (France)Some features: low overhead, bounded stack sizes, no race conditions, no threads or processes, verifiable (finite) behavior, certified compiler (avionics application)It has been used for programming controlers in the Arian V Launcher, Airbus, Renault, nuclear power plants, etc

Source: slides by E. Lee (http://ptolemy.eecs.berkeley.edu/presentations/04/citrisdaysinfineon.ppt ) and the Synchronous group at VERIMAG (http://www-verimag.imag.fr/SYNCHRONE/)

Page 10: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

10

INF 3110/4110 -2005

Embedded systems: Simulink

Simulink is a block library tool for modeling, simulating and analyzing dynamic systems. It is developed by The MathWorksUsed in control theory and digital signal processing for multidomain simulation and design Coupled with Real-Time Workshop: may be used for embedded control software. Widely used in the automotive industry

Source: http://en.wikipedia.org/wiki/Simulink

Page 11: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

11

INF 3110/4110 -2005

Aerospace: Mozart

The MISURE project is a big aerospace project involving Dassault, Alenia, Axlog, Datamat, and SIA, and UCL (Belgium) to build a mission planning system for patrols of uninhabited flying vehiclesThe planning system had to work in soft real time with online planning, written in Mozart. It was completed successfully (April 2005)Mozart is a multi-paradigm programming language developped by an international consortium (see http://www.mozart-oz.org/features.html)

Source: Personal communication by Peter Van Roy

Page 12: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

12

INF 3110/4110 -2005

Other projects using Mozart

BioMaze: will study the storage, visualization, and analysis of biochemical networksEvergrow: experiment with structured peer-to-peer systems based on DHTs (Distributed Hash Tables) and decentralized service architectures based on DHTs Alenia Aeronautica implemented a Sw module for the automatic production of FMEA (Failure Mode Effects Analysis) to identify, prioritise and eliminate known potential failures, problems and errors from systems under design, using Mozart

Source: Personal communication by Peter Van Roy. See also http://www.mozart-oz.org/archive.cgifor more project descriptions using Mozart

Page 13: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

13

INF 3110/4110 -2005

Telecommunications: Erlang

Erlang is a functional language for reliable concurrent and distributed systems developped at Ericsson and SICS (Sweden)Some applications: used by Ericsson in the phone switches AXD301, DWOS, A910, and ANx Highly reliable: less than 3 minutes of downtime in one year of operation

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 14: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

14

INF 3110/4110 -2005

Finance: Haskell

Haskell is a lazy pure functional language (see http://www.haskell.org)

Application: a combinator library for describing compositional financial contracts (by S. Peyton Jones, J.-M. Eber and J. Seward)A new enterprise was created based on these ideas (Lexifi Technologies)There are many other applications! (see http://www.haskell.org/practice.html)

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 15: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

15

INF 3110/4110 -2005

More applications of Haskell

Galois is a Sw development company based in U.S.A developing Sw under contract, and every project uses Haskell (also SML-NJ and OCaml) Some applications: • Cryptol, a domain-specific language for cryptography (with an

interpreter and a compiler); • A cross-domain file and web server; • A GUI debugger for a specialized chip; • A tool for easily embedding new syntax in the client's own

language;• A legacy code translator (translating from K&R C to ANSI C, while

moving from SunOS 4 to Solaris and a new abstract API)

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 16: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

16

INF 3110/4110 -2005

Theorem prover 1: ML

Isabelle is a generic theorem prover. New logics are introduced by specifying their syntax and rules of inference. Proof procedures can be expressed using tactics and tacticals It was developped by L.C. Paulson and T. NipkowWritten in SML

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 17: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

17

INF 3110/4110 -2005

Theorem prover 2: Lisp and ML

The HOL system is a powerful computer program for constructing formal specifications and proofs in higher order logic Used in both industry and academia to support formal reasoning in Hw design and verification, reasoning about security, proofs about real-time systems, semantics of Hw description lang., compiler verification, program correctness, modelling concurrency, and program refinement HOL88 is the original Cambridge HOL system built using Lisp; HOL90 is a reimplementation in SML

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 18: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

18

INF 3110/4110 -2005

Theorem prover 3: ML

ALF is an interactive theorem prover (a proof editor) based on Martin-Löf's type theory with explicit substitutionThe proof engine of ALF is written in SMLDevelopers: Thierry Coquand, Lena Magnusson and Bengt Nordström/ Programming Logic Group at Chalmers, Sweden

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 19: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

19

INF 3110/4110 -2005

Verification tool: ML and Lisp

The Java PathFinder, JPF, is a translator from Java to Promela, the programming language of the SPIN model checker. The purpose is to establish a framework for verification and debugging of Java programs using model checking. The system detects deadlocks and violations of assertions stated by the programmer Written in Common Lisp and Moscow MLDeveloped at NASA Ames (USA)

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 20: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

20

INF 3110/4110 -2005

Web, HTML, XML: Scheme

SXML and SXML tools are S-expression-based implementations of W3C XML recommendations, the embedding of XML data and XML query and manipulation toolsWritten in Scheme, a statically scoped and properly tail-recursive dialect of the Lisp programming language invented by Guy Lewis Steele Jr. and Gerald Jay Sussman. Designed to have an exceptionally clear and simple semantics and few different ways to form expressions

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 21: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

21

INF 3110/4110 -2005

Scheduling: Constraint Programming

FriarTuck is a Sw company dedicated to developing and marketing software for scheduling of complex events. FriarTuck powers its products with unique optimization technologies, utilizing the latest developments in constraint programming, Operations Research and heuristic searchExample of application: development of a scheduler for the NASA Mars Exploration Rover Mission (see http://www.friartuck.net/)

Source: Thanks to Peter Van Roy for pointing out the link

Page 22: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

22

INF 3110/4110 -2005

Distributed architecture: Rebol

REBOL (pronounced "reble") was designed over a 20 year period by Carl Sassenrath. In 1997, REBOL was released for 3 operating systems to a small group of users. Today REBOL runs on more than 40 OS platforms and has grown to reach more than 1,000,000 usersFeatures: highly reflective and promotes functional-style scripting. The syntax and features are similar to Scheme, Forth and Logo

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 23: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

23

INF 3110/4110 -2005

Design-to-software dev.: 001 AXEL

001 Axel is a graphical, executable specification language that adopts (or reinvents) many ideas from functional programming The 001 Tool Suite is a seamless systems engineering design-to-software development environment for automatically generating complete, ultra-reliable, software systems, models, and simulationsUsed successfully at Citibank, AT&T, and for other commercial systems

Source: http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 24: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

24

INF 3110/4110 -2005

More applications of non-Java like PL

Much, much more: I haven’t mentioned here much on actor- and aspect-oriented programming, Internet programming, real-time languages, embedded systems, logic programming (Prolog), etcFor more applications of functional languages see for instance ”Functional Programming in the Real World”:

http://homepages.inf.ed.ac.uk/wadler/realworld/index.html

Page 25: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

25

INF 3110/4110 -2005

Why to study ML then?

There are enough applications of ML-like languages!Anyway, the intention of the course is to teach new concepts, not a new languageML is a simple function-oriented language with many interesting features• Type inference algorithm• Polymorphism• Higher-order functions• Garbage collection• Abstract data types• Module system• Exceptions

Page 26: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

26

INF 3110/4110 -2005

Outline

Why (not) Java?

Some successful stories of non-Java-like languages

Solution to the implementation of sets as functions (Exercise 3-(b))

A bit more on higher-order functions

Page 27: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

27

INF 3110/4110 -2005

Signature

Implement a set structure with the following signature:

signature Set_def =sig type ''Elem Set

val empty: ''Elem Setval has : ''Elem Set * ''Elem -> boolval add : ''Elem Set * ''Elem -> ''Elem Setval del : ''Elem Set * ''Elem -> ''Elem Setval union : ''Elem Set * ''Elem Set -> ''Elem Setval inter : ''Elem Set * ''Elem Set -> ''Elem Set

end;

Page 28: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

28

INF 3110/4110 -2005

Informal description

This is the informal description of the functions:• has: Tests whether a given element is member of a set• add: Adds an element to a set• del: Delete an element from the set • union: Performs the union of two sets• inter: Performs the intersection of two sets• empty: Gives the empty set

Additionally we need the following function:• less: Takes two elements and determines whether the first is smaller than the second one

Page 29: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

29

INF 3110/4110 -2005

Sets as functions

Examples (set of integers)• A = {} =>

fA = {(1, false), (2, false), ..., (12353, false), ...}• B = {1, 3, 56} =>

fB = {(1,true), (2,false), (3,true), (4,false), ..., (55,false), (56,true), (57,false), ...}

Ex:• has(fB, 4) = fB(4) = false• has(fB, 3) = fB(3) = true

Page 30: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

30

INF 3110/4110 -2005

Implementation

We will define the function less as follows:

This is one possible implementation (set of integers):type ''Elem Set = int -> bool;

val empty = fn(x) => false;fun has(s,x) = s x; (* has returns bool *)fun add(s,x) = fn(y) => if x = y then true else s y;fun del(s,x) = fn(y) => if x = y then false else s y;fun union(s1,s2) = fn(x) => (s1 x) orelse (s2 x);fun inter(s1,s2) = fn(x) => (s1 x) andalso (s2 x);

fun less (x,x') = x<x';

Page 31: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

31

INF 3110/4110 -2005

empty

val empty = fn(x) => false;> val empty = fn : 'a -> bool

“empty” is the function which applied to any element gives false

Example:

- empty 3;> val it = false : bool

Page 32: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

32

INF 3110/4110 -2005

add

fun add(s,x)= fn(y) => if x=y then true else s y; > val add = fn : (''a -> bool) * ''a -> ''a -> bool

“add” is the function which takes a function (set) and an element (added to the set) and when applied to any other element gives trueif the element is in the set and gives false otherwise

Examples- add(empty, 1); > val it = fn : int -> bool- add(add(empty, 1),3); > val it = fn : int -> bool- val B = add(add(add(empty, 1),3),56);

> val it = fn : int -> bool

- B 3; > val it = true : bool- B 6; > val it = false : bool

Page 33: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

33

INF 3110/4110 -2005

has

fun has (s,x) = s x;> val has = fn : ('a -> 'b) * 'a -> 'b

“has” is the function which takes a function (set) and an element and applies the function to the element

Example. Let us take B as before - val B = add(add(add(empty, 1), 3), 56);

Then

- has(B, 4); > val it = false : bool- has(B, 56); > val it = true : bool

Page 34: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

34

INF 3110/4110 -2005

del

fun del (s,x) = fn(y) => if x = y then false else s y;> val del = fn : (''a -> bool) * ''a -> ''a -> bool

“del” is the function which takes a function (set) and an element and gives a function

Example. Let B be: val B = add(add(add(empty, 1), 3), 56);

Then- del(B, 3) 3; > val it = false : bool- del(B, 4) 3; > val it = true : bool- has(del(B, 4), 4); > val it = false : bool- has(del(B, 56), 3); > val it = true : bool

Page 35: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

35

INF 3110/4110 -2005

union

fun union(s1,s2) = fn(x) => (s1 x) orelse (s2 x);> val del = fn : ('a -> bool) * ('a -> bool) -> 'a -> bool

“union” takes two functions (sets) and applies to an element to check whether the element is in at least one of the sets

Example. Let B and C be: val B = add(add(add(empty, 1), 3), 56); val C = add(add(empty, 2), 3);

Then- union(B, C) 3; > val it = true : bool- union(B, C) 1; > val it = true : bool- union(B, C) 50; > val it = false : bool- union(del(B, 1), C) 1; > val it = false : bool

Page 36: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

36

INF 3110/4110 -2005

inter

fun inter(s1,s2) = fn(x) => (s1 x) andalso (s2 x);> val del = fn : ('a -> bool) * ('a -> bool) -> 'a -> bool

“inter” takes two functions (sets) and applies to an element to check whether the element is in both sets

Example. Let B and C be: val B = add(add(add(empty, 1), 3), 56); val C = add(add(empty, 2), 3);

Then- inter(B, C) 3; > val it = true : bool- inter(B, C) 1; > val it = false : bool- inter(union(B, C), C) 56; > val it = false : bool- inter(union(B, C), C) 2; > val it = true : bool

Page 37: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

37

INF 3110/4110 -2005

Outline

Why (not) Java?

Some successful stories of non-Java-like languages

Solution to the implementation of sets as functgions (Exercise 3-(b))

A bit more on higher-order functions

Page 38: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

38

INF 3110/4110 -2005

Higher-order functions (functionals)

Functions are computational values, hence they can be passed as an argument to another function

Program are more concise and clear when using functionalsSome predefined functionals in ML: map, filter, foldr, foldl

A functional is a function that operates on other functions

Page 39: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

39

INF 3110/4110 -2005

The definition of foldr

fun foldr f e [] = e| foldr f e (x::xs) = f (x, foldr f e xs);> val foldr = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b

foldr takes three arguments • A function f : 'a * 'b -> 'b• An argument e : 'b• A list xs : 'a list

And returns something of type 'bIt recursively applies f to e and all the elements of the list xs

Page 40: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

40

INF 3110/4110 -2005

An example

Remember that :: is the “cons” operator of lists:- "three"::["four", "five"];> val it = ["three","four","five"] : string listWith op:: we denote that :: is an infix operator

Application of foldr- foldr op:: ["three", "four", "five"] ["one","two"];> val it = ["one","two","three","four","five"] : string list

How does it work?

Page 41: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

41

INF 3110/4110 -2005

An example (cont.)

fun foldr f e [] = e | foldr f e (x::xs) = f (x, foldr f e xs);

foldr op:: ["three", "four", "five"] ["one","two"]= op:: ("one", foldr op:: ["three", "four", "five"] ["two"])= op:: ("one", op::("two", foldr op:: ["three", "four",

"five"] [])= op:: ("one", op::("two", ["three", "four", "five"])= "one" :: ("two" :: ["three", "four", "five"]);= ["one","two","three","four","five"]

Page 42: Something on non-Java-like languages, sets as functions ... · • Large parts of programs can be written with it Shared-state concurrency is the most complicated (the worst to

42

INF 3110/4110 -2005

Defining union with foldr

Remember that union may be defined as (Mandatory exercise 3-(a))- fun union(s1,s2) =

case s2 of [] => s1| x::r2 => union(add(x,s1),r2);

An alternative definition using foldr

- fun union(s1,s2) = foldr add s1 s2;

Check it and try to understand how does it work!