Skip to content

Commit 19f8e78

Browse files
committed
ambr equivalent equal
1 parent 3624acc commit 19f8e78

File tree

9 files changed

+36
-35
lines changed

9 files changed

+36
-35
lines changed

TODO.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
`equivalentInCtx` with `ctx.trail` handle recursive function
2-
`equivalentInCtx` call `applyOneStep`
1+
`same` and `sameInCtx` -- do not expend function with `definedName`
2+
3+
`equalInCtx` with `ctx.trail` handle recursive function
4+
`equalInCtx` call `applyOneStep`
35

46
`readback` handle recursive function
57

docs/diary/2025-06-21-the-right-way-to-handle-recursion.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,43 +254,46 @@ f3
254254
设计一个带有 solution 的 relation 作为 judgement:
255255

256256
```scheme
257-
(claim equivalent solution-t exp-t exp-t judgement-t)
257+
(claim equal solution-t exp-t exp-t judgement-t)
258258
```
259259

260260
先不定义推演规则,直接用命题的列表来写证明:
261261

262262
```scheme
263-
(equivalent
263+
(equal
264+
[]
265+
f2 f3)
266+
(equal
264267
[(== f2 f3)]
265268
(F (F f2)) (F (F (F f3))))
266-
(equivalent
269+
(equal
267270
[(== f2 f3)]
268271
f2 (F f3))
269-
(equivalent
272+
(equal
270273
[(== f2 f3)
271274
(== f2 (F f3))]
272275
(F (F f2)) (F f3))
273-
(equivalent
276+
(equal
274277
[(== f2 f3)
275278
(== f2 (F f3))]
276279
(F f2) f3)
277-
(equivalent
280+
(equal
278281
[(== f2 f3)
279282
(== f2 (F f3))
280283
(== f3 (F f2))]
281284
(F f2) (F (F (F f3))))
282-
(equivalent
285+
(equal
283286
[(== f2 f3)
284287
(== f2 (F f3))
285288
(== f3 (F f2))]
286289
f2 (F (F f3)))
287-
(equivalent
290+
(equal
288291
[(== f2 f3)
289292
(== f2 (F f3))
290293
(== f3 (F f2))
291294
(== f2 (F (F f3)))]
292295
(F (F f2)) (F (F f3)))
293-
(equivalent
296+
(equal
294297
[(== f2 f3)
295298
(== f2 (F f3))
296299
(== f3 (F f2))
@@ -304,13 +307,13 @@ f3
304307
而是带有相位差的表达式相等呢?
305308

306309
```scheme
307-
(equivalent
310+
(equal
308311
[]
309312
f2 (F f2))
310-
(equivalent
313+
(equal
311314
[(== f2 (F f2))]
312315
(F (F f2)) (F f2))
313-
(equivalent
316+
(equal
314317
[(== f2 (F f2))]
315318
(F f2) f2)
316319
;; prove by lookup the substitution
File renamed without changes.

src/lang/equal/equal.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { type Value } from "../value/index.ts"
2+
import { emptyCtx } from "./Ctx.ts"
3+
import { equalInCtx } from "./equalInCtx.ts"
4+
5+
export function equal(left: Value, right: Value): boolean {
6+
return equalInCtx(emptyCtx(), left, right)
7+
}

src/lang/equivalent/equivalentInCtx.ts renamed to src/lang/equal/equalInCtx.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,34 @@ import * as Values from "../value/index.ts"
55
import { type Neutral, type Value } from "../value/index.ts"
66
import { ctxUseName, type Ctx } from "./Ctx.ts"
77

8-
export function equivalentInCtx(ctx: Ctx, left: Value, right: Value): boolean {
8+
export function equalInCtx(ctx: Ctx, left: Value, right: Value): boolean {
99
left = Values.lazyActiveDeep(left)
1010
right = Values.lazyActiveDeep(right)
1111

1212
if (left.kind === "NotYet" && right.kind === "NotYet") {
13-
return equivalentNeutralInCtx(ctx, left.neutral, right.neutral)
13+
return equalNeutralInCtx(ctx, left.neutral, right.neutral)
1414
}
1515

1616
if (left.kind === "Lambda" && right.kind === "Lambda") {
1717
const freshName = freshen(ctx.usedNames, left.name)
1818
ctx = ctxUseName(ctx, freshName)
1919
const v = Neutrals.Var(freshName)
2020
const arg = Values.NotYet(v)
21-
return equivalentInCtx(ctx, apply(left, arg), apply(right, arg))
21+
return equalInCtx(ctx, apply(left, arg), apply(right, arg))
2222
}
2323

2424
return false
2525
}
2626

27-
function equivalentNeutralInCtx(
28-
ctx: Ctx,
29-
left: Neutral,
30-
right: Neutral,
31-
): boolean {
27+
function equalNeutralInCtx(ctx: Ctx, left: Neutral, right: Neutral): boolean {
3228
if (left.kind === "Var" && right.kind === "Var") {
3329
return right.name === left.name
3430
}
3531

3632
if (left.kind === "Apply" && right.kind === "Apply") {
3733
return (
38-
equivalentNeutralInCtx(ctx, left.target, right.target) &&
39-
equivalentInCtx(ctx, left.arg, right.arg)
34+
equalNeutralInCtx(ctx, left.target, right.target) &&
35+
equalInCtx(ctx, left.arg, right.arg)
4036
)
4137
}
4238

src/lang/equal/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./equal.ts"

src/lang/equivalent/equivalent.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/lang/equivalent/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/lang/run/execute.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dedent from "dedent"
22
import { emptyEnv } from "../env/index.ts"
3-
import { equivalent } from "../equivalent/index.ts"
3+
import { equal } from "../equal/index.ts"
44
import { evaluate } from "../evaluate/index.ts"
55
import { type Exp } from "../exp/index.ts"
66
import { formatExp } from "../format/formatExp.ts"
@@ -38,7 +38,7 @@ export function execute(mod: Mod, stmt: Stmt): null {
3838
function assertEqual(mod: Mod, left: Exp, right: Exp): void {
3939
const leftValue = evaluate(mod, emptyEnv(), left)
4040
const rightValue = evaluate(mod, emptyEnv(), right)
41-
if (!equivalent(leftValue, rightValue)) {
41+
if (!equal(leftValue, rightValue)) {
4242
throw new Error(dedent`
4343
[assertEqual] Fail to assert equal.
4444
@@ -51,7 +51,7 @@ function assertEqual(mod: Mod, left: Exp, right: Exp): void {
5151
function assertNotEqual(mod: Mod, left: Exp, right: Exp): void {
5252
const leftValue = evaluate(mod, emptyEnv(), left)
5353
const rightValue = evaluate(mod, emptyEnv(), right)
54-
if (equivalent(leftValue, rightValue)) {
54+
if (equal(leftValue, rightValue)) {
5555
throw new Error(dedent`
5656
[assertNotEqual] Fail to assert NOT equal.
5757

0 commit comments

Comments
 (0)