Last active
May 23, 2017 09:16
-
-
Save hodzanassredin/dbe9b115645ee793d527 to your computer and use it in GitHub Desktop.
from (co)monad to (co)monoid
This file contains hidden or 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
type IntG<'a> = IntG of int * 'a | |
let fmap f (IntG(i,v)) : IntG<_>= IntG(i,f v) | |
let mzeroInt32 = 0 | |
let mappendInt32 a b = a + b | |
let destroyInt32 a = () | |
let pairInt32 a = (a,a) | |
//monad | |
let ret x = IntG(mzeroInt32, x) | |
let join (IntG(i,IntG(i2,v))) = IntG(mappendInt32 i i2,v) | |
let bind v f = join (fmap f v) | |
let (>>=) a b = bind a b | |
let fmapM ab ma = ma >>= (ret << ab) | |
//comonad | |
let extract (IntG(i,a)) = destroyInt32 i | |
a | |
let rec extend (f:(IntG<'a> -> 'b)) (c:IntG<'a>) = | |
let (IntG(i,v)) = c | |
let i,i1 = pairInt32 i | |
IntG(i, f(IntG(i,v))) | |
let fmapC f = extend (f << extract) | |
let duplicate c = extend id c | |
type Int = IntG<unit> | |
//from monad to monoid | |
let mzero () : Int = ret () | |
let mappend a : Int = join a | |
//from comonad to comonoid | |
let destroy x : unit = extract x | |
let pair (x: Int) = duplicate x | |
type DoubleInt = IntG<int> | |
let mzero2 () : DoubleInt = ret mzeroInt32 | |
let mappend2 a : DoubleInt = join a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment