Hazel reports · ^report(46) · the Witch's picture · one page per output · updated 2026-11-08 08:03 MST
The Witch's picture view: Melange 5.0.0'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.
Melange's output is ReScript's with numbers for tags (TAG: /* Node */ 0). Its string concatenation is nested to the right, as OCaml's ^ associates, and higher-order calls in the markup helpers go through Curry._1 (a stub here: the view itself makes none).
OCaml source (witch-melange.ml)
(* ^report(46): the Witch slide's ^picture view, in OCaml as the Hazel is written (witch.hz, Drawing.^picture), for Melange *) let fixed1 (x : float) = Js.Float.toFixed ~digits:1 x (* the picture, as the slide's types *) type point = float * float type label = Plain | Anchor of string type vertex = { at : point; label : label } type stroke = { width : float; color : string } type shape = | Polygon of { name : string; points : vertex list; stroke : stroke; fill : string } | Path of { name : string; points : vertex list; stroke : stroke } type picture = shape list (* what a view returns: Hazel's Html and Attr, as far as this view uses them *) type attr = Create of string * string | Width of string | Height of string | Style of (string * string) list type html = Node of string * attr list * html list | Div of attr list * html list | Text of string (* the view *) let num (x : float) = fixed1 x let rec coords (vs : vertex list) = match vs with | [] -> "" | v :: tl -> let (x, y) = v.at in fixed1 x ^ "," ^ fixed1 y ^ " " ^ coords tl let shape_node (s : shape) = match s with | Polygon p -> Node ("polygon", [ 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") ], []) | Path p -> Node ("polyline", [ 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") ], []) let rec shape_nodes (ss : picture) = match ss with [] -> [] | s :: tl -> shape_node s :: shape_nodes tl let vertices_of (s : shape) = match s with Polygon p -> p.points | Path p -> p.points let grow ((x0, y0, x1, y1) : float * float * float * float) (v : vertex) = let (x, y) = v.at in ((if x -. 12. < x0 then x -. 12. else x0), (if y -. 12. < y0 then y -. 12. else y0), (if x +. 12. > x1 then x +. 12. else x1), (if y +. 12. > y1 then y +. 12. else y1)) let rec grow_all b (vs : vertex list) = match vs with [] -> b | v :: tl -> grow_all (grow b v) tl let rec bounds b (ss : picture) = match ss with [] -> b | s :: tl -> bounds (grow_all b (vertices_of s)) tl let rec framing (ss : picture) = match ss with | [] -> [] | s :: tl -> (match s with Path p -> if p.name = "frame" then [ s ] else framing tl | _ -> framing tl) let view_box (pic : picture) = let boxed = match framing pic with [] -> pic | f -> f in let start = match boxed with | [] -> (0., 0., 0., 0.) | s :: _ -> (match vertices_of s with [] -> (0., 0., 0., 0.) | v :: _ -> let (x, y) = v.at in (x, y, x, y)) in let (x0, y0, x1, y1) = bounds start boxed in let (w, h) = (x1 -. x0, y1 -. y0) in let (w2, h2) = if w *. 440. > h *. 320. then (w, w *. 440. /. 320.) else (h *. 320. /. 440., h) in 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 : picture option) = match r with | Some pic -> Node ("svg", [ Create ("viewBox", view_box pic); Width "320"; Height "440"; Style [ ("border", "1px solid #ccc"); ("background", "#fafafa") ] ], shape_nodes pic) | None -> Div ([ Style [ ("font-family", "var(--code-font)"); ("font-size", "0.8em") ] ], [ Text "no picture yet" ]) (* (not timed) a drawing as markup, to compare with hazel-jit's; the picture's strings need no escaping *) let rec join f xs = match xs with [] -> "" | x :: tl -> f x ^ join f tl let rec join_sep f sep xs = match xs with [] -> "" | [ x ] -> f x | x :: tl -> f x ^ sep ^ join_sep f sep tl let attr_markup a = match a with | Create (k, v) -> " " ^ k ^ "=\"" ^ v ^ "\"" | Width v -> " width=\"" ^ v ^ "\"" | Height v -> " height=\"" ^ v ^ "\"" | Style kvs -> " style=\"" ^ join_sep (fun (k, v) -> k ^ ": " ^ v) ";" kvs ^ "\"" let rec markup h = match h with | Node (tag, attrs, kids) -> "<" ^ tag ^ join attr_markup attrs ^ ">" ^ join markup kids ^ "</" ^ tag ^ ">" | Div (attrs, kids) -> markup (Node ("div", attrs, kids)) | Text s -> s (* (not timed) the picture from the test's JSON: records are JS objects, tuples arrays *) type js_shape = { kind : string; name : string; points : (float * float * string Js.nullable) array; stroke : stroke; fill : string Js.nullable } let to_list f (a : 'a array) = let rec go i acc = if i < 0 then acc else go (i - 1) (f (Array.unsafe_get a i) :: acc) in go (Js.Array.length a - 1) [] let picture_of (shapes : js_shape array) : picture = to_list (fun s -> let points = to_list (fun (x, y, l) -> { at = (x, y); label = (match Js.Nullable.toOption l with Some n -> Anchor n | None -> Plain) }) s.points in if s.kind = "Polygon" then Polygon { name = s.name; points; stroke = s.stroke; fill = (match Js.Nullable.toOption s.fill with Some f -> f | None -> "") } else Path { name = s.name; points; stroke = s.stroke }) shapes
Melange 5.0.0's JavaScript (from its playground)
// Generated by Melange import * as Curry from "./curry_stub.js"; function fixed1(x) { return x.toFixed(1); } function num(x) { return x.toFixed(1); } function coords(vs) { if (!vs) { return ""; } const match = vs.hd.at; return match[0].toFixed(1) + ("," + (match[1].toFixed(1) + (" " + coords(vs.tl)))); } function shape_node(s) { if (s.TAG === /* Polygon */ 0) { return { TAG: /* Node */ 0, _0: "polygon", _1: { hd: { TAG: /* Create */ 0, _0: "points", _1: coords(s.points) }, tl: { hd: { TAG: /* Create */ 0, _0: "fill", _1: s.fill }, tl: { hd: { TAG: /* Create */ 0, _0: "stroke", _1: s.stroke.color }, tl: { hd: { TAG: /* Create */ 0, _0: "stroke-width", _1: s.stroke.width.toFixed(1) }, tl: { hd: { TAG: /* Create */ 0, _0: "stroke-linejoin", _1: "round" }, tl: /* [] */ 0 } } } } }, _2: /* [] */ 0 }; } else { return { TAG: /* Node */ 0, _0: "polyline", _1: { hd: { TAG: /* Create */ 0, _0: "points", _1: coords(s.points) }, tl: { hd: { TAG: /* Create */ 0, _0: "fill", _1: "none" }, tl: { hd: { TAG: /* Create */ 0, _0: "stroke", _1: s.stroke.color }, tl: { hd: { TAG: /* Create */ 0, _0: "stroke-width", _1: s.stroke.width.toFixed(1) }, tl: { hd: { TAG: /* Create */ 0, _0: "stroke-linejoin", _1: "round" }, tl: { hd: { TAG: /* Create */ 0, _0: "stroke-linecap", _1: "round" }, tl: /* [] */ 0 } } } } } }, _2: /* [] */ 0 }; } } function shape_nodes(ss) { if (ss) { return { hd: shape_node(ss.hd), tl: shape_nodes(ss.tl) }; } else { return /* [] */ 0; } } function vertices_of(s) { return s.points; } function grow(param, v) { const y1 = param[3]; const x1 = param[2]; const y0 = param[1]; const x0 = param[0]; const match = v.at; const y = match[1]; const 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 grow_all(_b, _vs) { while (true) { const vs = _vs; const b = _b; if (!vs) { return b; } _vs = vs.tl; _b = grow(b, vs.hd); continue; }; } function bounds(_b, _ss) { while (true) { const ss = _ss; const b = _b; if (!ss) { return b; } _ss = ss.tl; _b = grow_all(b, ss.hd.points); continue; }; } function framing(_ss) { while (true) { const ss = _ss; if (!ss) { return /* [] */ 0; } const tl = ss.tl; const s = ss.hd; if (s.TAG === /* Polygon */ 0) { _ss = tl; continue; } if (s.name === "frame") { return { hd: s, tl: /* [] */ 0 }; } _ss = tl; continue; }; } function view_box(pic) { const f = framing(pic); const boxed = f ? f : pic; let start; if (boxed) { const match = boxed.hd.points; if (match) { const match$1 = match.hd.at; const y = match$1[1]; const x = match$1[0]; start = [ x, y, x, y ]; } else { start = [ 0, 0, 0, 0 ]; } } else { start = [ 0, 0, 0, 0 ]; } const match$2 = bounds(start, boxed); const y0 = match$2[1]; const x0 = match$2[0]; const w = match$2[2] - x0; const h = match$2[3] - y0; const match$3 = w * 440 > h * 320 ? [ w, w * 440 / 320 ] : [ h * 320 / 440, h ]; const h2 = match$3[1]; const 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, _0: "svg", _1: { hd: { TAG: /* Create */ 0, _0: "viewBox", _1: view_box(r) }, tl: { hd: { TAG: /* Width */ 1, _0: "320" }, tl: { hd: { TAG: /* Height */ 2, _0: "440" }, tl: { hd: { TAG: /* Style */ 3, _0: { hd: [ "border", "1px solid #ccc" ], tl: { hd: [ "background", "#fafafa" ], tl: /* [] */ 0 } } }, tl: /* [] */ 0 } } } }, _2: shape_nodes(r) }; } else { return { TAG: /* Div */ 1, _0: { hd: { TAG: /* Style */ 3, _0: { hd: [ "font-family", "var(--code-font)" ], tl: { hd: [ "font-size", "0.8em" ], tl: /* [] */ 0 } } }, tl: /* [] */ 0 }, _1: { hd: { TAG: /* Text */ 2, _0: "no picture yet" }, tl: /* [] */ 0 } }; } } function join(f, xs) { if (xs) { return Curry._1(f, xs.hd) + join(f, xs.tl); } else { return ""; } } function join_sep(f, sep, xs) { if (!xs) { return ""; } const x = xs.hd; if (xs.tl) { return Curry._1(f, x) + (sep + join_sep(f, sep, xs.tl)); } else { return Curry._1(f, x); } } function attr_markup(a) { switch (a.TAG) { case /* Create */ 0 : return " " + (a._0 + ("=\"" + (a._1 + "\""))); case /* Width */ 1 : return " width=\"" + (a._0 + "\""); case /* Height */ 2 : return " height=\"" + (a._0 + "\""); case /* Style */ 3 : return " style=\"" + (join_sep((function (param) { return param[0] + (": " + param[1]); }), ";", a._0) + "\""); } } function markup(_h) { while (true) { const h = _h; switch (h.TAG) { case /* Node */ 0 : const tag = h._0; return "<" + (tag + (join(attr_markup, h._1) + (">" + (join(markup, h._2) + ("</" + (tag + ">")))))); case /* Div */ 1 : _h = { TAG: /* Node */ 0, _0: "div", _1: h._0, _2: h._1 }; continue; case /* Text */ 2 : return h._0; } }; } function to_list(f, a) { let _i = a.length - 1 | 0; let _acc = /* [] */ 0; while (true) { const acc = _acc; const i = _i; if (i < 0) { return acc; } _acc = { hd: Curry._1(f, a[i]), tl: acc }; _i = i - 1 | 0; continue; }; } function picture_of(shapes) { return to_list((function (s) { const points = to_list((function (param) { const l = param[2]; return { at: [ param[0], param[1] ], label: !(l == null) ? ({ TAG: /* Anchor */ 0, _0: l }) : /* Plain */ 0 }; }), s.points); if (s.kind !== "Polygon") { return { TAG: /* Path */ 1, name: s.name, points: points, stroke: s.stroke }; } const f = s.fill; return { TAG: /* Polygon */ 0, name: s.name, points: points, stroke: s.stroke, fill: (f == null) ? "" : f }; }), shapes); } export { fixed1, num, coords, shape_node, shape_nodes, vertices_of, grow, grow_all, bounds, framing, view_box, view, join, join_sep, attr_markup, markup, to_list, picture_of, } /* 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.