Skip to content

Instantly share code, notes, and snippets.

@dvanhorn
dvanhorn / 256.rkt
Created October 10, 2011 03:17
The straightforward way to write 256
#lang racket
;; Mogensen-Scott encoding
(define-syntax encode
(syntax-rules (λ)
[(encode (λ (x) e))
(λ (VAR) (λ (APP) (λ (LAM) (LAM (λ (x) (encode e))))))]
[(encode (e0 e1))
(λ (VAR) (λ (APP) (λ (LAM) ((APP (encode e0)) (encode e1)))))]
[(encode x)
@dvanhorn
dvanhorn / nbe.rkt
Created October 10, 2011 06:16
Normalization by evaluation -- aka magic
#lang typed/racket
(define-type Term (U VAR LAM APP))
(struct: VAR ([x : String]) #:transparent)
(struct: LAM ([x : String] [m0 : Term]) #:transparent)
(struct: APP ([m1 : Term] [m2 : Term]) #:transparent)
(define-type Sem (U TM FUN))
(struct: TM ([l : (Integer -> Term)]))
(struct: FUN ([f : ((-> Sem) -> Sem)]))
@dvanhorn
dvanhorn / nbe-machine.rkt
Created October 10, 2011 09:22
An abstract machine for NBE
#lang typed/racket
(define-type Term (U VAR LAM APP))
(struct: VAR ([x : String]) #:transparent)
(struct: LAM ([x : String] [m0 : Term]) #:transparent)
(struct: APP ([m1 : Term] [m2 : Term]) #:transparent)
(define-type Sem (U Ind Fun))
(define-type Env (U R1 R2))
(struct: R1 ())
@dvanhorn
dvanhorn / peer.txt
Created October 15, 2011 19:22
The essence of peer review
diff popl12-reviews.txt popl12-reviews-final.txt
333c333
< Updated Tuesday 6 Sep 2011 6:28:00pm EDT
---
> Updated Monday 19 Sep 2011 1:03:02pm EDT
339c339
< Overall merit: 4. Accept
---
> Overall merit: 3. Weak accept
525d524
@dvanhorn
dvanhorn / flat-v-ho.rkt
Created October 20, 2011 02:41
The smart way and the dumb way
#lang racket
(require redex/reduction-semantics)
(define-language pre-C
(CON X
nat?
(or/c CON CON)
(rec/c X CON)
(cons/c CON CON)
(CON -> CON))
@dvanhorn
dvanhorn / circular.rkt
Created October 20, 2011 03:00
Circular language
#lang racket
(require redex/reduction-semantics)
(define-language C
(CON X
nat?
(or/c CON CON)
(rec/c X CON)
(cons/c CON CON)
(CON -> CON))
@dvanhorn
dvanhorn / stack-trace.txt
Created October 28, 2011 01:51
Higher-order programming in Java
doom:shan dvanhorn$ java Eval
Exception in thread "main" java.lang.StackOverflowError
at Evaluate.visit(Eval.java:142)
at Evaluate.visit(Eval.java:119)
at App.accept(Eval.java:56)
at Evaluate.visit(Eval.java:142)
at Evaluate.visit(Eval.java:119)
at App.accept(Eval.java:56)
at Evaluate$1.apply(Eval.java:136)
at Evaluate.visit(Eval.java:142)
@dvanhorn
dvanhorn / Evaluator.java
Created October 28, 2011 02:12
Compositional interpreter for lambda calculus as Visitor in Java for @ccshan @djspiewak @kaleidic
// Compositional evaluator as visitor
import java.math.BigInteger;
// Syntax
interface Exp {
<X> X accept(Visitor<X> v);
}
@dvanhorn
dvanhorn / cpcf-den.rkt
Created July 3, 2012 18:32
Denotational semantics for Contract PCF
#lang typed/racket
;; Denotational semantics for Contract PCF
;; SYNTAX
;; ======
;;
;; E = (vbl X)
;; | (app E E)
;; | (if E E E)
;; | (lam X E)
@dvanhorn
dvanhorn / cpcf-machine.rkt
Created July 4, 2012 03:21
Machine semantics for Contract PCF
#lang typed/racket
;; Machine semantics for Contract PCF
;; SYNTAX
;; ======
;;
;; E = (vbl X)
;; | (app E E)
;; | (if E E E)
;; | (lam X E)