Hazel reports · ^report(46) · the Witch's picture · one page per output · updated 2026-11-08 08:03 MST
The Witch's picture view: ReScript 12.3.1's output
One of six pages, one per version of the Witch slide's ^picture view (^report(46), a view): Hazel · hazel-jit's output · ReScript 12.3.1's output · Melange 5.0.0's output · js_of_ocaml 5.9.1's output · JavaScript by hand.
ReScript calls toFixed directly, turns grow_all, bounds and framing into while loops, reads a vertex's point as v.at, and makes each Html node an object with a string tag, {TAG: "Node", _0, _1, _2}. Since both of the shape's constructors carry points in the same place, verticesOf is just s.points.
ReScript source (Witch.res, the view as the Hazel is written)
// ^report(46): the Witch slide's ^picture view, in ReScript as the Hazel is written (witch.hz, Drawing.^picture) // the picture, as the slide's types type point = (float, float) type label = Plain | Anchor(string) type vertex = {at: point, label: label} type stroke = {width: float, color: string} type shape = | Polygon({name: string, points: list<vertex>, stroke: stroke, fill: string}) | Path({name: string, points: list<vertex>, stroke: stroke}) type picture = list<shape> // what a view returns: Hazel's Html and Attr, as far as this view uses them type attr = Create(string, string) | Width(string) | Height(string) | Style(list<(string, string)>) type rec html = Node(string, list<attr>, list<html>) | Div(list<attr>, list<html>) | Text(string) // the view let num = x => Float.toFixed(x, ~digits=1) let rec coords = (vs: list<vertex>) => switch vs { | list{} => "" | list{v, ...tl} => let (x, y) = v.at Float.toFixed(x, ~digits=1) ++ "," ++ Float.toFixed(y, ~digits=1) ++ " " ++ coords(tl) } let shapeNode = s => switch s { | Polygon(p) => Node( "polygon", list{ Create("points", coords(p.points)), Create("fill", p.fill), Create("stroke", p.stroke.color), Create("stroke-width", num(p.stroke.width)), Create("stroke-linejoin", "round"), }, list{}, ) | Path(p) => Node( "polyline", list{ Create("points", coords(p.points)), Create("fill", "none"), Create("stroke", p.stroke.color), Create("stroke-width", num(p.stroke.width)), Create("stroke-linejoin", "round"), Create("stroke-linecap", "round"), }, list{}, ) } let rec shapeNodes = (ss: picture) => switch ss { | list{} => list{} | list{s, ...tl} => list{shapeNode(s), ...shapeNodes(tl)} } let verticesOf = s => switch s { | Polygon(p) => p.points | Path(p) => p.points } let grow = ((x0, y0, x1, y1), v: vertex) => { let (x, y) = v.at ( x -. 12. < x0 ? x -. 12. : x0, y -. 12. < y0 ? y -. 12. : y0, x +. 12. > x1 ? x +. 12. : x1, y +. 12. > y1 ? y +. 12. : y1, ) } let rec growAll = (b, vs) => switch vs { | list{} => b | list{v, ...tl} => growAll(grow(b, v), tl) } let rec bounds = (b, ss: picture) => switch ss { | list{} => b | list{s, ...tl} => bounds(growAll(b, verticesOf(s)), tl) } let rec framing = (ss: picture) => switch ss { | list{} => list{} | list{s, ...tl} => switch s { | Path(p) => p.name == "frame" ? list{s} : framing(tl) | _ => framing(tl) } } let viewBox = (pic: picture) => { let boxed = switch framing(pic) { | list{} => pic | f => f } let start = switch boxed { | list{} => (0., 0., 0., 0.) | list{s, ..._} => switch verticesOf(s) { | list{} => (0., 0., 0., 0.) | list{v, ..._} => let (x, y) = v.at (x, y, x, y) } } let (x0, y0, x1, y1) = bounds(start, boxed) let (w, h) = (x1 -. x0, y1 -. y0) let (w2, h2) = w *. 440. > h *. 320. ? (w, w *. 440. /. 320.) : (h *. 320. /. 440., h) num(x0 -. (w2 -. w) /. 2.) ++ " " ++ num(y0 -. (h2 -. h) /. 2.) ++ " " ++ num(w2) ++ " " ++ num(h2) } // the view's work once its splice is evaluated: Hazel's `case r | Some(Val(pic)) => ... | _ => ...` let view = (r: option<picture>) => switch r { | Some(pic) => Node( "svg", list{ Create("viewBox", viewBox(pic)), Width("320"), Height("440"), Style(list{("border", "1px solid #ccc"), ("background", "#fafafa")}), }, shapeNodes(pic), ) | None => Div( list{Style(list{("font-family", "var(--code-font)"), ("font-size", "0.8em")})}, list{Text("no picture yet")}, ) } // (not timed) the picture from the test's JSON, and a drawing as markup, to compare with hazel-jit's type jsShape = { kind: string, name: string, points: array<(float, float, Nullable.t<string>)>, stroke: stroke, fill: Nullable.t<string>, } let pictureOf = (shapes: array<jsShape>): picture => shapes ->Array.map(s => { let points = s.points ->Array.map(((x, y, l)) => { at: (x, y), label: switch Nullable.toOption(l) { | Some(n) => Anchor(n) | None => Plain }, }) ->List.fromArray s.kind == "Polygon" ? Polygon({name: s.name, points, stroke: s.stroke, fill: Nullable.getOr(s.fill, "")}) : Path({name: s.name, points, stroke: s.stroke}) }) ->List.fromArray let esc = s => s ->String.replaceAll("&", "&") ->String.replaceAll("\"", """) ->String.replaceAll("<", "<") let attrMarkup = a => switch a { | Create(k, v) => " " ++ k ++ "=\"" ++ esc(v) ++ "\"" | Width(v) => " width=\"" ++ esc(v) ++ "\"" | Height(v) => " height=\"" ++ esc(v) ++ "\"" | Style(kvs) => " style=\"" ++ esc(kvs->List.map(((k, v)) => k ++ ": " ++ v)->List.toArray->Array.join(";")) ++ "\"" } let rec markup = h => switch h { | Node(tag, attrs, kids) => "<" ++ tag ++ attrs->List.map(attrMarkup)->List.toArray->Array.join("") ++ ">" ++ kids->List.map(markup)->List.toArray->Array.join("") ++ "</" ++ tag ++ ">" | Div(attrs, kids) => markup(Node("div", attrs, kids)) | Text(s) => esc(s) }
ReScript 12.3.1's JavaScript (Witch.res.js)
// Generated by ReScript, PLEASE EDIT WITH CARE import * as Stdlib_List from "@rescript/runtime/lib/es6/Stdlib_List.js"; import * as Stdlib_Nullable from "@rescript/runtime/lib/es6/Stdlib_Nullable.js"; function num(x) { return x.toFixed(1); } function coords(vs) { if (vs === 0) { return ""; } let match = vs.hd.at; return match[0].toFixed(1) + "," + match[1].toFixed(1) + " " + coords(vs.tl); } function shapeNode(s) { if (s.TAG === "Polygon") { return { TAG: "Node", _0: "polygon", _1: { hd: { TAG: "Create", _0: "points", _1: coords(s.points) }, tl: { hd: { TAG: "Create", _0: "fill", _1: s.fill }, tl: { hd: { TAG: "Create", _0: "stroke", _1: s.stroke.color }, tl: { hd: { TAG: "Create", _0: "stroke-width", _1: s.stroke.width.toFixed(1) }, tl: { hd: { TAG: "Create", _0: "stroke-linejoin", _1: "round" }, tl: /* [] */0 } } } } }, _2: /* [] */0 }; } else { return { TAG: "Node", _0: "polyline", _1: { hd: { TAG: "Create", _0: "points", _1: coords(s.points) }, tl: { hd: { TAG: "Create", _0: "fill", _1: "none" }, tl: { hd: { TAG: "Create", _0: "stroke", _1: s.stroke.color }, tl: { hd: { TAG: "Create", _0: "stroke-width", _1: s.stroke.width.toFixed(1) }, tl: { hd: { TAG: "Create", _0: "stroke-linejoin", _1: "round" }, tl: { hd: { TAG: "Create", _0: "stroke-linecap", _1: "round" }, tl: /* [] */0 } } } } } }, _2: /* [] */0 }; } } function shapeNodes(ss) { if (ss !== 0) { return { hd: shapeNode(ss.hd), tl: shapeNodes(ss.tl) }; } else { return /* [] */0; } } function verticesOf(s) { return s.points; } function grow(param, v) { let y1 = param[3]; let x1 = param[2]; let y0 = param[1]; let x0 = param[0]; let match = v.at; let y = match[1]; let x = match[0]; return [ x - 12 < x0 ? x - 12 : x0, y - 12 < y0 ? y - 12 : y0, x + 12 > x1 ? x + 12 : x1, y + 12 > y1 ? y + 12 : y1 ]; } function growAll(_b, _vs) { while (true) { let vs = _vs; let b = _b; if (vs === 0) { return b; } _vs = vs.tl; _b = grow(b, vs.hd); continue; }; } function bounds(_b, _ss) { while (true) { let ss = _ss; let b = _b; if (ss === 0) { return b; } _ss = ss.tl; _b = growAll(b, ss.hd.points); continue; }; } function framing(_ss) { while (true) { let ss = _ss; if (ss === 0) { return /* [] */0; } let tl = ss.tl; let s = ss.hd; if (s.TAG === "Polygon") { _ss = tl; continue; } if (s.name === "frame") { return { hd: s, tl: /* [] */0 }; } _ss = tl; continue; }; } function viewBox(pic) { let f = framing(pic); let boxed = f !== 0 ? f : pic; let start; if (boxed !== 0) { let match = boxed.hd.points; if (match !== 0) { let match$1 = match.hd.at; let y = match$1[1]; let x = match$1[0]; start = [ x, y, x, y ]; } else { start = [ 0, 0, 0, 0 ]; } } else { start = [ 0, 0, 0, 0 ]; } let match$2 = bounds(start, boxed); let y0 = match$2[1]; let x0 = match$2[0]; let w = match$2[2] - x0; let h = match$2[3] - y0; let match$3 = w * 440 > h * 320 ? [ w, w * 440 / 320 ] : [ h * 320 / 440, h ]; let h2 = match$3[1]; let w2 = match$3[0]; return (x0 - (w2 - w) / 2).toFixed(1) + " " + (y0 - (h2 - h) / 2).toFixed(1) + " " + w2.toFixed(1) + " " + h2.toFixed(1); } function view(r) { if (r !== undefined) { return { TAG: "Node", _0: "svg", _1: { hd: { TAG: "Create", _0: "viewBox", _1: viewBox(r) }, tl: { hd: { TAG: "Width", _0: "320" }, tl: { hd: { TAG: "Height", _0: "440" }, tl: { hd: { TAG: "Style", _0: { hd: [ "border", "1px solid #ccc" ], tl: { hd: [ "background", "#fafafa" ], tl: /* [] */0 } } }, tl: /* [] */0 } } } }, _2: shapeNodes(r) }; } else { return { TAG: "Div", _0: { hd: { TAG: "Style", _0: { hd: [ "font-family", "var(--code-font)" ], tl: { hd: [ "font-size", "0.8em" ], tl: /* [] */0 } } }, tl: /* [] */0 }, _1: { hd: { TAG: "Text", _0: "no picture yet" }, tl: /* [] */0 } }; } } function pictureOf(shapes) { return Stdlib_List.fromArray(shapes.map(s => { let points = Stdlib_List.fromArray(s.points.map(param => { let l = param[2]; return { at: [ param[0], param[1] ], label: (l == null) ? "Plain" : ({ TAG: "Anchor", _0: l }) }; })); if (s.kind === "Polygon") { return { TAG: "Polygon", name: s.name, points: points, stroke: s.stroke, fill: Stdlib_Nullable.getOr(s.fill, "") }; } else { return { TAG: "Path", name: s.name, points: points, stroke: s.stroke }; } })); } function esc(s) { return s.replaceAll("&", "&").replaceAll("\"", """).replaceAll("<", "<"); } function attrMarkup(a) { switch (a.TAG) { case "Create" : return " " + a._0 + "=\"" + esc(a._1) + "\""; case "Width" : return " width=\"" + esc(a._0) + "\""; case "Height" : return " height=\"" + esc(a._0) + "\""; case "Style" : return " style=\"" + esc(Stdlib_List.toArray(Stdlib_List.map(a._0, param => param[0] + ": " + param[1])).join(";")) + "\""; } } function markup(_h) { while (true) { let h = _h; switch (h.TAG) { case "Node" : let tag = h._0; return "<" + tag + Stdlib_List.toArray(Stdlib_List.map(h._1, attrMarkup)).join("") + ">" + Stdlib_List.toArray(Stdlib_List.map(h._2, markup)).join("") + "</" + tag + ">"; case "Div" : _h = { TAG: "Node", _0: "div", _1: h._0, _2: h._1 }; continue; case "Text" : return esc(h._0); } }; } export { num, coords, shapeNode, shapeNodes, verticesOf, grow, growAll, bounds, framing, viewBox, view, pictureOf, esc, attrMarkup, markup, } /* No side effect */
The files, in 046/data/witch (sources, outputs, the benchmark, the timings): Witch.res, Witch.res.js, bench-runs.jsonl, edit-share.json, edit-share.mjs, firefox-answer-probes.json, firefox-answer.json, firefox-before.json, firefox-cuts.json, firefox-edit.json, firefox-noshare.json, firefox-timeline.json, firefox.json, hazeljit-runtime-src.js, hazeljit-witch-pretty.js, hazeljit-witch-src.js, jsoo-excerpt.js, jsoo-runs.jsonl, picture.hz, picture.json, raw-run1.json, raw-run2.json, timings.json, witch-bench.mjs, witch-core.ml, witch-jsoo-tofixed.ml, witch-jsoo.ml, witch-melange.js, witch-melange.ml.