s and k - risc machineats/plt-2005-1/ski.pdfto mock a mockingbird • and other logic puzzles...
TRANSCRIPT
S and K - RISC MachineRhys Price Jones
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
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
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”
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)
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
Portrait of a Bluebird
z
y
xBx
y z
A Dove
z
y
xD
w
z wx y
Looks like 2 compositions
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)
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
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
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
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
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
Sage Bird
• What has this to do with computing?
Θ x = x (Θ x)
xΘ x
Θ x
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!
Some people
• believe in the Lambda Calculus
• aka Scheme!
• exp :== identifier λ id. exp exp1 exp2
• α-reduction, β-reduction
Alonzo Church
Kent Dybvig
Guy Steele
Some Scheme Programs> 4242> (+ 2 2)4> (+ 2 (* 3 4))14> (* 1357908642 91357990864201) ; no wimpy ints/floats124055805310255586325042>
More Scheme> (define fac (lambda (x) (if (zero? x) 1 (* x (fac (1- x))))))> (fac 3)6> (fac 5)120> (fac 100)93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000>
What is computing?(1)
• A computer is a device that correctly interprets Scheme programs
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
What is computing? (2)
• A computer is a device that correctly interprets C programs
Segmentationfault for(i=0;i<n;i+
+){...
What is computing?
• CGI etc.
Yet Others
• could work in assembly language
• MOV R3,R4
John von Neumann
Eckert and Mauchly
What is computing? (3)
• A computer is a device that correctly interprets Assembly Language programs
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
Computing is
•a finite number of NAND gates connected together reasonably cleverly
•and some interface devices.
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
Turing’s Thesis
• The processes which could naturally be called algorithms are precisely those which can be carried out on Turing machines
Church’s Thesis
• All formalizations of algorithms will yield the same class of computable functions.
Theorem
• The preceding definitions of “computer’’ and “computing” are all equivalent. Each definition specifies exactly the same set of computable functions.
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
Programming Languages
• Any algorithm expressible in one language can be equivalently expressed in another
• So why not choose the best?
Back to Smullyan’s enchanted forest
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
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)))
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)
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]
Example
• The Idiot Bird (Identity Bird) I
• I x = x
• I = S K K
• Proof
• S K K x = K x (K x) = x
Exercise
• Idiot bird = S K <any> x
any
KSany xK x
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
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)
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
Graph Reduction
• Simon Peyton Jones
• GRIP
• but Moses Schönfinkel could have told you that in 1927
Some Demos
• Run demo.ss (works locally)
• or gdemo.ss (on Tiger)