Concepts of programming languages Janus Joris ten Tusscher, Joris - - PowerPoint PPT Presentation

concepts of programming languages
SMART_READER_LITE
LIVE PREVIEW

Concepts of programming languages Janus Joris ten Tusscher, Joris - - PowerPoint PPT Presentation

[Faculty of Science Information and Computing Sciences] Concepts of programming languages Janus Joris ten Tusscher, Joris Burgers, Ivo Gabe de Wolff, Cas van der Rest, Orestis Melkonian 1 [Faculty of Science Information and Computing


slide-1
SLIDE 1

[Faculty of Science Information and Computing Sciences] 1

Concepts of programming languages

Janus

Joris ten Tusscher, Joris Burgers, Ivo Gabe de Wolff, Cas van der Rest, Orestis Melkonian

slide-2
SLIDE 2

[Faculty of Science Information and Computing Sciences] 2

Janus

A reversible programming language. Not turing complete!

slide-3
SLIDE 3

[Faculty of Science Information and Computing Sciences] 3

Reversibility

Every statement can be reverted. No history is stored.

x += y * 3 x -= y * 3

slide-4
SLIDE 4

[Faculty of Science Information and Computing Sciences] 3

Reversibility

Every statement can be reverted. No history is stored.

x += y * 3 x -= y * 3

slide-5
SLIDE 5

[Faculty of Science Information and Computing Sciences] 4

Injective functions

Reversible languages can only compute injective functions. ∀x, y : f(x) = f(y) = ⇒ x = y (1) Every output has only a single input. (2)

slide-6
SLIDE 6

[Faculty of Science Information and Computing Sciences] 4

Injective functions

Reversible languages can only compute injective functions. ∀x, y : f(x) = f(y) = ⇒ x = y (1) Every output has only a single input. h(x) = (x, g(x)) (2)

slide-7
SLIDE 7

[Faculty of Science Information and Computing Sciences] 5

Turing completeness

Turing machines can compute non-injective functions. Reversible languages are not turing complete. Reversible Turing complete.

slide-8
SLIDE 8

[Faculty of Science Information and Computing Sciences] 6

Turing machines

Infinite tape of memory Finite set of states Transition function

▶ Current state ▶ Current symbol on tape ▶ Write symbol ▶ Move tape pointer ▶ Next state

slide-9
SLIDE 9

[Faculty of Science Information and Computing Sciences] 7

Turing machines

Forward deterministic: given any state and tape, there is at most one transition from that state. Backward deterministic: given any state and tape, there is at most one transition to that state. P is the class of forward deterministic turing machines, NP

  • f non-deterministic turing machines.

Reversible Turing complete: a language that can simulate forward and backward deterministic turing machines.

slide-10
SLIDE 10

[Faculty of Science Information and Computing Sciences] 8

What do reversible languages compute

Given a forward deterministic turing machine that computes f(x), There exists a reversible turing machine that computes x → (x, f(x)). More memory.

slide-11
SLIDE 11

[Faculty of Science Information and Computing Sciences] 9

Motivation

What are reasons to pursue logically reversible computations? More heat efficient circuitry Quantum computing

slide-12
SLIDE 12

[Faculty of Science Information and Computing Sciences] 9

Motivation

What are reasons to pursue logically reversible computations?

▶ More heat efficient circuitry

Quantum computing

slide-13
SLIDE 13

[Faculty of Science Information and Computing Sciences] 9

Motivation

What are reasons to pursue logically reversible computations?

▶ More heat efficient circuitry ▶ Quantum computing

slide-14
SLIDE 14

[Faculty of Science Information and Computing Sciences] 10

A small detour

Let’s venture into the realm of physics: Imagine a set of balls bouncing around in a frictionless world:

slide-15
SLIDE 15

[Faculty of Science Information and Computing Sciences] 11

Bouncing balls 1

Figure 1: bouncing balls in a world without friction

All information about both future and past configurations is preserved.

slide-16
SLIDE 16

[Faculty of Science Information and Computing Sciences] 11

Bouncing balls 1

Figure 1: bouncing balls in a world without friction

All information about both future and past configurations is preserved.

slide-17
SLIDE 17

[Faculty of Science Information and Computing Sciences] 12

Bouncing balls 2

Figure 2: bouncing balls in the real world

Information about past configurations gets lost as the balls lose velocity.

slide-18
SLIDE 18

[Faculty of Science Information and Computing Sciences] 12

Bouncing balls 2

Figure 2: bouncing balls in the real world

Information about past configurations gets lost as the balls lose velocity.

slide-19
SLIDE 19

[Faculty of Science Information and Computing Sciences] 13

This information is not truely lost, however. Entropy of the system must increase or remain equal. In this case, heat is dissipated into the environment.

slide-20
SLIDE 20

[Faculty of Science Information and Computing Sciences] 13

This information is not truely lost, however. Entropy of the system must increase or remain equal. In this case, heat is dissipated into the environment.

slide-21
SLIDE 21

[Faculty of Science Information and Computing Sciences] 14

Landauer’s principle

In computers, information about past states is often lost (or erased) as computations are carried out. However, the second law of thermodynamics still applies.

slide-22
SLIDE 22

[Faculty of Science Information and Computing Sciences] 14

Landauer’s principle

In computers, information about past states is often lost (or erased) as computations are carried out. However, the second law of thermodynamics still applies.

slide-23
SLIDE 23

[Faculty of Science Information and Computing Sciences] 15

This means that circuits must dissipate some amount of heat as information gets destroyed. Commonly refered to as Landauer’s principle.

slide-24
SLIDE 24

[Faculty of Science Information and Computing Sciences] 15

This means that circuits must dissipate some amount of heat as information gets destroyed. Commonly refered to as Landauer’s principle.

slide-25
SLIDE 25

[Faculty of Science Information and Computing Sciences] 16

Reversible computing

Figure 3: Billiard ball AND-gate

This is also refered to as a Toffoli gate.

slide-26
SLIDE 26

[Faculty of Science Information and Computing Sciences] 16

Reversible computing

Figure 3: Billiard ball AND-gate

This is also refered to as a Toffoli gate.

slide-27
SLIDE 27

[Faculty of Science Information and Computing Sciences] 17

Toffoli gates are mainly theoretical But ciruits with energy dissipation below the von Neumann-Landauer limit have been built.

slide-28
SLIDE 28

[Faculty of Science Information and Computing Sciences] 17

Toffoli gates are mainly theoretical But ciruits with energy dissipation below the von Neumann-Landauer limit have been built.

slide-29
SLIDE 29

[Faculty of Science Information and Computing Sciences] 18

Quantum Computing

How does all this apply to quantum computing? The underlying physical processes of quantum computing are actually fundamentally reversible.

slide-30
SLIDE 30

[Faculty of Science Information and Computing Sciences] 18

Quantum Computing

How does all this apply to quantum computing? The underlying physical processes of quantum computing are actually fundamentally reversible.

slide-31
SLIDE 31

[Faculty of Science Information and Computing Sciences] 19

Similar to the frictionless billiard ball gate, information cannot leave a quantum circuit in the form of heat. The system is said to be locigally reversible.

slide-32
SLIDE 32

[Faculty of Science Information and Computing Sciences] 20

Logical reversiblity

Since all logical information is preserved in such systems, it is imposible to carry out certain computations. Specifically, it is impossible to carry out computations that reach a logical state that can also be reached through other paths of computation. Notice the similarity with reversible Turing machines!

slide-33
SLIDE 33

[Faculty of Science Information and Computing Sciences] 20

Logical reversiblity

Since all logical information is preserved in such systems, it is imposible to carry out certain computations. Specifically, it is impossible to carry out computations that reach a logical state that can also be reached through other paths of computation. Notice the similarity with reversible Turing machines!

slide-34
SLIDE 34

[Faculty of Science Information and Computing Sciences] 21

Variables

▶ All global variables ▶ Default value ▶ Modification operators ▶ Only support for +=, -= and ˆ=

slide-35
SLIDE 35

[Faculty of Science Information and Computing Sciences] 22

Limitations

▶ There is no *= and /= ▶ A variable that occurs on the left can not occur on the

right in the same statement

▶ x-=x is forbidden

a b c procedure main a += 3 b -= a + 4 c += a - b

slide-36
SLIDE 36

[Faculty of Science Information and Computing Sciences] 23

Procedures

▶ No parameters ▶ There exists a version with parameters ▶ Pass by reference a procedure main call f uncall g procedure f a += 3 procedure g a -= 5 a += 1

slide-37
SLIDE 37

[Faculty of Science Information and Computing Sciences] 24

Loop

from e1 do s1 loop s2 until e2 ▶ e1 is true only the first iteration, false every other

iteration

▶ s1 is executed after e1 on every iteration ▶ e2 is false until the last run ▶ s2 is executed if e2 is true, continue to e1

slide-38
SLIDE 38

[Faculty of Science Information and Computing Sciences] 25

Loop

a b procedure main from a = 0 do a += 1 loop b += a until a = 10

The result is { a = 10, b = 45 }

slide-39
SLIDE 39

[Faculty of Science Information and Computing Sciences] 26

Example

fib: calculates (n+1)-th and (n+2)-th Fibonacci number. procedure fib if n = 0 then x1 += 1 ; -- 1st Fib nr is 1. x2 += 1 ; -- 2nd Fib nr is 1. else n -= 1 call fib x1 += x2 x1 <=> x2 fi x1 = x2

slide-40
SLIDE 40

[Faculty of Science Information and Computing Sciences] 27

Example

fib: calculates (n+1)-th and (n+2)-th Fibonacci number. procedure fib if n = 0 then x1 += 1 ; -- 1st Fib nr is 1. x2 += 1 ; -- 2nd Fib nr is 1. else n -= 1 call fib x1 += x2 x1 <=> x2 fi x1 = x2 ; -- Why do we need this?

Q: How do we calculate the inverse?

slide-41
SLIDE 41

[Faculty of Science Information and Computing Sciences] 27

Example

fib: calculates (n+1)-th and (n+2)-th Fibonacci number. procedure fib if n = 0 then x1 += 1 ; -- 1st Fib nr is 1. x2 += 1 ; -- 2nd Fib nr is 1. else n -= 1 call fib x1 += x2 x1 <=> x2 fi x1 = x2 ; -- Why do we need this? ▶ Q: How do we calculate the inverse?

slide-42
SLIDE 42

[Faculty of Science Information and Computing Sciences] 28

Example

fib: calculates (n+1)-th and (n+2)-th Fibonacci number. procedure fib if n = 0 then x1 += 1 ; -- 1st Fib nr is 1. x2 += 1 ; -- 2nd Fib nr is 1. else n -= 1 call fib x1 += x2 x1 <=> x2 fi x1 = x2 ; -- Used for inverting the if-statement.

slide-43
SLIDE 43

[Faculty of Science Information and Computing Sciences] 29

Example

fib: calculates (n+1)-th and (n+2)-th Fibonacci number. procedure fibInverse if x1 = x2 then x2 -= 1 ; -- 2nd Fib nr is 1. x1 -= 1 ; -- 1st Fib nr is 1. else x1 <=> x2 x1 -= x2 call fibInverse n += 1 fi n = 0

slide-44
SLIDE 44

[Faculty of Science Information and Computing Sciences] 30

Example

fib: calculates (n+1)-th and (n+2)-th Fibonacci number. ▶ Q: What does the inverse of fib do? procedure fibInverse if x1 = x2 then x2 -= 1 ; -- 2nd Fib nr is 1. x1 -= 1 ; -- 1st Fib nr is 1. else x1 <=> x2 x1 -= x2 call fibInverse n += 1 fi n = 0

slide-45
SLIDE 45

[Faculty of Science Information and Computing Sciences] 31

Relational Programming

Injective Programming Relational Programming r-Turing Complete Turing Complete backwards deterministic backwards non-deterministic restricted language constructs search procedure (aka resolution)

slide-46
SLIDE 46

[Faculty of Science Information and Computing Sciences] 32

Prolog basics

A logic program consists of facts and rules.

parent(alice, joe). parent(bob, joe). parent(joe, mary). parent(gloria, mary). ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). descendant(X, Y) :- ancestor(Y, X).

slide-47
SLIDE 47

[Faculty of Science Information and Computing Sciences] 33

The user can then query the runtime system, as such:

?- ancestor(alice, mary). true. ?- parent(X, mary). X = joe; X = gloria. ?- ancestor(X, mary), not parent(X, mary). X = alice; X = bob.

slide-48
SLIDE 48

[Faculty of Science Information and Computing Sciences] 34

Demonstration - Type Predicate

Assume a type predicate, relating expressions with types:

type(expr, t) :- ... .

You would normally use it to perform type-checking:

?- type(1 + 1, int). true. ?- type(1 + 1, string). false.

slide-49
SLIDE 49

[Faculty of Science Information and Computing Sciences] 34

Demonstration - Type Predicate

Assume a type predicate, relating expressions with types:

type(expr, t) :- ... .

You would normally use it to perform type-checking:

?- type(1 + 1, int). true. ?- type(1 + 1, string). false.

slide-50
SLIDE 50

[Faculty of Science Information and Computing Sciences] 35

But you can also perform type-inference:

?- type(1 + 1, Type). Type = int. ?- type("hello world", Type). Type = string. ?- type(λx:int -> x, Type). Type = int -> int. ?- type(λx -> x, int -> Type). Type = int.

slide-51
SLIDE 51

[Faculty of Science Information and Computing Sciences] 36

Going in the reverse direction, you can generate programs:

?- type(Expr, int). Expr = 1; Expr = 2; ... Expr = 1 + 1; Expr = 1 + 2; ... Expr = if true then 1 else 1; ...

Of course, this does not make much sense without a sufficiently expressive type system.

slide-52
SLIDE 52

[Faculty of Science Information and Computing Sciences] 37

Demonstration - Relational Interpreter

Assume you have implemented a relational interpreter:

eval(program, result) :- ... . ?- eval(map (+ 1) [1 2 3], Result). Result = [2 3 4].

Non-deterministic constructs are also natural:

?- eval(amb [a, b, c], Result). Result = a; Result = b; Result = c.

slide-53
SLIDE 53

[Faculty of Science Information and Computing Sciences] 37

Demonstration - Relational Interpreter

Assume you have implemented a relational interpreter:

eval(program, result) :- ... . ?- eval(map (+ 1) [1 2 3], Result). Result = [2 3 4].

Non-deterministic constructs are also natural:

?- eval(amb [a, b, c], Result). Result = a; Result = b; Result = c.

slide-54
SLIDE 54

[Faculty of Science Information and Computing Sciences] 38

But you can also perform program synthesis by-example:

?- eval(F 1, 2),...,eval(map F [1 2 3], [2 3 4]). ... F = λx -> x + 1; ... F = λx -> x - 10 + 10 + 1; ...

Quine generation is straightforward:

?- eval(Quine, Quine). ... Quine = (λa -> a ++ show a) "(λa -> a ++ show a) "; ...

slide-55
SLIDE 55

[Faculty of Science Information and Computing Sciences] 38

But you can also perform program synthesis by-example:

?- eval(F 1, 2),...,eval(map F [1 2 3], [2 3 4]). ... F = λx -> x + 1; ... F = λx -> x - 10 + 10 + 1; ...

Quine generation is straightforward:

?- eval(Quine, Quine). ... Quine = (λa -> a ++ show a) "(λa -> a ++ show a) "; ...

slide-56
SLIDE 56

[Faculty of Science Information and Computing Sciences] 39

Logic Programming IRL

In practice, bi-directionality breaks with the usage of extra-logical features:

▶ Variable projection: inspecting values at runtime ▶ Cut (!): disables backtracking in certain places ▶ Assert/Retract: dynamically insert/remove facts

MiniKanren is a more recent logic programming language, which avoids extra-logical features (as much as possible).

slide-57
SLIDE 57

[Faculty of Science Information and Computing Sciences] 39

Logic Programming IRL

In practice, bi-directionality breaks with the usage of extra-logical features:

▶ Variable projection: inspecting values at runtime ▶ Cut (!): disables backtracking in certain places ▶ Assert/Retract: dynamically insert/remove facts

MiniKanren is a more recent logic programming language, which avoids extra-logical features (as much as possible).

slide-58
SLIDE 58

[Faculty of Science Information and Computing Sciences] 40

Higher abstraction

▶ Relational programming, as well as functional

programming, both belong to the declarative paradigm.

▶ They both focus on what a program does, rather than

how. Question How can we combine them, to get the best of both worlds?

slide-59
SLIDE 59

[Faculty of Science Information and Computing Sciences] 40

Higher abstraction

▶ Relational programming, as well as functional

programming, both belong to the declarative paradigm.

▶ They both focus on what a program does, rather than

how. Question How can we combine them, to get the best of both worlds?

slide-60
SLIDE 60

[Faculty of Science Information and Computing Sciences] 41

Hanus: Janus embedded in Haskell

In our research project, we use TemplateHaskell and QuasiQuotation to embed Janus in Haskell:

[hanus| procedure encode(im :: Image, ret :: [Byte]) {

  • - Janus commands with antiquotation

}|] encode :: Image -> [Byte] encode = call encode decode :: [Byte] -> Image decode = uncall encode

Come and check out our poster in de Vagant!

slide-61
SLIDE 61

[Faculty of Science Information and Computing Sciences] 42

Thanks! Questions?

From CodeComics.com, modified.

slide-62
SLIDE 62

[Faculty of Science Information and Computing Sciences] 43

References

Axelsen, Holger Bock, and Robert Glück. “What do reversible programs compute?.” FoSSaCS. 2011. Yokoyama, Tetsuo, and Robert Glück. “A reversible programming language and its invertible self-interpreter.” Proceedings of the 2007 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation. ACM, 2007.