Skip to content

Commit

Permalink
Stmts.Compute -- use deepForce
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Oct 27, 2022
1 parent 87d8e19 commit 2d557e9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
improve termination error report -- `span` to `Clause` first

Stmts.Compute -- use `deepForce`

support mutual recursion within `Mod`

# later
Expand Down
2 changes: 1 addition & 1 deletion src/lang/stmts/Compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export class Compute extends Stmt {

async execute(mod: Mod): Promise<StmtOutput> {
const value = evaluate(mod.env, this.exp)
return formatValue(Values.force(value))
return formatValue(Values.deepForce(value))
}
}
31 changes: 31 additions & 0 deletions src/lang/value/deepForce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as Actions from "../actions"
import { evaluate } from "../exp"
import * as Values from "../value"
import { Value } from "../value"

export function deepForce(value: Value): Value {
value = Values.force(value)

switch (value.kind) {
case "Lazy": {
return deepForce(evaluate(value.env, value.exp))
}

case "FnMatch": {
const result = Actions.doApUnfolded(value, [])
return result.kind === "FnMatch" ? result : deepForce(result)
}

case "Ap": {
const unfolded = Values.unfoldAp(value)
const target = deepForce(unfolded.target)
if (target.kind === "Coctor") return value
const result = Actions.doAp(deepForce(value.target), deepForce(value.arg))
return result.kind === "Ap" ? result : deepForce(result)
}

default: {
return value
}
}
}
1 change: 1 addition & 0 deletions src/lang/value/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./Clause"
export * from "./deepForce"
export * from "./force"
export * from "./formatClause"
export * from "./formatValue"
Expand Down
2 changes: 1 addition & 1 deletion std/datatypes/List.test.mu.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(null Nat)
(cons Nat zero (null Nat))
(cons Nat zero (cons Nat zero (null Nat)))
(add1 (length A tail))
(add1 (add1 zero))
4 changes: 2 additions & 2 deletions std/datatypes/Nat.test.mu.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ zero
zero
zero
(add1 zero)
(add1 (add x y))
(add1 zero)
(add1 (add x y))
(add1 zero)
(add1 (add1 zero))

0 comments on commit 2d557e9

Please sign in to comment.