Skip to content

Instantly share code, notes, and snippets.

@ivenmarquardt
Last active March 5, 2018 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivenmarquardt/09d54f90a47d617f56fbee28f233857b to your computer and use it in GitHub Desktop.
Save ivenmarquardt/09d54f90a47d617f56fbee28f233857b to your computer and use it in GitHub Desktop.
Effect type applicative computation - deferred runtime error
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