More sketching.
This commit is contained in:
parent
6489b1f1a3
commit
342b477896
144
sketching/hkt.dm
144
sketching/hkt.dm
@ -1,112 +1,88 @@
|
||||
pub hkt Monoid<T has A> {
|
||||
identity: T
|
||||
fn concat(first_item: A, second_item: A): T
|
||||
}
|
||||
|
||||
pub hkt Monad<T has A> {
|
||||
fn <B> map(mapper: fn (item: A): B): T<B>
|
||||
fn <B> flat_map(mapper: fn (item: A): T<B>): T<B>
|
||||
}
|
||||
|
||||
// Use this one!
|
||||
pub hkt Identity(a: A) {
|
||||
fn identity() = a
|
||||
pub int Unit
|
||||
|
||||
pub hkt Identity {
|
||||
fn identity() = Self
|
||||
}
|
||||
|
||||
pub hkt Monoid(a: A) : Identity(a) {
|
||||
pub int Addable<T : Addable<T>> {
|
||||
fn op+(other: T): T
|
||||
}
|
||||
|
||||
pub hkt Monoid<A : Addable<A>>(a: A) : Identity {
|
||||
fn concat(other: Self) = Self(a + other.a)
|
||||
}
|
||||
|
||||
pub hkt Monad1(a: A) {
|
||||
fn <B> map(mapper: fn (from: A): B) = Self(mapper(a))
|
||||
fn <B> flat_map(mapper: fn (from: A): T<B>) = Self(mapper(b)?)
|
||||
pub hkt Monad1<A>(item: A) {
|
||||
fn <B> map(m: fn (from: A): B) = item is A ? Self(m(item)) : self
|
||||
fn <B> flat_map(m: fn (from: A): T<B>) = item is A ? m(item) : self
|
||||
}
|
||||
|
||||
pub hkt Monad2<T has A, B> : Monad2<T> {
|
||||
fn map_right(mapper: fn (right: B): B): T
|
||||
fn flat_map_right(mapper: fn (right: B): T): T
|
||||
fn fold_right(folder: fn (right: B): A): T
|
||||
pub hkt Monad2<A, B>(item: A | B) : Monad1<A>(item is A ? item : Unit) {
|
||||
fn map_right(m: fn (right: B): B) = item is B ? Self(m(item)) : self
|
||||
fn flat_map_right(m: fn (right: B): Self) = item is B ? m(item) : self
|
||||
fn fold_right(f: fn (right: B): A) = item is B ? Self(f(item)) : self
|
||||
}
|
||||
|
||||
pub hkt Reader<T has A...> : Monad<T> {
|
||||
impl fn map(mapper) = Self(mapper(self))
|
||||
impl fn flat_map(mapper) = mapper(self)?
|
||||
fn bind(item: A): Self
|
||||
fn bind_from(item_producer: fn (): A): Self
|
||||
fn bind_from_map(item_mapper: fn (item: A): A): Self
|
||||
fn bind_from_flat_map(item_mapper: fn (item: A): T): Self
|
||||
fn ask(): A
|
||||
fn use(user: fn (item: A): T): Self
|
||||
pub hkt Reader2<R, A, B>(readable: R, item: A, other: B) : Monad2<A, B>(item) {
|
||||
fn read(reader: fn (readable: R): A) = Self(readable, reader(readable), other)
|
||||
fn use(user: fn (readable: R): Unit) = Self(readable, user(readable), other)
|
||||
}
|
||||
|
||||
pub enum type Option<T> : Monad1 {
|
||||
Some(item: T) {
|
||||
impl fn map(mapper) = Success(mapper(item))
|
||||
impl fn flat_map(mapper) = mapper(item)?
|
||||
},
|
||||
None {
|
||||
impl fn map = self
|
||||
impl fn flat_map = self
|
||||
}
|
||||
Some(item),
|
||||
None
|
||||
}
|
||||
|
||||
pub enum type IO<T, E> : Monad2 {
|
||||
Success(item: T) {
|
||||
impl fn map(mapper) = Success(mapper(item))
|
||||
impl fn flat_map(mapper) = mapper(item)?
|
||||
impl fn map_right(mapper) = self
|
||||
impl fn flat_map_right(mapper) = self
|
||||
impl fn fold_right(folder) = self
|
||||
}.
|
||||
Failure(error: E) {
|
||||
impl fn map(mapper) = self
|
||||
impl fn flat_map(mapper) = self
|
||||
impl fn map_right(mapper) = Failure(mapper(error))
|
||||
impl fn flat_map_right(mapper) = mapper(error)?
|
||||
impl fn fold_right(folder) = Success(folder(error))
|
||||
}
|
||||
Success(item: T),
|
||||
Failure(error: E)
|
||||
}
|
||||
|
||||
// usage in a file interface
|
||||
decl pub fn open_file(name: String): FileReader<IO<File, Error>>
|
||||
pub fn open_file(name: String): FileReader<Unit> {
|
||||
try {
|
||||
let file: LuaObject = lua {% fs.open(${name}) %}
|
||||
return Success(
|
||||
File {
|
||||
name,
|
||||
impl fn read_data() {
|
||||
lua {% ${file}.readAll() %}
|
||||
}
|
||||
impl fn close() {
|
||||
lua {% ${file}.close() %}
|
||||
}
|
||||
},
|
||||
Unit
|
||||
)
|
||||
} catch (e: LuaError) {
|
||||
return Failure(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum type FileReader<R, E> : Reader2<File, R, E> {
|
||||
Success(file: File, result: R) : Super(file, result, Unit),
|
||||
Failure(error: E) : Super(file, Unit, error)
|
||||
}
|
||||
|
||||
pub int File {
|
||||
name: String
|
||||
fn read_data(): IO<String, Error>
|
||||
fn close: IO<Void, Error>
|
||||
fn read_data(): String
|
||||
fn close()
|
||||
}
|
||||
|
||||
pub type FileReader : Reader<IO<File, Error>> {
|
||||
fld io: IO<File, Error>
|
||||
// usage in a main file, perhaps
|
||||
|
||||
impl fn bind(file) {
|
||||
io = Success(file)
|
||||
}
|
||||
|
||||
impl fn bind_from(producer) {
|
||||
io = Success(producer())
|
||||
}
|
||||
|
||||
impl fn bind_from_map(mapper) {
|
||||
io = Success(mapper(io?))
|
||||
}
|
||||
|
||||
impl fn bind_from_flat_map(flat_mapper) {
|
||||
io = Success(flat_mapper(io?)?)
|
||||
}
|
||||
|
||||
impl fn ask() = io?
|
||||
|
||||
impl fn use(user) = {
|
||||
user(io?)
|
||||
self
|
||||
match open_file('test.txt')
|
||||
.read { f -> f.read_data() }
|
||||
.map { data -> data.length() }
|
||||
{
|
||||
Success(file, length) => {
|
||||
f.close()
|
||||
println(result)
|
||||
},
|
||||
Failure(error) => {
|
||||
println(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
// some other file
|
||||
|
||||
open_file('test.txt') // FileReader<IO<File, Error>>
|
||||
.flat_map { f -> f.read_data() } // FileReader<IO<String, Error>>
|
||||
.use { f -> f.close() } // FileReader<IO<String, Error>>
|
||||
.ask()? // String
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user