Skip to content

Commit de20dcd

Browse files
committed
Bend 2.0.11: term_show keys are unsugared, so templates and show descriptors no longer collide on Array shapes (#834)
term_show output was the identity key of two caches: the template specialization table (parse_term_ops, tm.is[key]) and the compiled main show descriptor table (comp.ts show_main). Its Array sugar prints every ALeaf/ANode tree with the same leaves as one list, so two shapes shared one specialization (the checker saw the wrong type; a proof of Empty checked) and one descriptor (the C main read a stray heap word, the JS lane fail-stopped). term_show takes a key flag that skips the Array sugar, the only one that is not injective. Both key sites pass it. Every other print is byte-identical. Two regression tests: tests/comptime/template_key_shape and tests/check/template_key_empty.
1 parent 2b9fc8d commit de20dcd

5 files changed

Lines changed: 78 additions & 5 deletions

File tree

‎bend2/bend.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ const ESCAPES: Record<string, U32> = {
12411241
"n": 10, "t": 9, "r": 13, "0": 0, "\\": 92, "'": 39, '"': 34,
12421242
};
12431243

1244-
export function term_show(term: LTerm, top: number = -1, bnd: Name[] = []): string {
1244+
export function term_show(term: LTerm, top: number = -1, bnd: Name[] = [], key: boolean = false): string {
12451245
function term_show_sugar_exi(tm: LTerm, prc: number): string | null {
12461246
const [h, xs] = term_unapply(tm);
12471247
const b = xs[1];
@@ -1408,7 +1408,7 @@ export function term_show(term: LTerm, top: number = -1, bnd: Name[] = []): stri
14081408
const u32 = u32_from_term(tm);
14091409
const f32 = u32_from_term(tm, "F32");
14101410
const chr = term_show_sugar_chr(tm, "'");
1411-
const arr = term_show_sugar_arr(tm);
1411+
const arr = key ? null : term_show_sugar_arr(tm);
14121412
const sug = u32 !== null ? String(u32) : f32 !== null ? f32_show(f32_from_bits(f32))
14131413
: term_show_sugar_nat(tm, prc)
14141414
?? (chr !== null ? "'" + chr + "'" : null)
@@ -2073,7 +2073,7 @@ export function parse_term_ops(p: Parse, tm: LTerm, lvl: number): LTerm {
20732073
const s = parse_grow(p, out);
20742074
if (out.$ === "Ref" && tm !== undefined) {
20752075
const env = p.sc.stk.reduce((env: Env, e) => list_set(env, e[1], Ref("\0")), null);
2076-
const key = ts.map((x) => term_show(term_lower(term_higher(x, env)))).join("\n");
2076+
const key = ts.map((x) => term_show(term_lower(term_higher(x, env)), -1, [], true)).join("\n");
20772077
if (!key.includes("\0") && key.length <= 2048) {
20782078
if (tm.is[key] === undefined) {
20792079
tm.is[key] = out.k + "~" + Object.keys(tm.is).length;

‎bend2/comp.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ function show_main(book: Bend.Book): Show | null {
12221222
const node = (T: HTerm, lay: Lay): number => {
12231223
const t = ty_wnf(book, T) as HTerm;
12241224
const box = lay_box(lay);
1225-
const key = String(box) + Bend.term_show(Bend.term_lower(t));
1225+
const key = String(box) + Bend.term_show(Bend.term_lower(t), -1, [], true);
12261226
const got = ids.get(key);
12271227
if (got !== undefined) {
12281228
return got;

‎bend2/main.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import * as Comp from "./comp.ts";
2929
// Constants
3030
// =========
3131

32-
const VERSION = "2.0.10";
32+
const VERSION = "2.0.11";
3333

3434
const HELP = `Bend ${VERSION}: check, run, build and publish Bend programs.
3535
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# a template applied to two Array trees with the same leaves got one
2+
# specialization (#834): term_show keyed both as `[Unit{}, Unit{}, Unit{}]`,
3+
# so contradiction's Pick reduced to seed's Unit and a proof of Empty
4+
# checked. The key now tells the shapes apart.
5+
type Empty is Data:
6+
7+
type Unit is Data:
8+
Unit{}
9+
10+
type Array<-T: Type> is Type:
11+
ALeaf{value: T}
12+
ANode{xs: Array<T>, ys: Array<T>}
13+
14+
def EmptyIfLeftIsNode(xs: Array<Unit>) -> Type:
15+
match xs:
16+
case ALeaf{x}:
17+
Unit
18+
case ANode{ALeaf{x}, r}:
19+
Unit
20+
case ANode{ANode{a, b}, r}:
21+
Empty
22+
23+
def Pick(~xs: Array<Unit>) -> Type:
24+
EmptyIfLeftIsNode(xs)
25+
26+
def seed() -> Pick(~ANode{ALeaf{Unit{}}, ANode{ALeaf{Unit{}}, ALeaf{Unit{}}}}):
27+
Unit{}
28+
29+
def contradiction() -> Pick(~ANode{ANode{ALeaf{Unit{}}, ALeaf{Unit{}}}, ALeaf{Unit{}}}):
30+
seed
31+
32+
#|Error:
33+
#|- expected : Empty
34+
#|- observed : Unit
35+
#|Location: contradiction
36+
#|29 | def contradiction() -> Pick(~ANode{ANode{ALeaf{Unit{}}, ALeaf{Unit{}}}, ALeaf{Unit{}}}):
37+
#|30>| seed
38+
#|31 |
39+
#|exit 1
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# a template's specialization key and a compiled main's show descriptor
2+
# key are term_show output, which flattened every Array tree with the
3+
# same leaves to one `[1, 2, 3]` (#834): two shapes shared one
4+
# specialization and one descriptor, so the second got the first's type
5+
# and layout. The key now prints array trees as constructors.
6+
import Base
7+
8+
def LowType(xs: Array<U32>) -> Type:
9+
match xs:
10+
case ALeaf{x}:
11+
U32
12+
case ANode{ALeaf{x}, r}:
13+
U32 & U32
14+
case ANode{ANode{a, b}, r}:
15+
U32
16+
17+
def Baked(~xs: Array<U32>) -> Type:
18+
LowType(xs)
19+
20+
type Box<-xs: Array<U32>> is Type:
21+
Box{value: LowType(xs)}
22+
23+
type Out is Type:
24+
Out{
25+
first: Baked(~ANode{ALeaf{1}, ANode{ALeaf{2}, ALeaf{3}}}),
26+
second: Baked(~ANode{ANode{ALeaf{1}, ALeaf{2}}, ALeaf{3}}),
27+
wide: Box<ANode{ALeaf{1}, ANode{ALeaf{2}, ALeaf{3}}}>,
28+
narrow: Box<ANode{ANode{ALeaf{1}, ALeaf{2}}, ALeaf{3}}>
29+
}
30+
31+
def main() -> Out:
32+
Out{(8, 9), 7, Box{(8, 9)}, Box{7}}
33+
34+
#|Out{(8, 9), 7, Box{(8, 9)}, Box{7}}

0 commit comments

Comments
 (0)