Skip to content

Commit

Permalink
utils/coercing -- improve generic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jun 24, 2024
1 parent cf33b3a commit 5805bfe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
31 changes: 15 additions & 16 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
improve interface for `put` -- second argument should be list of optional promises

`applyAfter` (`coercing`)
# propagator 支持 dependencies for provenance

> propagator 支持 dependencies for provenance
>
> - https://github.com/cicada-lang/propagator/issues/2
测试反向运算的 provenance

test about "a justified-intervals anomaly"
> `coercing(toInterval, f)`
> `coercing(toSupported, f)`
> 测试反向运算的 provenance
> test about "a justified-intervals anomaly"
- 即 supports 的集合可能与 supported value put 进来的顺序有关

# later

improve interface for `put` -- second argument should be list of optional promises
`utils/Set` -- `setIntersection` & `setDifference` -- 为了完备

> propagator 支持 dependencies for alternate worldviews
>
# propagator 支持 dependencies for alternate worldviews

> - https://github.com/cicada-lang/propagator/issues/3
> propagator 支持 dependencies for implicit search
>
# propagator 支持 dependencies for implicit search

> - https://github.com/cicada-lang/propagator/issues/4
> 完成 "The Art" 中的 Heron 例子
>
# 完成 "The Art" 中的 Heron 例子

> - 需要设计 lattice 来逼近结果
> - 一个叫 近似值 的数据类型,并且为 generic 函数实现相关的 handles
> interval 支持完整的 arithmetic,包括正数与负数
# interval 支持完整的 arithmetic,包括正数与负数

`interval/` -- arithmetic 支持负数

Expand Down
10 changes: 4 additions & 6 deletions src/generics/arithmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import {
intervalMul,
intervalSub,
isInterval,
toInterval,
} from "../interval/index.js"
import { coercing } from "../utils/coercing.js"
import { isNumber } from "../utils/isNumber.js"

export const add = defineGeneric()
defineHandler(add, [isNumber, isNumber], (x, y) => x + y)
defineHandler(add, [isInterval, isInterval], intervalAdd)
defineHandler(add, [isInterval, isNumber], (x, y) =>
intervalAdd(x, exactInterval(y)),
)
defineHandler(add, [isNumber, isInterval], (x, y) =>
intervalAdd(exactInterval(x), y),
)
defineHandler(add, [isInterval, isNumber], coercing(toInterval, intervalAdd))
defineHandler(add, [isNumber, isInterval], coercing(toInterval, intervalAdd))

export const sub = defineGeneric()
defineHandler(sub, [isNumber, isNumber], (x, y) => x - y)
Expand Down
5 changes: 5 additions & 0 deletions src/interval/Interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ export function exactInterval(x: number): Interval {
export function isInterval(x: any): x is Interval {
return isNonNullObject(x) && x["@type"] === "Interval"
}

export function toInterval(x: number | Interval): Interval {
if (isInterval(x)) return x
return exactInterval(x)
}
6 changes: 6 additions & 0 deletions src/utils/coercing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function coercing<A, B, C>(
to: (x: A) => B,
fn: (...args: Array<B>) => C,
): (...args: Array<A>) => C {
return (...args) => fn(...args.map(to))
}

0 comments on commit 5805bfe

Please sign in to comment.