s and k - risc machineats/plt-2005-1/ski.pdfto mock a mockingbird • and other logic puzzles...

45
S and K - RISC Machine Rhys Price Jones

Upload: others

Post on 19-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

S and K - RISC MachineRhys Price Jones

Page 2: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

To Mock a Mockingbird• and other logic puzzles including an

amazing adventure in combinatory logic

• Raymond Smullyan

• What is the name of this book

• Chess mysteries of Sherlock Holmes

• Knights and Knaves

• Lady or the Tiger

Page 3: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Combinatory Logic•Combinatory logic was invented by

Moses Schönfinkel in the early 1920s, and was mostly developed by Haskell Curry. The idea was to reduce the notation of logic to the simplest terms possible. As such, combinatory logic consists only of combinators, combination operations, and no free variables

•[ http://planetmath.org/encyclopedia/CombinatoryLogic.html ]

• What has this to do with Mockingbirds?

www.mockingbirds.org/ mockingbirds-pictures.htm

Page 4: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Smullyan Says:

• “A certain enchanted forest is inhabited by talking birds. Given any two birds A and B, if you call out the name of B to A, then A will respond by calling out the name of some bird to you; this bird we designate by AB”

Page 5: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Rules of Birds

• In general, AB != BA

• A’s response to B is not necessarily the same as B’s response to A

• In general (AB)C != A(BC)

Page 6: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

A familiar bird

• You probably know the Bluebird

• B x y z = x (y z)

• Perhaps it’s more familiar with differently named variable birds:

• B f g x = f (g x)

• Composition

Page 7: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Portrait of a Bluebird

z

y

xBx

y z

Page 8: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

A Dove

z

y

xD

w

z wx y

Looks like 2 compositions

Page 9: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

A Dove = 2 Bluebirds

• B x y z = x (y z)

• D x y z w = x y (z w)

• B B x y z w = B (x y) z w= (x y) (z w)= x y (z w)

Page 10: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Mockingbird

• A mockingbird is a bird M such that, for any bird x

• M x = x x

• M’s response to any x is the same as x’s response to itself

Page 11: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Rules of Combinatory Logic

• Composition: For any two birds A and B there is a bird C such that for any bird x

• C x = A (B x)

• C composes A with B

• Mockingbird: The forest contains a mockingbird

Page 12: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Fondness

• It may happen that if you call out bird B to bird A, bird A will respond to you with bird B.

• This means A is fond of B

• A B = B

Page 13: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Theorem (Gödel)

• (But not the famous one)

• Fixpoint theorem

• Every bird in the forest is fond of at least one bird ∀x ∃y xy = y

• Proof: For any A, by composition, there is a C composing A with the mockingbird MSo for any x, C x = A (M x)In particular C C = A (M C) = A (C C) [since M is a mockingbird]Thus A is fond of C C

Page 14: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

More birds• Is there an Egocentric Bird, E E = E?

• (hint, who is M fond of?)

• Is there a hopelessly egocentric bird?

• H x = H for all birds x

• (depends on existence of a subtractive bird, such as the Kestrel)

• (K x) y = x

Page 15: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Sage Bird

• What has this to do with computing?

Θ x = x (Θ x)

xΘ x

Θ x

Page 16: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

What is Computing?

• Let’s ask some experts

• When did computing begin?

• Don’t believe any Association for Computing Machinery

• Let’s form an Association for Computing People!

Page 17: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Some people

• believe in the Lambda Calculus

• aka Scheme!

• exp :== identifier λ id. exp exp1 exp2

• α-reduction, β-reduction

Alonzo Church

Kent Dybvig

Guy Steele

Page 18: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Some Scheme Programs> 4242> (+ 2 2)4> (+ 2 (* 3 4))14> (* 1357908642 91357990864201) ; no wimpy ints/floats124055805310255586325042>

Page 19: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

More Scheme> (define fac (lambda (x) (if (zero? x) 1 (* x (fac (1- x))))))> (fac 3)6> (fac 5)120> (fac 100)93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000>

Page 20: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

What is computing?(1)

• A computer is a device that correctly interprets Scheme programs

Page 21: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

C for others#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#define t(x) typedef x#define G return#define Y(x) x;x#define e(s) exit(s);#define b(x,o) x o x#define Z while#define y fclose#define end static

t(signed)char U;t(struct) b(O,);

t( U*)H; t(O*) *o;struct O{ O* l, **h; void* L; } ; t(int)i; i P(U g) { Gisspace(g); } H D(H s){H p,r;if(!s)G 0;for(p=(H)s;*p &&!P(*p); p++); if(r=malloc(p-s))for(p=r; *s&&! P(*s);p++,s++)b(*,p=) s; G r;} void l(o p,O*x){*(o)x=* p; *p=x;}#define m(x) do{ if(!(q = malloc(sizeof(O)))) e(1)q->l\=0 ;q\-> L=\

Brian Kernighan

Dennis Ritchie

Bjarne Stroustrup

Page 22: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

What is computing? (2)

• A computer is a device that correctly interprets C programs

Segmentationfault for(i=0;i<n;i+

+){...

Page 23: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

What is computing?

• CGI etc.

Page 24: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Yet Others

• could work in assembly language

• MOV R3,R4

John von Neumann

Eckert and Mauchly

Page 25: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

What is computing? (3)

• A computer is a device that correctly interprets Assembly Language programs

Page 26: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Computing is

• An accumulator, a program counter, a memory, and an ALU that can correctly interpret• BR op ; branch to operand+1

• BRREL op ; branch to PC+op+1

• LNEG op ; load negated contents of op into accumulator

• STORE op ; store contents of accumulator at op

• SUB op ; subtract contents of op from accumulator

• SKIPNEG ; if (accumulator < 0) PC++

• HALT

Fred Williams

Tom Kilburn

Alan Turing

Manchester Mark I, 1949

Page 27: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Computing is

•a finite number of NAND gates connected together reasonably cleverly

•and some interface devices.

Page 28: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Computing is

•a finite number of states, a one-way infinite tape, a read-write head, a transition table specifying for some state/tape-symbol pairs a new state, a new symbol and a move L or R.

Philip Franks as Alan Turing

Page 29: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Turing’s Thesis

• The processes which could naturally be called algorithms are precisely those which can be carried out on Turing machines

Page 30: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Church’s Thesis

• All formalizations of algorithms will yield the same class of computable functions.

Page 31: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Theorem

• The preceding definitions of “computer’’ and “computing” are all equivalent. Each definition specifies exactly the same set of computable functions.

Page 32: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

And a Whole Lot More

• Post Machine

• Conway’s Game of Life

Emil Post

John Horton Conwaycells with < 2 living neighbors die of loneliness

cells with > 3 living neighbors die of overcrowdingdead cells with 3 living neighbors come alive

Page 33: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Programming Languages

• Any algorithm expressible in one language can be equivalently expressed in another

• So why not choose the best?

Page 34: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Back to Smullyan’s enchanted forest

Page 35: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

What?• Birds are lambda expressions with no

free variable : Combinators

• Kestrel

• K x y = x

• or, K = λx λy x

• Mockingbird

• M x = x x

• or, M = λx x x

Page 36: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Thanks to Sage Bird

• “define” is unnecessary!

• Scheme can be improved!

• Θ (λ f . λ x (if (0? x) 1 (* x (f (1- x)))))

• Θ F 3 = F (Θ F) 3 = (* 3 ((Θ F) 2))= (* 3 (F (Θ F) 2)) = (* 3 (* 2 ((Θ F) 1)))= (* 3 (* 2 (F (Θ F) 1))) = (* 3 (* 2 (* 1 ((Θ F) 0))))= (* 3 (* 2 (* 1 (F (Θ F) 0)) = (* 3 (* 2 (* 1 1)))

Page 37: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Sage bird as λ expression

• (not in scheme -- why??)

• Θ= λ h . (λ x . h (x x)) (λ x . h (x x))

• Θ f = (λ x . f (x x)) (λ x . f (x x)) = f ((λ x . f (x x) (λ x . f (x x)) = f (Θ f)

Page 38: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Schönfinkel proved• All the birds in the forest can be

generated from two birds

• The Starling

• S x y z = x z (y z)

• S = λ x λ y λ z x z (y z)

• The Kestrel

• K x y = x

• K = λx λy x

Barendregt:X = λx . x K S K

K = XXXS = X(XX)

[Thanks Sidney]

Page 39: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Example

• The Idiot Bird (Identity Bird) I

• I x = x

• I = S K K

• Proof

• S K K x = K x (K x) = x

Page 40: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Exercise

• Idiot bird = S K <any> x

any

KSany xK x

Page 41: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

So the whole forest

• the whole enchanted forest of all the birds you could possibly imagine

• the whole forest can be constructed from just two birds

• S and K

Page 42: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Proof• We know every bird can be written as a λ expression

• So we just need a translator from λ expressions to S K

• skcomp.ss

• We added luxuries:

• ints, primitives, I, Θ (called Y here)

Page 43: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Target machine

• correctly reduces S applications and K applications

• is trivially parallelizable

• can be optimized

• even better -- let’s program in S K!

• a simulator: myeval.ss

Page 44: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Graph Reduction

• Simon Peyton Jones

• GRIP

• but Moses Schönfinkel could have told you that in 1927

Page 45: S and K - RISC Machineats/plt-2005-1/ski.pdfTo Mock a Mockingbird • and other logic puzzles including an amazing adventure in combinatory logic • Raymond Smullyan • What is the

Some Demos

• Run demo.ss (works locally)

• or gdemo.ss (on Tiger)