Skip to content

Instantly share code, notes, and snippets.

View ekmett's full-sized avatar
🤞

Edward Kmett ekmett

🤞
View GitHub Profile
@ekmett
ekmett / monads for plt-racket
Created June 26, 2010 05:06
Monads for PLT Racket
(module monad scheme
(require "curry.ss")
;; i'm too lazy to repeat this pattern for now.
(define-syntax init-public
(syntax-rules ()
((_) (begin))
((_ (m default) ms ...) (begin
(init-field (m default))
(public (internal-m m))
(define (internal-m . rest) (apply (get-field m this) rest))
import Control.Parallel.Strategies
data NF a = NF () a
nfBy :: Strategy a -> a -> NF a
nfBy s a0 = r where r@(NF _ a) = NF (s a) a0
nf :: NFData a => a -> NF a
nf = nfBy rnf
package scalaz
/**
* Defines a category.
*
* <p>
* All instances must satisfy 3 laws:
* <ol>
* <li><strong>left identity</strong><br/><code>∀ a. compose(id, a) == a</code></li>
@ekmett
ekmett / dual
Created December 1, 2010 21:49
trait HomClass { self =>
type L
type H>:L
type C[_>:L<:H,_>:L<:H]
type Dual <: HomClass {
type L=self.type#L
type H=self.type#H
type C[A>:L<:H,B>:L<:H] = self.type#C[B,A]
type Dual = self.type
}
object hom {
trait set {
type inf
type sup >: inf
type hom[_>:inf<:sup,_>:inf<:sup]
type dual = set.of[inf,sup,({type λ[x>:inf<:sup,y>:inf<:sup]=hom[y,x]})#λ]
}
object set {
trait of[l,h>:l,c[_>:l<:h,_>:l<:h]] extends set {
type inf = l
scala> import magpie._
import magpie._
scala> type i = hom.set.singleton[Int]
defined type alias i
scala> type arr = hom.set.of[Nothing,Any,({type o[-x,+y] = y})#o, ({type f[-x,+y] = x => y })#f]
defined type alias arr
scala> (_/2) : arr#hom[Int,Double]
@ekmett
ekmett / Speculation.scala
Created December 28, 2010 07:58
a sketch of a scala speculation framework
package speculation
import java.util.concurrent.{ Callable, ExecutorService, Future }
class Speculation(val executor: ExecutorService, val mayInterruptIfRunning: Boolean) {
def spec[A,B](guess: => A)(f: A => B)(actual: => A): B = {
val g = executor.submit(new Callable[A] {
def call = guess
})
val speculation = executor.submit(new Callable[B] {
@ekmett
ekmett / Space.hs
Created June 7, 2011 14:58
haskell vector spaces
{-# LANGUAGE TypeFamilies, FlexibleContexts, TypeOperators, UndecidableInstances, GeneralizedNewtypeDeriving #-}
module Numeric.Vector.Space where
import Data.Functor.Representable.Trie hiding (Entry)
import Data.Key (Adjustable(..), Key)
import qualified Data.Key as Key
import qualified Data.Heap as Heap
import Data.Heap (Heap, Entry(..))
import qualified Data.List.NonEmpty as NonEmpty
import Data.List.NonEmpty (NonEmpty(..))
@ekmett
ekmett / codensity-yield
Created June 8, 2011 18:40
Yield: Mainstream Delimited Continuations via Codensity
module Yield where
-- Cleaned up version of Yield: Mainstream Delimited Continuations by Roshan James and Amr Sabry from TPDC 2011
import Data.Traversable
import Control.Monad.Trans
import Control.Monad.Free
import Control.Monad.Codensity
import Control.Comonad.Trans.Store
@ekmett
ekmett / Chebyshev.hs
Created June 20, 2011 05:31
chebyshev and other orthogonal polynomial bases (Spread polynomials borked)
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, FlexibleContexts, UndecidableInstances #-}
module Numeric.Polynomial.Chebyshev where
import qualified Data.Map as Map
import Data.List (foldl')
zipWithT :: (a -> b -> c) -> ([a] -> [c]) -> ([b] -> [c]) -> [a] -> [b] -> [c]
zipWithT f fa fb = go where
go (a:as) (b:bs) = f a b : go as bs
go [] bs = fb bs