Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

My lambda calculus interpreter

Name: Anonymous 2012-04-24 8:20

What do you guys think about it ?


(define (lc-eval exp env)
  ;  (display "ev ")(display exp)(newline)
  (cond ((not (pair? exp)) (lookup-symbol exp env))
        ((lambda? exp) (make-lambda (lambda-arg exp)
                                    (lc-eval (lambda-body exp)
                                             (add-binding (lambda-arg exp)
                                                          (lambda-arg exp)
                                                          env))))
        ((application? exp) (lc-apply (lc-eval (operator exp) env)
                                      (lc-eval (operands exp) env)
                                      env))
        ((definition? exp) (set! global-env (add-binding (def-symbol exp)
                                                         (lc-eval (def-value exp) env)
                                                         global-env))
                           (list 'set (def-symbol exp) (lookup-symbol (def-symbol exp) global-env)))
        (else (error "cannot eval " exp))))

(define (lc-apply exp arg env)
  ;  (display "ap ")(display exp)(display " to ")(display arg)(newline)
  (if (lambda? exp)
      (lc-eval (lambda-body exp)
               (add-binding (lambda-arg exp) arg env))
      (make-application exp arg)))

(define (make-application exp arg)
  (list exp arg))

(define (make-lambda arg body)
  (list 'λ arg body))

;(define error (lambda message
;                (map display message)
;                (newline)
;                'undefined))

(define operator car)
(define operands cadr)
(define lambda-body caddr)
(define lambda-arg cadr)
(define def-symbol cadr)
(define def-value caddr)

(define (list-of-n? l n)
  (and (list? l)
       (= (length l) n)))

(define (any-symbol? exp)
  (or (symbol? exp) (number? exp)))
(define (lambda? exp)
  (and (list-of-n? exp 3) (any-symbol? (cadr exp))
       (equal? (car exp) 'λ)))
(define (application? exp) (list-of-n? exp 2))
(define (definition? exp)
  (and (list-of-n? exp 3) (any-symbol? (cadr exp))
       (equal? (car exp) 'set)))

(define (empty-env) (list))
(define (add-binding symbol value env)
  (cons (cons symbol value) env))
(define binding-symbol caar)
(define binding-value cdar)
(define parent-env cdr)
(define (lookup-symbol symbol env)
  (cond ((null? env) (error "unbound variable " symbol))
        ((equal? symbol (binding-symbol env)) (binding-value env))
        (else (lookup-symbol symbol (parent-env env)))))
(define env-with (lambda bindings
                   (if (null? bindings)
                       (empty-env)
                       (add-binding (caar bindings) (cadar bindings)
                                    (apply env-with (cdr bindings))))))

(define global-env (empty-env))

; code here
; largely copied and inspired from http://en.wikipedia.org/wiki/Lambda_calculus#Arithmetic_in_lambda_calculus
(define code (quote ((set 1+ (λ n (λ f (λ x (f ((n f) x))))))
                     (set + (λ m (λ n (λ f (λ x ((m f) ((n f) x)))))))
                     (set * (λ m (λ n (λ f (m (n f))))))
                    
                     (set I (λ x x))
                     (set K (λ x (λ y x)))
                     (set S (λ x (λ y (λ z ((x z) (y z))))))
                     (set ω (λ x (x x)))
                     ; both these hang indefinitely:
                     ;(set Ω (ω ω))
                     ;(set Y (λ g ((λ x (g (x x))) (λ x (g (x x))))))
                    
                     ; numbers, calculated in many different ways
                     (set 0 (λ f (λ x x)))
                     (set 1 (1+ 0))
                     (set 2 (1+ 1))
                     (set 3 (λ f (λ x (f (f (f x))))))
                     (set 4 (1+ (1+ (1+ 1))))
                     (set 5 ((+ 2) 3))
                     (set 6 ((* 2) 3))
                     (set 7 ((+ 1) ((* 2) 3)))
                    
                     (set true (λ x (λ y x)))
                     (set false (λ x (λ y y)))
                     (set if (λ p (λ a (λ b ((p a) b)))))
                    
                     (set cons (λ a (λ b (λ p (((if p) a) b)))))
                     (set car (λ p (p true)))
                     (set cdr (λ p (p false)))
                     (set nil (λ x true))
                     (set null? (λ p (p (λ x (λ y false)))))
                    
                     (car ((cons 0) 1))
                     (cdr ((cons 0) 1))
                     )))

(begin (map (lambda (e) (display (lc-eval e global-env))(newline)) code) 'done)

(define (loop)
  (let ((e (read)))
    (if (eof-object? e)
        'done
        (begin
          (display (lc-eval e global-env))
          (newline)
          (loop)))))
(loop)

Name: Anonymous 2012-04-24 8:34

lameda lameda calcyaless



LUNIX

Name: VIPPER 2012-04-24 14:27

This is interesting, i wish i could understand it.
And so should /prog/.

Name: Anonymous 2012-04-24 15:04

lisp is shit

Name: Anonymous 2012-04-24 15:12

What the fuck is lambda calculus and why should I care?

yes i'm a java code monkey

Name: VIPPER 2012-04-24 15:44

/prog/ is shitting up again. Ima have to bump this a bit.

Name: Anonymous 2012-04-24 18:07

>>6
Lisp is shit and you are a fucking fag.

Name: VIPPER 2012-04-25 4:55

>>7
Burn in hell.

Name: Anonymous 2012-04-25 6:04

>>5
MY SIDES

Name: Anonymous 2012-04-25 12:11

>>8
fuck off and die you cock sucking faggot

Name: Anonymous 2012-04-25 21:08

>>10
After you.

Name: Anonymous 2012-04-25 23:40

ITT faggots mad they can't understand lisp

Name: Anonymous 2012-04-25 23:42

>>12
lisp surface syntax is shit

>>10
no, fuck you, faggot

Name: VIPPER 2012-04-26 5:14

>>13
Your face is shit.

Name: Anonymous 2012-04-26 12:03

>>14
Go fuck an autistic nigger you cock sucking faggot.

Name: Anonymous 2012-04-26 12:07

Someone said recently on /sci/:

C teaches you how computers work.
Lisp teaches you how computation works.

Someone else corrected the statement:

C teaches you how computers work.
Lisp teaches you how to jerk of on internet forums.

I was never more proud of /sci/.
Then fuck off back to /sci/.
I guess I probably should.

Name: Anonymous 2012-04-26 12:25

WTF is lambada calculus?

Name: Anonymous 2012-04-26 12:53

Name: Anonymous 2012-04-26 12:59

Nice, here's mine that compiles to SKI combinators.

--
-- SKI.hs
--

data LC  = Lm String LC | LApp LC LC | LVar String
data SKI = S | K | I | App SKI SKI | Var String

instance Show SKI where
  show = show' False

show' :: Bool -> SKI -> String
show' _ S               = "S"
show' _ K               = "K"
show' _ I               = "I"
show' _ (Var s)         = s
show' True e@(App _ _)  = "(" ++ show e ++ ")"
show' False (App e1 e2) =
  case e1 of
    App e3 e4 -> show e3 ++ " " ++ show (App e4 e2)
    _         -> show' True e1 ++ " " ++ show' True e2

abstract :: String -> SKI -> SKI
abstract x (App e1 e2)      = App (App S (abstract x e1)) (abstract x e2)
abstract x (Var y) | x == y = I
abstract x y                = App K y

compile :: LC -> SKI
compile (LApp e1 e2) = App (compile e1) (compile e2)
compile (Lm x e)     = abstract x (compile e)
compile (LVar s)     = Var s

reduce :: SKI -> SKI
reduce (App (App (App S f) g) x) = reduce $ App (App f x) (App g x)
reduce (App (App K x) _)         = reduce x
reduce (App I x)                 = reduce x
reduce e                         = e

eval :: LC -> SKI
eval = reduce . compile


I'm too lazy to write a parser, so it needs to be used like this:

*Main> eval $ LApp (Lm "x" (LVar "x")) (LVar "y") -- (\x. x) y
y
*Main> eval $ Lm "f" $ LApp (LVar "f") (LVar "x") -- (\f. f x)
S I (K x)

Name: Anonymous 2012-04-26 13:40

40 years since Lisp was invented and still every single flavour is pure shit.

if sussman was still alive he'd roll in his grave.

Name: Anonymous 2012-04-26 14:58

the sussman is alive

Name: Anonymous 2012-04-26 15:05

>>21
Your dumb.

Name: Anonymous 2012-04-26 15:07

>>22
Your dubs.

Name: Anonymous 2012-04-26 20:47

>>21
he's dead just like the disgusting monstrosity he created.

>>22-23 fagtos

Name: Anonymous 2012-04-26 21:40

>>24
NO U

Name: Anonymous 2012-04-26 22:28

WHAT DID I SAY ABOUT SUMMONING THE SUSS

Name: Anonymous 2012-04-26 22:40

>>25fuck you faggot
>>26go suck his dick

Name: Anonymous 2012-04-27 10:11

Lambada celsius

Name: bampu pantsu 2012-05-29 4:36

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List