Skip to content

Instantly share code, notes, and snippets.

object Formatter {
sealed abstract class Dir[A] {
def formatAux: String => A
def format: A = formatAux.apply("")
}
case object E extends Dir[String] {
def formatAux = identity
}
@einblicker
einblicker / gist:1758706
Created February 7, 2012 09:35
Javaで型安全なprintf
interface Func<A, B> {
B apply(final A arg);
}
abstract class Dir<A, B> {
public Dir() {}
abstract public Func<Func<String, A>, Func<String, B>> format();
}
class DLit<A> extends Dir<A, A> {
object TypesafeFormatter {
sealed abstract class Dir[A, B] {
def format : (String => A) => (String => B)
}
def l[A](s: String): Dir[A, A] = L[A](s)
case class L[A](s: String) extends Dir[A, A] {
def format = {
(cont: String => A) =>
(out: String) =>
cont(out + s)
@einblicker
einblicker / gist:1714506
Created February 1, 2012 01:58
sf chap2
Section List_J.
Module NatList.
Inductive natprod : Type :=
pair : nat -> nat -> natprod.
Definition fst (p : natprod) : nat :=
match p with
| pair x y => x
@einblicker
einblicker / gist:1714469
Created February 1, 2012 01:49
Rubyでリスト内包表記
require 'continuation'
def shift
callcc {|c1| $ctn.(yield(proc {|v| callcc {|c2| $ctn = c2; c1.(v) } })) }
end
def reset
callcc {|c| $ctn = c; v = yield; $ctn.(v) }
end
object ParityBit {
type B = Int
def f(x1: B, x2: B, x3: B, x4: B) =
(x1 ^ x2 ^ x3,
x1 ^ x2 ^ x4,
x2 ^ x3 ^ x4)
def g(x1: B, x2: B, x3: B, x4: B)(
c1:B, c2:B, c3:B
) =
@einblicker
einblicker / gist:1706564
Created January 30, 2012 20:42
sf chap1
Section Basics_J.
Definition nandb (b1 b2 : bool) : bool :=
match (b1, b2) with
| (false, _) => true
| (_, false) => true
| _ => false
end.
Example test_nandb1: (nandb true false) = true.
@einblicker
einblicker / gist:1700312
Created January 29, 2012 19:39
CPDT Exercise 5
Section CPDT_Exercise_5.
Section Tree.
Variable A : Set.
CoInductive tree :=
| Node : A -> tree -> tree -> tree.
CoInductive tree_eq : tree -> tree -> Prop :=
@einblicker
einblicker / gist:1699089
Created January 29, 2012 14:39
CPDT Exercise 4.6
Section CPDT_Exercise_4_6.
(* problem 1 *)
Variable P Q R : Prop.
Goal (True \/ False) /\ (False \/ True).
Proof.
split; [ left | right ]; apply I.
Qed.
@einblicker
einblicker / gist:1698273
Created January 29, 2012 10:57
closure translation
(define (mark e)
(let loop ((e e) (envs '()))
(match
e
(('lambda args es ...)
`(lambda ,args ,@(map (cut loop <> (cons args envs)) es)))
((? symbol? s)
(cond
((find
(lambda (e) (memq s e))