Skip to content

Instantly share code, notes, and snippets.

Avatar
🤞

Edward Kmett ekmett

🤞
View GitHub Profile
View Tarjan.hs
{-# 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
Replacing Failure by a Heap of Successes
View Success.hs
{-# 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
basic operations on continued fractions
View CF.hs
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 Aug 5, 2015
unsafeCoerce all the things
View WordMap.hs
{-# 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 Jun 26, 2010
Monads for PLT Racket
View 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))
View deepSeq once and only once
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
View product categories in scala
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>
View dual
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
}
View why not conformant?
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
View example
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]