Skip to content

Instantly share code, notes, and snippets.

View philnguyen's full-sized avatar

Phil Nguyen philnguyen

View GitHub Profile
#lang racket
(define-syntax-rule (define/memo (f x ...) e ...)
(define f
(let ([m (make-hash)])
(λ (x ...) (hash-ref! m (list x ...) (λ () e ...))))))
; memoized fibonacci
(define/memo (fib n)
(if (< n 2) n
@philnguyen
philnguyen / ffn.rkt
Created June 25, 2013 04:19
To illustrate how this clever Python solution here can be typed in Typed Racket http://stackoverflow.com/a/732049/159759
#lang typed/racket
(: f (case->
[Number -> (-> Number)]
[(-> Number) -> Number]))
(define (f x)
(if (number? x) (λ () (- x)) (x)))
(module list
(provide
[reverse ([l : (listof int?)]
. -> .
(and/c (listof int?)
(λ (r) (and (equal? (empty? l) (empty? r))
(equal? (cons? l) (cons? r))))))]
[mk-list ([n : int?]
. -> .
(and/c (listof int?)
@philnguyen
philnguyen / ex14.ml
Last active December 22, 2015 20:09
Example 14 from "Logical Types for Untyped Languages" translated to OCaml
type top = N of int | S of string
let f input extra =
match input,extra with
| N m, (N n,_) -> m + n
| _, (N n,_) -> (match input with
| S s -> String.length s + n)
| _,_ -> 0
let main i e = f i e
@philnguyen
philnguyen / bwt.rkt
Last active August 29, 2015 13:58
Code for CMSC702 midterm
#lang racket
;; String -> [Listof String]
;; return all rotations of a string
(define (>>n s)
;; rotate a string to the right
(define (>> s)
(string-append (substring s 1 (string-length s)) (substring s 0 1)))
(let-values ([(ss _)
#lang racket/base
(define-syntax-rule (gc)
(begin (collect-garbage) (collect-garbage) (collect-garbage)))
(define N 10000000)
(printf "Immutable map:~n")
(let ([M (hash)])
(printf " Write: ")
(gc) (time (for ([i N]) (set! M (hash-set M i #f))))
@philnguyen
philnguyen / struct-expansion.rkt
Created December 9, 2014 16:34
Fully expanded `(struct x (a b) #:transparent)`
#lang racket/base
(begin
(define-syntaxes (struct:x x1 x? x-a x-b) (#%app values))
(define-syntaxes
(x)
(#%app
make-self-ctor-checked-struct-info
(lambda ()
(#%app
@philnguyen
philnguyen / mc-ctl.rkt
Last active August 29, 2015 14:18
Model checking of CTL from 630 lecture
#lang typed/racket
(define ∅ : (Setof Nothing) {set})
(define ∪ set-union)
(define ∩ set-intersect)
(define ∋ set-member?)
(: fix : (∀ (X) (X → X) X → X))
;; Compute `f`'s fixpoint starting from `x`
(define (fix f x)
@philnguyen
philnguyen / ValueEnum.scala
Last active November 8, 2015 01:45
Concise, type-safe and unboxed Enum by Scala macros
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.annotation.StaticAnnotation
import scala.annotation.compileTimeOnly
/**
This annotation turns a simple class declaration into a type-safe and unboxed Enum.
It does not attempt to be compatible with Java, because I have no respect for Java.
There is an example usage at the end of the file.
*/
@philnguyen
philnguyen / BraunTree.scala
Last active August 29, 2015 14:21
This file demonstrates a seemingly bug in the Leon verifier
import leon.lang._
import leon.lang.synthesis._
import leon.annotation._
object BraunTree {
sealed abstract class Tree
case object Leaf extends Tree
case class Node(l: Tree, n: Int, r: Tree) extends Tree
def size(t: Tree): Int = t match {