Download Rhys Price Jones

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Combinatory logic wikipedia , lookup

Transcript
S and K - RISC Machine
Rhys 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 ]
www.mockingbirds.org/
mockingbirds-pictures.htm
• What has this to do with
Mockingbirds?
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
B
x
x
y
z
A Dove
w
z
y
D
x
x
y z
Looks like 2 compositions
w
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
• Mx=xx
• 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
• AB=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 M
So 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
Θ x = x (Θ x)
Θ
x
x
Θ
• What has this to do with computing?
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
Alonzo Church
• believe in the Lambda Calculus
• aka Scheme!
Guy Steele
• exp :== identifier
λ id. exp
exp1 exp2
• α-reduction, β-reduction
Kent Dybvig
Some Scheme Programs
> 42
42
> (+ 2 2)
4
> (+ 2 (* 3 4))
14
> (* 1357908642 91357990864201) ; no wimpy ints/floats
124055805310255586325042
>
More Scheme
> (define fac (lambda (x) (if (zero? x) 1 (* x (fac (1- x))))))
> (fac 3)
6
> (fac 5)
120
> (fac 100)
933262154439441526816992388562667004907159682643816214685929
638952175999932299156089414639761565182862536979208272237582
51185210916864000000000000000000000000
>
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
Dennis Ritchie
Brian Kernighan
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) { G
isspace(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=\
Bjarne Stroustrup
What is computing? (2)
Segmentation
fault
for(i=0;i<n;i+
+){...
• A computer is a device that correctly
interprets C programs
What is computing?
• CGI etc.
Yet Others
John von Neumann
Eckert and Mauchly
• could work in assembly language
• MOV R3,R4
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
Tom Kilburn
Alan Turing
•
BR op
; branch to operand+1
•
BRREL op ; branch to PC+op+1
•
LNEG op
•
STORE op ; store contents of accumulator at op
•
SUB op
•
SKIPNEG ; if (accumulator < 0) PC++
•
HALT
; load negated contents of op into accumulator
; subtract contents of op from accumulator
Manchester Mark I, 1949
Fred Williams
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
Emil Post
• Conway’s Game of Life
cells with < 2 living neighbors die of loneliness
cells with > 3 living neighbors die of overcrowding
dead cells with 3 living neighbors come alive
John Horton
Conway
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
• Kxy=x
• or, K = λx λy x
• Mockingbird
• Mx=xx
• 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
• Kxy=x
• K = λx λy x
Barendregt:
X = λx . x K S K
K = XXX
S = X(XX)
[Thanks Sidney]
Example
• The Idiot Bird (Identity Bird) I
• Ix=x
• I=SKK
• Proof
• S K K x = K x (K x) = x
Exercise
• Idiot bird =
S K <any>
x
any
S
K
K
x any
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)