Skip to content

Instantly share code, notes, and snippets.

View jozefg's full-sized avatar

daniel gratzer jozefg

View GitHub Profile
@jozefg
jozefg / gist:5190455
Created March 18, 2013 20:20
Logic based system in Scheme
(define (amb-fail f)
(error "amb" "Unable to satisfy conditions"))
(define (amb . it)
(define old amb-fail)
(call/cc (lambda (top)
(map (lambda (val)
(call/cc (lambda (cont)
(set! amb-fail cont)
@jozefg
jozefg / Trie.hs
Last active December 18, 2015 00:19
{-# LANGUAGE ViewPatterns #-}
import Data.List
import Data.Maybe
data Tree a = Node{value :: a, subtree :: [Tree a]}
deriving (Eq, Show)
type Trie = Tree Char
toTrie :: String -> Trie
toTrie (reverse -> c : s) = foldl' (flip Node . (:[])) (Node c []) s
@jozefg
jozefg / gist:8622801
Created January 25, 2014 20:12
Implementation of the calculus of constructions
import Control.Monad.State
import Control.Monad.Error
import Control.Applicative
type Sym = Int
data Term = Box Int
| Star
| Abs Term (Term -> Term)
| App Term Term
| Forall Term (Term -> Term)
@jozefg
jozefg / closconv.lhs
Last active March 5, 2024 11:52
Tutorial on Closure Conversion and Lambda Lifting
This is my short-ish tutorial on how to implement closures in
a simple functional language: Foo.
First, some boilerplate.
> {-# LANGUAGE DeriveFunctor, TypeFamilies #-}
> import Control.Applicative
> import Control.Monad.Gen
> import Control.Monad.Writer
> import Data.Functor.Foldable
### Keybase proof
I hereby claim:
* I am jozefg on github.
* I am jozefg (https://keybase.io/jozefg) on keybase.
* I have a public key whose fingerprint is F6CF 1EA2 013C 8700 D24B 030D D5DC A663 AAE6 2FAE
To claim this, I am signing this object:
@jozefg
jozefg / Logic
Created October 6, 2014 04:25
mini-mini Kanren
{-# LANGUAGE RecordWildCards #-}
module Unify where
import Control.Monad.Logic
import qualified Data.Map as M
type Id = Integer
type Atom = String
data Term = Var Id
| Atom Atom
@jozefg
jozefg / seq.elf
Created October 24, 2014 21:57
Cut Admissibility
term : type.
and : term -> term -> term.
or : term -> term -> term.
imp : term -> term -> term.
top : term.
hyp : term -> type.
true : term -> type.
init : true A
@jozefg
jozefg / FingerTrees.hs
Last active August 29, 2015 14:08
Finger Trees
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Data.Foldable
import Data.Traversable
import Data.Monoid
@jozefg
jozefg / Hm.rs
Created November 14, 2014 19:24
Lousy Rust Code
use std::io::stdin;
fn fizzbuzz (n : int) -> String {
match (n % 3 == 0, n % 5 == 0) {
(true, true) => "FizzBuzz".to_string(),
(false, true) => "Fizz".to_string(),
(true, false) => "Buzz".to_string(),
(false, false) => n.to_string()
}
}
@jozefg
jozefg / looks_scary.rs
Created November 14, 2014 23:04
Trees gone wrong
enum Tree<A> {
Leaf(A),
Node(Box<Tree<(A, A)>>)
}
fn size<A>(t : &Tree<A>) -> int {
match t {
&Leaf(_) => 1,
&Node(box ref t) => size(t)
}