Skip to content

Instantly share code, notes, and snippets.

View ekmett's full-sized avatar
🤞

Edward Kmett ekmett

🤞
View GitHub Profile
@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 / 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
@ekmett
ekmett / octree
Created August 24, 2011 18:55
octree
{-# LANGUAGE TypeFamilies, DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
import Control.Applicative {- from base -}
import Data.Distributive {- from distributive -}
import Data.Key {- from the keys package -}
import Data.Functor.Bind {- from semigroupoids -}
import Data.Functor.Representable {- from representable-functors -}
-- lets define unpacked affine 3-vectors of doubles
data Vector = Vector {-# UNPACK #-} !Double {-# UNPACK #-} !Double {-# UNPACK #-} !Double deriving (Eq, Ord, Show)
@ekmett
ekmett / Rat.hs
Created September 23, 2011 03:40
parsec-like packet
import Control.Applicative
import Control.Monad (MonadPlus(..), guard)
import Data.Char (isDigit, digitToInt, isSpace)
import Data.Foldable
import Data.Traversable
import Data.Monoid
-- a parsec-like packrat parser
data Result d a
@ekmett
ekmett / Braun.hs
Created June 7, 2012 11:56
Braun Trees
module Data.Tree.Braun
( Tree
, replicate
, fromList
, singleton
, null
, empty
, twist
-- * Sanity check
, braun
@ekmett
ekmett / Remote.hs
Created August 4, 2012 16:46
remote dsl
{-# LANGUAGE GADTs, Rank2Types, KindSignatures, ScopedTypeVariables, TypeOperators, DataKinds, PolyKinds, MultiParamTypeClasses, FlexibleInstances, TypeFamilies, DoRec, ExtendedDefaultRules #-}
import Control.Applicative
import Control.Category
import Control.Comonad
import Control.Monad.Fix
import Control.Monad (ap)
import Data.Functor.Identity
import Data.Typeable
import Data.Monoid
import Data.Unique
@ekmett
ekmett / cmp_haskell.hs
Created September 26, 2012 22:12
Stroustrup et al. Haskell Benchmark
import Text.Printf
import Data.List
import System.CPUTime.Rdtsc
import Control.Seq
import Data.Bits
data Shape = Shape00 | Shape01 | Shape02 | Shape03 | Shape04 | Shape05 | Shape06 | Shape07 | Shape08 | Shape09
| Shape10 | Shape11 | Shape12 | Shape13 | Shape14 | Shape15 | Shape16 | Shape17 | Shape18 | Shape19
| Shape20 | Shape21 | Shape22 | Shape23 | Shape24 | Shape25 | Shape26 | Shape27 | Shape28 | Shape29
| Shape30 | Shape31 | Shape32 | Shape33 | Shape34 | Shape35 | Shape36 | Shape37 | Shape38 | Shape39
@ekmett
ekmett / Golf.hs
Created November 2, 2012 23:58
golfing pi and e
-- e
> let z a b c d w@(x:y)=let t=a`div`c in if all(>0)[a,b,c,d]&&t==b`div`d then t:z(10*(a-c*t))(10*(b-d*t))c d w else z(x*a+b)a(x*c+d)c y;f n=1:1:n:f(n+2)in z 1 0 0 1(2:1:2:f 4)>>=show
> let z a b c d w@(x:y)|t<-a`div`c=if all(>0)[a,b,c,d]&&t==b`div`d then t:z(10*(a-c*t))(10*(b-d*t))c d w else z(x*a+b)a(x*c+d)c y;f n=1:1:n:f(n+2)in z 1 0 0 1(2:1:2:f 4)>>=show
> let z a b c d w@(x:y)|all(>0)[a,b,c,d]&&t==b`div`d|True=t:z(10*(a-c*t))(10*(b-d*t))c d w else z(x*a+b)a(x*c+d)c y where t=a`div`c;f n=1:1:n:f(n+2)in z 1 0 0 1(2:1:2:f 4)>>=show
> let z a b c d w@(x:y)|all(>0)[a,b,c,d]&&t==b`div`d=t:z(10*(a-c*t))(10*(b-d*t))c d w|True=z(x*a+b)a(x*c+d)c y where{t=a`div`c};f n=1:1:n:f(n+2)in z 1 0 0 1(2:1:2:f 4)>>=show
> let f n=1:1:n:f(n+2);z a b c d w@(x:y)|t<-a`div`c,all(>0)[a,b,c,d]&&t==b`div`d=t:z(10*(a-c*t))(10*(b-d*t))c d w|True=z(x*a+b)a(x*c+d)c y in z 1 0 0 1(2:1:2:f 4)>>=show
> let f n=1:1:n:f(n+2);z a b c d w@(x:y)|any(<=0)[a,b,c,d]||t/=b`div`d=z(x*a+b)a(x*c+d)c y|t<-a`div`c=t:z(10*(a-c*t))(10*(b-d*t))c d w
@ekmett
ekmett / SimplifiedIso.hs
Created November 27, 2012 00:15
Toward unifying Iso and Projection.
{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE Trustworthy #-}
@ekmett
ekmett / Isos.hs
Created November 27, 2012 02:10
Isos
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
module Isos where
import Control.Applicative
import Unsafe.Coerce