Last active
March 5, 2018 16:14
-
-
Save ivenmarquardt/09d54f90a47d617f56fbee28f233857b to your computer and use it in GitHub Desktop.
Effect type applicative computation - deferred runtime error
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
const Data = Tcons => Dcons => { | |
const Data = x => { | |
const t = new Tcons(); | |
t[`run${Tcons.name}`] = x; | |
t.tag = Tcons.name | |
return t; | |
}; | |
return Dcons(Data); | |
}; | |
const Eff = Data(function Eff() {}) (Eff => thunk => Eff(thunk)); | |
const runEff = f => tf => | |
f(tf.runEff()); | |
const map = f => tx => | |
Eff(() => f(tx.runEff())); | |
const of = x => | |
Eff(() => x); | |
const ap = tf => tx => | |
Eff(() => tf.runEff() (tx.runEff())); | |
const chain = mx => fm => | |
Eff(() => fm(mx.runEff()).runEff()); | |
const append = s => t => s.concat(t); | |
const id = x => x; | |
const readLn = Eff(() => window.prompt()); | |
const log = x => Eff(() => console.log(x)); | |
const tx = ap(map(append) (log("foo"))) | |
(readLn); // malicious code | |
// execution is deferred just like the runtime error... | |
runEff(id) (tx); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment