Skip to content

Instantly share code, notes, and snippets.

View jozefg's full-sized avatar

daniel gratzer jozefg

View GitHub Profile
@jozefg
jozefg / Lift.hs
Created March 31, 2016 22:26
WIP - Doodle of a fully lazy lambda lifter
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
module Lift where
import Control.Monad.Gen
import Data.Functor.Foldable (Fix(..))
@jozefg
jozefg / PatCompile.hs
Created March 25, 2016 07:19
Wadler's classic pattern matching algorithm implemented for a core language with Bound.
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module PatCompile where
import Bound
import Bound.Var
import Bound.Scope
import Control.Monad (ap)
@jozefg
jozefg / PickRandom.hs
Last active August 3, 2017 13:55
A simple trick to pick a random element from a stream in constant memory
{-# LANGUAGE FlexibleContexts #-}
module PickRandom where
import Data.List (group, sort)
import Control.Monad
import Control.Monad.Random (MonadRandom, getRandomR)
import qualified Control.Foldl as F
-- Pick a value uniformly from a fold
pickRandom :: MonadRandom m => a -> F.FoldM m a a
pickRandom a = F.FoldM choose (return (a, 0 :: Int)) (return . fst)
@jozefg
jozefg / Async.hs
Created March 16, 2016 07:36
Subtle bug in Async code
module Main where
import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception
import Control.Monad
-- To start with, an intermezzo of the book's definition of Async and some key
-- operations on it.
data Async a = Async { pid :: ! ThreadId
, getAsync :: STM (Either SomeException a)
@jozefg
jozefg / compile.rkt
Created February 13, 2016 23:19
Learning some Racket, don't hate my code too much for I know not what I do.
#lang racket
;; This compiles some nontrivial subset of scheme with let, letrec, let*
;; quote, set!, and if to some subset of Scheme which is CPSed, has no
;; nested lambdas and is explicitly closure converted. Mostly done to stretch
;; my legs a bit with Racket.
(define *primops*
'(+ - * / equal? < = and or not display cons car list box unbox set-box!))
@jozefg
jozefg / ir.agda
Last active November 8, 2015 23:24
ir in agda
module ind-rec where
open import Data.Empty
open import Data.Product
open import Function
open import Level
record ⊤ {ℓ : Level} : Set ℓ where
constructor tt
mutual
@jozefg
jozefg / mltt.agda
Created November 2, 2015 06:15
Defining a realizability model for MLTT using induction-recursion.
module realize where
open import Relation.Nullary
import Data.Empty as E
open import Data.Product
open import Data.Sum
open import Data.Nat
data Term : Set where
var : ℕ → Term
lam : Term → Term
module realize where
open import Relation.Nullary
import Data.Unit as U
import Data.Empty as E
open import Data.Product
open import Data.Sum
open import Data.Nat
data Lam : Set where
var : ℕ → Lam
@jozefg
jozefg / tag.sml
Last active October 8, 2015 21:51
The magic of extensible types.
signature TAG =
sig
type tagged
type 'a tag
val new : unit -> 'a tag
val tag : 'a tag -> 'a -> tagged
val untag : 'a tag -> tagged -> 'a option
end
signature COROUTINE =
sig
type ('a, 'b, 'r) t
val await : ('a, 'b, 'a) t
val yield : 'b -> ('a, 'b, unit) t
(* Monadic interface and stuffs *)
val map : ('c -> 'd) -> ('a, 'b, 'c) t -> ('a, 'b, 'd) t
val return : 'c -> ('a, 'b, 'c) t