Towards internet of code ukasz Dbek May 27, 2015 This was supposed - - PowerPoint PPT Presentation

towards internet of code
SMART_READER_LITE
LIVE PREVIEW

Towards internet of code ukasz Dbek May 27, 2015 This was supposed - - PowerPoint PPT Presentation

Towards internet of code ukasz Dbek May 27, 2015 This was supposed to be about lens library! Yes, but... This was supposed to be about lens library! Why internet is awesome? Because every device connected to it speaks the same language.


slide-1
SLIDE 1

Towards internet of code

Łukasz Dąbek May 27, 2015

slide-2
SLIDE 2

This was supposed to be about lens library!

Yes, but...

slide-3
SLIDE 3

This was supposed to be about lens library!

slide-4
SLIDE 4

Why internet is awesome?

Because every device connected to it speaks the same language. This is what enabled its fast growth.

slide-5
SLIDE 5

Why programming is awesome?

Because every programmer uses different language, libraries, data representations, all incompatible witch each other.

slide-6
SLIDE 6

Why programming is awesome?

Because every programmer uses different language, libraries, data representations, all incompatible witch each other. Take any useful piece of code and chances are that the same functionality is reimplemented in many languages.

slide-7
SLIDE 7

The problem

Suppose we are writing a website in Haskell. Example: customized gift store.

slide-8
SLIDE 8

The problem

Suppose we are writing a website in Haskell. Example: customized gift store. price :: Product -> [Addons] -> Address -> Money

slide-9
SLIDE 9

The problem

Suppose we are writing a website in Haskell. Example: customized gift store. price :: Product -> [Addons] -> Address -> Money function price(product, addons, address) { ... }

slide-10
SLIDE 10

The problem

Client needs to know total cost of order. It is not just a sum of prices!

◮ Many shipping options =

⇒ different prices.

◮ Shipping discount for big orders. ◮ Buy two Combulbulators and get third FOR FREE!

slide-11
SLIDE 11

Non-solutions

◮ Make a request to server after each state change

slide-12
SLIDE 12

Non-solutions

◮ Make a request to server after each state change (kills performance).

slide-13
SLIDE 13

Non-solutions

◮ Make a request to server after each state change (kills performance). ◮ Implement logic in Haskell and JavaScript

slide-14
SLIDE 14

Non-solutions

◮ Make a request to server after each state change (kills performance). ◮ Implement logic in Haskell and JavaScript (maintenance nightmare).

slide-15
SLIDE 15

Non-solutions

◮ Make a request to server after each state change (kills performance). ◮ Implement logic in Haskell and JavaScript (maintenance nightmare). ◮ Write in JavaScript on the servers

slide-16
SLIDE 16

Non-solutions

◮ Make a request to server after each state change (kills performance). ◮ Implement logic in Haskell and JavaScript (maintenance nightmare). ◮ Write in JavaScript on the servers (don’t even get me started on

this).

slide-17
SLIDE 17

Non-solutions

◮ Make a request to server after each state change (kills performance). ◮ Implement logic in Haskell and JavaScript (maintenance nightmare). ◮ Write in JavaScript on the servers (don’t even get me started on

this).

◮ Embedding Haskell interpreter in JavaScript

slide-18
SLIDE 18

Non-solutions

◮ Make a request to server after each state change (kills performance). ◮ Implement logic in Haskell and JavaScript (maintenance nightmare). ◮ Write in JavaScript on the servers (don’t even get me started on

this).

◮ Embedding Haskell interpreter in JavaScript (cumbersome).

slide-19
SLIDE 19

Non-solutions

◮ Make a request to server after each state change (kills performance). ◮ Implement logic in Haskell and JavaScript (maintenance nightmare). ◮ Write in JavaScript on the servers (don’t even get me started on

this).

◮ Embedding Haskell interpreter in JavaScript (cumbersome). ◮ Use language compiled to native code and JavaScript (think

js of ocaml)

slide-20
SLIDE 20

Non-solutions

◮ Make a request to server after each state change (kills performance). ◮ Implement logic in Haskell and JavaScript (maintenance nightmare). ◮ Write in JavaScript on the servers (don’t even get me started on

this).

◮ Embedding Haskell interpreter in JavaScript (cumbersome). ◮ Use language compiled to native code and JavaScript (think

js of ocaml) (not a bad solution really!).

slide-21
SLIDE 21

Core of the problem

We have shared logic, operating on shared data structures. In most use cases the functions implementing shared logic are pure.

slide-22
SLIDE 22

The problem #2

We are writing social media client for Android and iOS. User interface code is completely separate, but code for data fetching should be almost the same.

slide-23
SLIDE 23

The problem #2

We are writing social media client for Android and iOS. User interface code is completely separate, but code for data fetching should be almost the same. Sometimes we are interested in sharing little more than pure functions.

slide-24
SLIDE 24

The idea

Small language as a target for compilation and decompilation. Think of high level assembly language.

slide-25
SLIDE 25

The idea

Small language as a target for compilation and decompilation. Think of high level assembly language. It should be:

◮ pure, ◮ functional, ◮ simple, but expressive, ◮ typed, maybe even dependently typed.

slide-26
SLIDE 26

Integration

From practical point of view interaction with shared language should be hassle free. This is wrong: var ctx = new Morte.Context(); ctx.loadFile(...); ctx.callFunction("price", Morte.ADT.List(...), ...);

slide-27
SLIDE 27

This is better: import ’/my/awesome/library/prices’; price([product1], [], shipping_address);

slide-28
SLIDE 28

This is better: import ’/my/awesome/library/prices’; price([product1], [], shipping_address); After importing code it should be indistinguishable from JavaScript code in use.

slide-29
SLIDE 29

In practice the system will do more interesting things, like mapping data types to representation idiomatic in host language. We will talk about this at the end.

slide-30
SLIDE 30

The language

We will construct desired language. Let’s start with simply typed lambda calculus: E = x | λ(x : T).E | E E T = X | T → T

slide-31
SLIDE 31

The language

We will construct desired language. Let’s start with simply typed lambda calculus: E = x | λ(x : T).E | E E T = X | T → T I will use another syntax: (fun (x:A) => x) y

slide-32
SLIDE 32

The language

We will construct desired language. Let’s start with simply typed lambda calculus: E = x | λ(x : T).E | E E T = X | T → T I will use another syntax: (fun (x:A) => x) y What can we express in this language?

slide-33
SLIDE 33

Booleans

true = fun (x:A) (b:A) => x false = fun (x:A) (b:A) => y

slide-34
SLIDE 34

Booleans

true = fun (x:A) (b:A) => x false = fun (x:A) (b:A) => y if b x y = b x y

slide-35
SLIDE 35

Natural numbers

zero = fun (f:A -> A) (z:A) => z

  • ne = fun (f:A -> A) (z:A) => f z
slide-36
SLIDE 36

Natural numbers

zero = fun (f:A -> A) (z:A) => z

  • ne = fun (f:A -> A) (z:A) => f z

succ n = fun (f:A -> A) (z:A) => f (n f z)

slide-37
SLIDE 37

Natural numbers

zero = fun (f:A -> A) (z:A) => z

  • ne = fun (f:A -> A) (z:A) => f z

succ n = fun (f:A -> A) (z:A) => f (n f z) add n m = fun (f:A -> A) (z:A) => n f (m f z) mul n m = fun (f:A -> A) (z:A) => n (m f) z

slide-38
SLIDE 38

Does it look like a fold?

slide-39
SLIDE 39

STLC – problem

How to express identity function? fun (x:A) => x is not polymorphic! We need richer type system.

slide-40
SLIDE 40

System F

Polymorphic lambda calculus. We can quantify over types: id = fun (A:*) (x:A) => x - polymorphic identity.

slide-41
SLIDE 41

System F

Polymorphic lambda calculus. We can quantify over types: id = fun (A:*) (x:A) => x - polymorphic identity. The type of identity function is: id : forall (A:*). A -> A

slide-42
SLIDE 42

Pairs

Pair A B = forall (R:*). (A -> B -> R) -> R fst : forall (A B:*). (forall (R:*). (A -> B -> R) -> R) -> A In pseudonotation: fst: forall (A B:*). Pair A B -> A.

slide-43
SLIDE 43

Pairs

Pair A B = forall (R:*). (A -> B -> R) -> R fst : forall (A B:*). (forall (R:*). (A -> B -> R) -> R) -> A In pseudonotation: fst: forall (A B:*). Pair A B -> A. fst A B p = p A (fun (x:A) (y:B) => x) snd A B p = p B (fun (x:A) (y:B) => y)

slide-44
SLIDE 44

Natural number, honestly

Nat = forall (R:*). R -> (R -> R) -> R Implementation of common functions are same as in STLC.

slide-45
SLIDE 45

Lists

List A = forall (R:*). R -> (A -> R -> R) -> R nil : forall (A:*). List A cons : forall (A:*). A -> List A -> List A map : forall(A B:*). (A -> B) -> List A -> List B

slide-46
SLIDE 46

Lists

List A = forall (R:*). R -> (A -> R -> R) -> R nil : forall (A:*). List A cons : forall (A:*). A -> List A -> List A map : forall(A B:*). (A -> B) -> List A -> List B map A B f xs = xs (List B) (nil B xs) (fun (x:A) (ys:List B) => cons B (f x) ys)

slide-47
SLIDE 47

We can represent algebraic data types in System F.

slide-48
SLIDE 48

We can represent algebraic data types in System F. What else can we do?

slide-49
SLIDE 49

Existential types

Suppose that we have a module with hidden type S and functions f : S

  • > S, g : S -> Nat and constant c : S. How to express it in System

F?

slide-50
SLIDE 50

Existential types

Suppose that we have a module with hidden type S and functions f : S

  • > S, g : S -> Nat and constant c : S. How to express it in System

F? forall (R:*). (forall (S:*). S -> (S -> S) -> (S -> Nat) -> R) -> R

slide-51
SLIDE 51

Existential types

Suppose that we have a module with hidden type S and functions f : S

  • > S, g : S -> Nat and constant c : S. How to express it in System

F? forall (R:*). (forall (S:*). S -> (S -> S) -> (S -> Nat) -> R) -> R

slide-52
SLIDE 52

System Fω

To get rid of pseudonotation for polymorphic list we need another, richer type system called Fω. In a nutshell: we are introducing higher kinded types, also known as type constructors.

slide-53
SLIDE 53

Lists, honestly

List : * -> * List = fun (A:*) => forall (R:* -> *). R A -> (A -> R A -> R A) -> R A Nothing else changed much.

slide-54
SLIDE 54

Calculus of Constructions

Dependently typed version of Fω, basis for Coq (Calculus of Inductive Constructions).

slide-55
SLIDE 55

Strong normalization

All of the mentioned languages are strongly normalizing. What about the Android/iOS problem?

slide-56
SLIDE 56

Possibly infinite behaviors

We can use streams for that! That was one of the first versions of Haskell I/O.

slide-57
SLIDE 57

Possibly infinite behaviors

We can use streams for that! That was one of the first versions of Haskell I/O. And because of strong normalization property we have progress guarantee for free.

slide-58
SLIDE 58

If we have time left, we shall take a look at free monads.

slide-59
SLIDE 59

Implementation – Morte and Annah

Morte is core language – currently something between CoC and System Fω.

slide-60
SLIDE 60

Implementation – Morte and Annah

Morte is core language – currently something between CoC and System Fω. Annah is higher level language compiled to and from Morte. Imports over network works now. Most interesting feature – translating Morte data definitions into inductive definitions, GADT style.

slide-61
SLIDE 61

Implementation – Morte and Annah

Morte is core language – currently something between CoC and System Fω. Annah is higher level language compiled to and from Morte. Imports over network works now. Most interesting feature – translating Morte data definitions into inductive definitions, GADT style. Work in progress – decompilation of Annah to Haskell.

slide-62
SLIDE 62

Implementation – Morte and Annah

The author is Garbriel Gonzalez, author of „Haskell for all” blog. You can check out his Github profile and dive into the code!

slide-63
SLIDE 63

Other solutions? LLVM? asm.js? One language to rule them all?

slide-64
SLIDE 64

Thank you. Any questions?