Skip to content

Instantly share code, notes, and snippets.

@zerobias
Created July 8, 2018 18:20
Show Gist options
  • Save zerobias/ccdf12d3d188914eba2d3614a37dd6d9 to your computer and use it in GitHub Desktop.
Save zerobias/ccdf12d3d188914eba2d3614a37dd6d9 to your computer and use it in GitHub Desktop.
Prepack + reason compilation
type idInt = int;
type countInt = int;
let nextId = {
let id: ref(idInt) = ref(0);
(.) => {
id := id^ + 1;
id^;
};
};
type frame =
| NoFrame(idInt)
| FirstFrame(idInt, frame)
| Frame(idInt, countInt, frame);
let f = ref(NoFrame(nextId(.)));
let ask =
(.) => {
let idd = nextId(.);
switch (f^) {
| NoFrame(i) => f := FirstFrame(idd, f^)
| FirstFrame(i, nf) => f := Frame(idd, 1, f^)
| Frame(i, n, ff) => f := Frame(idd, n + 1, f^)
};
idd;
};
let forget =
(.) =>
switch (f^) {
| NoFrame(i) => i
| FirstFrame(i, nf) =>
f := nf;
i;
| Frame(i, n, ff) when n > 1 =>
f := ff;
i;
| Frame(i, _, ff) =>
f := ff;
i;
};
type data(_) =
| Pred(bool): data(bool)
| Field(Js.Dict.t('t), string): data('t => unit)
| Val('t): data('t);
/* | Ref(unit => data('t)): data('t) */
/* | Fn(data('s) => data('t)): data('s => 't) */
/* | Hidden(data(_) => unit): data(unit => unit); */
type expr(_) =
| Static(data('a)): expr('a)
| If(expr(bool), expr('a), expr('a)): expr('a)
| Eq(expr('a), expr('a)): expr(bool)
| WriteProp(expr('t => unit), expr('t)): expr(unit => unit)
| SideEffect(unit => unit): expr(unit)
/* | Switch(expr('a), expr('a => bool)): expr('a) */
| Join(expr(expr('a))): expr('a);
let eq1 =
If(
Eq(Static(Val(0)), Static(Val(0))),
Static(Val("gadt is ok")),
Static(Val("gadt not ok")),
);
let rec eval: type a. expr(a) => a =
fun
| Static(Val(t)) => t
| Static(Pred(t)) => t
| Static(Field(d, key)) => (t => Js.Dict.set(d, key, t))
| WriteProp(ex, e) => {
let writer = () => {
let exFn = eval(ex);
let d = eval(e);
exFn(d);
};
writer;
}
| SideEffect(fn) => fn()
| If(cond, win, fail) =>
if (eval(cond)) {
eval(win);
} else {
eval(fail);
}
| Eq(a, b) => eval(a) == eval(b)
| Join(a) => eval(eval(a));
Js.log(eval(eq1));
let w: Weak.t(string) = Weak.create(5);
Weak.set(w, 1, Some("ok"));
Weak.set(w, 2, Some("more"));
Weak.set(w, 3, None);
Js.log(w);
import * as Weak from "bs-platform/lib/es6/weak.js";
import * as Block from "bs-platform/lib/es6/block.js";
import * as Curry from "bs-platform/lib/es6/curry.js";
import * as Caml_obj from "bs-platform/lib/es6/caml_obj.js";
var id = [0];
function nextId() {
id[0] = id[0] + 1 | 0;
return id[0];
}
var f = [/* NoFrame */Block.variant("NoFrame", 0, [nextId()])];
function ask() {
var idd = nextId();
var match = f[0];
switch (match.tag | 0) {
case 0 :
f[0] = /* FirstFrame */Block.variant("FirstFrame", 1, [
idd,
f[0]
]);
break;
case 1 :
f[0] = /* Frame */Block.variant("Frame", 2, [
idd,
1,
f[0]
]);
break;
case 2 :
f[0] = /* Frame */Block.variant("Frame", 2, [
idd,
match[1] + 1 | 0,
f[0]
]);
break;
}
return idd;
}
function forget() {
var match = f[0];
switch (match.tag | 0) {
case 0 :
return match[0];
case 1 :
f[0] = match[1];
return match[0];
case 2 :
var ff = match[2];
var i = match[0];
f[0] = ff;
return i;
}
}
function $$eval(_param) {
while(true) {
var param = _param;
switch (param.tag | 0) {
case 0 :
var match = param[0];
switch (match.tag | 0) {
case 1 :
var key = match[1];
var d = match[0];
return (function(d,key){
return function (t) {
d[key] = t;
return /* () */0;
}
}(d,key));
case 0 :
case 2 :
return match[0];
}
case 1 :
if ($$eval(param[0])) {
_param = param[1];
continue ;
} else {
_param = param[2];
continue ;
}
case 2 :
return Caml_obj.caml_equal($$eval(param[0]), $$eval(param[1]));
case 3 :
var e = param[1];
var ex = param[0];
return (function(ex,e){
return function () {
var exFn = $$eval(ex);
return Curry._1(exFn, $$eval(e));
}
}(ex,e));
case 4 :
return Curry._1(param[0], /* () */0);
case 5 :
_param = $$eval(param[0]);
continue ;
}
};
}
console.log($$eval(/* If */Block.variant("If", 1, [
/* Eq */Block.variant("Eq", 2, [
/* Static */Block.variant("Static", 0, [/* Val */Block.variant("Val", 2, [0])]),
/* Static */Block.variant("Static", 0, [/* Val */Block.variant("Val", 2, [0])])
]),
/* Static */Block.variant("Static", 0, [/* Val */Block.variant("Val", 2, ["gadt is ok"])]),
/* Static */Block.variant("Static", 0, [/* Val */Block.variant("Val", 2, ["gadt not ok"])])
])));
var w = Weak.create(5);
Weak.set(w, 1, /* Some */Block.simpleVariant("Some", ["ok"]));
Weak.set(w, 2, /* Some */Block.simpleVariant("Some", ["more"]));
Weak.set(w, 3, /* None */0);
console.log(w);
export {
ask ,
forget ,
}
/* f Not a pure module */
var ab = function(r) {
"use strict";
var n = [ "Invalid_argument", -3 ];
function t(r, n, t) {
for (var e = new Array(t), u = 0, a = n; u < t; ) e[u] = r[a], u = u + 1 | 0, a = a + 1 | 0;
return e;
}
function e(r, n, e) {
if (e > 7 || e < 0) return function r(n, e) {
for (;;) {
var u = e, a = n, i = a.length, c = 0 === i ? 1 : i, o = c - u.length | 0;
if (0 === o) return a.apply(null, u);
if (!(o < 0)) return function(n, t) {
return function(e) {
return r(n, t.concat([ e ]));
};
}(a, u);
e = t(u, c, 0 | -o), n = a.apply(null, t(u, 0, c));
}
}(r, [ n ]);
switch (e) {
case 0:
case 1:
return r(n);
case 2:
return function(t) {
return r(n, t);
};
case 3:
return function(t, e) {
return r(n, t, e);
};
case 4:
return function(t, e, u) {
return r(n, t, e, u);
};
case 5:
return function(t, e, u, a) {
return r(n, t, e, u, a);
};
case 6:
return function(t, e, u, a, i) {
return r(n, t, e, u, a, i);
};
case 7:
return function(t, e, u, a, i, c) {
return r(n, t, e, u, a, i, c);
};
}
}
function u(r, n) {
var t = r.length;
return 1 === t ? r(n) : e(r, n, t);
}
n.tag = 248;
var a = [ 0 ];
function i(r) {
var n = [ r, (a[0] += 1, a[0]) ];
return n.tag = 248, n;
}
i("Js_exn.Error"), i("Array.Bottom");
function c(r, n, t) {
return t.tag = n, Object.defineProperty(t, Symbol.for("BsVariant"), {
value: r
});
}
function o(r, n) {
return Object.defineProperty(n, Symbol.for("BsVariant"), {
value: r
});
}
var f = function(r, n) {
for (var t in r) n(t);
};
function s(r, t) {
for (;;) {
var e = t, u = r;
if (u === e) return !0;
var a = typeof u;
if ("string" === a || "number" === a || "boolean" === a || "undefined" === a || null === u) return !1;
var i = typeof e;
if ("function" === a || "function" === i) throw [ n, "equal: functional value" ];
if ("number" === i || "undefined" === i || null === e) return !1;
var c = 0 | u.tag, o = 0 | e.tag;
if (250 !== c) if (250 !== o) {
if (248 === c) return u[1] === e[1];
if (251 === c) throw [ n, "equal: abstract value" ];
if (c !== o) return !1;
var l = 0 | u.length;
if (l !== (0 | e.length)) return !1;
if (!Array.isArray(u)) {
var v = u, g = e, y = [ !0 ], h = function(r, n) {
return function(t) {
return r.hasOwnProperty(t) ? 0 : (n[0] = !1, 0);
};
}(g, y), m = function(r, n, t) {
return function(e) {
return r.hasOwnProperty(e) && s(n[e], r[e]) ? 0 : (t[0] = !1, 0);
};
}(v, g, y);
return f(v, h), y[0] && f(g, m), y[0];
}
for (var b = u, w = e, p = 0, d = l; ;) {
var S = p;
if (S === d) return !0;
if (!s(b[S], w[S])) return !1;
p = S + 1 | 0;
}
} else t = e[0]; else r = u[0];
}
}
i("Pervasives.Exit");
var l = function(r) {
return new Array(r);
}, v = function(r, n, t) {
return t ? (r[n] = t[0], 0) : 0;
}, g = [ 0 ];
function y() {
return g[0] = g[0] + 1 | 0, g[0];
}
var h = [ c("NoFrame", 0, [ y() ]) ];
console.log(function r(n) {
for (;;) {
var t = n;
switch (0 | t.tag) {
case 0:
var e = t[0];
switch (0 | e.tag) {
case 1:
return function(r, n) {
return function(t) {
return r[n] = t, 0;
};
}(e[0], e[1]);
case 0:
case 2:
return e[0];
}
case 1:
if (r(t[0])) {
n = t[1];
continue;
}
n = t[2];
continue;
case 2:
return s(r(t[0]), r(t[1]));
case 3:
return function(n, t) {
return function() {
return u(r(n), r(t));
};
}(t[0], t[1]);
case 4:
return u(t[0], 0);
case 5:
n = r(t[0]);
continue;
}
}
}(c("If", 1, [ c("Eq", 2, [ c("Static", 0, [ c("Val", 2, [ 0 ]) ]), c("Static", 0, [ c("Val", 2, [ 0 ]) ]) ]), c("Static", 0, [ c("Val", 2, [ "gadt is ok" ]) ]), c("Static", 0, [ c("Val", 2, [ "gadt not ok" ]) ]) ])));
var m = l(5);
return v(m, 1, o("Some", [ "ok" ])), v(m, 2, o("Some", [ "more" ])), v(m, 3, 0),
console.log(m), r.ask = function() {
var r = y(), n = h[0];
switch (0 | n.tag) {
case 0:
h[0] = c("FirstFrame", 1, [ r, h[0] ]);
break;
case 1:
h[0] = c("Frame", 2, [ r, 1, h[0] ]);
break;
case 2:
h[0] = c("Frame", 2, [ r, n[1] + 1 | 0, h[0] ]);
}
return r;
}, r.forget = function() {
var r = h[0];
switch (0 | r.tag) {
case 0:
return r[0];
case 1:
return h[0] = r[1], r[0];
case 2:
var n = r[0];
return h[0] = r[2], n;
}
}, r;
}({});
var ab;
(function() {
"use strict";
var r = {
enumerable: !1,
configurable: !1,
writable: !1
}, a = this.Symbol.for, e = this.Object.defineProperty, t = function(r, a, e) {
return e.tag = a, Object.defineProperty(e, Symbol.for("BsVariant"), {
value: r
});
};
console.log("gadt is ok");
var n = [ , "ok", "more" ];
n.length = 5, console.log(n);
var o = [ 1 ], s = [ 1 ], c = a("BsVariant");
r.value = "NoFrame", e(s, c, r), s.tag = 0;
var i = [ s ];
this.ab = {
ask: function() {
var r = (o[0] = o[0] + 1 | 0, o[0]), a = i[0];
switch (0 | a.tag) {
case 0:
i[0] = t("FirstFrame", 1, [ r, i[0] ]);
break;
case 1:
i[0] = t("Frame", 2, [ r, 1, i[0] ]);
break;
case 2:
i[0] = t("Frame", 2, [ r, a[1] + 1 | 0, i[0] ]);
}
return r;
},
forget: function() {
var r = i[0];
switch (0 | r.tag) {
case 0:
return r[0];
case 1:
return i[0] = r[1], r[0];
case 2:
var a = r[0];
return i[0] = r[2], a;
}
}
};
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment