Skip to content

Instantly share code, notes, and snippets.

View bond15's full-sized avatar

bond15

View GitHub Profile
module MonadTransformers where
import Text.Read (readMaybe)
import Control.Monad (ap, liftM )
import GHC.Base(returnIO)
-- maybe monad
-- short circuit semantics
ex1 :: Maybe Int
ex1 = do
_ <- Just 4
_ <- Nothing
module FunctorExample where
import Prelude hiding (Functor)
-- an Option data type in haskell
-- this is used to avoid returning nulls
data Option a = Some a | None
-- 'a' is a type parameter
ex1 :: Option Int
ex1 = Some 7
data Maybe a = None | Some a
class Monad f where
return :: a -> f a
bind :: f a -> (a -> f b) -> f b
instance Monad Maybe where
return x = Some x
bind None f = None
record Monad (F : Set₀ -> Set₀) : Set₁ where
field
return : ∀ {A : Set} -> A -> F A
_>>=_ : ∀ {A B : Set} -> F A -> (A -> F B) -> F B
-- laws
leftUnit : ∀ {A B : Set}
(a : A)
(f : A -> F B)
-> (return a) >>= f ≡ f a
rightUnit : ∀ {A : Set}
Taking the derivative of a type gives you "the type of one hole contexts" of your original type.
What it gives you is a generic way to traverse your data type from the perspective of a 'hole' (akin to pointers).
ex) If I had a tree data type, I would get a way to write primative combinators
'left', 'right', 'up', 'down', etc.
-- binary tree with no data
data Tree = Leaf | Node Tree Tree
@bond15
bond15 / Fusion.hs
Last active November 23, 2020 21:47
data Term a b where
Map :: (a -> b) -> Term a b
Filter :: (a -> Maybe a) -> Term a (Maybe a)
fuseMap :: Term a b -> Term b c -> Term a c
fuseMap (Map f ) (Map g) = Map $ g . f
filterFuse :: Term a (Maybe a) -> Term a b -> Term a (Maybe b)
filterFuse (Filter p) (Map f ) = Map (\x -> p x >>= Just . f)
-- List in Scala
sealed trait List[+A]
case object Nil extends List[Nothing]
case class Cons[+A](head: A, tail: List[A]) extends List[A
-- map in Scala
def map[A,B](as: List[A])(f: A => B): List[B] = as match {
case Nil => Nil
case x :: xs => f(x) :: map(xs)(f)
@bond15
bond15 / FibHist.hs
Last active November 4, 2022 15:42
Fibonacci Histomorphism
module Fib where
import Control.Arrow((>>>),(&&&))
import Control.Comonad.Cofree
newtype Fix f = In { out :: (f (Fix f) ) }
data NatF a = Z | S a deriving Show
-- Peano natural numbers as the least fixed point of functor NatF
type Nat = Fix NatF
@bond15
bond15 / MergeSortHylo.hs
Created February 20, 2021 22:48
Merge Sort Hylomorphism
module Test where
import Control.Arrow((>>>))
type Algebra f a = f a -> a
type CoAlgebra f a = a -> f a
newtype Fix f = In { out :: (f (Fix f)) }
data TreeF a r = Empty | Leaf a | Node r r
@bond15
bond15 / adt.java
Last active February 22, 2021 09:13
early Algebraic Data Types in Java (semicolons in 2021.. lol ;))
package adt;
public class ADT {
// sealed is an experimental feature (requrires feature preview flag) of Java 15
public sealed interface Expr
permits Const, Plus, Times {
}
// records in Java 15
// records are Scala's Case Class (I'll admit Record is a much better and more accurate name!)