substitution API
用途
substitution 包用项替换自由变量,且不会捕获变量。Substitution[T] 作用于 @syntax.Term[T];GenericSubstitution[N] 作用于任何实现了 @syntax.BindingSyntax 的 AST。两者都是有限的、同时的且不可变的:它们是抽象类型,每个操作都返回新值,且每个返回数组的方法都返回一个新的副本。
Substitution::apply、Substitution::then 和 GenericSubstitution::apply_once 对项及替换项的嵌套深度是栈安全的:它们把待处理的工作保存在堆数组中,只使用常数大小的宿主栈,因此嵌套 100 000 层的项在每个后端上都能处理。用 Debug 格式化这样的项不是栈安全的;见 syntax API。
避免捕获的代换的定义及其定律的证明见代换设计。
导入
将该包以及其签名中出现的类型所在的包添加到你的 moon.pkg:
import {
"Luna-Flow/type_theory/core",
"Luna-Flow/type_theory/syntax",
"Luna-Flow/type_theory/substitution",
}
本页的示例都通过包别名引用名字,例如 @core.Name。
Term 上的代换
Substitution
Substitution[T] 是从名字到替换项的有限映射。
type Substitution[T]
它表示映射 :对于条目 有 ,对于其他所有名字有 (即变量本身)。该类型是抽象的:包外无法触及这些条目,因此下面的函数是构造替换的唯一途径,且每个名字至多有一个条目。
替换项按原样存储和返回,不会被复制:get、to_array 和 apply 返回的项与传给 singleton 和 set 的项共享结构。Term 是一个普通的枚举,其 Apply 的参数是一个 Array,因此请把项视为不可变值;原地修改某个项的参数数组,也会改变所有持有该项的替换。
Substitution::empty, Substitution::singleton, Substitution::set
以下函数用于构建代换。
pub fn[T] Substitution::empty() -> Self[T]
pub fn[T] Substitution::singleton(@core.Name, @syntax.Term[T]) -> Self[T]
pub fn[T] Substitution::set(Self[T], @core.Name, @syntax.Term[T]) -> Self[T]
set(x, s) 添加 ,若 x 已有条目则就地替换。
Substitution::get
Substitution::get 返回某个名字的替换项(如果有)。
pub fn[T] Substitution::get(Self[T], @core.Name) -> @syntax.Term[T]?
Substitution::length, Substitution::to_array
这些方法读取替换的条目。
pub fn[T] Substitution::length(Self[T]) -> Int
pub fn[T] Substitution::to_array(Self[T]) -> Array[(@core.Name, @syntax.Term[T])]
length 是定义域中名字的个数。to_array 以新数组的形式返回条目 (x, s),顺序为名字首次加入的顺序;修改该数组不会改变替换。
test "substitution entries are read through a copy" {
let x = @core.Name::new("x")
let y = @core.Name::new("y")
let s : @substitution.Substitution[Int] = @substitution.Substitution::singleton(
x,
Value(1),
).set(y, Value(2))
let entries = s.to_array()
assert_eq(entries, [(x, Value(1)), (y, Value(2))])
entries.push((x, Value(3)))
assert_eq(s.length(), 2)
assert_eq(s.get(x), Some(@syntax.Value(1)))
}
Substitution::without, Substitution::restrict
以下方法用于缩小代换的定义域。
pub fn[T] Substitution::without(Self[T], @core.Name) -> Self[T]
pub fn[T] Substitution::restrict(Self[T], @hashset.HashSet[@core.Name]) -> Self[T]
without(x) 删除 x 的条目,这正是 x 的绑定子所起的作用。restrict(names) 只保留名字属于 names 的条目。
test "build and shrink substitutions" {
let x = @core.Name::new("x")
let y = @core.Name::new("y")
let s : @substitution.Substitution[Int] = @substitution.Substitution::singleton(
x,
Value(1),
).set(y, Value(2))
assert_eq(s.get(x), Some(@syntax.Value(1)))
assert_eq(s.without(x).get(x), None)
assert_eq(s.restrict(@hashset.HashSet([y])).get(x), None)
assert_eq(s.restrict(@hashset.HashSet([y])).get(y), Some(@syntax.Value(2)))
}
Substitution::apply
Substitution::apply 同时替换项中的自由变量,并重命名绑定子以避免捕获。
pub fn[T] Substitution::apply(Self[T], @syntax.Term[T]) -> @syntax.Term[T]
定义域中名字 x 的每个自由出现都被替换为其替换项;不会在插入的替换项内部再做代换。在 Bind(y, body) 之下,y 的条目被忽略。当某个实际插入到 body 中的替换项含有自由的 y 时,会先将绑定子重命名为新鲜名字,从而替换项中的自由变量保持自由。结果在新鲜名字的选取意义下是确定的,而该选取本身是确定性的。
test "substitution avoids capture" {
let x = @core.Name::new("x")
let y = @core.Name::new("y")
let term : @syntax.Term[Int] = Bind(y, Apply(Variable(x), [Variable(y)]))
let result = @substitution.Substitution::singleton(x, Variable(y)).apply(term)
let y1 = @core.Name::new("y_1")
let expected : @syntax.Term[Int] = Bind(y1, Apply(Variable(y), [Variable(y1)]))
assert_eq(result, expected)
}
Substitution::then
Substitution::then 复合两个代换,先应用 self。
pub fn[T] Substitution::then(Self[T], Self[T]) -> Self[T]
s.then(t) 将 s 定义域中的每个 x 映射到 t.apply(s(x)),将 t 定义域中的其余每个 x 映射到 t(x)。对于每个项,s.then(t).apply(term) 与 t.apply(s.apply(term)) α-等价。
test "composition agrees with sequential application" {
let x = @core.Name::new("x")
let y = @core.Name::new("y")
let first : @substitution.Substitution[Int] = @substitution.Substitution::singleton(
x,
Variable(y),
)
let second = @substitution.Substitution::singleton(y, Value(42))
let term : @syntax.Term[Int] = Apply(Variable(x), [Variable(y)])
let both = first.then(second)
assert_true(
@syntax.alpha_equal(both.apply(term), second.apply(first.apply(term))),
)
assert_eq(both.apply(term), Apply(Value(42), [Value(42)]))
}
from_renaming
from_renaming 将一个重命名转换为作用于给定名字集合上的代换。
pub fn[T] from_renaming(Array[@core.Name], @core.Renaming) -> Substitution[T]
结果将每个满足 renaming.apply(x) != x 的所列名字 x 映射到 Variable(renaming.apply(x))。当项的自由变量都在所列名字之中时,应用该结果与 term.rename_free(renaming) α-等价。
test "a renaming as a substitution" {
let x = @core.Name::new("x")
let y = @core.Name::new("y")
let s : @substitution.Substitution[Int] = @substitution.from_renaming(
[x],
@core.Renaming::singleton(x, y),
)
assert_eq(s.apply(Variable(x)), Variable(y))
}
感知绑定的 AST 上的代换
GenericSubstitution
GenericSubstitution[N] 是从名字到下游 AST N 的节点的有限映射。
type GenericSubstitution[N]
与 Substitution 一样,该类型是抽象的,每个名字至多有一个条目。替换节点在存储和返回时都不会被复制。
GenericSubstitution::empty, GenericSubstitution::singleton, GenericSubstitution::set, GenericSubstitution::get, GenericSubstitution::without, GenericSubstitution::length, GenericSubstitution::to_array
以下函数用于构建和查询泛型代换;它们的行为与对应的 Substitution 版本完全相同。
pub fn[N] GenericSubstitution::empty() -> Self[N]
pub fn[N] GenericSubstitution::singleton(@core.Name, N) -> Self[N]
pub fn[N] GenericSubstitution::set(Self[N], @core.Name, N) -> Self[N]
pub fn[N] GenericSubstitution::get(Self[N], @core.Name) -> N?
pub fn[N] GenericSubstitution::without(Self[N], @core.Name) -> Self[N]
pub fn[N] GenericSubstitution::length(Self[N]) -> Int
pub fn[N] GenericSubstitution::to_array(Self[N]) -> Array[(@core.Name, N)]
GenericSubstitution::apply_once
GenericSubstitution::apply_once 以一趟同时且避免捕获的遍历将代换应用于节点。
pub fn[N : @syntax.BindingSyntax] GenericSubstitution::apply_once(Self[N], N) -> N
算法与 Substitution::apply 相同,通过 BindingSyntax::project 执行,并用 variable、apply 和 bind 重建。Opaque 节点原样返回。“Once”的意思是不会再次访问插入的替换项:在 中代换 和 得到 ,而不是 。
test "generic substitution on Term" {
let x = @core.Name::new("x")
let y = @core.Name::new("y")
let s = @substitution.GenericSubstitution::singleton(x, @syntax.Term::Variable(y))
.set(y, @syntax.Value(2))
let term : @syntax.Term[Int] = Apply(Variable(x), [Variable(y)])
assert_eq(s.apply_once(term), Apply(Variable(y), [Value(2)]))
}
在 Term[T] 上,apply_once 与 Substitution::apply 一致。关于下游 AST,请参阅适配器教程。
已移除的字段
Substitution 和 GenericSubstitution 的字段 entries 不再公开。请用 get、length 和 to_array 读取替换。