diff --git a/src/Component1.js b/src/Component1.js deleted file mode 100644 index 73518f2..0000000 --- a/src/Component1.js +++ /dev/null @@ -1,21 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as React from "react"; - -function Component1(Props) { - var message = Props.message; - return React.createElement("div", { - onClick: (function (_event) { - console.log("clicked!"); - return /* () */0; - }) - }, message); -} - -var make = Component1; - -export { - make , - -} -/* react Not a pure module */ diff --git a/src/Component1.re b/src/Component1.re deleted file mode 100644 index 8586655..0000000 --- a/src/Component1.re +++ /dev/null @@ -1,6 +0,0 @@ -[@react.component] -let make = (~message) => { -
Js.log("clicked!")}> - {React.string(message)} -
; -}; \ No newline at end of file diff --git a/src/Demo.js b/src/Demo.js deleted file mode 100644 index 8bd3fec..0000000 --- a/src/Demo.js +++ /dev/null @@ -1,161 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as Caml_obj from "bs-platform/lib/es6/caml_obj.js"; -import * as Belt_List from "bs-platform/lib/es6/belt_List.js"; -import * as Belt_Array from "bs-platform/lib/es6/belt_Array.js"; -import * as Diff$ReactTemplate from "./Diff.js"; -import * as Styp$ReactTemplate from "./Styp.js"; -import * as Samples$ReactTemplate from "./Samples.js"; -import * as Caml_builtin_exceptions from "bs-platform/lib/es6/caml_builtin_exceptions.js"; -import * as Serialize$ReactTemplate from "./Serialize.js"; -import * as TypeCheck$ReactTemplate from "./TypeCheck.js"; -import * as DynTypedJson$ReactTemplate from "./DynTypedJson.js"; - -function testSmall(param) { - var small = JSON.parse(" [{\"name\":null} ] "); - var styp = TypeCheck$ReactTemplate.fromJson(undefined, small); - console.log(JSON.stringify(Styp$ReactTemplate.stypToJson(styp))); - return /* () */0; -} - -function logDiff(diff) { - console.log(JSON.stringify(Diff$ReactTemplate.toJson(diff))); - return /* () */0; -} - -function testSmallDiff($staropt$star, n) { - var mode = $staropt$star !== undefined ? $staropt$star : TypeCheck$ReactTemplate.defaultMode; - var examples = /* array */[ - /* tuple */[ - " {\"x\": \"hello\"} ", - " {\"x\": null, \"y\":0} " - ], - /* tuple */[ - " [ \"hell\", 0, \"world\"] ", - " [ \"hell\", \"o\", \"world\"] " - ], - /* tuple */[ - " [\"h\", \"e\", \"l\", \"l\", \"o\", \"w\", \"o\", \"r\", \"l\", \"d\"] ", - " [\"h\", \"e\", \"l\", \"l\", 0, \"w\", \"o\", \"r\", \"l\", \"d\"] " - ], - /* tuple */[ - "[null,2,3,4]", - "[3]" - ], - /* tuple */[ - " [{\"x\": {\"y\" : \"hello\"}}] ", - " [{\"x\": {\"z\" : \"hello\"}}] " - ], - /* tuple */[ - "null", - "3" - ], - /* tuple */[ - " [{\"a\":3},{\"b\":3}] ", - " [] " - ], - /* tuple */[ - " [null,{\"b\":3}] ", - " [] " - ], - /* tuple */[ - " [3, \"hello\", true, 4] ", - " [1,2,3] " - ], - /* tuple */[ - " 3 ", - " \"abc\" " - ], - /* tuple */[ - "[{\"x\": [\"p\",\"s\"]}]", - "[{\"x\":[\"p\",\"b\"]},\n {\"x\":[\"p\",\"s\"]},\n {\"x\": [\"p\",\"s\"]}]" - ] - ]; - var styp1 = TypeCheck$ReactTemplate.fromJson(mode, JSON.parse(Belt_Array.getExn(examples, n)[0])); - var styp2 = TypeCheck$ReactTemplate.fromJson(mode, JSON.parse(Belt_Array.getExn(examples, n)[1])); - var diff = Diff$ReactTemplate.diffCheck(styp1, styp2); - logDiff(diff); - return diff; -} - -function testSamples($staropt$star, param) { - var mode = $staropt$star !== undefined ? $staropt$star : TypeCheck$ReactTemplate.defaultMode; - var partial_arg = mode; - var styps = Belt_List.map(Belt_List.map(/* :: */[ - " {\"x\": 1, \"y\":\"hello\"} ", - /* :: */[ - " {\"x\": 2} ", - /* :: */[ - " {\"x\": 3, \"y\":null} ", - /* [] */0 - ] - ] - ], (function (prim) { - return JSON.parse(prim); - })), (function (param) { - return TypeCheck$ReactTemplate.fromJson(partial_arg, param); - })); - return Samples$ReactTemplate.getAllDiffs(Samples$ReactTemplate.fromList(styps)); -} - -function testDynamicallyTypedJson(param) { - var json1 = JSON.parse(" null "); - var json2 = JSON.parse(" {\"b\":3} "); - var x = DynTypedJson$ReactTemplate.ref(json1); - console.log("x:", x[/* json */0]); - DynTypedJson$ReactTemplate.$colon$eq(x, json2); - console.log("x:", x[/* json */0]); - if (DynTypedJson$ReactTemplate.$less$great(x, DynTypedJson$ReactTemplate.$$null)) { - var x1 = DynTypedJson$ReactTemplate.$at(x, "b"); - console.log("x1:", x1[/* json */0]); - return /* () */0; - } else { - return 0; - } -} - -function testSerializer(o, testName) { - var s = Serialize$ReactTemplate.toString(o); - var o1 = Serialize$ReactTemplate.fromString(s); - if (Caml_obj.caml_notequal(o, o1)) { - console.log("Serialize " + testName); - console.log("o:", o); - console.log("s:", s); - console.log("o1:", o1); - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "Demo.re", - 93, - 4 - ] - ]; - } - return o; -} - -var res_000 = testSmallDiff(undefined, 0); - -var res = /* :: */[ - res_000, - /* [] */0 -]; - -function test(param) { - return res; -} - -testDynamicallyTypedJson(/* () */0); - -export { - testSmall , - logDiff , - testSmallDiff , - testSamples , - testDynamicallyTypedJson , - testSerializer , - res , - test , - -} -/* res Not a pure module */ diff --git a/src/Demo.re b/src/Demo.re deleted file mode 100644 index 93f1085..0000000 --- a/src/Demo.re +++ /dev/null @@ -1,116 +0,0 @@ -let testSmall = () => { - let small = Js.Json.parseExn({| [{"name":null} ] |}); - - let styp = TypeCheck.fromJson(small); - Js.log(styp->Styp.stypToJson->Js.Json.stringify); -}; - -let logDiff = diff => Js.log(diff->Diff.toJson->Js.Json.stringify); - -let testSmallDiff = (~mode=TypeCheck.defaultMode, n) => { - let examples = [| - ({| {"x": "hello"} |}, {| {"x": null, "y":0} |}), - ({| [ "hell", 0, "world"] |}, {| [ "hell", "o", "world"] |}), - ( - {| ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"] |}, - {| ["h", "e", "l", "l", 0, "w", "o", "r", "l", "d"] |}, - ), - ("[null,2,3,4]", "[3]"), - ({| [{"x": {"y" : "hello"}}] |}, {| [{"x": {"z" : "hello"}}] |}), - ("null", "3"), - ({| [{"a":3},{"b":3}] |}, {| [] |}), - ({| [null,{"b":3}] |}, {| [] |}), - ({| [3, "hello", true, 4] |}, {| [1,2,3] |}), - ({| 3 |}, {| "abc" |}), - ( - {|[{"x": ["p","s"]}]|}, - {|[{"x":["p","b"]}, - {"x":["p","s"]}, - {"x": ["p","s"]}]|}, - ), - |]; - let styp1 = - examples - ->(Belt.Array.getExn(n)) - ->fst - ->Js.Json.parseExn - ->(TypeCheck.fromJson(~mode)); - let styp2 = - examples - ->(Belt.Array.getExn(n)) - ->snd - ->Js.Json.parseExn - ->(TypeCheck.fromJson(~mode)); - let diff = Diff.diffCheck(styp1, styp2); - logDiff(diff); - diff; -}; - -let testSamples = (~mode=TypeCheck.defaultMode, ()) => { - let styps = - [{| {"x": 1, "y":"hello"} |}, {| {"x": 2} |}, {| {"x": 3, "y":null} |}] - ->(Belt.List.map(Js.Json.parseExn)) - ->(Belt.List.map(TypeCheck.fromJson(~mode))); - Samples.(styps->fromList->getAllDiffs); -}; - -/* let testBigDiff = () => { - let styp1 = - Query.reasonBronzeThread |. Js.Json.parseExn |. TypeCheck.fromJson; - let styp2 = - Query.reasonPlatinumThread |. Js.Json.parseExn |. TypeCheck.fromJson; - let diff = Diff.diffCheck(styp1, styp2); - logDiff(diff); - diff; - }; */ - -let testDynamicallyTypedJson = () => { - open! DynTypedJson; - - let json1 = Js.Json.parseExn({| null |}); - let json2 = Js.Json.parseExn({| {"b":3} |}); - - let x = ref(json1); - Js.log2("x:", x.json); - - x := json2; - Js.log2("x:", x.json); - - if (x != null) { - let x1 = x @ "b"; - Js.log2("x1:", x1.json); - }; -}; - -let testSerializer = (o, testName) => { - let s = Serialize.toString(o); - let o1 = Serialize.fromString(s); - if (o != o1) { - Js.log("Serialize " ++ testName); - Js.log2("o:", o); - Js.log2("s:", s); - Js.log2("o1:", o1); - assert(false); - }; - o; -}; - -let res = [testSmallDiff(0)]; -/* let res = [testSmallDiff(~mode=TypeCheck.singletonMode, 2)]; */ -/* let res = [testBigDiff()]; */ -/* let res = testSamples(); */ -// let test = () => res->(testSerializer("diff")); -let test = () => res; - -/* fails: serialization of functions - let _ = (x => x + 1) |. testSerializer("function"); - */ - -/* fails: serialization of cyclic values - type cyclic = - | C(cyclic); - let rec cyclic = C(cyclic); - let _ = cyclic |. testSerializer("cyclic"); - */ - -testDynamicallyTypedJson(); \ No newline at end of file diff --git a/src/Diff.js b/src/Diff.js deleted file mode 100644 index 7605358..0000000 --- a/src/Diff.js +++ /dev/null @@ -1,663 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as Block from "bs-platform/lib/es6/block.js"; -import * as Js_dict from "bs-platform/lib/es6/js_dict.js"; -import * as Caml_obj from "bs-platform/lib/es6/caml_obj.js"; -import * as Belt_List from "bs-platform/lib/es6/belt_List.js"; -import * as Belt_Array from "bs-platform/lib/es6/belt_Array.js"; -import * as Belt_SetString from "bs-platform/lib/es6/belt_SetString.js"; -import * as Styp$ReactTemplate from "./Styp.js"; -import * as Caml_builtin_exceptions from "bs-platform/lib/es6/caml_builtin_exceptions.js"; -import * as TypeCheck$ReactTemplate from "./TypeCheck.js"; - -function diffStyp(styp1, styp2) { - var match = styp1[/* typ */0]; - var match$1 = styp2[/* typ */0]; - var exit = 0; - if (typeof match === "number" || match.tag !== /* Union */6) { - exit = 2; - } else { - var styps1 = match[0]; - var exit$1 = 0; - if (typeof match$1 === "number" || match$1.tag !== /* Union */6) { - exit$1 = 3; - } else { - return diffUnion(styp1, styp2, styps1, match$1[0]); - } - if (exit$1 === 3) { - return diffUnion(styp1, styp2, styps1, /* :: */[ - styp2, - /* [] */0 - ]); - } - - } - if (exit === 2 && typeof match$1 !== "number" && match$1.tag === /* Union */6) { - return diffUnion(styp1, styp2, /* :: */[ - styp1, - /* [] */0 - ], match$1[0]); - } - if (TypeCheck$ReactTemplate.plusTyp(match, match$1) === undefined) { - return diffUnion(styp1, styp2, /* :: */[ - styp1, - /* [] */0 - ], /* :: */[ - styp2, - /* [] */0 - ]); - } else { - var match$2 = diffTyp(match, match$1); - var match$3 = diffO(styp1[/* o */1], styp2[/* o */1]); - var pB = Caml_obj.caml_min(styp1[/* p */2], styp2[/* p */2]); - var pA1 = Styp$ReactTemplate.P["--"](styp1[/* p */2], pB); - var pA2 = Styp$ReactTemplate.P["--"](styp2[/* p */2], pB); - var stypA1_000 = /* typ */match$2[/* typA1 */0]; - var stypA1_001 = /* o */match$3[0]; - var stypA1 = /* record */[ - stypA1_000, - stypA1_001, - /* p */pA1 - ]; - var stypA2_000 = /* typ */match$2[/* typA2 */1]; - var stypA2_001 = /* o */match$3[1]; - var stypA2 = /* record */[ - stypA2_000, - stypA2_001, - /* p */pA2 - ]; - var stypB_000 = /* typ */match$2[/* typB */2]; - var stypB_001 = /* o */match$3[2]; - var stypB = /* record */[ - stypB_000, - stypB_001, - /* p */pB - ]; - return /* record */[ - /* styp1 */styp1, - /* styp2 */styp2, - /* stypA1 */stypA1, - /* stypA2 */stypA2, - /* stypB */stypB - ]; - } -} - -function diffO(o1, o2) { - if (o1) { - if (o2) { - var p2 = o2[0]; - var p1 = o1[0]; - var match = Caml_obj.caml_greaterthan(p1, p2); - var match$1 = Caml_obj.caml_greaterthan(p2, p1); - return /* tuple */[ - match ? /* Opt */[Styp$ReactTemplate.P["--"](p1, p2)] : /* NotOpt */0, - match$1 ? /* Opt */[Styp$ReactTemplate.P["--"](p2, p1)] : /* NotOpt */0, - /* Opt */[Caml_obj.caml_min(p1, p2)] - ]; - } else { - return /* tuple */[ - o1, - /* NotOpt */0, - /* NotOpt */0 - ]; - } - } else { - return /* tuple */[ - /* NotOpt */0, - o2, - /* NotOpt */0 - ]; - } -} - -function diffTyp(typ1, typ2) { - var makeSame = function (typ) { - return /* record */[ - /* typA1 : Same */Block.__(0, [typ]), - /* typA2 : Same */Block.__(0, [typ]), - /* typB */typ - ]; - }; - var exit = 0; - var exit$1 = 0; - if (typeof typ1 === "number") { - exit$1 = 3; - } else { - switch (typ1.tag | 0) { - case /* Number */1 : - if (typeof typ2 === "number") { - exit$1 = 3; - } else { - switch (typ2.tag | 0) { - case /* Same */0 : - exit$1 = 3; - break; - case /* Number */1 : - if (Caml_obj.caml_equal(typ1[0], typ2[0])) { - return makeSame(typ1); - } else { - exit = 2; - } - break; - case /* Array */5 : - break; - default: - exit = 2; - } - } - break; - case /* String */2 : - if (typeof typ2 === "number") { - exit$1 = 3; - } else { - switch (typ2.tag | 0) { - case /* Same */0 : - exit$1 = 3; - break; - case /* String */2 : - if (Caml_obj.caml_equal(typ1[0], typ2[0])) { - return makeSame(typ1); - } else { - exit = 2; - } - break; - case /* Array */5 : - break; - default: - exit = 2; - } - } - break; - case /* Boolean */3 : - if (typeof typ2 === "number") { - exit$1 = 3; - } else { - switch (typ2.tag | 0) { - case /* Same */0 : - exit$1 = 3; - break; - case /* Boolean */3 : - if (Caml_obj.caml_equal(typ1[0], typ2[0])) { - return makeSame(typ1); - } else { - exit = 2; - } - break; - case /* Array */5 : - break; - default: - exit = 2; - } - } - break; - case /* Object */4 : - var d1 = typ1[0]; - if (typeof typ2 === "number") { - exit$1 = 3; - } else { - switch (typ2.tag | 0) { - case /* Same */0 : - exit$1 = 3; - break; - case /* Object */4 : - var d2 = typ2[0]; - var dA1 = { }; - var dA2 = { }; - var dB = { }; - var doItem2 = function (param) { - var styp2 = param[1]; - var lbl = param[0]; - var match = Js_dict.get(d1, lbl); - if (match !== undefined) { - var match$1 = diffStyp(match, styp2); - var stypA2 = match$1[/* stypA2 */3]; - var stypA1 = match$1[/* stypA1 */2]; - if (!Styp$ReactTemplate.stypIsEmpty(stypA1)) { - dA1[lbl] = stypA1; - } - if (!Styp$ReactTemplate.stypIsEmpty(stypA2)) { - dA2[lbl] = stypA2; - } - dB[lbl] = match$1[/* stypB */4]; - return /* () */0; - } else if (Styp$ReactTemplate.stypIsEmpty(styp2)) { - return 0; - } else { - dA2[lbl] = styp2; - return /* () */0; - } - }; - var doItem1 = function (param) { - var styp1 = param[1]; - var lbl = param[0]; - var match = Js_dict.get(d2, lbl); - if (match !== undefined || Styp$ReactTemplate.stypIsEmpty(styp1)) { - return /* () */0; - } else { - dA1[lbl] = styp1; - return /* () */0; - } - }; - Belt_Array.forEach(Js_dict.entries(d2), doItem2); - Belt_Array.forEach(Js_dict.entries(d1), doItem1); - var entries1 = Js_dict.entries(dA1); - var entries2 = Js_dict.entries(dA2); - var t = Styp$ReactTemplate.makeObject(entries1); - var match = entries1.length === 0; - var typA1 = match ? /* Same */Block.__(0, [t]) : t; - var t$1 = Styp$ReactTemplate.makeObject(entries2); - var match$1 = entries2.length === 0; - var typA2 = match$1 ? /* Same */Block.__(0, [t$1]) : t$1; - var typB = Styp$ReactTemplate.makeObject(Js_dict.entries(dB)); - return /* record */[ - /* typA1 */typA1, - /* typA2 */typA2, - /* typB */typB - ]; - case /* Array */5 : - break; - default: - exit = 2; - } - } - break; - case /* Array */5 : - if (typeof typ2 === "number") { - exit$1 = 3; - } else { - switch (typ2.tag | 0) { - case /* Same */0 : - exit$1 = 3; - break; - case /* Array */5 : - var match$2 = diffStyp(typ1[0], typ2[0]); - var stypA2 = match$2[/* stypA2 */3]; - var stypA1 = match$2[/* stypA1 */2]; - var match$3 = Styp$ReactTemplate.stypIsEmpty(stypA1); - var typA1$1 = match$3 ? /* Same */Block.__(0, [/* Array */Block.__(5, [stypA1])]) : /* Array */Block.__(5, [stypA1]); - var match$4 = Styp$ReactTemplate.stypIsEmpty(stypA2); - var typA2$1 = match$4 ? /* Same */Block.__(0, [/* Array */Block.__(5, [stypA2])]) : /* Array */Block.__(5, [stypA2]); - var typB$1 = /* Array */Block.__(5, [match$2[/* stypB */4]]); - return /* record */[ - /* typA1 */typA1$1, - /* typA2 */typA2$1, - /* typB */typB$1 - ]; - default: - exit = 2; - } - } - break; - default: - exit$1 = 3; - } - } - if (exit$1 === 3) { - if (typeof typ2 === "number" || !(typ2.tag && typeof typ1 !== "number")) { - return /* record */[ - /* typA1 */typ1, - /* typA2 */typ2, - /* typB : Empty */0 - ]; - } else { - switch (typ1.tag | 0) { - case /* Same */0 : - return /* record */[ - /* typA1 */typ1, - /* typA2 */typ2, - /* typB : Empty */0 - ]; - case /* Union */6 : - exit = 2; - break; - case /* Diff */7 : - return /* record */[ - /* typA1 : Empty */0, - /* typA2 */typ2, - /* typB : Empty */0 - ]; - - } - } - } - if (exit === 2 && typeof typ2 !== "number") { - switch (typ2.tag | 0) { - case /* Array */5 : - break; - case /* Diff */7 : - return /* record */[ - /* typA1 */typ1, - /* typA2 : Empty */0, - /* typB : Empty */0 - ]; - default: - - } - } - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "Diff.re", - 127, - 21 - ] - ]; -} - -function diffUnion(styp1, styp2, styps1, styps2) { - var findMatch = function (t, _ts, _acc) { - while(true) { - var acc = _acc; - var ts = _ts; - if (ts) { - var ts1 = ts[1]; - var t1 = ts[0]; - if (TypeCheck$ReactTemplate.plusTyp(t[/* typ */0], t1[/* typ */0]) !== undefined) { - return /* tuple */[ - t1, - Belt_List.concat(Belt_List.reverse(acc), ts1) - ]; - } else { - _acc = /* :: */[ - t1, - acc - ]; - _ts = ts1; - continue ; - } - } else { - return ; - } - }; - }; - var plus = function (ls1, ls2) { - if (ls1) { - var ts1 = ls1[1]; - var t1 = ls1[0]; - var match = findMatch(t1, ls2, /* [] */0); - if (match !== undefined) { - var match$1 = match; - var match$2 = plus(ts1, match$1[1]); - var match$3 = diffStyp(t1, match$1[0]); - return /* record */[ - /* stypUA1 : :: */[ - match$3[/* stypA1 */2], - match$2[/* stypUA1 */0] - ], - /* stypUA2 : :: */[ - match$3[/* stypA2 */3], - match$2[/* stypUA2 */1] - ], - /* stypUB : :: */[ - match$3[/* stypB */4], - match$2[/* stypUB */2] - ] - ]; - } else { - var diffUnion = plus(ts1, ls2); - return /* record */[ - /* stypUA1 : :: */[ - t1, - diffUnion[/* stypUA1 */0] - ], - /* stypUA2 */diffUnion[/* stypUA2 */1], - /* stypUB */diffUnion[/* stypUB */2] - ]; - } - } else { - return /* record */[ - /* stypUA1 : [] */0, - /* stypUA2 */ls2, - /* stypUB : [] */0 - ]; - } - }; - var match = plus(styps1, styps2); - var toUnion = function (styps) { - var styps1 = Belt_List.keep(styps, (function (styp) { - return !Styp$ReactTemplate.stypIsEmpty(styp); - })); - if (styps1) { - if (styps1[1]) { - return Styp$ReactTemplate.makeUnion(styps1); - } else { - return styps1[0][/* typ */0]; - } - } else { - return /* Empty */0; - } - }; - var toStyp = function (stypU) { - var typ = toUnion(stypU); - var p = Belt_List.reduce(stypU, Styp$ReactTemplate.P.zero, (function (p, styp) { - return Styp$ReactTemplate.P["^"](p, styp[/* p */2]); - })); - var o = Belt_List.reduce(stypU, /* NotOpt */0, (function (o, styp) { - return TypeCheck$ReactTemplate.plusO(o, styp[/* o */1]); - })); - return /* record */[ - /* typ */typ, - /* o */o, - /* p */p - ]; - }; - return /* record */[ - /* styp1 */styp1, - /* styp2 */styp2, - /* stypA1 */toStyp(match[/* stypUA1 */0]), - /* stypA2 */toStyp(match[/* stypUA2 */1]), - /* stypB */toStyp(match[/* stypUB */2]) - ]; -} - -function combineStyp(stypA1, stypA2, stypB) { - if (Caml_obj.caml_notequal(stypA1[/* p */2], Styp$ReactTemplate.P.zero) || stypA1[/* o */1] !== /* NotOpt */0 || Caml_obj.caml_notequal(stypA2[/* p */2], Styp$ReactTemplate.P.zero) || stypA2[/* o */1] !== /* NotOpt */0) { - return /* record */[ - /* typ : Diff */Block.__(7, [ - stypB[/* typ */0], - stypA1, - stypA2 - ]), - /* o */stypB[/* o */1], - /* p */stypB[/* p */2] - ]; - } else { - return /* record */[ - /* typ */combineTyp(stypA1[/* typ */0], stypA2[/* typ */0], stypB[/* typ */0]), - /* o */stypB[/* o */1], - /* p */stypB[/* p */2] - ]; - } -} - -function combineTyp(typA1, typA2, typB) { - var exit = 0; - if (typeof typA1 === "number") { - exit = 2; - } else { - switch (typA1.tag | 0) { - case /* Object */4 : - break; - case /* Same */0 : - case /* Array */5 : - exit = 2; - break; - default: - return typB; - } - } - if (exit === 2) { - var exit$1 = 0; - if (typeof typA2 === "number") { - exit$1 = 3; - } else { - switch (typA2.tag | 0) { - case /* Object */4 : - break; - case /* Same */0 : - case /* Array */5 : - exit$1 = 3; - break; - default: - return typB; - } - } - if (exit$1 === 3) { - if (typeof typB === "number") { - return typB; - } else { - switch (typB.tag | 0) { - case /* Object */4 : - break; - case /* Array */5 : - var getStyp = function (typ) { - if (typeof typ === "number" || typ.tag !== /* Array */5) { - return Styp$ReactTemplate.stypEmpty; - } else { - return typ[0]; - } - }; - var stypA1 = getStyp(typA1); - var stypA2 = getStyp(typA2); - return /* Array */Block.__(5, [combineStyp(stypA1, stypA2, typB[0])]); - default: - return typB; - } - } - } - - } - if (typeof typA1 !== "number" && typA1.tag === /* Array */5) { - return typB; - } - if (typeof typA2 !== "number") { - switch (typA2.tag | 0) { - case /* Same */0 : - case /* Object */4 : - break; - default: - return typB; - } - } - if (typeof typB === "number" || typB.tag !== /* Object */4) { - return typB; - } else { - var dictB = typB[0]; - var d = { }; - var getDict = function (typ) { - if (typeof typ === "number") { - return { }; - } else if (typ.tag === /* Object */4) { - return typ[0]; - } else { - return { }; - } - }; - var dictA1 = getDict(typA1); - var dictA2 = getDict(typA2); - var doItem = function (lbl) { - var getStyp = function (dict) { - var match = Js_dict.get(dict, lbl); - if (match !== undefined) { - return match; - } else { - return Styp$ReactTemplate.stypEmpty; - } - }; - d[lbl] = combineStyp(getStyp(dictA1), getStyp(dictA2), getStyp(dictB)); - return /* () */0; - }; - Belt_SetString.forEach(Belt_SetString.union(Belt_SetString.union(Belt_SetString.fromArray(Object.keys(dictA1)), Belt_SetString.fromArray(Object.keys(dictA2))), Belt_SetString.fromArray(Object.keys(dictB))), doItem); - return Styp$ReactTemplate.makeObject(Js_dict.entries(d)); - } -} - -function diff(styp1, styp2) { - var d = diffStyp(styp1, styp2); - return /* record */[ - /* styp1 */d[/* styp1 */0], - /* styp2 */d[/* styp2 */1], - /* stypA1 */d[/* stypA1 */2], - /* stypA2 */d[/* stypA2 */3], - /* stypB */combineStyp(d[/* stypA1 */2], d[/* stypA2 */3], d[/* stypB */4]) - ]; -} - -function diffCheck(styp1, styp2) { - var d = diffStyp(styp1, styp2); - if (!Caml_obj.caml_equal(TypeCheck$ReactTemplate.$caret(d[/* stypB */4], d[/* stypA1 */2]), styp1)) { - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "Diff.re", - 246, - 2 - ] - ]; - } - if (!Caml_obj.caml_equal(TypeCheck$ReactTemplate.$caret(d[/* stypB */4], d[/* stypA2 */3]), styp2)) { - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "Diff.re", - 247, - 2 - ] - ]; - } - return /* record */[ - /* styp1 */d[/* styp1 */0], - /* styp2 */d[/* styp2 */1], - /* stypA1 */d[/* stypA1 */2], - /* stypA2 */d[/* stypA2 */3], - /* stypB */combineStyp(d[/* stypA1 */2], d[/* stypA2 */3], d[/* stypB */4]) - ]; -} - -function toJson(diff) { - var styp1 = Styp$ReactTemplate.stypToJson(diff[/* styp1 */0]); - var styp2 = Styp$ReactTemplate.stypToJson(diff[/* styp2 */1]); - var stypB = Styp$ReactTemplate.stypToJson(diff[/* stypB */4]); - var stypA1 = Styp$ReactTemplate.stypToJson(diff[/* stypA1 */2]); - var stypA2 = Styp$ReactTemplate.stypToJson(diff[/* stypA2 */3]); - return Js_dict.fromArray(/* array */[ - /* tuple */[ - "styp1", - styp1 - ], - /* tuple */[ - "styp2", - styp2 - ], - /* tuple */[ - "stypB", - stypB - ], - /* tuple */[ - "stypA1", - stypA1 - ], - /* tuple */[ - "stypA2", - stypA2 - ] - ]); -} - -var inlineDifferences = true; - -export { - inlineDifferences , - diffStyp , - diffO , - diffTyp , - diffUnion , - combineStyp , - combineTyp , - diff , - diffCheck , - toJson , - -} -/* No side effect */ diff --git a/src/Diff.re b/src/Diff.re deleted file mode 100644 index 241a56c..0000000 --- a/src/Diff.re +++ /dev/null @@ -1,267 +0,0 @@ -open Styp; - -type diffStyp = { - styp1: styp, - styp2: styp, - stypA1: styp, - stypA2: styp, - stypB: styp, -}; - -type diffTyp = { - typA1: typ, - typA2: typ, - typB: typ, -}; - -type diffUnion = { - stypUA1: list(styp), - stypUA2: list(styp), - stypUB: list(styp), -}; - -type t = diffStyp; - -/* Inline the differences in the B part */ -let inlineDifferences = true; - -let rec diffStyp = (styp1: styp, styp2: styp): t => - switch (styp1.typ, styp2.typ) { - | (Union(styps1), Union(styps2)) => - diffUnion(styp1, styp2, styps1, styps2) - | (Union(styps1), _) => diffUnion(styp1, styp2, styps1, [styp2]) - | (_, Union(styps2)) => diffUnion(styp1, styp2, [styp1], styps2) - | (typ1, typ2) when TypeCheck.plusTyp(typ1, typ2) == None => - diffUnion(styp1, styp2, [styp1], [styp2]) - | (typ1, typ2) => - let {typA1, typA2, typB} = diffTyp(typ1, typ2); - let (oA1, oA2, oB) = diffO(styp1.o, styp2.o); - let pB = min(styp1.p, styp2.p); - let (pA1, pA2) = (P.(styp1.p -- pB), P.(styp2.p -- pB)); - let stypA1 = {typ: typA1, o: oA1, p: pA1}; - let stypA2 = {typ: typA2, o: oA2, p: pA2}; - let stypB = {typ: typB, o: oB, p: pB}; - open! TypeCheck; - {styp1, styp2, stypA1, stypA2, stypB}; - } -and diffO = (o1: o, o2: o): (o, o, o) => - switch (o1, o2) { - | (NotOpt, _) => (NotOpt, o2, NotOpt) - | (_, NotOpt) => (o1, NotOpt, NotOpt) - | (Opt(p1), Opt(p2)) => ( - p1 > p2 ? Opt(P.(p1 -- p2)) : NotOpt, - p2 > p1 ? Opt(P.(p2 -- p1)) : NotOpt, - Opt(min(p1, p2)), - ) - } -and diffTyp = (typ1: typ, typ2: typ): diffTyp => { - let makeSame = typ => {typA1: Same(typ), typA2: Same(typ), typB: typ}; - switch (typ1, typ2) { - | (Empty | Same(_), _) - | (_, Empty | Same(_)) => {typA1: typ1, typA2: typ2, typB: Empty} - | (Diff(_), _) => {typA1: Empty, typA2: typ2, typB: Empty} - | (_, Diff(_)) => {typA1: typ1, typA2: Empty, typB: Empty} - - | (Number(x), Number(y)) when x == y => makeSame(typ1) - | (String(x), String(y)) when x == y => makeSame(typ1) - | (Boolean(x), Boolean(y)) when x == y => makeSame(typ1) - - | (Object(d1), Object(d2)) => - let dA1 = Js.Dict.empty(); - let dA2 = Js.Dict.empty(); - let dB = Js.Dict.empty(); - let doItem2 = ((lbl, styp2)) => - switch (d1->(Js.Dict.get(lbl))) { - | None => - if (!stypIsEmpty(styp2)) { - dA2->(Js.Dict.set(lbl, styp2)); - } - | Some(styp1) => - let {stypA1, stypA2, stypB} = diffStyp(styp1, styp2); - if (!stypIsEmpty(stypA1)) { - dA1->(Js.Dict.set(lbl, stypA1)); - }; - if (!stypIsEmpty(stypA2)) { - dA2->(Js.Dict.set(lbl, stypA2)); - }; - dB->(Js.Dict.set(lbl, stypB)); - }; - let doItem1 = ((lbl, styp1)) => - switch (d2->(Js.Dict.get(lbl))) { - | None => - if (!stypIsEmpty(styp1)) { - dA1->(Js.Dict.set(lbl, styp1)); - } - | Some(_) => () - }; - d2->Js.Dict.entries->(Belt.Array.forEach(doItem2)); - d1->Js.Dict.entries->(Belt.Array.forEach(doItem1)); - let entries1 = dA1->Js.Dict.entries; - let entries2 = dA2->Js.Dict.entries; - let typA1 = { - let t = entries1->makeObject; - Array.length(entries1) == 0 ? t->Same : t; - }; - let typA2 = { - let t = entries2->makeObject; - Array.length(entries2) == 0 ? t->Same : t; - }; - let typB = dB->Js.Dict.entries->makeObject; - {typA1, typA2, typB}; - - | (Array(styp1), Array(styp2)) => - let {stypA1, stypA2, stypB} = diffStyp(styp1, styp2); - let typA1 = stypIsEmpty(stypA1) ? Same(Array(stypA1)) : Array(stypA1); - let typA2 = stypIsEmpty(stypA2) ? Same(Array(stypA2)) : Array(stypA2); - let typB = Array(stypB); - {typA1, typA2, typB}; - | (Number(_), _) - | (_, Number(_)) - | (String(_), _) - | (_, String(_)) - | (Boolean(_), _) - | (_, Boolean(_)) - | (Object(_), _) - | (_, Object(_)) - | (Union(_), _) - | (_, Union(_)) => assert(false) - }; -} -and diffUnion = (styp1, styp2, styps1: list(styp), styps2: list(styp)): t => { - let rec findMatch = (t, ts, acc) => - switch (ts) { - | [t1, ...ts1] => - if (TypeCheck.plusTyp(t.typ, t1.typ) != None) { - Some((t1, acc->Belt.List.reverse->(Belt.List.concat(ts1)))); - } else { - findMatch(t, ts1, [t1, ...acc]); - } - | [] => None - }; - let rec plus = (ls1, ls2): diffUnion => - switch (ls1, ls2) { - | ([t1, ...ts1], _) => - switch (findMatch(t1, ls2, [])) { - | None => - let diffUnion = plus(ts1, ls2); - {...diffUnion, stypUA1: [t1, ...diffUnion.stypUA1]}; - | Some((t2, ts2)) => - let {stypUA1, stypUA2, stypUB} = plus(ts1, ts2); - let {stypA1, stypA2, stypB} = diffStyp(t1, t2); - { - stypUA1: [stypA1, ...stypUA1], - stypUA2: [stypA2, ...stypUA2], - stypUB: [stypB, ...stypUB], - }; - } - | ([], _) => {stypUA1: [], stypUA2: ls2, stypUB: []} - }; - let {stypUA1, stypUA2, stypUB} = plus(styps1, styps2); - let toUnion = styps => - switch (styps->(Belt.List.keep(styp => !stypIsEmpty(styp)))) { - | [] => Empty - | [styp] => styp.typ - | styps1 => styps1->makeUnion - }; - let toStyp = stypU => { - let typ = stypU->toUnion; - let p = stypU->(Belt.List.reduce(P.zero, (p, styp) => p->(P.(++)(styp.p)))); - let o = - stypU->( - Belt.List.reduce(NotOpt, (o, styp) => o->(TypeCheck.plusO(styp.o))) - ); - {typ, o, p}; - }; - { - styp1, - styp2, - stypA1: stypUA1->toStyp, - stypA2: stypUA2->toStyp, - stypB: stypUB->toStyp, - }; -}; - -let rec combineStyp = (stypA1: styp, stypA2: styp, stypB: styp): styp => - if (stypA1.p != P.zero - || stypA1.o != NotOpt - || stypA2.p != P.zero - || stypA2.o != NotOpt) { - {...stypB, typ: Diff(stypB.typ, stypA1, stypA2)}; - } else { - {...stypB, typ: combineTyp(stypA1.typ, stypA2.typ, stypB.typ)}; - } -and combineTyp = (typA1: typ, typA2: typ, typB: typ): typ => - switch (typA1, typA2, typB) { - | (Array(_) | Empty | Same(_), Array(_) | Empty | Same(_), Array(stypB)) => - let getStyp = typ => - switch (typ) { - | Array(styp) => styp - | _ => stypEmpty - }; - let stypA1 = typA1->getStyp; - let stypA2 = typA2->getStyp; - combineStyp(stypA1, stypA2, stypB)->Array; - | (Object(_) | Empty | Same(_), Object(_) | Empty | Same(_), Object(dictB)) => - let d = Js.Dict.empty(); - let getDict = typ => - switch (typ) { - | Object(dict) => dict - | _ => Js.Dict.empty() - }; - let dictA1 = typA1->getDict; - let dictA2 = typA2->getDict; - let doItem = lbl => { - let getStyp = dict => - switch (dict->(Js.Dict.get(lbl))) { - | None => stypEmpty - | Some(styp) => styp - }; - d->( - Js.Dict.set( - lbl, - combineStyp(dictA1->getStyp, dictA2->getStyp, dictB->getStyp), - ) - ); - }; - Belt.Set.String.( - dictA1 - ->Js.Dict.keys - ->fromArray - ->(union(dictA2->Js.Dict.keys->fromArray)) - ->(union(dictB->Js.Dict.keys->fromArray)) - ->(forEach(doItem)) - ); - d->Js.Dict.entries->makeObject; - | _ => typB - }; - -let diff = (styp1, styp2) => { - let d = diffStyp(styp1, styp2); - inlineDifferences - ? {...d, stypB: combineStyp(d.stypA1, d.stypA2, d.stypB)} : d; -}; -let diffCheck = (styp1, styp2) => { - let d = diffStyp(styp1, styp2); - open! TypeCheck; - assert(d.stypB ++ d.stypA1 == styp1); - assert(d.stypB ++ d.stypA2 == styp2); - inlineDifferences - ? {...d, stypB: combineStyp(d.stypA1, d.stypA2, d.stypB)} : d; -}; - -let toJson = (diff: t): Js.Json.t => { - let styp1 = diff.styp1->stypToJson; - let styp2 = diff.styp2->stypToJson; - let stypB = diff.stypB->stypToJson; - let stypA1 = diff.stypA1->stypToJson; - let stypA2 = diff.stypA2->stypToJson; - [| - ("styp1", styp1), - ("styp2", styp2), - ("stypB", stypB), - ("stypA1", stypA1), - ("stypA2", stypA2), - |] - ->Js.Dict.fromArray - ->Js.Json.object_; -}; \ No newline at end of file diff --git a/src/DynTypedJson.js b/src/DynTypedJson.js deleted file mode 100644 index 4e6b075..0000000 --- a/src/DynTypedJson.js +++ /dev/null @@ -1,172 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as Js_json from "bs-platform/lib/es6/js_json.js"; -import * as Caml_obj from "bs-platform/lib/es6/caml_obj.js"; -import * as Caml_option from "bs-platform/lib/es6/caml_option.js"; -import * as Styp$ReactTemplate from "./Styp.js"; -import * as Caml_builtin_exceptions from "bs-platform/lib/es6/caml_builtin_exceptions.js"; -import * as TypeCheck$ReactTemplate from "./TypeCheck.js"; - -function empty(param) { - return /* record */[ - /* json */null, - /* styp : record */[/* contents */Styp$ReactTemplate.stypEmpty] - ]; -} - -function log(x) { - console.log("json:", x[/* json */0]); - console.log("styp:", JSON.stringify(Styp$ReactTemplate.stypToJson(x[/* styp */1][0]))); - return /* () */0; -} - -function assignJson(x, json) { - x[/* styp */1][0] = TypeCheck$ReactTemplate.$caret(x[/* styp */1][0], TypeCheck$ReactTemplate.fromJson(undefined, json)); - x[/* json */0] = json; - return /* () */0; -} - -function getFld(x, fld) { - if (x[/* styp */1][0][/* o */1] !== /* NotOpt */0) { - console.log("Type error: access field " + (fld + " of object with nullable type")); - log(x); - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "DynTypedJson.re", - 26, - 4 - ] - ]; - } - var match = x[/* styp */1][0][/* typ */0]; - var styp1; - if (typeof match === "number") { - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "DynTypedJson.re", - 47, - 11 - ] - ]; - } else if (match.tag === /* Object */4) { - var styp1$1 = match[0][fld]; - var stypP = x[/* styp */1][0][/* p */2]; - var styp1P = styp1$1[/* p */2]; - var match$1 = x[/* styp */1][0][/* o */1]; - var stypOP = match$1 ? match$1[0] : Styp$ReactTemplate.P.zero; - if (Caml_obj.caml_notequal(stypP, Styp$ReactTemplate.P["^"](styp1P, stypOP))) { - console.log("Type error: access field " + (fld + " of object with optional type")); - log(x); - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "DynTypedJson.re", - 44, - 8 - ] - ]; - } - styp1 = styp1$1; - } else { - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "DynTypedJson.re", - 47, - 11 - ] - ]; - } - var match$2 = Js_json.decodeObject(x[/* json */0]); - var json; - if (match$2 !== undefined) { - json = Caml_option.valFromOption(match$2)[fld]; - } else { - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "DynTypedJson.re", - 51, - 14 - ] - ]; - } - return /* record */[ - /* json */json, - /* styp : record */[/* contents */styp1] - ]; -} - -var $$null$1 = /* record */[ - /* json */null, - /* styp : record */[/* contents */Styp$ReactTemplate.stypEmpty] -]; - -function makeNotNullable(x) { - var styp = x[/* styp */1][0]; - if (styp[/* o */1] !== /* NotOpt */0) { - var match = styp[/* o */1]; - var stypOP = match ? match[0] : Styp$ReactTemplate.P.zero; - x[/* styp */1][0] = /* record */[ - /* typ */styp[/* typ */0], - /* o : NotOpt */0, - /* p */Styp$ReactTemplate.P["--"](styp[/* p */2], stypOP) - ]; - return /* () */0; - } else { - return 0; - } -} - -function $eq(x, y) { - var match = Caml_obj.caml_equal(x, $$null$1); - var match$1 = Caml_obj.caml_equal(y, $$null$1); - if (match) { - if (match$1) { - return true; - } else { - makeNotNullable(y); - return false; - } - } else if (match$1) { - makeNotNullable(x); - return false; - } else { - return Caml_obj.caml_equal(x, y); - } -} - -function $less$great(x, y) { - return !$eq(x, y); -} - -function ref(json) { - var x = /* record */[ - /* json */null, - /* styp : record */[/* contents */Styp$ReactTemplate.stypEmpty] - ]; - assignJson(x, json); - return x; -} - -var $colon$eq = assignJson; - -var $at = getFld; - -export { - empty , - log , - assignJson , - getFld , - $$null$1 as $$null, - makeNotNullable , - $eq , - $less$great , - ref , - $colon$eq , - $at , - -} -/* null Not a pure module */ diff --git a/src/DynTypedJson.re b/src/DynTypedJson.re deleted file mode 100644 index 39fe85d..0000000 --- a/src/DynTypedJson.re +++ /dev/null @@ -1,95 +0,0 @@ -open Styp; - -type t = { - mutable json: Js.Json.t, - styp: ref(styp), -}; - -let empty = () => {json: Js.Json.null, styp: ref(stypEmpty)}; - -let log = x => { - Js.log2("json:", x.json); - Js.log2("styp:", (x.styp^)->stypToJson->Js.Json.stringify); -}; - -let assignJson = (x, json) => { - x.styp := TypeCheck.(++)(x.styp^, TypeCheck.fromJson(json)); - x.json = json; -}; - -let getFld = (x, fld) => { - if (x.styp^.o != NotOpt) { - Js.log( - "Type error: access field " ++ fld ++ " of object with nullable type", - ); - log(x); - assert(false); - }; - let styp1 = - switch (x.styp^.typ) { - | Styp.Object(dict) => - let styp1 = dict->(Js.Dict.unsafeGet(fld)); - let stypP = x.styp^.p; - let styp1P = styp1.p; - let stypOP = - switch (x.styp^.o) { - | NotOpt => P.zero - | Opt(p) => p - }; - if (stypP != P.(++)(styp1P, stypOP)) { - Js.log( - "Type error: access field " ++ fld ++ " of object with optional type", - ); - log(x); - assert(false); - }; - styp1; - | _ => assert(false) - }; - let json = - switch (x.json->Js.Json.decodeObject) { - | None => assert(false) - | Some(dict) => dict->(Js.Dict.unsafeGet(fld)) - }; - {json, styp: ref(styp1)}; -}; - -let null = empty(); - -let makeNotNullable = x => { - let styp = x.styp^; - if (styp.o != NotOpt) { - let stypOP = - switch (styp.o) { - | NotOpt => P.zero - | Opt(p) => p - }; - x.styp := {...styp, o: NotOpt, p: P.(styp.p -- stypOP)}; - }; -}; -let (==) = (x, y) => { - let magicX = Obj.magic(x); - let magicY = Obj.magic(y); - switch (magicX == null, magicY == null) { - | (true, true) => true - | (true, false) => - makeNotNullable(magicY); - false; - | (false, true) => - makeNotNullable(magicX); - false; - | (false, false) => x == y - }; -}; - -let (!=) = (x, y) => !(x == y); - -let ref = json => { - let x = empty(); - x->(assignJson(json)); - x; -}; - -let (:=) = assignJson; - -let (@) = getFld; \ No newline at end of file diff --git a/src/Index.js b/src/Index.js index d8cc116..754f484 100644 --- a/src/Index.js +++ b/src/Index.js @@ -1,31 +1,10 @@ // Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE -import * as React from "react"; -import * as Belt_List from "bs-platform/lib/es6/belt_List.js"; -import * as Belt_Array from "bs-platform/lib/es6/belt_Array.js"; -import * as ReactDOMRe from "reason-react/src/ReactDOMRe.js"; -import * as UI$ReactTemplate from "./UI.js"; -import * as Demo$ReactTemplate from "./Demo.js"; +import * as VendoredArray$ReactTemplate from "./VendoredArray.js"; -function Index$Main(Props) { - var diffs = Demo$ReactTemplate.test(/* () */0); - return React.createElement("div", undefined, Belt_Array.mapWithIndex(Belt_List.toArray(diffs), (function (i, diff) { - return React.createElement("div", { - key: String(i) - }, React.createElement("h3", undefined, "Sample " + String(i)), React.createElement(UI$ReactTemplate.Diff.make, { - diff: diff - })); - }))); -} - -var Main = { - make: Index$Main -}; - -ReactDOMRe.renderToElementWithId(React.createElement(Index$Main, { }), "main"); +console.log(VendoredArray$ReactTemplate.get(/* array */[3], 0)); export { - Main , } /* Not a pure module */ diff --git a/src/Index.re b/src/Index.re index 3f7435c..e41133d 100644 --- a/src/Index.re +++ b/src/Index.re @@ -1,21 +1 @@ -module Main = { - [@react.component] - let make = () => { - let diffs = Demo.test(); -
- {diffs - ->Belt.List.toArray - ->( - Belt.Array.mapWithIndex((i, diff) => -
string_of_int}> -

{React.string("Sample " ++ string_of_int(i))}

- -
- ) - ) - ->React.array} -
; - }; -}; - -ReactDOMRe.renderToElementWithId(
, "main"); \ No newline at end of file +Js.log([|3|]->VendoredArray.get(0)); \ No newline at end of file diff --git a/src/Samples.js b/src/Samples.js deleted file mode 100644 index 666f7db..0000000 --- a/src/Samples.js +++ /dev/null @@ -1,110 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as Belt_List from "bs-platform/lib/es6/belt_List.js"; -import * as Diff$ReactTemplate from "./Diff.js"; -import * as Styp$ReactTemplate from "./Styp.js"; -import * as TypeCheck$ReactTemplate from "./TypeCheck.js"; - -var empty = /* record */[ - /* samples : [] */0, - /* sum */Styp$ReactTemplate.stypEmpty, - /* prevSum */Styp$ReactTemplate.stypEmpty, - /* common */Styp$ReactTemplate.stypEmpty, - /* prevCommon */Styp$ReactTemplate.stypEmpty -]; - -function toList(param) { - return Belt_List.reverse(param[/* samples */0]); -} - -function getSum(param) { - return param[/* sum */1]; -} - -function getCommon(param) { - return param[/* common */3]; -} - -function getLast(param) { - var samples = param[/* samples */0]; - if (samples) { - return samples[0]; - } else { - return Styp$ReactTemplate.stypEmpty; - } -} - -function getPrev(param) { - var samples = param[/* samples */0]; - if (samples) { - var match = samples[1]; - if (match) { - return match[0]; - } else { - return Styp$ReactTemplate.stypEmpty; - } - } else { - return Styp$ReactTemplate.stypEmpty; - } -} - -function getPrevSum(param) { - return param[/* prevSum */2]; -} - -function getPrevCommon(param) { - return param[/* prevCommon */4]; -} - -function add(t, styp) { - var samples_001 = t[/* samples */0]; - var samples = /* :: */[ - styp, - samples_001 - ]; - var prevSum = t[/* sum */1]; - var sum = TypeCheck$ReactTemplate.$caret(t[/* sum */1], styp); - var prevCommon = t[/* common */3]; - var match = t[/* samples */0] === /* [] */0; - var common = match ? styp : Styp$ReactTemplate.stripDiffStyp(Diff$ReactTemplate.diff(t[/* common */3], styp)[/* stypB */4]); - return /* record */[ - /* samples */samples, - /* sum */sum, - /* prevSum */prevSum, - /* common */common, - /* prevCommon */prevCommon - ]; -} - -function addMany(t, styps) { - var styp = Belt_List.reduce(styps, Styp$ReactTemplate.stypEmpty, TypeCheck$ReactTemplate.$caret); - return add(t, styp); -} - -function fromList(styps) { - return Belt_List.reduce(styps, empty, add); -} - -function getAllDiffs(t) { - var common = getCommon(t); - return Belt_List.map(toList(t), (function (styp) { - return Diff$ReactTemplate.diff(common, styp); - })); -} - -export { - add , - addMany , - empty , - fromList , - getAllDiffs , - getCommon , - getLast , - getPrev , - getPrevCommon , - getPrevSum , - getSum , - toList , - -} -/* No side effect */ diff --git a/src/Samples.re b/src/Samples.re deleted file mode 100644 index 7ee8324..0000000 --- a/src/Samples.re +++ /dev/null @@ -1,61 +0,0 @@ -type t = { - samples: list(Styp.styp), /* stored in reverse order */ - sum: Styp.styp, - prevSum: Styp.styp, - common: Styp.styp, - prevCommon: Styp.styp, -}; - -let empty = { - samples: [], - sum: Styp.stypEmpty, - prevSum: Styp.stypEmpty, - common: Styp.stypEmpty, - prevCommon: Styp.stypEmpty, -}; - -let toList = ({samples}) => samples->Belt.List.reverse; - -let getSum = ({sum}) => sum; - -let getCommon = ({common}) => common; - -let getLast = ({samples}) => - switch (samples) { - | [last, ..._] => last - | [] => Styp.stypEmpty - }; - -let getPrev = ({samples}) => - switch (samples) { - | [_, prev, ..._] => prev - | [_] - | [] => Styp.stypEmpty - }; - -let getPrevSum = ({prevSum}) => prevSum; - -let getPrevCommon = ({prevCommon}) => prevCommon; - -let add = (t, styp) => { - let samples = [styp, ...t.samples]; - let prevSum = t.sum; - let sum = TypeCheck.(++)(t.sum, styp); - let prevCommon = t.common; - let common = - t.samples == [] - ? styp : Diff.diff(t.common, styp).stypB->Styp.stripDiffStyp; - {samples, sum, prevSum, common, prevCommon}; -}; - -let addMany = (t, styps) => { - let styp = styps->(Belt.List.reduce(Styp.stypEmpty, TypeCheck.(++))); - t->(add(styp)); -}; - -let fromList = styps => styps->(Belt.List.reduce(empty, add)); - -let getAllDiffs = t => { - let common = t->getCommon; - t->toList->(Belt.List.map(styp => Diff.diff(common, styp))); -}; \ No newline at end of file diff --git a/src/Samples.rei b/src/Samples.rei deleted file mode 100644 index d9dec7a..0000000 --- a/src/Samples.rei +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Type for samples. - * - * Samples are obtained by adding types in a given order. - * For each addition, the common type and sum type are updated. - * The common type is the type in common to all samples. - * The sum type is the aggregate sum of all samples. - * There is access to the previous version of all values. - */ -type t; - -/** Add one sample. */ -let add: (t, Styp.styp) => t; - -/** - * Add a list as a single sample. - * Equivalent to adding a single sample with the sum type of the elements. - */ -let addMany: (t, list(Styp.styp)) => t; - -/** Empty samples. */ -let empty: t; - -/** Add the samples in the order they appear in the list. */ -let fromList: list(Styp.styp) => t; - -/** Get the list of diffs between the common type and each sample. */ -let getAllDiffs: t => list(Diff.t); - -/** Get the type in common to all the samples. */ -let getCommon: t => Styp.styp; - -/** Get the last sample added. */ -let getLast: t => Styp.styp; - -/** Get the second-last sample added. */ -let getPrev: t => Styp.styp; - -/** Get the common type as it was before adding the last sample. */ -let getPrevCommon: t => Styp.styp; - -/** Get the sum type as it was before adding the last sample. */ -let getPrevSum: t => Styp.styp; - -/** Get the type of the aggregate sum of all the samples. */ -let getSum: t => Styp.styp; - -/** Get the list of samples in the order they were added. */ -let toList: t => list(Styp.styp); \ No newline at end of file diff --git a/src/Serialize.js b/src/Serialize.js deleted file mode 100644 index 30eb222..0000000 --- a/src/Serialize.js +++ /dev/null @@ -1,99 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as Js_dict from "bs-platform/lib/es6/js_dict.js"; -import * as Belt_Array from "bs-platform/lib/es6/belt_Array.js"; -import * as Caml_option from "bs-platform/lib/es6/caml_option.js"; - -var tagID = "tag320775503"; - -var undefinedID = "undefined290180182"; - -function copy(o_) { - var match = Array.isArray(o_); - if (match) { - return o_.slice(0); - } else { - return Object.assign({ }, o_); - } -} - -function wrapTag(o_) { - var match = typeof o_; - switch (match) { - case "object" : - if (o_ === null) { - return o_; - } else { - var o1 = copy(o_); - var keys = Object.keys(o1); - for(var i = 1 ,i_finish = keys.length; i <= i_finish; ++i){ - var key = Belt_Array.getExn(keys, i - 1 | 0); - o1[key] = wrapTag(Js_dict.get(o_, key)); - } - var tagOpt = Js_dict.get(o_, "tag"); - if (tagOpt !== undefined) { - return /* array */[ - tagID, - Caml_option.valFromOption(tagOpt), - o1 - ]; - } else { - return o1; - } - } - case "undefined" : - return /* array */[undefinedID]; - default: - return o_; - } -} - -function unwrapTag(o_) { - var match = typeof o_; - if (match === "object" && o_ !== null) { - var match$1 = Array.isArray(o_); - var match$2 = match$1 ? Belt_Array.get(o_, 0) : undefined; - if (match$2 !== undefined) { - var id = Caml_option.valFromOption(match$2); - if (id === undefinedID) { - return ; - } else if (id === tagID) { - var tag = Belt_Array.getExn(o_, 1); - var innerObj = Belt_Array.getExn(o_, 2); - innerObj["tag"] = tag; - unwrapKeys(innerObj); - return innerObj; - } - - } - var o1 = copy(o_); - unwrapKeys(o1); - return o1; - } else { - return o_; - } -} - -function unwrapKeys(o) { - var keys = Object.keys(o); - for(var i = 1 ,i_finish = keys.length; i <= i_finish; ++i){ - var key = Belt_Array.getExn(keys, i - 1 | 0); - o[key] = unwrapTag(Js_dict.get(o, key)); - } - return /* () */0; -} - -function toString(o) { - return JSON.stringify(wrapTag(o)); -} - -function fromString(s) { - return unwrapTag(JSON.parse(s)); -} - -export { - toString , - fromString , - -} -/* No side effect */ diff --git a/src/Serialize.re b/src/Serialize.re deleted file mode 100644 index a6ffbfc..0000000 --- a/src/Serialize.re +++ /dev/null @@ -1,69 +0,0 @@ -/* wrap tags as they are not supported by json stringify */ -let tagID = "tag320775503"; -/* wrap undefined as it is not supported by json stringify */ -let undefinedID = "undefined290180182"; - -[@bs.val] external isArray: 'a => bool = "Array.isArray"; - -let copy = (o_: 'a): 'a => { - let o = o_->Obj.magic; - isArray(o) - ? Belt.Array.copy(o)->Obj.magic - : Js.Obj.assign(Js.Obj.empty(), o)->Obj.magic; -}; -let rec wrapTag = o_ => { - let o = Obj.magic(o_); - switch (Js.typeof(o)) { - | "undefined" => [|undefinedID|]->Obj.magic - | "object" when o === Js.null => o - | "object" => - let o1 = copy(o); - let keys = Js.Dict.keys(o1); - for (i in 1 to Belt.Array.length(keys)) { - let key = keys->(Belt.Array.getExn(i - 1)); - o1->(Js.Dict.set(key, o->(Js.Dict.get(key))->Obj.magic->wrapTag)); - }; - let tagOpt = Js.Dict.get(o, "tag"); - switch (tagOpt) { - | None => o1 - | Some(tag) => [|tagID, tag, o1|]->Obj.magic - }; - | _ => o - }; -}; -let rec unwrapTag = o_ => { - let o = Obj.magic(o_); - switch (Js.typeof(o)) { - | "object" when o === Js.null => o - | "object" => - switch (isArray(o) ? o->Belt.Array.get(0) : None) { - | Some(id) when id == undefinedID => Js.undefined->Obj.magic - | Some(id) when id == tagID => - let tag = o->(Belt.Array.getExn(1)); - let innerObj = o->(Belt.Array.getExn(2)); - innerObj->(Js.Dict.set("tag", tag)); - unwrapKeys(innerObj); - innerObj; - | _ => - let o1 = copy(o); - unwrapKeys(o1); - o1; - } - | _ => o - }; -} -and unwrapKeys = o => { - let keys = Js.Dict.keys(o); - for (i in 1 to Array.length(keys)) { - let key = keys->(Belt.Array.getExn(i - 1)); - o->(Js.Dict.set(key, o->(Js.Dict.get(key))->Obj.magic->unwrapTag)); - }; -}; -let toString = o => { - let o1 = wrapTag(o); - Js.Json.stringify(o1); -}; -let fromString = s => { - let o = Js.Json.parseExn(s); - unwrapTag(o); -}; \ No newline at end of file diff --git a/src/Serialize.rei b/src/Serialize.rei deleted file mode 100644 index 1262587..0000000 --- a/src/Serialize.rei +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Serializer for arbitrary values. - * Note: might not work across versions of the compiler (as the runtime representation might change). - * Note: uses json stringify under the hood: does not support functions or cyclic values. - */ - -let toString: 'a => string; - -let fromString: string => 'a; \ No newline at end of file diff --git a/src/Styp.js b/src/Styp.js deleted file mode 100644 index 24acf0c..0000000 --- a/src/Styp.js +++ /dev/null @@ -1,343 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as Block from "bs-platform/lib/es6/block.js"; -import * as Js_dict from "bs-platform/lib/es6/js_dict.js"; -import * as Caml_obj from "bs-platform/lib/es6/caml_obj.js"; -import * as Belt_List from "bs-platform/lib/es6/belt_List.js"; -import * as Belt_Array from "bs-platform/lib/es6/belt_Array.js"; -import * as Belt_Option from "bs-platform/lib/es6/belt_Option.js"; -import * as Caml_primitive from "bs-platform/lib/es6/caml_primitive.js"; -import * as Caml_builtin_exceptions from "bs-platform/lib/es6/caml_builtin_exceptions.js"; - -function $caret(prim, prim$1) { - return prim + prim$1 | 0; -} - -function $neg$neg(prim, prim$1) { - return prim - prim$1 | 0; -} - -function toString(prim) { - return String(prim); -} - -function toFloat(prim) { - return prim; -} - -var P = { - zero: 0, - one: 1, - "^": $caret, - "--": $neg$neg, - toString: toString, - toFloat: toFloat -}; - -function string_of_bool(b) { - if (b) { - return "true"; - } else { - return "false"; - } -} - -function constToString(typ) { - if (typeof typ === "number") { - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "Styp.re", - 54, - 9 - ] - ]; - } else { - switch (typ.tag | 0) { - case /* Number */1 : - return "number" + Belt_Option.mapWithDefault(typ[0], "", (function (f) { - return ":" + f.toString(); - })); - case /* String */2 : - return "string" + Belt_Option.mapWithDefault(typ[0], "", (function (s) { - return ":" + s; - })); - case /* Boolean */3 : - return "boolean" + Belt_Option.mapWithDefault(typ[0], "", (function (b) { - return ":" + ( - b ? "true" : "false" - ); - })); - default: - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "Styp.re", - 54, - 9 - ] - ]; - } - } -} - -function stripDiffStyp(styp) { - return /* record */[ - /* typ */stripDiffTyp(styp[/* typ */0]), - /* o */styp[/* o */1], - /* p */styp[/* p */2] - ]; -} - -function stripDiffTyp(_typ) { - while(true) { - var typ = _typ; - if (typeof typ === "number") { - return typ; - } else { - switch (typ.tag | 0) { - case /* Same */0 : - return /* Same */Block.__(0, [stripDiffTyp(typ[0])]); - case /* Object */4 : - return /* Object */Block.__(4, [Js_dict.map(stripDiffStyp, typ[0])]); - case /* Array */5 : - return /* Array */Block.__(5, [stripDiffStyp(typ[0])]); - case /* Union */6 : - return /* Union */Block.__(6, [Belt_List.map(typ[0], stripDiffStyp)]); - case /* Diff */7 : - _typ = typ[0]; - continue ; - default: - return typ; - } - } - }; -} - -function typIsEmpty(typ) { - if (typeof typ === "number" || !typ.tag) { - return true; - } else { - return false; - } -} - -function stypIsNull(styp) { - var match = styp[/* o */1]; - if (match && typIsEmpty(styp[/* typ */0])) { - return Caml_obj.caml_equal(styp[/* p */2], match[0]); - } else { - return false; - } -} - -var stypEmpty = /* record */[ - /* typ : Empty */0, - /* o : NotOpt */0, - /* p */0 -]; - -function stypIsEmpty(styp) { - if (styp[/* o */1] || !typIsEmpty(styp[/* typ */0])) { - return false; - } else { - return Caml_obj.caml_equal(styp[/* p */2], 0); - } -} - -function stypToUnion(styp) { - var match = styp[/* typ */0]; - if (typeof match === "number" || match.tag !== /* Union */6) { - return /* :: */[ - styp, - /* [] */0 - ]; - } else { - return match[0]; - } -} - -function compareEntries(param, param$1) { - return Caml_primitive.caml_string_compare(param[0], param$1[0]); -} - -function makeObject(arr) { - return /* Object */Block.__(4, [Js_dict.fromList(Belt_List.sort(Belt_List.fromArray(arr), compareEntries))]); -} - -var compareStyp = Caml_obj.caml_compare; - -function makeUnion(styps) { - return /* Union */Block.__(6, [Belt_List.sort(styps, compareStyp)]); -} - -function pToJson(p) { - return String(p); -} - -function stypToJson(styp) { - var dict = { }; - dict["typ"] = typToJson(styp[/* typ */0]); - var match = styp[/* o */1]; - if (match) { - dict["opt"] = String(match[0]); - } - dict["p"] = String(styp[/* p */2]); - return dict; -} - -function typToJson(typ) { - if (typeof typ === "number") { - return Js_dict.fromArray(/* array */[/* tuple */[ - "kind", - "Empty" - ]]); - } else { - switch (typ.tag | 0) { - case /* Same */0 : - return Js_dict.fromArray(/* array */[ - /* tuple */[ - "kind", - "Same" - ], - /* tuple */[ - "typ", - typToJson(typ[0]) - ] - ]); - case /* Object */4 : - var entries = Js_dict.fromArray(Belt_Array.map(Js_dict.entries(typ[0]), (function (param) { - return /* tuple */[ - param[0], - stypToJson(param[1]) - ]; - }))); - return Js_dict.fromArray(/* array */[ - /* tuple */[ - "kind", - "Object" - ], - /* tuple */[ - "entries", - entries - ] - ]); - case /* Array */5 : - var typ$1 = stypToJson(typ[0]); - return Js_dict.fromArray(/* array */[ - /* tuple */[ - "kind", - "Array" - ], - /* tuple */[ - "typ", - typ$1 - ] - ]); - case /* Union */6 : - var entries$1 = Js_dict.fromArray(Belt_Array.mapWithIndex(Belt_List.toArray(typ[0]), (function (i, styp) { - return /* tuple */[ - "u" + String(i), - stypToJson(styp) - ]; - }))); - return Js_dict.fromArray(/* array */[ - /* tuple */[ - "kind", - "Union" - ], - /* tuple */[ - "entries", - entries$1 - ] - ]); - case /* Diff */7 : - var common = typToJson(typ[0]); - var lhs = stypToJson(typ[1]); - var rhs = stypToJson(typ[2]); - return Js_dict.fromArray(/* array */[ - /* tuple */[ - "kind", - "Diff" - ], - /* tuple */[ - "common", - common - ], - /* tuple */[ - "lhs", - lhs - ], - /* tuple */[ - "rhs", - rhs - ] - ]); - default: - var kind; - if (typeof typ === "number") { - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "Styp.re", - 138, - 15 - ] - ]; - } else { - switch (typ.tag | 0) { - case /* Number */1 : - kind = "Number"; - break; - case /* String */2 : - kind = "String"; - break; - case /* Boolean */3 : - kind = "Boolean"; - break; - default: - throw [ - Caml_builtin_exceptions.assert_failure, - /* tuple */[ - "Styp.re", - 138, - 15 - ] - ]; - } - } - return Js_dict.fromArray(/* array */[ - /* tuple */[ - "kind", - kind - ], - /* tuple */[ - "value", - constToString(typ) - ] - ]); - } - } -} - -export { - P , - string_of_bool , - constToString , - stripDiffStyp , - stripDiffTyp , - typIsEmpty , - stypIsNull , - stypEmpty , - stypIsEmpty , - stypToUnion , - compareEntries , - makeObject , - compareStyp , - makeUnion , - pToJson , - stypToJson , - typToJson , - -} -/* No side effect */ diff --git a/src/Styp.re b/src/Styp.re deleted file mode 100644 index 1906d5b..0000000 --- a/src/Styp.re +++ /dev/null @@ -1,175 +0,0 @@ -module P: { - type t; - let zero: t; - let one: t; - let (++): (t, t) => t; - let (--): (t, t) => t; - let toString: t => string; - let toFloat: t => float; -} = { - type t = int; - let zero = 0; - let one = 1; - - let (++) = (+); - - let (--) = (-); - let toString = string_of_int; - let toFloat = float_of_int; -}; - -type p = P.t; - -type o = - | NotOpt - | Opt(p); - -type typ = - | Empty - | Same(typ) - | Number(option(float)) - | String(option(string)) - | Boolean(option(bool)) - | Object(Js.Dict.t(styp)) - | Array(styp) - | Union(list(styp)) - | Diff(typ, styp, styp) -and styp = { - typ, - o, - p, -}; - -let string_of_bool = b => b ? "true" : "false"; - -let constToString = typ => - switch (typ) { - | Number(x) => - "number" - ++ x->(Belt.Option.mapWithDefault("", f => ":" ++ Js.Float.toString(f))) - | String(x) => "string" ++ x->(Belt.Option.mapWithDefault("", s => ":" ++ s)) - | Boolean(x) => - "boolean" - ++ x->(Belt.Option.mapWithDefault("", b => ":" ++ string_of_bool(b))) - | _ => assert(false) - }; - -let rec stripDiffStyp = styp => {...styp, typ: styp.typ->stripDiffTyp} -and stripDiffTyp = typ => - switch (typ) { - | Empty - | Number(_) - | String(_) - | Boolean(_) => typ - | Same(typ) => Same(typ->stripDiffTyp) - | Object(d) => Js.Dict.map((. styp) => stripDiffStyp(styp), d)->Object - | Array(styp) => Array(styp->stripDiffStyp) - | Union(styps) => styps->(Belt.List.map(stripDiffStyp))->Union - | Diff(t, _, _) => t->stripDiffTyp - }; - -let typIsEmpty = typ => - switch (typ) { - | Empty - | Same(_) => true - | _ => false - }; - -let stypIsNull = (styp: styp) => - switch (styp.o) { - | NotOpt => false - | Opt(p) => styp.typ->typIsEmpty && styp.p == p - }; - -let stypEmpty = {typ: Empty, o: NotOpt, p: P.zero}; - -let stypIsEmpty = styp => - switch (styp) { - | {typ, o: NotOpt, p} when typ->typIsEmpty && p == P.zero => true - | _ => false - }; - -let stypToUnion = styp => - switch (styp.typ) { - | Union(styps) => styps - | _ => [styp] - }; - -let compareEntries = ((lbl1: string, _), (lbl2: string, _)) => - compare(lbl1, lbl2); - -let makeObject = arr => - arr - ->Belt.List.fromArray - ->(Belt.List.sort(compareEntries)) - ->Js.Dict.fromList - ->Object; - -let compareStyp = (x: styp, y: styp): int => compare(x, y); -let makeUnion = styps => styps->(Belt.List.sort(compareStyp))->Union; - -let pToJson = p => p->P.toString->Js.Json.string; - -let rec stypToJson = (styp: styp): Js.Json.t => { - let dict = Js.Dict.empty(); - dict->(Js.Dict.set("typ", styp.typ->typToJson)); - switch (styp.o) { - | NotOpt => () - | Opt(p1) => dict->(Js.Dict.set("opt", p1->pToJson)) - }; - dict->(Js.Dict.set("p", styp.p->pToJson)); - dict->Js.Json.object_; -} -and typToJson = (typ: typ): Js.Json.t => { - open Js.Json; - let arr = a => a->Js.Dict.fromArray->object_; - switch (typ) { - | Empty => [|("kind", "Empty"->string)|]->Js.Dict.fromArray->object_ - | Same(typ) => [|("kind", "Same"->string), ("typ", typ->typToJson)|]->arr - | Number(_) - | String(_) - | Boolean(_) => - let kind = - ( - switch (typ) { - | Number(_) => "Number" - | String(_) => "String" - | Boolean(_) => "Boolean" - | _ => assert(false) - } - ) - ->string; - [|("kind", kind), ("value", typ->constToString->string)|]->arr; - | Object(d) => - let entries = - Js.Dict.entries(d) - ->(Belt.Array.map(((lbl, styp)) => (lbl, styp->stypToJson))) - ->arr; - [|("kind", "Object"->string), ("entries", entries)|]->arr; - | Array(styp) => - let typ = styp->stypToJson; - [|("kind", "Array"->string), ("typ", typ)|]->arr; - | Union(styps) => - let entries = - styps - ->Belt.List.toArray - ->( - Belt.Array.mapWithIndex((i, styp) => - ("u" ++ string_of_int(i), styp->stypToJson) - ) - ) - ->arr; - [|("kind", "Union"->string), ("entries", entries)|]->arr; - | Diff(t, lhs, rhs) => - let common = t->typToJson; - let lhs = lhs->stypToJson; - let rhs = rhs->stypToJson; - [| - ("kind", "Diff"->string), - ("common", common), - ("lhs", lhs), - ("rhs", rhs), - |] - ->arr; - }; -}; \ No newline at end of file diff --git a/src/TypeCheck.js b/src/TypeCheck.js deleted file mode 100644 index 01dc1cf..0000000 --- a/src/TypeCheck.js +++ /dev/null @@ -1,353 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as Block from "bs-platform/lib/es6/block.js"; -import * as Js_dict from "bs-platform/lib/es6/js_dict.js"; -import * as Js_json from "bs-platform/lib/es6/js_json.js"; -import * as Caml_obj from "bs-platform/lib/es6/caml_obj.js"; -import * as Belt_List from "bs-platform/lib/es6/belt_List.js"; -import * as Belt_Array from "bs-platform/lib/es6/belt_Array.js"; -import * as Styp$ReactTemplate from "./Styp.js"; - -var defaultMode = /* record */[/* singletonTypes */false]; - -function fromJson($staropt$star, json) { - var mode = $staropt$star !== undefined ? $staropt$star : defaultMode; - var match = Js_json.classify(json); - if (typeof match === "number") { - switch (match) { - case /* JSONFalse */0 : - var match$1 = mode[/* singletonTypes */0]; - return /* record */[ - /* typ : Boolean */Block.__(3, [match$1 ? false : undefined]), - /* o : NotOpt */0, - /* p */Styp$ReactTemplate.P.one - ]; - case /* JSONTrue */1 : - var match$2 = mode[/* singletonTypes */0]; - return /* record */[ - /* typ : Boolean */Block.__(3, [match$2 ? true : undefined]), - /* o : NotOpt */0, - /* p */Styp$ReactTemplate.P.one - ]; - case /* JSONNull */2 : - return /* record */[ - /* typ : Empty */0, - /* o : Opt */[Styp$ReactTemplate.P.one], - /* p */Styp$ReactTemplate.P.one - ]; - - } - } else { - switch (match.tag | 0) { - case /* JSONString */0 : - var match$3 = mode[/* singletonTypes */0]; - return /* record */[ - /* typ : String */Block.__(2, [match$3 ? match[0] : undefined]), - /* o : NotOpt */0, - /* p */Styp$ReactTemplate.P.one - ]; - case /* JSONNumber */1 : - var match$4 = mode[/* singletonTypes */0]; - return /* record */[ - /* typ : Number */Block.__(1, [match$4 ? match[0] : undefined]), - /* o : NotOpt */0, - /* p */Styp$ReactTemplate.P.one - ]; - case /* JSONObject */2 : - var do_entry = function (param) { - var styp = fromJson(mode, param[1]); - return /* tuple */[ - param[0], - styp - ]; - }; - return /* record */[ - /* typ */Styp$ReactTemplate.makeObject(Belt_Array.map(Js_dict.entries(match[0]), do_entry)), - /* o : NotOpt */0, - /* p */Styp$ReactTemplate.P.one - ]; - case /* JSONArray */3 : - var styp = Belt_Array.reduce(match[0], /* record */[ - /* typ : Empty */0, - /* o : NotOpt */0, - /* p */Styp$ReactTemplate.P.zero - ], (function (styp, json) { - return plusStyp(styp, fromJson(mode, json)); - })); - return /* record */[ - /* typ : Array */Block.__(5, [styp]), - /* o : NotOpt */0, - /* p */Styp$ReactTemplate.P.one - ]; - - } - } -} - -function plusO(o1, o2) { - if (o1) { - if (o2) { - return /* Opt */[Styp$ReactTemplate.P["^"](o1[0], o2[0])]; - } else { - return o1; - } - } else { - return o2; - } -} - -function plusTyp(_typ1, _typ2) { - while(true) { - var typ2 = _typ2; - var typ1 = _typ1; - var exit = 0; - var exit$1 = 0; - if (typeof typ1 === "number") { - exit$1 = 3; - } else { - switch (typ1.tag | 0) { - case /* Number */1 : - var x = typ1[0]; - if (typeof typ2 === "number") { - exit = 2; - } else { - switch (typ2.tag | 0) { - case /* Number */1 : - var match = Caml_obj.caml_equal(x, typ2[0]); - if (match) { - return /* Number */Block.__(1, [x]); - } else { - return ; - } - case /* Array */5 : - return ; - case /* Diff */7 : - exit$1 = 3; - break; - default: - - } - } - break; - case /* String */2 : - var x$1 = typ1[0]; - if (typeof typ2 === "number") { - exit = 2; - } else { - switch (typ2.tag | 0) { - case /* String */2 : - var match$1 = Caml_obj.caml_equal(x$1, typ2[0]); - if (match$1) { - return /* String */Block.__(2, [x$1]); - } else { - return ; - } - case /* Array */5 : - return ; - case /* Diff */7 : - exit$1 = 3; - break; - default: - - } - } - break; - case /* Boolean */3 : - var x$2 = typ1[0]; - if (typeof typ2 === "number") { - exit = 2; - } else { - switch (typ2.tag | 0) { - case /* Boolean */3 : - var match$2 = Caml_obj.caml_equal(x$2, typ2[0]); - if (match$2) { - return /* Boolean */Block.__(3, [x$2]); - } else { - return ; - } - case /* Array */5 : - return ; - case /* Diff */7 : - exit$1 = 3; - break; - default: - - } - } - break; - case /* Object */4 : - if (typeof typ2 === "number") { - exit = 2; - } else { - switch (typ2.tag | 0) { - case /* Object */4 : - var d = { }; - var doItem = (function(d){ - return function doItem(param) { - var styp = param[1]; - var lbl = param[0]; - var match = Js_dict.get(d, lbl); - if (match !== undefined) { - d[lbl] = plusStyp(styp, match); - return /* () */0; - } else { - d[lbl] = styp; - return /* () */0; - } - } - }(d)); - Belt_Array.forEach(Js_dict.entries(typ1[0]), doItem); - Belt_Array.forEach(Js_dict.entries(typ2[0]), doItem); - return Styp$ReactTemplate.makeObject(Js_dict.entries(d)); - case /* Array */5 : - return ; - case /* Diff */7 : - exit$1 = 3; - break; - default: - - } - } - break; - case /* Array */5 : - if (typeof typ2 === "number") { - exit = 2; - } else { - switch (typ2.tag | 0) { - case /* Array */5 : - return /* Array */Block.__(5, [plusStyp(typ1[0], typ2[0])]); - case /* Diff */7 : - exit$1 = 3; - break; - default: - - } - } - break; - case /* Diff */7 : - _typ1 = typ1[0]; - continue ; - default: - exit$1 = 3; - } - } - if (exit$1 === 3) { - if (typeof typ2 === "number") { - if (typeof typ1 === "number") { - return typ2; - } else { - exit = 2; - } - } else if (typ2.tag === /* Diff */7) { - _typ2 = typ2[0]; - continue ; - } else if (typeof typ1 === "number") { - return typ2; - } else { - exit = 2; - } - } - if (exit === 2) { - if (typeof typ2 === "number") { - return typ1; - } else if (typeof typ1 !== "number" && !typ1.tag) { - _typ2 = typ1[0]; - _typ1 = typ2; - continue ; - } - - } - if (typeof typ2 === "number") { - return ; - } else { - switch (typ2.tag | 0) { - case /* Same */0 : - _typ2 = typ2[0]; - continue ; - case /* Array */5 : - return ; - default: - return ; - } - } - }; -} - -function plusUnion(styps1, styps2) { - var findMatch = function (t, _ts, _acc) { - while(true) { - var acc = _acc; - var ts = _ts; - if (ts) { - var ts1 = ts[1]; - var t1 = ts[0]; - if (plusTyp(t[/* typ */0], t1[/* typ */0]) !== undefined) { - return /* tuple */[ - t1, - Belt_List.concat(Belt_List.reverse(acc), ts1) - ]; - } else { - _acc = /* :: */[ - t1, - acc - ]; - _ts = ts1; - continue ; - } - } else { - return ; - } - }; - }; - var plus = function (ls1, ls2) { - if (ls1) { - var ts1 = ls1[1]; - var t1 = ls1[0]; - var match = findMatch(t1, ls2, /* [] */0); - if (match !== undefined) { - var match$1 = match; - return /* :: */[ - plusStyp(t1, match$1[0]), - plus(ts1, match$1[1]) - ]; - } else { - return /* :: */[ - t1, - plus(ts1, ls2) - ]; - } - } else { - return ls2; - } - }; - return Styp$ReactTemplate.makeUnion(plus(styps1, styps2)); -} - -function plusStyp(styp1, styp2) { - var match = plusTyp(styp1[/* typ */0], styp2[/* typ */0]); - var typ = match !== undefined ? match : plusUnion(Styp$ReactTemplate.stypToUnion(styp1), Styp$ReactTemplate.stypToUnion(styp2)); - var o = plusO(styp1[/* o */1], styp2[/* o */1]); - var p = Styp$ReactTemplate.P["^"](styp1[/* p */2], styp2[/* p */2]); - return /* record */[ - /* typ */typ, - /* o */o, - /* p */p - ]; -} - -var $caret = plusStyp; - -var singletonMode = /* record */[/* singletonTypes */true]; - -export { - defaultMode , - singletonMode , - fromJson , - plusStyp , - plusO , - plusTyp , - plusUnion , - $caret , - -} -/* No side effect */ diff --git a/src/TypeCheck.re b/src/TypeCheck.re deleted file mode 100644 index 3f4252b..0000000 --- a/src/TypeCheck.re +++ /dev/null @@ -1,124 +0,0 @@ -open Styp; - -type mode = {singletonTypes: bool}; - -let defaultMode = {singletonTypes: false}; -let singletonMode = {singletonTypes: true}; - -let rec fromJson = (~mode=defaultMode, json: Js.Json.t): styp => - switch (Js.Json.classify(json)) { - | JSONFalse => { - typ: Boolean(mode.singletonTypes ? Some(false) : None), - o: NotOpt, - p: P.one, - } - | JSONTrue => { - typ: Boolean(mode.singletonTypes ? Some(true) : None), - o: NotOpt, - p: P.one, - } - | JSONNull => {typ: Empty, o: Opt(P.one), p: P.one} - | JSONString(x) => { - typ: String(mode.singletonTypes ? Some(x) : None), - o: NotOpt, - p: P.one, - } - | JSONNumber(x) => { - typ: Number(mode.singletonTypes ? Some(x) : None), - o: NotOpt, - p: P.one, - } - | JSONObject(dict) => - let do_entry = ((lbl, v)) => { - let styp = fromJson(~mode, v); - (lbl, styp); - }; - { - typ: Js.Dict.entries(dict)->(Belt.Array.map(do_entry))->makeObject, - o: NotOpt, - p: P.one, - }; - | JSONArray(a) => - a - ->( - Belt.Array.reduce({typ: Empty, o: NotOpt, p: P.zero}, (styp, json) => - styp ++ fromJson(~mode, json) - ) - ) - ->(styp => {typ: Array(styp), o: NotOpt, p: P.one}) - } -and plusStyp = (styp1, styp2) => { - let typ = - switch (plusTyp(styp1.typ, styp2.typ)) { - | Some(typ) => typ - | None => plusUnion(styp1->stypToUnion, styp2->stypToUnion) - }; - let o = plusO(styp1.o, styp2.o); - open! P; - let p = styp1.p ++ styp2.p; - {typ, o, p}; -} -and plusO = (o1, o2) => - switch (o1, o2) { - | (NotOpt, o) - | (o, NotOpt) => o - | (Opt(p1), Opt(p2)) => - open! P; - Opt(p1 ++ p2); - } -and plusTyp = (typ1, typ2): option(typ) => - switch (typ1, typ2) { - | (Diff(typ, _, _), _) => plusTyp(typ, typ2) - | (_, Diff(typ, _, _)) => plusTyp(typ1, typ) - | (Empty, t) - | (t, Empty) => t->Some - | (Same(t1), t) - | (t, Same(t1)) => plusTyp(t, t1) - | (Number(x), Number(y)) => x == y ? Number(x)->Some : None - | (String(x), String(y)) => x == y ? String(x)->Some : None - | (Boolean(x), Boolean(y)) => x == y ? Boolean(x)->Some : None - | (Object(d1), Object(d2)) => - let d = Js.Dict.empty(); - let doItem = ((lbl, styp)) => - switch (d->(Js.Dict.get(lbl))) { - | None => d->(Js.Dict.set(lbl, styp)) - | Some(styp1) => d->(Js.Dict.set(lbl, styp ++ styp1)) - }; - d1->Js.Dict.entries->(Belt.Array.forEach(doItem)); - d2->Js.Dict.entries->(Belt.Array.forEach(doItem)); - d->Js.Dict.entries->makeObject->Some; - | (Array(styp1), Array(styp2)) => (styp1 ++ styp2)->Array->Some - | (Number(_), _) - | (_, Number(_)) - | (String(_), _) - | (_, String(_)) - | (Boolean(_), _) - | (_, Boolean(_)) - | (Object(_), _) - | (_, Object(_)) - | (Union(_), _) - | (_, Union(_)) => None - } -and plusUnion = (styps1, styps2) => { - let rec findMatch = (t, ts, acc) => - switch (ts) { - | [t1, ...ts1] => - if (plusTyp(t.typ, t1.typ) != None) { - Some((t1, acc->Belt.List.reverse->(Belt.List.concat(ts1)))); - } else { - findMatch(t, ts1, [t1, ...acc]); - } - | [] => None - }; - let rec plus = (ls1, ls2) => - switch (ls1, ls2) { - | ([t1, ...ts1], _) => - switch (findMatch(t1, ls2, [])) { - | None => [t1, ...plus(ts1, ls2)] - | Some((t2, ts2)) => [plusStyp(t1, t2), ...plus(ts1, ts2)] - } - | ([], ts) => ts - }; - plus(styps1, styps2)->makeUnion; -} -and (++) = (styp1, styp2) => plusStyp(styp1, styp2); \ No newline at end of file diff --git a/src/UI.js b/src/UI.js deleted file mode 100644 index f974adf..0000000 --- a/src/UI.js +++ /dev/null @@ -1,345 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE - -import * as React from "react"; -import * as Js_dict from "bs-platform/lib/es6/js_dict.js"; -import * as Caml_obj from "bs-platform/lib/es6/caml_obj.js"; -import * as Belt_List from "bs-platform/lib/es6/belt_List.js"; -import * as Belt_Array from "bs-platform/lib/es6/belt_Array.js"; -import * as Caml_option from "bs-platform/lib/es6/caml_option.js"; -import * as Styp$ReactTemplate from "./Styp.js"; - -var counter = /* record */[/* contents */0]; - -function gen(param) { - counter[0] = counter[0] + 1 | 0; - return String(counter[0]); -} - -var Key = { - counter: counter, - gen: gen -}; - -var black = "#000000"; - -var blue = "#0000FF"; - -var brown = "#795E26"; - -var green = "#09885A"; - -var grey = "#979797"; - -var red = "#D60A0A"; - -function style(color) { - return { - color: color - }; -} - -var Color = { - black: black, - blue: blue, - brown: brown, - green: green, - grey: grey, - red: red, - style: style -}; - -function reducer(state, action) { - return /* record */[/* collapsed */!state[/* collapsed */0]]; -} - -function UI$TreeView(Props) { - var nodeLabel = Props.nodeLabel; - var collapsed = Props.collapsed; - var child = Props.child; - var match = React.useReducer(reducer, /* record */[/* collapsed */collapsed]); - var dispatch = match[1]; - var state = match[0]; - var match$1 = state[/* collapsed */0]; - var arrowClassName = "tree-view_arrow" + ( - match$1 ? " tree-view_arrow-collapsed" : "" - ); - var match$2 = state[/* collapsed */0]; - var containerClassName = "tree-view_children" + ( - match$2 ? " tree-view_children-collapsed" : "" - ); - var match$3 = state[/* collapsed */0]; - return React.createElement("div", { - className: "tree-view" - }, React.createElement("div", { - className: "tree-view_item", - onClick: (function (param) { - return dispatch(/* Click */0); - }) - }, React.createElement("div", { - className: arrowClassName - }), React.createElement("span", undefined, nodeLabel)), React.createElement("div", { - className: containerClassName - }, match$3 ? null : child)); -} - -var TreeView = { - reducer: reducer, - make: UI$TreeView -}; - -function node(style, x) { - var tmp = { - className: "node" - }; - if (style !== undefined) { - tmp.style = Caml_option.valFromOption(style); - } - return React.createElement("span", tmp, x); -} - -function nodeGreen(x) { - return node(Caml_option.some(style(green)), x); -} - -function nodeBrown(x) { - return node(Caml_option.some(style(brown)), x); -} - -function questionMark(p) { - return React.createElement("span", { - style: style(red) - }, " ? " + Styp$ReactTemplate.P.toString(p)); -} - -var fmtDefault = /* record */[ - /* plus */false, - /* percent */false, - /* hideZeroOne */false, - /* hideP */false -]; - -var fmtDelta = /* record */[ - /* plus */true, - /* percent */false, - /* hideZeroOne */false, - /* hideP */false -]; - -function toComponentStyp(styp, ctx, fmt) { - var typ = toComponentT(styp[/* typ */0], styp[/* p */2], fmt); - var match = Styp$ReactTemplate.stypIsNull(styp); - var style$1 = style(match ? red : black); - var match$1 = styp[/* typ */0]; - var shouldAddDecorator; - if (typeof match$1 === "number") { - shouldAddDecorator = true; - } else { - switch (match$1.tag | 0) { - case /* Number */1 : - case /* String */2 : - case /* Boolean */3 : - shouldAddDecorator = true; - break; - default: - shouldAddDecorator = false; - } - } - var match$2 = Styp$ReactTemplate.stypIsNull(styp); - if (match$2) { - return React.createElement("div", { - className: "node", - style: style$1 - }, "null"); - } else { - return React.createElement("div", { - style: style$1 - }, shouldAddDecorator ? addDecorator(typ, styp, true, ctx, fmt) : typ); - } -} - -function addDecorator(x, styp, right, ctx, fmt) { - var pUnchanged = Caml_obj.caml_equal(ctx, Styp$ReactTemplate.P.zero) && Caml_obj.caml_equal(styp[/* p */2], Styp$ReactTemplate.P.zero); - var pString; - if (fmt[/* percent */1] && Caml_obj.caml_notequal(ctx, Styp$ReactTemplate.P.zero)) { - pString = (Styp$ReactTemplate.P.toFloat(styp[/* p */2]) / Styp$ReactTemplate.P.toFloat(ctx)).toString(); - } else { - var match = fmt[/* plus */0]; - pString = ( - match ? "+" : "" - ) + Styp$ReactTemplate.P.toString(styp[/* p */2]); - } - var match$1 = fmt[/* hideP */3] || fmt[/* hideZeroOne */2] && (pUnchanged || Caml_obj.caml_equal(styp[/* p */2], Styp$ReactTemplate.P.one)); - var p = match$1 ? null : React.createElement("span", { - style: style(blue) - }, pString); - var match$2 = styp[/* o */1]; - var o = match$2 ? questionMark(match$2[0]) : null; - if (right) { - return React.createElement("span", undefined, x, p, o); - } else { - return React.createElement("span", undefined, p, o, x); - } -} - -function toComponentT(typ, ctx, fmt) { - if (typeof typ === "number") { - return nodeBrown("empty"); - } else { - switch (typ.tag | 0) { - case /* Same */0 : - return React.createElement(UI$TreeView, { - nodeLabel: nodeBrown("same"), - collapsed: true, - child: toComponentT(typ[0], ctx, fmt), - key: "same" - }); - case /* Object */4 : - var doEntry = function (i, param) { - var styp = param[1]; - var match = Caml_obj.caml_equal(styp[/* p */2], Styp$ReactTemplate.P.zero); - return React.createElement("span", { - key: String(i), - style: style(match ? grey : black) - }, React.createElement(UI$TreeView, { - nodeLabel: addDecorator(node(undefined, param[0]), styp, true, ctx, fmt), - collapsed: false, - child: toComponentStyp(styp, ctx, fmt), - key: String(i) - })); - }; - return Belt_Array.mapWithIndex(Js_dict.entries(typ[0]), doEntry); - case /* Array */5 : - var styp = typ[0]; - if (Styp$ReactTemplate.stypIsEmpty(styp)) { - return React.createElement("span", undefined, addDecorator(node(undefined, "["), styp, false, ctx, fmt), node(undefined, "]")); - } else { - var match = Caml_obj.caml_equal(styp[/* p */2], Styp$ReactTemplate.P.zero); - return React.createElement("span", { - style: style(match ? grey : black) - }, React.createElement(UI$TreeView, { - nodeLabel: addDecorator(node(undefined, "["), styp, false, ctx, fmt), - collapsed: false, - child: toComponentStyp(styp, ctx, fmt) - }), node(undefined, "]")); - } - case /* Union */6 : - var doEntry$1 = function (i, styp) { - return React.createElement(UI$TreeView, { - nodeLabel: addDecorator(node(undefined, "u" + String(i + 1 | 0)), styp, true, ctx, fmt), - collapsed: false, - child: toComponentStyp(styp, ctx, fmt), - key: String(i) - }); - }; - return React.createElement("div", undefined, nodeBrown("union"), Belt_List.toArray(Belt_List.mapWithIndex(typ[0], doEntry$1))); - case /* Diff */7 : - var rhs = typ[2]; - var lhs = typ[1]; - var side = function (left) { - var lbl = left ? "lhs" : "rhs"; - var styp = left ? lhs : rhs; - var match = Styp$ReactTemplate.stypIsEmpty(styp); - return React.createElement(UI$TreeView, { - nodeLabel: nodeBrown(lbl), - collapsed: false, - child: match ? null : toComponentStyp(styp, ctx, fmtDelta), - key: lbl - }); - }; - return React.createElement("div", { - className: "row" - }, React.createElement("div", { - className: "column3" - }, side(true)), React.createElement("div", { - className: "column3" - }, React.createElement(UI$TreeView, { - nodeLabel: nodeBrown("common"), - collapsed: false, - child: toComponentT(typ[0], ctx, fmt) - })), React.createElement("div", { - className: "column3" - }, side(false))); - default: - return nodeGreen(Styp$ReactTemplate.constToString(typ)); - } - } -} - -function UI$Styp(Props) { - var name = Props.name; - var styp = Props.styp; - var match = Props.fmt; - var fmt = match !== undefined ? match : fmtDefault; - return React.createElement(UI$TreeView, { - nodeLabel: node(undefined, name), - collapsed: true, - child: toComponentStyp(styp, Styp$ReactTemplate.P.zero, fmt) - }); -} - -var Styp = { - make: UI$Styp -}; - -function UI$Diff(Props) { - var diff = Props.diff; - return React.createElement("div", undefined, React.createElement("div", { - className: "centerText" - }, "Inferred Types"), React.createElement("div", { - className: "row" - }, React.createElement("div", { - className: "column2" - }, React.createElement(UI$Styp, { - name: "styp1", - styp: diff[/* styp1 */0] - })), React.createElement("div", { - className: "column2" - }, React.createElement(UI$Styp, { - name: "styp2", - styp: diff[/* styp2 */1] - }))), React.createElement("div", { - className: "centerText" - }, "Common Part"), React.createElement("div", undefined, React.createElement(UI$Styp, { - name: "stypB", - styp: diff[/* stypB */4] - })), React.createElement("div", { - className: "centerText" - }, "Deltas"), React.createElement("div", { - className: "row" - }, React.createElement("div", { - className: "column2" - }, React.createElement(UI$Styp, { - name: "stypA1", - styp: diff[/* stypA1 */2], - fmt: fmtDelta - })), React.createElement("div", { - className: "column2" - }, React.createElement(UI$Styp, { - name: "stypA2", - styp: diff[/* stypA2 */3], - fmt: fmtDelta - })))); -} - -var Diff = { - make: UI$Diff -}; - -export { - Key , - Color , - TreeView , - node , - nodeGreen , - nodeBrown , - questionMark , - fmtDefault , - fmtDelta , - toComponentStyp , - addDecorator , - toComponentT , - Styp , - Diff , - -} -/* react Not a pure module */ diff --git a/src/UI.re b/src/UI.re deleted file mode 100644 index a85e3fd..0000000 --- a/src/UI.re +++ /dev/null @@ -1,254 +0,0 @@ -open Styp; - -module Key = { - let counter = ref(0); - let gen = () => { - incr(counter); - string_of_int(counter^); - }; -}; - -module Color = { - let black = "#000000"; - let blue = "#0000FF"; - let brown = "#795E26"; - let green = "#09885A"; - let grey = "#979797"; - let red = "#D60A0A"; - let style = color => ReactDOMRe.Style.make(~color, ()); -}; - -module TreeView = { - type state = {collapsed: bool}; - type actions = - | Click; - - let reducer = (state, action) => { - switch (action) { - | Click => {collapsed: !state.collapsed} - }; - }; - - [@bs.module "react"] - external useReducer: - ([@bs.uncurry] (('state, 'action) => 'state), 'state) => - ('state, (. 'action) => unit) = - "useReducer"; - - [@react.component] - let make = (~nodeLabel, ~collapsed, ~child, _) => { - let (state, dispatch) = useReducer(reducer, {collapsed: collapsed}); - let arrowClassName = - "tree-view_arrow" - ++ (state.collapsed ? " tree-view_arrow-collapsed" : ""); - let containerClassName = - "tree-view_children" - ++ (state.collapsed ? " tree-view_children-collapsed" : ""); - -
-
dispatch(. Click)}> -
- nodeLabel -
-
- {state.collapsed ? React.null : child} -
-
; - }; -}; - -let node = (~style=?, x) => - {React.string(x)} ; - -let nodeGreen = x => x->(node(~style=Color.(style(green)))); - -let nodeBrown = x => x->(node(~style=Color.(style(brown)))); - -let questionMark = p => - - {React.string(" ? " ++ P.toString(p))} - ; - -type fmt = { - plus: bool /* print '+' in front of number */, - percent: bool /* show percentage instead of absolute numbers */, - hideZeroOne: bool /* hide p when it's 0 or 1 */, - hideP: bool /* hide p completely */, -}; - -let fmtDefault = { - plus: false, - percent: false, - hideZeroOne: false, - hideP: false, -}; -let fmtDelta = {plus: true, percent: false, hideZeroOne: false, hideP: false}; - -let rec toComponentStyp = (styp: styp, ~ctx: p, ~fmt: fmt): React.element => { - let typ = styp.typ->(toComponentT(~ctx=styp.p, ~fmt)); - let style = Color.(style(stypIsNull(styp) ? red : black)); - let shouldAddDecorator = - switch (styp.typ) { - | Empty - | Number(_) - | String(_) - | Boolean(_) => true - | _ => false - }; - stypIsNull(styp) - ?
{React.string("null")}
- :
- {shouldAddDecorator - ? typ->(addDecorator(~styp, ~right=true, ~ctx, ~fmt)) : typ} -
; -} -and addDecorator = - (x: React.element, ~styp: styp, ~right, ~ctx: p, ~fmt: fmt): React.element => { - let pUnchanged = ctx == P.zero && styp.p == P.zero; - let pString = - if (fmt.percent && ctx != P.zero) { - (P.toFloat(styp.p) /. P.toFloat(ctx))->Js.Float.toString; - } else { - (fmt.plus ? "+" : "") ++ P.toString(styp.p); - }; - let p = - fmt.hideP || fmt.hideZeroOne && (pUnchanged || styp.p == P.one) - ? React.null - : {React.string(pString)} ; - let o = - switch (styp.o) { - | NotOpt => React.null - | Opt(p1) => questionMark(p1) - }; - right ? x p o : p o x ; -} -and toComponentT = (typ: typ, ~ctx: p, ~fmt: fmt): React.element => - switch (typ) { - | Empty => nodeBrown("empty") - | Same(typ) => - (toComponentT(~ctx, ~fmt))} - /> - | Number(_) - | String(_) - | Boolean(_) => typ->constToString->nodeGreen - | Object(d) => - let doEntry = (i, (lbl, styp)) => - - (addDecorator(~styp, ~right=true, ~ctx, ~fmt)) - } - collapsed=false - child={styp->(toComponentStyp(~ctx, ~fmt))} - /> - ; - - React.array(Js.Dict.entries(d)->(Belt.Array.mapWithIndex(doEntry))); - | Array(styp) when stypIsEmpty(styp) => - - {node("[")->(addDecorator(~styp, ~right=false, ~ctx, ~fmt))} - {node("]")} - - | Array(styp) => - - (addDecorator(~styp, ~right=false, ~ctx, ~fmt)) - } - collapsed=false - child={styp->(toComponentStyp(~ctx, ~fmt))} - /> - {node("]")} - - | Union(styps) => - let doEntry = (i, styp) => - (addDecorator(~styp, ~right=true, ~ctx, ~fmt)) - } - collapsed=false - child={styp->(toComponentStyp(~ctx, ~fmt))} - />; - -
- "union"->nodeBrown - {styps - ->(Belt.List.mapWithIndex(doEntry)) - ->Belt.List.toArray - ->React.array} -
; - - | Diff(t, lhs, rhs) => - let side = (~left) => { - let lbl = left ? "lhs" : "rhs"; - let styp = left ? lhs : rhs; - nodeBrown} - collapsed=false - child={ - Styp.stypIsEmpty(styp) - ? React.null : styp->(toComponentStyp(~ctx, ~fmt=fmtDelta)) - } - />; - }; -
-
{side(~left=true)}
-
- nodeBrown} - collapsed=false - child={t->(toComponentT(~ctx, ~fmt))} - /> -
-
{side(~left=false)}
-
; - }; - -module Styp = { - [@react.component] - let make = (~name, ~styp, ~fmt=fmtDefault, _) => { - (toComponentStyp(~ctx=P.zero, ~fmt))} - />; - }; -}; - -module Diff = { - [@react.component] - let make = (~diff: Diff.t, _) => { -
-
{React.string @@ "Inferred Types"}
-
-
- -
-
- -
-
-
{React.string @@ "Common Part"}
-
-
{React.string @@ "Deltas"}
-
-
- -
-
- -
-
-
; - }; -}; \ No newline at end of file diff --git a/src/VendoredArray.js b/src/VendoredArray.js new file mode 100644 index 0000000..f291396 --- /dev/null +++ b/src/VendoredArray.js @@ -0,0 +1,737 @@ +// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE + +import * as Curry from "bs-platform/lib/es6/curry.js"; +import * as Js_exn from "bs-platform/lib/es6/js_exn.js"; +import * as Js_math from "bs-platform/lib/es6/js_math.js"; +import * as Caml_int32 from "bs-platform/lib/es6/caml_int32.js"; +import * as Caml_option from "bs-platform/lib/es6/caml_option.js"; +import * as Caml_primitive from "bs-platform/lib/es6/caml_primitive.js"; + +function get(arr, i) { + if (i >= 0 && i < arr.length) { + return Caml_option.some(arr[i]); + } + +} + +function getExn(arr, i) { + if (!(i >= 0 && i < arr.length)) { + Js_exn.raiseError("File \"VendoredArray.re\", line 30, characters 3-9"); + } + return arr[i]; +} + +function set(arr, i, v) { + if (i >= 0 && i < arr.length) { + arr[i] = v; + return true; + } else { + return false; + } +} + +function setExn(arr, i, v) { + if (!(i >= 0 && i < arr.length)) { + Js_exn.raiseError("File \"VendoredArray.re\", line 43, characters 3-9"); + } + arr[i] = v; + return /* () */0; +} + +function swapUnsafe(xs, i, j) { + var tmp = xs[i]; + xs[i] = xs[j]; + xs[j] = tmp; + return /* () */0; +} + +function shuffleInPlace(xs) { + var len = xs.length; + for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){ + swapUnsafe(xs, i, Js_math.random_int(i, len)); + } + return /* () */0; +} + +function shuffle(xs) { + var result = xs.slice(0); + shuffleInPlace(result); + return result; +} + +function reverseInPlace(xs) { + var len = xs.length; + var xs$1 = xs; + var ofs = 0; + var len$1 = len; + for(var i = 0 ,i_finish = (len$1 / 2 | 0) - 1 | 0; i <= i_finish; ++i){ + swapUnsafe(xs$1, ofs + i | 0, ((ofs + len$1 | 0) - i | 0) - 1 | 0); + } + return /* () */0; +} + +function reverse(xs) { + var len = xs.length; + var result = new Array(len); + for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){ + result[i] = xs[(len - 1 | 0) - i | 0]; + } + return result; +} + +function make(l, f) { + if (l <= 0) { + return /* array */[]; + } else { + var res = new Array(l); + for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){ + res[i] = f; + } + return res; + } +} + +function makeByU(l, f) { + if (l <= 0) { + return /* array */[]; + } else { + var res = new Array(l); + for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){ + res[i] = f(i); + } + return res; + } +} + +function makeBy(l, f) { + return makeByU(l, Curry.__1(f)); +} + +function makeByAndShuffleU(l, f) { + var u = makeByU(l, f); + shuffleInPlace(u); + return u; +} + +function makeByAndShuffle(l, f) { + return makeByAndShuffleU(l, Curry.__1(f)); +} + +function range(start, finish) { + var cut = finish - start | 0; + if (cut < 0) { + return /* array */[]; + } else { + var arr = new Array(cut + 1 | 0); + for(var i = 0; i <= cut; ++i){ + arr[i] = start + i | 0; + } + return arr; + } +} + +function rangeBy(start, finish, step) { + var cut = finish - start | 0; + if (cut < 0 || step <= 0) { + return /* array */[]; + } else { + var nb = Caml_int32.div(cut, step) + 1 | 0; + var arr = new Array(nb); + var cur = start; + for(var i = 0 ,i_finish = nb - 1 | 0; i <= i_finish; ++i){ + arr[i] = cur; + cur = cur + step | 0; + } + return arr; + } +} + +function zip(xs, ys) { + var lenx = xs.length; + var leny = ys.length; + var len = lenx < leny ? lenx : leny; + var s = new Array(len); + for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){ + s[i] = /* tuple */[ + xs[i], + ys[i] + ]; + } + return s; +} + +function zipByU(xs, ys, f) { + var lenx = xs.length; + var leny = ys.length; + var len = lenx < leny ? lenx : leny; + var s = new Array(len); + for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){ + s[i] = f(xs[i], ys[i]); + } + return s; +} + +function zipBy(xs, ys, f) { + return zipByU(xs, ys, Curry.__2(f)); +} + +function concat(a1, a2) { + var l1 = a1.length; + var l2 = a2.length; + var a1a2 = new Array(l1 + l2 | 0); + for(var i = 0 ,i_finish = l1 - 1 | 0; i <= i_finish; ++i){ + a1a2[i] = a1[i]; + } + for(var i$1 = 0 ,i_finish$1 = l2 - 1 | 0; i$1 <= i_finish$1; ++i$1){ + a1a2[l1 + i$1 | 0] = a2[i$1]; + } + return a1a2; +} + +function concatMany(arrs) { + var lenArrs = arrs.length; + var totalLen = 0; + for(var i = 0 ,i_finish = lenArrs - 1 | 0; i <= i_finish; ++i){ + totalLen = totalLen + arrs[i].length | 0; + } + var result = new Array(totalLen); + totalLen = 0; + for(var j = 0 ,j_finish = lenArrs - 1 | 0; j <= j_finish; ++j){ + var cur = arrs[j]; + for(var k = 0 ,k_finish = cur.length - 1 | 0; k <= k_finish; ++k){ + result[totalLen] = cur[k]; + totalLen = totalLen + 1 | 0; + } + } + return result; +} + +function slice(a, offset, len) { + if (len <= 0) { + return /* array */[]; + } else { + var lena = a.length; + var ofs = offset < 0 ? Caml_primitive.caml_int_max(lena + offset | 0, 0) : offset; + var hasLen = lena - ofs | 0; + var copyLength = hasLen < len ? hasLen : len; + if (copyLength <= 0) { + return /* array */[]; + } else { + var result = new Array(copyLength); + for(var i = 0 ,i_finish = copyLength - 1 | 0; i <= i_finish; ++i){ + result[i] = a[ofs + i | 0]; + } + return result; + } + } +} + +function sliceToEnd(a, offset) { + var lena = a.length; + var ofs = offset < 0 ? Caml_primitive.caml_int_max(lena + offset | 0, 0) : offset; + var len = lena - ofs | 0; + var result = new Array(len); + for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){ + result[i] = a[ofs + i | 0]; + } + return result; +} + +function fill(a, offset, len, v) { + if (len > 0) { + var lena = a.length; + var ofs = offset < 0 ? Caml_primitive.caml_int_max(lena + offset | 0, 0) : offset; + var hasLen = lena - ofs | 0; + var fillLength = hasLen < len ? hasLen : len; + if (fillLength > 0) { + for(var i = ofs ,i_finish = (ofs + fillLength | 0) - 1 | 0; i <= i_finish; ++i){ + a[i] = v; + } + return /* () */0; + } else { + return 0; + } + } else { + return 0; + } +} + +function blitUnsafe(a1, srcofs1, a2, srcofs2, blitLength) { + if (srcofs2 <= srcofs1) { + for(var j = 0 ,j_finish = blitLength - 1 | 0; j <= j_finish; ++j){ + a2[j + srcofs2 | 0] = a1[j + srcofs1 | 0]; + } + return /* () */0; + } else { + for(var j$1 = blitLength - 1 | 0; j$1 >= 0; --j$1){ + a2[j$1 + srcofs2 | 0] = a1[j$1 + srcofs1 | 0]; + } + return /* () */0; + } +} + +function blit(a1, ofs1, a2, ofs2, len) { + var lena1 = a1.length; + var lena2 = a2.length; + var srcofs1 = ofs1 < 0 ? Caml_primitive.caml_int_max(lena1 + ofs1 | 0, 0) : ofs1; + var srcofs2 = ofs2 < 0 ? Caml_primitive.caml_int_max(lena2 + ofs2 | 0, 0) : ofs2; + var blitLength = Caml_primitive.caml_int_min(len, Caml_primitive.caml_int_min(lena1 - srcofs1 | 0, lena2 - srcofs2 | 0)); + if (srcofs2 <= srcofs1) { + for(var j = 0 ,j_finish = blitLength - 1 | 0; j <= j_finish; ++j){ + a2[j + srcofs2 | 0] = a1[j + srcofs1 | 0]; + } + return /* () */0; + } else { + for(var j$1 = blitLength - 1 | 0; j$1 >= 0; --j$1){ + a2[j$1 + srcofs2 | 0] = a1[j$1 + srcofs1 | 0]; + } + return /* () */0; + } +} + +function forEachU(a, f) { + for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){ + f(a[i]); + } + return /* () */0; +} + +function forEach(a, f) { + return forEachU(a, Curry.__1(f)); +} + +function mapU(a, f) { + var l = a.length; + var r = new Array(l); + for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){ + r[i] = f(a[i]); + } + return r; +} + +function map(a, f) { + return mapU(a, Curry.__1(f)); +} + +function getByU(a, p) { + var l = a.length; + var i = 0; + var r = undefined; + while(r === undefined && i < l) { + var v = a[i]; + if (p(v)) { + r = Caml_option.some(v); + } + i = i + 1 | 0; + }; + return r; +} + +function getBy(a, p) { + return getByU(a, Curry.__1(p)); +} + +function getIndexByU(a, p) { + var l = a.length; + var i = 0; + var r = undefined; + while(r === undefined && i < l) { + var v = a[i]; + if (p(v)) { + r = i; + } + i = i + 1 | 0; + }; + return r; +} + +function getIndexBy(a, p) { + return getIndexByU(a, Curry.__1(p)); +} + +function keepU(a, f) { + var l = a.length; + var r = new Array(l); + var j = 0; + for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){ + var v = a[i]; + if (f(v)) { + r[j] = v; + j = j + 1 | 0; + } + + } + r.length = j; + return r; +} + +function keep(a, f) { + return keepU(a, Curry.__1(f)); +} + +function keepWithIndexU(a, f) { + var l = a.length; + var r = new Array(l); + var j = 0; + for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){ + var v = a[i]; + if (f(v, i)) { + r[j] = v; + j = j + 1 | 0; + } + + } + r.length = j; + return r; +} + +function keepWithIndex(a, f) { + return keepWithIndexU(a, Curry.__2(f)); +} + +function keepMapU(a, f) { + var l = a.length; + var r = new Array(l); + var j = 0; + for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){ + var v = a[i]; + var match = f(v); + if (match !== undefined) { + r[j] = Caml_option.valFromOption(match); + j = j + 1 | 0; + } + + } + r.length = j; + return r; +} + +function keepMap(a, f) { + return keepMapU(a, Curry.__1(f)); +} + +function forEachWithIndexU(a, f) { + for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){ + f(i, a[i]); + } + return /* () */0; +} + +function forEachWithIndex(a, f) { + return forEachWithIndexU(a, Curry.__2(f)); +} + +function mapWithIndexU(a, f) { + var l = a.length; + var r = new Array(l); + for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){ + r[i] = f(i, a[i]); + } + return r; +} + +function mapWithIndex(a, f) { + return mapWithIndexU(a, Curry.__2(f)); +} + +function reduceU(a, x, f) { + var r = x; + for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){ + r = f(r, a[i]); + } + return r; +} + +function reduce(a, x, f) { + return reduceU(a, x, Curry.__2(f)); +} + +function reduceReverseU(a, x, f) { + var r = x; + for(var i = a.length - 1 | 0; i >= 0; --i){ + r = f(r, a[i]); + } + return r; +} + +function reduceReverse(a, x, f) { + return reduceReverseU(a, x, Curry.__2(f)); +} + +function reduceReverse2U(a, b, x, f) { + var r = x; + var len = Caml_primitive.caml_int_min(a.length, b.length); + for(var i = len - 1 | 0; i >= 0; --i){ + r = f(r, a[i], b[i]); + } + return r; +} + +function reduceReverse2(a, b, x, f) { + return reduceReverse2U(a, b, x, Curry.__3(f)); +} + +function reduceWithIndexU(a, x, f) { + var r = x; + for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){ + r = f(r, a[i], i); + } + return r; +} + +function reduceWithIndex(a, x, f) { + return reduceWithIndexU(a, x, Curry.__3(f)); +} + +function everyU(arr, b) { + var len = arr.length; + var arr$1 = arr; + var _i = 0; + var b$1 = b; + var len$1 = len; + while(true) { + var i = _i; + if (i === len$1) { + return true; + } else if (b$1(arr$1[i])) { + _i = i + 1 | 0; + continue ; + } else { + return false; + } + }; +} + +function every(arr, f) { + return everyU(arr, Curry.__1(f)); +} + +function someU(arr, b) { + var len = arr.length; + var arr$1 = arr; + var _i = 0; + var b$1 = b; + var len$1 = len; + while(true) { + var i = _i; + if (i === len$1) { + return false; + } else if (b$1(arr$1[i])) { + return true; + } else { + _i = i + 1 | 0; + continue ; + } + }; +} + +function some(arr, f) { + return someU(arr, Curry.__1(f)); +} + +function everyAux2(arr1, arr2, _i, b, len) { + while(true) { + var i = _i; + if (i === len) { + return true; + } else if (b(arr1[i], arr2[i])) { + _i = i + 1 | 0; + continue ; + } else { + return false; + } + }; +} + +function every2U(a, b, p) { + return everyAux2(a, b, 0, p, Caml_primitive.caml_int_min(a.length, b.length)); +} + +function every2(a, b, p) { + return every2U(a, b, Curry.__2(p)); +} + +function some2U(a, b, p) { + var arr1 = a; + var arr2 = b; + var _i = 0; + var b$1 = p; + var len = Caml_primitive.caml_int_min(a.length, b.length); + while(true) { + var i = _i; + if (i === len) { + return false; + } else if (b$1(arr1[i], arr2[i])) { + return true; + } else { + _i = i + 1 | 0; + continue ; + } + }; +} + +function some2(a, b, p) { + return some2U(a, b, Curry.__2(p)); +} + +function eqU(a, b, p) { + var lena = a.length; + var lenb = b.length; + if (lena === lenb) { + return everyAux2(a, b, 0, p, lena); + } else { + return false; + } +} + +function eq(a, b, p) { + return eqU(a, b, Curry.__2(p)); +} + +function cmpU(a, b, p) { + var lena = a.length; + var lenb = b.length; + if (lena > lenb) { + return 1; + } else if (lena < lenb) { + return -1; + } else { + var arr1 = a; + var arr2 = b; + var _i = 0; + var b$1 = p; + var len = lena; + while(true) { + var i = _i; + if (i === len) { + return 0; + } else { + var c = b$1(arr1[i], arr2[i]); + if (c === 0) { + _i = i + 1 | 0; + continue ; + } else { + return c; + } + } + }; + } +} + +function cmp(a, b, p) { + return cmpU(a, b, Curry.__2(p)); +} + +function partitionU(a, f) { + var l = a.length; + var i = 0; + var j = 0; + var a1 = new Array(l); + var a2 = new Array(l); + for(var ii = 0 ,ii_finish = l - 1 | 0; ii <= ii_finish; ++ii){ + var v = a[ii]; + if (f(v)) { + a1[i] = v; + i = i + 1 | 0; + } else { + a2[j] = v; + j = j + 1 | 0; + } + } + a1.length = i; + a2.length = j; + return /* tuple */[ + a1, + a2 + ]; +} + +function partition(a, f) { + return partitionU(a, Curry.__1(f)); +} + +function unzip(a) { + var l = a.length; + var a1 = new Array(l); + var a2 = new Array(l); + for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){ + var match = a[i]; + a1[i] = match[0]; + a2[i] = match[1]; + } + return /* tuple */[ + a1, + a2 + ]; +} + +export { + get , + getExn , + set , + setExn , + shuffleInPlace , + shuffle , + reverseInPlace , + reverse , + make , + range , + rangeBy , + makeByU , + makeBy , + makeByAndShuffleU , + makeByAndShuffle , + zip , + zipByU , + zipBy , + unzip , + concat , + concatMany , + slice , + sliceToEnd , + fill , + blit , + blitUnsafe , + forEachU , + forEach , + mapU , + map , + getByU , + getBy , + getIndexByU , + getIndexBy , + keepU , + keep , + keepWithIndexU , + keepWithIndex , + keepMapU , + keepMap , + forEachWithIndexU , + forEachWithIndex , + mapWithIndexU , + mapWithIndex , + partitionU , + partition , + reduceU , + reduce , + reduceReverseU , + reduceReverse , + reduceReverse2U , + reduceReverse2 , + reduceWithIndexU , + reduceWithIndex , + someU , + some , + everyU , + every , + every2U , + every2 , + some2U , + some2 , + cmpU , + cmp , + eqU , + eq , + +} +/* No side effect */ diff --git a/src/VendoredArray.re b/src/VendoredArray.re new file mode 100644 index 0000000..4956bb4 --- /dev/null +++ b/src/VendoredArray.re @@ -0,0 +1,614 @@ +/***********************************************************************/ +/* */ +/* OCaml */ +/* */ +/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */ +/* */ +/* Copyright 1996 Institut National de Recherche en Informatique et */ +/* en Automatique. All rights reserved. This file is distributed */ +/* under the terms of the GNU Library General Public License, with */ +/* the special exception on linking described in file ../LICENSE. */ +/* */ +/***********************************************************************/ + +/* Array operations */ + +external length: array('a) => int = "%array_length"; +external size: array('a) => int = "%array_length"; +external getUnsafe: (array('a), int) => 'a = "%array_unsafe_get"; +external setUnsafe: (array('a), int, 'a) => unit = "%array_unsafe_set"; +external getUndefined: (array('a), int) => Js.undefined('a) = + "%array_unsafe_get"; +external get: (array('a), int) => 'a = "%array_safe_get"; +let get = (arr, i) => + if (i >= 0 && i < length(arr)) { + Some(getUnsafe(arr, i)); + } else { + None; + }; +let getExn = (arr, i) => { + %assert + i >= 0 && i < length(arr); + getUnsafe(arr, i); +}; +let set = (arr, i, v) => + if (i >= 0 && i < length(arr)) { + setUnsafe(arr, i, v); + true; + } else { + false; + }; + +let setExn = (arr, i, v) => { + %assert + i >= 0 && i < length(arr); + setUnsafe(arr, i, v); +}; + +[@bs.set] +external truncateToLengthUnsafe: (array('a), int) => unit = "length"; +[@bs.new] +external makeUninitialized: int => array(Js.undefined('a)) = "Array"; +[@bs.new] external makeUninitializedUnsafe: int => array('a) = "Array"; + +[@bs.send] external copy: (array('a), [@bs.as 0] _) => array('a) = "slice"; + +let swapUnsafe = (xs, i, j) => { + let tmp = getUnsafe(xs, i); + setUnsafe(xs, i, getUnsafe(xs, j)); + setUnsafe(xs, j, tmp); +}; + +let shuffleInPlace = xs => { + let len = length(xs); + for (i in 0 to len - 1) + { + swapUnsafe(xs, i, Js_math.random_int(i, len)); + }; /* [i,len)*/ +}; + +let shuffle = xs => { + let result = copy(xs); + shuffleInPlace(result); /* TODO: improve*/ + result; +}; + +let reverseAux = (xs, ofs, len) => + for (i in 0 to len / 2 - 1) { + swapUnsafe(xs, ofs + i, ofs + len - i - 1); + }; + +let reverseInPlace = xs => { + let len = length(xs); + reverseAux(xs, 0, len); +}; + +let reverse = xs => { + let len = length(xs); + let result = makeUninitializedUnsafe(len); + for (i in 0 to len - 1) { + setUnsafe(result, i, getUnsafe(xs, len - 1 - i)); + }; + result; +}; + +let make = (l, f) => + if (l <= 0) { + [||]; + } else { + let res = makeUninitializedUnsafe(l); + for (i in 0 to l - 1) { + setUnsafe(res, i, f); + }; + res; + }; + +/* See #6575. We could also check for maximum array size, but this depends + on whether we create a float array or a regular one... */ +let makeByU = (l, f) => + if (l <= 0) { + [||]; + } else { + let res = makeUninitializedUnsafe(l); + for (i in 0 to l - 1) { + setUnsafe(res, i, f(. i)); + }; + res; + }; + +let makeBy = (l, f) => makeByU(l, (. a) => f(a)); + +let makeByAndShuffleU = (l, f) => { + let u = makeByU(l, f); + shuffleInPlace(u); + u; +}; + +let makeByAndShuffle = (l, f) => makeByAndShuffleU(l, (. a) => f(a)); + +let range = (start, finish) => { + let cut = finish - start; + if (cut < 0) { + [||]; + } else { + let arr = makeUninitializedUnsafe(cut + 1); + for (i in 0 to cut) { + setUnsafe(arr, i, start + i); + }; + arr; + }; +}; + +let rangeBy = (start, finish, ~step) => { + let cut = finish - start; + if (cut < 0 || step <= 0) { + [||]; + } else { + let nb = cut / step + 1; + let arr = makeUninitializedUnsafe(nb); + let cur = ref(start); + for (i in 0 to nb - 1) { + setUnsafe(arr, i, cur^); + cur := cur^ + step; + }; + arr; + }; +}; + +let zip = (xs, ys) => { + let (lenx, leny) = (length(xs), length(ys)); + let len = Pervasives.min(lenx, leny); + let s = makeUninitializedUnsafe(len); + for (i in 0 to len - 1) { + setUnsafe(s, i, (getUnsafe(xs, i), getUnsafe(ys, i))); + }; + s; +}; + +let zipByU = (xs, ys, f) => { + let (lenx, leny) = (length(xs), length(ys)); + let len = Pervasives.min(lenx, leny); + let s = makeUninitializedUnsafe(len); + for (i in 0 to len - 1) { + setUnsafe(s, i, f(. getUnsafe(xs, i), getUnsafe(ys, i))); + }; + s; +}; + +let zipBy = (xs, ys, f) => zipByU(xs, ys, (. a, b) => f(a, b)); + +let concat = (a1, a2) => { + let l1 = length(a1); + let l2 = length(a2); + let a1a2 = makeUninitializedUnsafe(l1 + l2); + for (i in 0 to l1 - 1) { + setUnsafe(a1a2, i, getUnsafe(a1, i)); + }; + for (i in 0 to l2 - 1) { + setUnsafe(a1a2, l1 + i, getUnsafe(a2, i)); + }; + a1a2; +}; + +let concatMany = arrs => { + let lenArrs = length(arrs); + let totalLen = ref(0); + for (i in 0 to lenArrs - 1) { + totalLen := totalLen^ + length(getUnsafe(arrs, i)); + }; + let result = makeUninitializedUnsafe(totalLen^); + totalLen := 0; + for (j in 0 to lenArrs - 1) { + let cur = getUnsafe(arrs, j); + for (k in 0 to length(cur) - 1) { + setUnsafe(result, totalLen^, getUnsafe(cur, k)); + incr(totalLen); + }; + }; + result; +}; + +let slice = (a, ~offset, ~len) => + if (len <= 0) { + [||]; + } else { + let lena = length(a); + let ofs = + if (offset < 0) { + Pervasives.max(lena + offset, 0); + } else { + offset; + }; + let hasLen = lena - ofs; + let copyLength = Pervasives.min(hasLen, len); + if (copyLength <= 0) { + [||]; + } else { + let result = makeUninitializedUnsafe(copyLength); + for (i in 0 to copyLength - 1) { + setUnsafe(result, i, getUnsafe(a, ofs + i)); + }; + result; + }; + }; + +let sliceToEnd = (a, offset) => { + let lena = length(a); + let ofs = + if (offset < 0) { + Pervasives.max(lena + offset, 0); + } else { + offset; + }; + let len = lena - ofs; + let result = makeUninitializedUnsafe(len); + for (i in 0 to len - 1) { + setUnsafe(result, i, getUnsafe(a, ofs + i)); + }; + result; +}; + +let fill = (a, ~offset, ~len, v) => + if (len > 0) { + let lena = length(a); + let ofs = + if (offset < 0) { + Pervasives.max(lena + offset, 0); + } else { + offset; + }; + let hasLen = lena - ofs; + let fillLength = Pervasives.min(hasLen, len); + if (fillLength > 0) { + for (i in ofs to ofs + fillLength - 1) { + setUnsafe(a, i, v); + }; + }; + }; + +let blitUnsafe = + ( + ~src as a1, + ~srcOffset as srcofs1, + ~dst as a2, + ~dstOffset as srcofs2, + ~len as blitLength, + ) => + if (srcofs2 <= srcofs1) { + for (j in 0 to blitLength - 1) { + setUnsafe(a2, j + srcofs2, getUnsafe(a1, j + srcofs1)); + }; + } else { + for (j in blitLength - 1 downto 0) { + setUnsafe(a2, j + srcofs2, getUnsafe(a1, j + srcofs1)); + }; + }; + +/* We don't need check [blitLength] since when [blitLength < 0] the + for loop will be nop + */ +let blit = + (~src as a1, ~srcOffset as ofs1, ~dst as a2, ~dstOffset as ofs2, ~len) => { + let lena1 = length(a1); + let lena2 = length(a2); + let srcofs1 = + if (ofs1 < 0) { + Pervasives.max(lena1 + ofs1, 0); + } else { + ofs1; + }; + let srcofs2 = + if (ofs2 < 0) { + Pervasives.max(lena2 + ofs2, 0); + } else { + ofs2; + }; + let blitLength = + Pervasives.min(len, Pervasives.min(lena1 - srcofs1, lena2 - srcofs2)); + /* blitUnsafe a1 srcofs1 a2 srcofs2 blitLength */ + if (srcofs2 <= srcofs1) { + for (j in 0 to blitLength - 1) { + setUnsafe(a2, j + srcofs2, getUnsafe(a1, j + srcofs1)); + }; + } else { + for (j in blitLength - 1 downto 0) { + setUnsafe(a2, j + srcofs2, getUnsafe(a1, j + srcofs1)); + }; + }; +}; + +let forEachU = (a, f) => + for (i in 0 to length(a) - 1) { + f(. getUnsafe(a, i)); + }; + +let forEach = (a, f) => forEachU(a, (. a) => f(a)); + +let mapU = (a, f) => { + let l = length(a); + let r = makeUninitializedUnsafe(l); + for (i in 0 to l - 1) { + setUnsafe(r, i, f(. getUnsafe(a, i))); + }; + r; +}; + +let map = (a, f) => mapU(a, (. a) => f(a)); + +let getByU = (a, p) => { + let l = length(a); + let i = ref(0); + let r = ref(None); + while (r^ == None && i^ < l) { + let v = getUnsafe(a, i^); + if (p(. v)) { + r := Some(v); + }; + incr(i); + }; + r^; +}; + +let getBy = (a, p) => getByU(a, (. a) => p(a)); + +let getIndexByU = (a, p) => { + let l = length(a); + let i = ref(0); + let r = ref(None); + while (r^ == None && i^ < l) { + let v = getUnsafe(a, i^); + if (p(. v)) { + r := Some(i^); + }; + incr(i); + }; + r^; +}; + +let getIndexBy = (a, p) => getIndexByU(a, (. a) => p(a)); + +let keepU = (a, f) => { + let l = length(a); + let r = makeUninitializedUnsafe(l); + let j = ref(0); + for (i in 0 to l - 1) { + let v = getUnsafe(a, i); + if (f(. v)) { + setUnsafe(r, j^, v); + incr(j); + }; + }; + truncateToLengthUnsafe(r, j^); + r; +}; + +let keep = (a, f) => keepU(a, (. a) => f(a)); + +let keepWithIndexU = (a, f) => { + let l = length(a); + let r = makeUninitializedUnsafe(l); + let j = ref(0); + for (i in 0 to l - 1) { + let v = getUnsafe(a, i); + if (f(. v, i)) { + setUnsafe(r, j^, v); + incr(j); + }; + }; + truncateToLengthUnsafe(r, j^); + r; +}; + +let keepWithIndex = (a, f) => keepWithIndexU(a, (. a, i) => f(a, i)); + +let keepMapU = (a, f) => { + let l = length(a); + let r = makeUninitializedUnsafe(l); + let j = ref(0); + for (i in 0 to l - 1) { + let v = getUnsafe(a, i); + switch (f(. v)) { + | None => () + | Some(v) => + setUnsafe(r, j^, v); + incr(j); + }; + }; + truncateToLengthUnsafe(r, j^); + r; +}; + +let keepMap = (a, f) => keepMapU(a, (. a) => f(a)); + +let forEachWithIndexU = (a, f) => + for (i in 0 to length(a) - 1) { + f(. i, getUnsafe(a, i)); + }; + +let forEachWithIndex = (a, f) => forEachWithIndexU(a, (. a, b) => f(a, b)); + +let mapWithIndexU = (a, f) => { + let l = length(a); + let r = makeUninitializedUnsafe(l); + for (i in 0 to l - 1) { + setUnsafe(r, i, f(. i, getUnsafe(a, i))); + }; + r; +}; + +let mapWithIndex = (a, f) => mapWithIndexU(a, (. a, b) => f(a, b)); + +let reduceU = (a, x, f) => { + let r = ref(x); + for (i in 0 to length(a) - 1) { + r := f(. r^, getUnsafe(a, i)); + }; + r^; +}; + +let reduce = (a, x, f) => reduceU(a, x, (. a, b) => f(a, b)); + +let reduceReverseU = (a, x, f) => { + let r = ref(x); + for (i in length(a) - 1 downto 0) { + r := f(. r^, getUnsafe(a, i)); + }; + r^; +}; + +let reduceReverse = (a, x, f) => reduceReverseU(a, x, (. a, b) => f(a, b)); + +let reduceReverse2U = (a, b, x, f) => { + let r = ref(x); + let len = Pervasives.min(length(a), length(b)); + for (i in len - 1 downto 0) { + r := f(. r^, getUnsafe(a, i), getUnsafe(b, i)); + }; + r^; +}; + +let reduceReverse2 = (a, b, x, f) => + reduceReverse2U(a, b, x, (. a, b, c) => f(a, b, c)); + +let reduceWithIndexU = (a, x, f) => { + let r = ref(x); + for (i in 0 to length(a) - 1) { + r := f(. r^, getUnsafe(a, i), i); + }; + r^; +}; + +let reduceWithIndex = (a, x, f) => + reduceWithIndexU(a, x, (. a, b, c) => f(a, b, c)); + +let rec everyAux = (arr, i, b, len) => + if (i == len) { + true; + } else if (b(. getUnsafe(arr, i))) { + everyAux(arr, i + 1, b, len); + } else { + false; + }; + +let rec someAux = (arr, i, b, len) => + if (i == len) { + false; + } else if (b(. getUnsafe(arr, i))) { + true; + } else { + someAux(arr, i + 1, b, len); + }; + +let everyU = (arr, b) => { + let len = length(arr); + everyAux(arr, 0, b, len); +}; + +let every = (arr, f) => everyU(arr, (. b) => f(b)); + +let someU = (arr, b) => { + let len = length(arr); + someAux(arr, 0, b, len); +}; +let some = (arr, f) => someU(arr, (. b) => f(b)); + +let rec everyAux2 = (arr1, arr2, i, b, len) => + if (i == len) { + true; + } else if (b(. getUnsafe(arr1, i), getUnsafe(arr2, i))) { + everyAux2(arr1, arr2, i + 1, b, len); + } else { + false; + }; + +let rec someAux2 = (arr1, arr2, i, b, len) => + if (i == len) { + false; + } else if (b(. getUnsafe(arr1, i), getUnsafe(arr2, i))) { + true; + } else { + someAux2(arr1, arr2, i + 1, b, len); + }; + +let every2U = (a, b, p) => + everyAux2(a, b, 0, p, Pervasives.min(length(a), length(b))); + +let every2 = (a, b, p) => every2U(a, b, (. a, b) => p(a, b)); + +let some2U = (a, b, p) => + someAux2(a, b, 0, p, Pervasives.min(length(a), length(b))); + +let some2 = (a, b, p) => some2U(a, b, (. a, b) => p(a, b)); + +let eqU = (a, b, p) => { + let lena = length(a); + let lenb = length(b); + if (lena == lenb) { + everyAux2(a, b, 0, p, lena); + } else { + false; + }; +}; + +let eq = (a, b, p) => eqU(a, b, (. a, b) => p(a, b)); + +let rec everyCmpAux2 = (arr1, arr2, i, b, len) => + if (i == len) { + 0; + } else { + let c = b(. getUnsafe(arr1, i), getUnsafe(arr2, i)); + if (c == 0) { + everyCmpAux2(arr1, arr2, i + 1, b, len); + } else { + c; + }; + }; + +let cmpU = (a, b, p) => { + let lena = length(a); + let lenb = length(b); + if (lena > lenb) { + 1; + } else if (lena < lenb) { + (-1); + } else { + everyCmpAux2(a, b, 0, p, lena); + }; +}; + +let cmp = (a, b, p) => cmpU(a, b, (. a, b) => p(a, b)); + +let partitionU = (a, f) => { + let l = length(a); + let i = ref(0); + let j = ref(0); + let a1 = makeUninitializedUnsafe(l); + let a2 = makeUninitializedUnsafe(l); + for (ii in 0 to l - 1) { + let v = getUnsafe(a, ii); + if (f(. v)) { + setUnsafe(a1, i^, v); + incr(i); + } else { + setUnsafe(a2, j^, v); + incr(j); + }; + }; + truncateToLengthUnsafe(a1, i^); + truncateToLengthUnsafe(a2, j^); + (a1, a2); +}; + +let partition = (a, f) => partitionU(a, (. x) => f(x)); + +let unzip = a => { + let l = length(a); + let a1 = makeUninitializedUnsafe(l); + let a2 = makeUninitializedUnsafe(l); + for (i in 0 to l - 1) { + let (v1, v2) = getUnsafe(a, i); + setUnsafe(a1, i, v1); + setUnsafe(a2, i, v2); + }; + (a1, a2); +}; diff --git a/src/VendoredArray.rei b/src/VendoredArray.rei new file mode 100644 index 0000000..958f9f6 --- /dev/null +++ b/src/VendoredArray.rei @@ -0,0 +1,678 @@ +/***********************************************************************/ +/* */ +/* OCaml */ +/* */ +/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */ +/* */ +/* Copyright 1996 Institut National de Recherche en Informatique et */ +/* en Automatique. All rights reserved. This file is distributed */ +/* under the terms of the GNU Library General Public License, with */ +/* the special exception on linking described in file ../LICENSE. */ +/* */ +/***********************************************************************/ +/* Adapted significantly by Authors of BuckleScript */ + +/** {!Belt.Array} + Utililites for Array functions +*/; + +/** [length xs] return the size of the array */ +external length: array('a) => int = "%array_length"; + +/** {b See} {!length} */ external size: array('a) => int = "%array_length"; + +/** + [get arr i] + + If [i <= 0 <= length arr];returns [Some value] where [value] is the item at index [i] + If [i] is out of range;returns [None] + + @example {[ + Belt.Array.get [|"a";"b";"c"|] 0 = Some "a";; + Belt.Array.get [|"a";"b";"c"|] 3 = None;; + Belt.Array.get [|"a";"b";"c"|] (-1) = None;; + ]} +*/ + +let get: (array('a), int) => option('a); + +/** [getExn arr i] + + {b raise} an exception if [i] is out of range;otherwise return the value at index [i] in [arr] +*/ + +let getExn: (array('a), int) => 'a; + +/** [getUnsafe arr i] + + {b Unsafe} + + no bounds checking;this would cause type error + if [i] does not stay within range +*/ +external getUnsafe: (array('a), int) => 'a = "%array_unsafe_get"; + +/** [getUndefined arr i] + + It does the samething in the runtime as {!getUnsafe}; + it is {i type safe} since the return type still track whether it is + in range or not +*/ +external getUndefined: (array('a), int) => Js.undefined('a) = + "%array_unsafe_get"; + +/** [set arr n x] modifies [arr] in place; + it replaces the nth element of [arr] with [x] + @return false means not updated due to out of range +*/ + +let set: (array('a), int, 'a) => bool; + +/** [setExn arr i x] + {b raise} an exception if [i] is out of range +*/ + +let setExn: (array('a), int, 'a) => unit; + +external setUnsafe: (array('a), int, 'a) => unit = "%array_unsafe_set"; + +/** [shuffleInPlace arr] randomly re-orders the items in [arr] */ + +let shuffleInPlace: array('a) => unit; + +/** [shuffle xs] + @return a fresh array with items in original array randomly shuffled */ + +let shuffle: array('a) => array('a); + +/** [reverseInPlace arr] reverses items in [arr] in place + + @example {[ + let arr = [|10;11;12;13;14|];; + let () = reverseInPlace arr;; + arr = [|14;13;12;11;10|];; + ]} +*/ + +let reverseInPlace: array('a) => unit; + +/** [reverse arr] + @return a fresh array with items in [arr] in reverse order + + @example {[ + reverse [|10;11;12;13;14|] = [|14;13;12;11;10|];; + ]} +*/ + +let reverse: array('a) => array('a); + +/** + [makeUninitialized n] creates an array of length [n] filled with the undefined value. + You must specify the type of data that will eventually fill the array. + + @example {[ + let arr: string Js.undefined array = makeUninitialized 5;; + getExn arr 0 = Js.undefined;; + ]} +*/ +[@bs.new] +external makeUninitialized: int => array(Js.undefined('a)) = "Array"; + +/** [makeUninitializedUnsafe n] + + {b Unsafe} + + @example {[ + let arr = Belt.Array.makeUninitializedUnsafe 5;; + let () = Js.log(Belt.Array.getExn arr 0);; (* undefined *) + Belt.Array.setExn arr 0 "example";; + let () = Js.log(Belt.Array.getExn arr 0 = "example");; + ]} +*/ +[@bs.new] +external makeUninitializedUnsafe: int => array('a) = "Array"; + +/** [make n e] + return an array of size [n] filled with value [e] + @return an empty array when [n] is negative. +*/ + +let make: (int, 'a) => array('a); + +/** [range start finish] create an inclusive array + @example {[ + range 0 3 = [|0;1;2;3|];; + range 3 0 = [||] ;; + range 3 3 = [|3|];; + ]} +*/ + +let range: (int, int) => array(int); + +/** [range start finish] create an inclusive array + @example {[ + range 0 3 = [|0;1;2;3|];; + range 3 0 = [||] ;; + range 3 3 = [|3|];; + ]} +*/ +/** [rangeBy start finish ~step] + + @return empty array when step is 0 or negative + it also return empty array when [start > finish] + + @example {[ + rangeBy 0 10 ~step:3 = [|0;3;6;9|];; + rangeBy 0 12 ~step:3 = [|0;3;6;9;12|];; + rangeBy 33 0 ~step:1 = [||];; + rangeBy 33 0 ~step:(-1) = [||];; + rangeBy 3 12 ~step:(-1) = [||];; + rangeBy 3 3 ~step:0 = [||] ;; + rangeBy 3 3 ~step:(1) = [|3|] ;; + ]} +*/ + +let rangeBy: (int, int, ~step: int) => array(int); + +let makeByU: (int, (. int) => 'a) => array('a); +/** [makeBy n f] + + return an empty array when [n] is negative + return an array of size [n] populated by [f i] start from [0] to [n - 1] + + @example {[ + makeBy 5 (fun i -> i) = [|0;1;2;3;4|];; + makeBy 5 (fun i -> i * i) = [|0;1;4;9;16|] + ]} +*/ + +let makeBy: (int, int => 'a) => array('a); + +let makeByAndShuffleU: (int, (. int) => 'a) => array('a); +/** [makeByAndShuffle n f] + + Equivalent to [shuffle (makeBy n f)] +*/ + +let makeByAndShuffle: (int, int => 'a) => array('a); + +/** [zip a b] + + Create an array of pairs from corresponding elements of [a] and [b]. + Stop with the shorter array + + @example {[ + zip [|1;2|] [|3;4;5|] = [|(1, 3);(2, 4)|] + ]} + */ + +let zip: (array('a), array('b)) => array(('a, 'b)); + +let zipByU: (array('a), array('b), (. 'a, 'b) => 'c) => array('c); +/** + [zipBy xs ys f] + + Create an array by applying [f] to corresponding elements of [xs] and [ys] + Stops with shorter array + + Equivalent to [map (zip xs ys) (fun (a,b) -> f a b) ] + + @example {[ + zipBy [|1;2;3|] [|4;5|] (fun a b -> 2 * a + b) = [|6;9|];; + ]} + */ + +let zipBy: (array('a), array('b), ('a, 'b) => 'c) => array('c); + +/** [unzip a] takes an array of pairs and creates a pair of arrays. The first array contains all the first items of the pairs; the second array contains all the second items. + + @example {[ + unzip [|(1,2) ; (3,4)|] = ([|1;3|], [|2;4|]);; + unzip [|(1,2) ; (3,4) ; (5,6) ; (7,8)|] = ([|1;3;5;7|], [|2;4;6;8|]);; + ]} +*/ + +let unzip: array(('a, 'b)) => (array('a), array('b)); + +/** [concat xs ys] + + @return a fresh array containing the + concatenation of the arrays [v1] and [v2];so even if [v1] or [v2] + is empty;it can not be shared + + @example {[ + concat [|1;2;3|] [|4;5|] = [|1;2;3;4;5|];; + concat [| |] [|"a";"b";"c"|] = [|"a";"b";"c"|];; + ]} +*/ + +let concat: (array('a), array('a)) => array('a); + +/** + [concatMany xss] + + @return a fresh array as the concatenation of [xss] (an array of arrays) + + @example {[ + concatMany [| [|1;2;3|]; [|4;5;6|]; [|7;8|] |] = [|1;2;3;4;5;6;7;8|];; + ]} +*/ + +let concatMany: array(array('a)) => array('a); + +/** [slice xs offset len] creates a new array with the [len] elements of [xs] starting at [offset] for + + [offset] can be negative;and is evaluated as [length xs - offset] + [slice xs -1 1] means get the last element as a singleton array + + [slice xs (-len) len] will return a copy of the array + + if the array does not have enough data;[slice] extracts through + the end of sequence. + + if [len] is negative;returns the empty array. + + @example {[ + slice [|10;11;12;13;14;15;16|] ~offset: 2 ~len: 3 = [|12;13;14|];; + slice [|10;11;12;13;14;15;16|] ~offset: (-4) ~len: 3 = [|13;14;15|];; + slice [|10;11;12;13;14;15;16|] ~offset:4 ~len:9 = [|14;15;16|];; + ]} +*/ + +let slice: (array('a), ~offset: int, ~len: int) => array('a); + +/** [sliceToEnd xs offset] creates a new array with the elements of [xs] starting at [offset] + + [offset] can be negative;and is evaluated as [length xs - offset] + [sliceToEnd xs -1] means get the last element as a singleton array + + [sliceToEnd xs 0] will return a copy of the array + + @example {[ + sliceToEnd [|10;11;12;13;14;15;16|] 2 = [|12;13;14;15;16|];; + sliceToEnd [|10;11;12;13;14;15;16|] (-4) = [|13;14;15;16|];; + ]} +*/ + +let sliceToEnd: (array('a), int) => array('a); + +/** [copy a] + + @return a copy of [a];that is;a fresh array + containing the same elements as [a]. +*/ +[@bs.send] +external copy: (array('a), [@bs.as 0] _) => array('a) = "slice"; + +/** [fill arr ~offset ~len x] + + Modifies [arr] in place, + storing [x] in elements number [offset] to [offset + len - 1]. + + [offset] can be negative;and is evaluated as [length arr - offset] + + [fill arr ~offset:(-1) ~len:1] means fill the last element, + if the array does not have enough data;[fill] will ignore it + + @example {[ + + let arr = makeBy 5 (fun i -> i) ;; + fill arr ~offset:2 ~len:2 9 ;; + arr = [|0;1;9;9;4|];; + fill arr ~offset:7 ~len:2 8;; + arr = [|0;1;9;9;4|];; + ]} + */ + +let fill: (array('a), ~offset: int, ~len: int, 'a) => unit; + +/** [blit ~src:v1 ~srcOffset:o1 ~dst:v2 ~dstOffset:o2 ~len] + + copies [len] elements + from array [v1];starting at element number [o1];to array [v2], + starting at element number [o2]. + + It works correctly even if + [v1] and [v2] are the same array;and the source and + destination chunks overlap. + + [offset] can be negative;[-1] means [len - 1];if [len + offset] is still + negative;it will be set as 0 + + For each of the examples;presume that [v1 = \[|10;11;12;13;14;15;16;17|\]] and + [v2 = \[|20;21;22;23;24;25;26;27|\]]. The result shown is the content of the destination array. + + @example {[ + Belt.Array.blit ~src: v1 ~srcOffset: 4 ~dst: v2 ~dstOffset: 2 ~len: 3 |. + [|20;21;14;15;16;25;26;27|] + Belt.Array.blit ~src: v1 ~srcOffset: 4 ~dst: v1 ~dstOffset: 2 ~len: 3 |. + [|10;11;14;15;16;15;16;17|] + ]} + +*/ + +let blit: + ( + ~src: array('a), + ~srcOffset: int, + ~dst: array('a), + ~dstOffset: int, + ~len: int + ) => + unit; + +/** + {b Unsafe} blit without bounds checking +*/ + +let blitUnsafe: + ( + ~src: array('a), + ~srcOffset: int, + ~dst: array('a), + ~dstOffset: int, + ~len: int + ) => + unit; + +let forEachU: (array('a), (. 'a) => unit) => unit; +/** [forEach xs f] + + Call [f] on each element of [xs] from the beginning to end. [f] returns [unit];so no + new array is created. Use [forEach] when you are primarily concerned with repetitively + creating side effects. + + @example {[ + forEach [|"a";"b";"c"|] (fun x -> Js.log("Item: " ^ x));; + (* prints: + Item: a + Item: b + Item: c + *) + + let total = ref 0;; + forEach [|1;2;3;4|] (fun x -> total := !total + x);; + !total = 1 + 2 + 3 + 4;; + + ]} +*/ + +let forEach: (array('a), 'a => unit) => unit; + +let mapU: (array('a), (. 'a) => 'b) => array('b); +/** [map xs f ] + + @return a new array by calling [f] for each element of [xs] from + the beginning to end + + @example {[ + map [|1;2|] (fun x-> x + 1) = [|3;4|] + ]} + +*/ + +let map: (array('a), 'a => 'b) => array('b); + +let getByU: (array('a), (. 'a) => bool) => option('a); +/** [getBy xs p] returns [Some value] for the first value in [xs] that satisifies the predicate function [p]; returns [None] if no element satisifies the function. + + @example {[ + getBy [|1;4;3;2|] (fun x -> x mod 2 = 0) = Some 4 + getBy [|15;13;11|] (fun x -> x mod 2 = 0) = None + ]} +*/ + +let getBy: (array('a), 'a => bool) => option('a); + +let getIndexByU: (array('a), (. 'a) => bool) => option(int); +/** [getIndexBy xs p] returns [Some index] for the first value in [xs] that satisifies the predicate function [p]; returns [None] if no element satisifies the function. + + @example {[ + getIndexBy [|1;4;3;2|] (fun x -> x mod 2 = 0) = Some 1 + getIndexBy [|15;13;11|] (fun x -> x mod 2 = 0) = None + ]} +*/ + +let getIndexBy: (array('a), 'a => bool) => option(int); + +let keepU: (array('a), (. 'a) => bool) => array('a); +/** [keep xs p ] + @return a new array that keep all elements satisfy [p] + + @example {[ + keep [|1;2;3|] (fun x -> x mod 2 = 0) = [|2|] + ]} +*/ + +let keep: (array('a), 'a => bool) => array('a); + +let keepWithIndexU: (array('a), (. 'a, int) => bool) => array('a); +/** [keepWithIndex xs p ] + @return a new array that keep all elements satisfy [p] + + @example {[ + keepWithIndex [|1;2;3|] (fun _x i -> i = 1) = [|2|] + ]} +*/ + +let keepWithIndex: (array('a), ('a, int) => bool) => array('a); + +let keepMapU: (array('a), (. 'a) => option('b)) => array('b); +/** [keepMap xs p] + @return a new array that keep all elements that return a non-None applied [p] + + @example {[ + keepMap [|1;2;3|] (fun x -> if x mod 2 then Some x else None) + = [| 2 |] + ]} +*/ + +let keepMap: (array('a), 'a => option('b)) => array('b); + +let forEachWithIndexU: (array('a), (. int, 'a) => unit) => unit; +/** [forEachWithIndex xs f] + + The same as {!forEach};except that [f] is supplied two arguments: + the index starting from 0 and the element from [xs] + + @example {[ + + forEach [|"a";"b";"c"|] (fun i x -> Js.log("Item " ^ (string_of_int i) ^ " is " ^ x));; + (* prints: + Item 0 is a + Item 1 is b + Item 2 is cc + *) + + let total = ref 0 ;; + forEachWithIndex [|10;11;12;13|] (fun i x -> total := !total + x + i);; + !total = 0 + 10 + 1 + 11 + 2 + 12 + 3 + 13;; + ]} + +*/ + +let forEachWithIndex: (array('a), (int, 'a) => unit) => unit; + +let mapWithIndexU: (array('a), (. int, 'a) => 'b) => array('b); +/** [mapWithIndex xs f ] + + [mapWithIndex xs f] applies [f] to each element of [xs]. Function [f] takes two arguments: + the index starting from 0 and the element from [xs]. + + @example {[ + mapWithIndex [|1;2;3|] (fun i x -> i + x) = + [|0 + 1; 1 + 2; 2 + 3|] + ]} +*/ + +let mapWithIndex: (array('a), (int, 'a) => 'b) => array('b); + +let partitionU: (array('a), (. 'a) => bool) => (array('a), array('a)); +/** [partition f a] split array into tuple of two arrays based on predicate f; first of tuple where predicate cause true, second where predicate cause false + + @example {[ + partition [|1;2;3;4;5|] (fun x -> x mod 2 = 0 ) = ([|2;4|], [|1;2;3|]);; + partition [|1;2;3;4;5|] (fun x -> x mod 2 <> 0 ) = ([|1;2;3|], [|2;4|]);; + ]} +*/ + +let partition: (array('a), 'a => bool) => (array('a), array('a)); + +let reduceU: (array('b), 'a, (. 'a, 'b) => 'a) => 'a; +/** [reduce xs init f] + + Applies [f] to each element of [xs] from beginning to end. Function [f] has two parameters: the item + from the list and an “accumulator”;which starts with a value of [init]. [reduce] + returns the final value of the accumulator. + + @example {[ + reduce [|2;3;4|] 1 (+) = 10;; + reduce [|"a";"b";"c";"d"|] "" (^) = "abcd";; + ]} + +*/ + +let reduce: (array('b), 'a, ('a, 'b) => 'a) => 'a; + +let reduceReverseU: (array('b), 'a, (. 'a, 'b) => 'a) => 'a; +/** [reduceReverse xs init f] + + Works like {!reduce};except that function [f] is applied to each item of [xs] from the last + back to the first. + + @example {[ + reduceReverse [|"a";"b";"c";"d"|] "" (^) = "dcba";; + ]} +*/ + +let reduceReverse: (array('b), 'a, ('a, 'b) => 'a) => 'a; + +let reduceReverse2U: (array('a), array('b), 'c, (. 'c, 'a, 'b) => 'c) => 'c; +/** + [reduceReverse2 xs ys init f] + Reduces two arrays [xs] and [ys];taking items starting at [min (length xs) (length ys)] + down to and including zero. + + @example {[ + reduceReverse2 [|1;2;3|] [|1;2|] 0 (fun acc x y -> acc + x + y) = 6 + ]} +*/ + +let reduceReverse2: (array('a), array('b), 'c, ('c, 'a, 'b) => 'c) => 'c; + +let reduceWithIndexU: (array('a), 'b, (. 'b, 'a, int) => 'b) => 'b; +/** [reduceWithIndex xs f] + + Applies [f] to each element of [xs] from beginning to end. Function [f] has three parameters: the item + from the array and an “accumulator”, which starts with a value of [init] and the index of each element. [reduceWithIndex] + returns the final value of the accumulator. + + @example {[ + reduceWithIndex [|1;2;3;4|] 0 (fun acc x i -> acc + x + i) = 16; + ]} +*/ + +let reduceWithIndex: (array('a), 'b, ('b, 'a, int) => 'b) => 'b; + +let someU: (array('a), (. 'a) => bool) => bool; +/** [some xs p] + + @return true if at least one of the elements in [xs] satifies [p];where [p] is a {i predicate}: a function taking + an element and returning a [bool]. + + @example {[ + some [|2; 3; 4|] (fun x -> x mod 2 = 1) = true;; + some [|-1; -3; -5|] (fun x -> x > 0) = false;; + ]} +*/ + +let some: (array('a), 'a => bool) => bool; + +let everyU: (array('a), (. 'a) => bool) => bool; +/** [every xs p] + + @return true if all elements satisfy [p];where [p] is a {i predicate}: a function taking + an element and returning a [bool]. + + @example {[ + every [|1; 3; 5|] (fun x -> x mod 2 = 1) = true;; + every [|1; -3; 5|] (fun x -> x > 0) = false;; + ]} +*/ + +let every: (array('a), 'a => bool) => bool; + +let every2U: (array('a), array('b), (. 'a, 'b) => bool) => bool; +/** [every2 xs ys p] returns true if [p xi yi] is true for all pairs of elements + up to the shorter length (i.e. [min (length xs) (length ys)]) + @example {[ + every2 [|1;2;3|] [|0;1|] (>) = true;; + every2 [||] [|1|] (fun x y -> x > y) = true;; + every2 [|2;3|] [|1|] (fun x y -> x > y) = true;; + every2 [|0;1|] [|5;0|] (fun x y -> x > y) = false; + ]} +*/ + +let every2: (array('a), array('b), ('a, 'b) => bool) => bool; + +let some2U: (array('a), array('b), (. 'a, 'b) => bool) => bool; +/** [some2 xs ys p] returns true if [p xi yi] is true for any pair of elements + up to the shorter length (i.e. [min (length xs) (length ys)]) + + @example {[ + some2 [|0;2|] [|1;0;3|] (>) = true ;; + (some2 [||] [|1|] (fun x y -> x > y)) = false;; + (some2 [|2;3|] [|1;4|] (fun x y -> x > y)) = true;; + ]} +*/ + +let some2: (array('a), array('b), ('a, 'b) => bool) => bool; + +let cmpU: (array('a), array('a), (. 'a, 'a) => int) => int; +/** [cmp xs ys f] + + - Compared by length if [length xs <> length ys];returning -1 if[length xs < length ys] or 1 if [length xs > length ys] + - Otherwise compare one by one [f x y]. [f] returns + - a negative number if [x] is “less than” [y] + - zero if [x] is “equal to” [y] + - a positive number if [x] is “greater than” [y] + - The comparison returns the first non-zero result of [f];or zero if [f] returns zero for all [x] and [y]. + + @example {[ + cmp [|1; 3; 5|] [|1; 4; 2|] (fun a b -> compare a b) = -1;; + cmp [|1; 3; 5|] [|1; 2; 3|] (fun a b -> compare a b) = 1;; + cmp [|1; 3; 5|] [|1; 3; 5|] (fun a b -> compare a b) = 0;; + ]} +*/ + +let cmp: (array('a), array('a), ('a, 'a) => int) => int; + +let eqU: (array('a), array('a), (. 'a, 'a) => bool) => bool; +/** [eq xs ys] + + - return false if length is not the same + - otherwise compare items one by one using [f xi yi];and return true if all results are true;false otherwise + + @example {[ + eq [|1; 2; 3|] [|-1; -2; -3|] (fun a b -> abs a = abs b) = true + ]} +*/ + +let eq: (array('a), array('a), ('a, 'a) => bool) => bool; + +/** {b Unsafe} + [truncateToLengthUnsafe xs n] sets length of array [xs] to [n]. + + If [n] is greater than the length of [xs];the extra elements are set to [Js.Null_undefined.null] + + If [n] is less than zero;raises a [RangeError]. + + @example {[ + let arr = [|"ant";"bee";"cat";"dog";"elk"|];; + let () = truncateToLengthUnsafe arr 3;; + arr = [|"ant";"bee";"cat"|];; + ]} + +*/ +[@bs.set] +external truncateToLengthUnsafe: (array('a), int) => unit = "length";