Skip to content

Instantly share code, notes, and snippets.

View ekmett's full-sized avatar
🤞

Edward Kmett ekmett

🤞
View GitHub Profile
@ekmett
ekmett / Tarjan.hs
Last active August 29, 2015 14:03
{-# LANGUAGE RankNTypes, GADTs, PolyKinds #-}
module Tarjan where
import Control.Applicative
import Control.Category
import Data.Monoid
import Prelude hiding ((.),id)
-- Radu Mihaesau and Robert Tarjan's Catenable Deque
@ekmett
ekmett / Success.hs
Created May 1, 2015 22:50
Replacing Failure by a Heap of Successes
{-# LANGUAGE DeriveFunctor, TypeFamilies #-}
module Success
( Parser(..)
, parse
) where
import Control.Applicative
import Control.Monad
import Data.Bifunctor
import Data.Profunctor
@ekmett
ekmett / CF.hs
Created May 8, 2015 03:13
basic operations on continued fractions
bihom a b _ _ e f _ _ xs [] = hom a b e f xs
bihom a _ c _ e _ g _ [] ys = hom a c e g ys
bihom a b c d e f g h xs@(x:xs') ys@(y:ys')
| e /= 0, f /= 0, g /= 0, h /= 0
, q <- quot a e, q == quot b f
, q == quot c g, q == quot d h
= q : go e f g h (a-q*e) (b-q*f) (c-q*g) (d-q*h) xs ys
| e /= 0 || f /= 0
, (e == 0 && g == 0) || abs (g*e*b - g*a*f) > abs (f*e*c - g*a*f)
= go (a*x+b) a (c*x+d) c (e*x+f) e (g*x+h) g xs' ys
@ekmett
ekmett / WordMap.hs
Created August 5, 2015 02:03
unsafeCoerce all the things
{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE MultiParamTypeClasses #-}
@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]