racket - 了解 `case->` 周围的类型检查器行为
问题描述
在 REPL 上,这是我检查类型时得到的car
> car
- : (All (a b) (case-> (-> (Pairof a b) a) (-> (Listof a) a)))
当传递一个可能不存在第二个值的对时,它返回一对中第一个类型的元素。
现在我想在一个映射对列表并获取第一个元素的函数中使用它
(: f (-> (Listof (Pairof Integer Symbol)) (Listof Integer))
当我尝试编写如下函数时,类型检查失败
(define (f l) (map car l))
; stdin::9317: Type Checker: Polymorphic function `map' could not be applied to arguments:
; Types: (-> a c) (Pairof a (Listof a)) -> (Pairof c (Listof c))
; (-> a b ... b c) (Listof a) (Listof b) ... b -> (Listof c)
; Arguments: (All (a b) (case-> (-> (Pairof a b) a) (-> (Listof a) a))) (Listof (Pairof Integer Symbol))
; Expected result: (Listof Integer)
;
; in: (map car l)
但是,如果我将 car 函数包装在具有将输入类型指定为 的效果的 lambda 中car
,那么我的实现类型检查完美
(define (f l) (map (lambda ([x : (Pairof Integer Symbol)]) (car x)) l))
这里到底发生了什么,为什么类型检查器不够聪明,无法从函数参数的类型注释中推断出它应该使用car
函数中两种情况中的第一种?
解决方案
推荐阅读
- android-studio - 我该如何做这个布尔条件?
- mysql - 使用 Pivot 在 MySQL 8.0.17 版中从不同表(联合)进行行到列转换
- javascript - 有没有办法在firebase auth中定义用户?
- amazon-web-services - aws cdk 管道:将合成的 CloudFormation 模板名称和配置文件传递给 DeployCdkStackAction
- python - 如何从麦克风的其他样本中检索音高和响度?
- vue.js - 如何在 Nuxt 应用中禁用打开重定向?
- c - 如何在 C 中使用整数数组作为 char 数组的索引?
- python - 如何从两个模型表单更新 html 表单?视图不能与两个模型一起正常工作
- sql - Oracle 19 - 选择带有任何值的 json 列的行或与数组元素匹配的值
- sql - 如何在 oracle 数据库中为具有复杂聚合的数据透视编写等效的 sql 查询?