This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ConstraintKinds #-} | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
module ParserCombinators where | |
{- | |
We'll build a set of parser combinators from scratch demonstrating how | |
they arise as a monad transformer stack. Actually, how they arise as a | |
choice between two different monad transformer stacks! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("\033[39mRunning tests…"); | |
function assertEquals(actual, expected, description) { | |
if(typeof actual === "undefined") { | |
console.error("\033[31m" + description + " not implemented\033[39m"); | |
} else { | |
if(actual !== expected) { | |
console.error("\033[31m" + description + " failed, expected " + expected + ", got " + actual + "\033[39m"); | |
} else { | |
console.log(description + " \033[32m ok\033[39m"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE RankNTypes #-} | |
import Control.Applicative | |
import Data.Functor.Identity | |
import Data.Monoid | |
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t | |
type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f t | |
_1 :: Lens (a,b) (a',b) a a' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, Rank2Types #-} | |
import Control.Applicative | |
import Control.Monad | |
import Data.Functor.Identity | |
import Data.Foldable | |
import Data.Monoid | |
import Data.Tagged | |
class Profunctor p where | |
dimap :: (a -> b) -> (c -> d) -> p b c -> p a d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Prod = t => ({ | |
extract : () => t[1], | |
extend : f => Prod([t[0], f([t[0], t[1]])]), | |
map : f => Prod([t[0], f(t[1])]), | |
duplicate : () => Prod([t[0], Prod(t)]) | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const compose = (f, g) => x => f(g(x)) | |
const Id = x => | |
({ | |
fold: f => f(x), | |
map: f => Id(f(x)) | |
}) | |
Id.of = Id | |
const Tuple = (_1, _2) => |