import { Unit, Fun, Prod, Sum, unit, absurd, fst, snd, defun, fun, fun2, inl, inr, apply, apply_pair, id, constant, curry, uncurry, lazy, swap_prod, swap_sum, compose_pair, distribute_sum_prod_inv, distribute_sum_prod, fun3, co_get_state, co_set_state, Option, CoPreRes, CoCont, CoRet, CoRes, continuation, no_error } from "ts-bccc"
import * as CCC from "ts-bccc"
import { mk_coroutine, Coroutine, suspend, co_unit, co_run, co_error } from "ts-bccc"
import * as Immutable from "immutable"
export let option_to_array = function(x:Option) : Array {
let l = fun>(x => [x])
let r = fun>(_ => [])
let f = l.plus(r)
return apply(f, x)
}
export let some = function() { return fun((x:A) => apply(inl(), x)) }
export let none = function() { return fun((x:Unit) => apply(inr(), {}) ) }
export let option_plus = function(p:Fun>, q:Fun>) : Fun> {
let f = (id().times(p)).then(distribute_sum_prod()).then((snd().then(some())).plus(fst().then(q)))
return fun(c => apply(f,c))
}
export let co_run_to_end = function(p:Coroutine,s:S) : Sum> {
let run_rec = fun2, S, Sum>>((p,s) => co_run_to_end(p,s))
let j1:Fun, CoRet>, Sum>> = run_rec.plus(inr())
let j:Fun, CoRet>>, Sum>> = inl>().plus(j1)
let i = apply(p.run.then(j),s)
return i
}
export let co_repeat = function(p:Coroutine) : Coroutine> {
return co_catch>((e1,e2)=>e1)(
p.then(x =>
co_repeat(p).then(xs =>
co_unit([x, ...xs])
)))(co_unit(Array()))
}
export let comm_list_coroutine = function(ps:Immutable.List>) : Coroutine> {
if (ps.isEmpty()) return co_unit>(Immutable.List())
let h = ps.first()
let t = ps.rest().toList()
return h.then(h_res =>
comm_list_coroutine(t).then(t_res =>
co_unit>(t_res.unshift(h_res))
))
}
export let co_lookup = (p: Coroutine): Coroutine =>
co_get_state().then(s => p.then(p_res => co_set_state(s).then(_ => co_unit(p_res))))
// Note: ugly (non categorical) version made in a hurry because Giulia, Rachel and Rebecca are screaming in my ears
// and I just want to get started on the actual parser.
export let co_not = (e:E) => (p: Coroutine) : Coroutine =>
mk_coroutine(fun(s => {
let res = p.run.f(s)
if (res.kind == "left") { // error case
let f = inr, CoRet>().then(inr>())
let res1:Prod = {fst:{}, snd:s}
return f.f(res1)
}
if (res.value.kind == "left") { // continuation case
let k = res.value.value.fst
let s1 = res.value.value.snd
let k1 = co_not(e)(k)
let res1 = k1.run.f(s1)
return res1
}
return inl>().f(e)
}))
export let co_catch = (merge_errors:(e1:E,e2:E)=>E) => (p: Coroutine) => (on_err:Coroutine) : Coroutine =>
mk_coroutine(fun(s => {
let res = p.run.f(s)
if (res.kind == "left") { // error case
let e1 = res.value
let k = co_map_error(e2 => merge_errors(e1,e2))(on_err)
return k.run.f(s)
}
if (res.value.kind == "left") { // continuation case
let k = res.value.value.fst
let s1 = res.value.value.snd
let k1 = co_catch(merge_errors)(k)(co_set_state(s).then(_ => on_err))
return apply(continuation().then(no_error()), { fst:k1, snd:s1 })
}
return res
}))
export let co_catch_many = (empty_error:E) => (p: Immutable.List>) : Coroutine =>
p.isEmpty() ? co_error(empty_error)
: co_catch((_,e) => e)(p.first())(co_catch_many(empty_error)(p.skip(1).toList()))
export let co_map_error = (f:(_:E) => E1) => (p:Coroutine) : Coroutine =>
mk_coroutine(fun(
s => {
let res = p.run.f(s)
if (res.kind == "left") { // error case
let g:CoPreRes = apply(inl>(), f(res.value))
return g
}
if (res.value.kind == "left") { // continuation case
let k = res.value.value.fst
let s1 = res.value.value.snd
let k1:Coroutine = co_map_error(f)(k)
return apply(continuation().then(no_error()), { fst:k1, snd:s1 })
}
let actual_res = res.value.value
let h:CoPreRes = apply(inr>().after(inr,CoRet>()), actual_res)
return h
}))
export let co_stateless = (p:Coroutine) : Coroutine => {
return co_get_state().then(s =>
p.then(p_res =>
co_set_state(s).then(_ =>
co_unit(p_res))))
}