Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE TypeFamilies, DeriveFunctor, DeriveFoldable #-}
module HAD.Y2014.M04.D01.Exercise where
import Prelude hiding (any, elem)
import Data.Reify
import Control.Applicative
import Data.Foldable
import Data.Monoid
import Data.Maybe
import System.IO.Unsafe
@sjoerdvisscher
sjoerdvisscher / implicit.hs
Created June 28, 2014 13:17
The implicit calculus: polymorphic rules
{-# LANGUAGE RankNTypes, FlexibleContexts, ScopedTypeVariables #-}
import Data.Reflection
import Data.Constraint
import GHC.Exts (Any)
import Unsafe.Coerce (unsafeCoerce)
test :: ((Int, Int), (Bool, Bool))
test = give (close givePair) $ give (3 :: Int) $ give True $ (open given, open given)
givePair :: Given a => (a, a)
@sjoerdvisscher
sjoerdvisscher / Free.hs
Last active August 29, 2015 14:03
Free category
{-# LANGUAGE RankNTypes, GADTs #-}
module Free (FreeCat(..), singleton, null) where
import Prelude hiding ((.), id, null)
import Control.Applicative
import Control.Category
import Catenated
import View
@sjoerdvisscher
sjoerdvisscher / listTraversal.hs
Created July 21, 2014 14:22
List traversal with hask
listTraversal :: (Strong p, Monoidal (p [a])) => p a b -> p [a] [b]
listTraversal p = dimap f g $ _2 $ ap2 (nil, cons)
where
f as = (as, as)
g ([], (bs, _)) = bs
g (_ , (_, bs)) = bs
nil = return []
cons = return (:) `ap` lmap Prelude.head p `ap` lmap Prelude.tail (listTraversal p)
@sjoerdvisscher
sjoerdvisscher / coarb.hs
Last active August 29, 2015 14:06
Coarbitrary as contravariant Applicative
newtype CoArb a = CoArb { unCoArb :: forall b. a -> Gen b -> Gen b }
instance Contravariant CoArb where
contramap f (CoArb g) = CoArb $ \a -> g (f a)
instance Divisible CoArb where
divide f (CoArb g) (CoArb h) = CoArb $ \a -> case f a of
(b, c) -> g b . h c
conquer = CoArb $ const id
protocol Semigroup {
func +(x: Self, y:Self) -> Self
}
extension String: Semigroup {}
extension Int: Semigroup {}
func foldMap1<A, M : Semigroup, S : SequenceType where S.Generator.Element == A>(t: S, f: A -> M) -> M? {
var g = t.generate()
if let x0 = g.next() {
var x = f(x0)
@sjoerdvisscher
sjoerdvisscher / mutating.swift
Created January 14, 2015 10:04
zipper-like mutation in Swift
struct Rect {
var width = 1
var height = 1
mutating func scaleBy(value: Int) {
width *= value
height *= value
}
}
import Foundation
enum JSONValue: StringLiteralConvertible, FloatLiteralConvertible, DictionaryLiteralConvertible, ArrayLiteralConvertible, Equatable, DebugPrintable {
case JSONString(String)
case JSONNumber(Double)
case JSONObject([String: JSONValue])
case JSONArray([JSONValue])
init(stringLiteral value: String) {
self = JSONString(value)
}
@sjoerdvisscher
sjoerdvisscher / FreeWeb.hs
Created December 19, 2009 16:35
Free Monad Web example
import Control.Monad.Free -- from category-extras
data WebF r = Display String r | Form String (String -> r)
instance Functor WebF where
fmap f (Display m r) = Display m (f r)
fmap f (Form m g) = Form m (f . g)
type Web = Free WebF
@sjoerdvisscher
sjoerdvisscher / SLC.hs
Created December 29, 2009 10:28
Symmetric Lambda Calculus
{-# LANGUAGE GADTs, KindSignatures #-}
data Val = B Int | Unit | Pair Val Val | In1 Val | In2 Val | Closr (Val -> Cnt -> Ans) | Contx Val Cnt
type Cnt = Val -> Ans
type Env = Ide -> Either Val Cnt
type Ans = IO ()
type Ide = String
data E :: * -> * where