Skip to content

Instantly share code, notes, and snippets.

@joliss
Last active June 20, 2017 18:06
Show Gist options
  • Save joliss/bce6017904544a11da00d28abf4005a3 to your computer and use it in GitHub Desktop.
Save joliss/bce6017904544a11da00d28abf4005a3 to your computer and use it in GitHub Desktop.
for next time I need to understand how Monads, higher-kinded types, sorts, and dependent types fit together
// let's turn Promise and List into Monads
Promise::bind (then but must get promise)
List::bind (flatmap)
Promise::pure = Promise.resolve
List::pure = [x]
// This works in JS:
map(f: Fn, m: Monad) {
m.bind(|x| m.constructor.pure(f(x)))
}
// in Rust: this could work:
map(f: Fn<T, T>, m: M) -> M where M: Monad<T> {
m.bind(|x| M::pure(f(x)))
}
// but not with different types:
map(f: Fn<A, B>, m: M<A>) -> M<B> where M: Monad {
m.bind(|x| M::pure(f(x)))
}
// Haskell has kinds like this, like Type -> Type or
// Type -> Type -> Type
//
// Monad is an example
// but you cannot define
f: * -> * // * = Type
f t =
if ...
return a * object
// also note you don't have sorts etc.
//
// Idris does though. (all of it?)
// How is this useful? Still not so sure how often we are generic over Monads.
// But Foldable/Collection occurs a lot:
// e.g. would like to implement all our functions like
f(stuff: Collection<T>, more_stuff: Collection<U>)
-> Collection<V>
// so we don't tie to Vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment