ocaml - 类型与列表操作不匹配
问题描述
Ocaml 中的新功能,我正在尝试实现几个函数来管理自定义类型的列表。
我正在尝试管理类型列表,当我需要向列表中添加新元素时,我需要进行一些检查。这是我的实际代码:
open Ast
open Easy_logging
exception DuplicateEntry
let logger = Logging.make_logger "SymbolTable" Debug [Cli Debug]
type variable = {
id: string;
tipe: Ast.typ
}
type dec = {
variables: variable list;
parent: dec option
}
let begin_block table =
if List.length table > 1 then
let last_scope = List.nth table ((List.length table) -1) in
let new_scope = {variables=[]; parent=last_scope} in
new_scope::table
else {variables=[]; parent=None}::table
let add_entry symbol info table =
let tail_scope = List.nth table ((List.length table) - 1) in
{id=symbol; tipe=info}::tail_scope.variables;
logger#debug "Added inside the table the node with id %s" symbol
let rec lookup symbol table = failwith "lookup: Not implemented yet"
我正在尝试实现操作 begin_block 但我收到以下错误:
File "src/symbol_table.ml", line 31, characters 16-21:
31 | new_scope::table
^^^^^
Error: This expression has type dec option list
but an expression was expected of type dec list
Type dec option is not compatible with type dec
Command exited with code 2.
Compilation unsuccessful after building 26 targets (0 cached) in 00:00:01.
在这种情况下,表格是我丢失的东西的列表,但目前我无法找到错误:/,也许这是一个愚蠢的问题。
解决方案
您在这里省略了很多上下文,我必须删除对缺失模块的引用才能重现,这意味着我做了几个可能错误的假设。
鉴于此,我的猜测是您复制了最初使用 Base 或 Core 标准库替换的代码,其中List.nth
函数返回一个'a option
,而不是标准 OCaml 实现,如果给定索引超出范围,则会引发异常.
我这么认为的原因是该parent
字段dec
具有类型dec option
并且是直接分配的last_scope
,这意味着last_scope
必须具有相同的类型。如果List.nth
有类型'a list -> int -> 'a
,则'a
必须有类型dec option
,这意味着table
必须有类型dec option list
。而且您不能将 adec
添加到 a dec option list
,因此会出现错误。
最后,找出此类问题原因的一个好方法是通过注释变量的类型来明确假设。例如,在table
此处注释类型会给您一个不同的错误,将其范围缩小到last_scope
具有类型但在分配给时dec
期望具有类型。dec option
parent
推荐阅读
- windows - Git 和 Vim - git diff 格式错误,但 git commit --amend 正确
- javascript - 超时完成后多次运行去抖动功能
- python - tqdm 不计算 jupyter notebook 中的迭代次数
- android - 以编程方式更改软键盘中候选视图的颜色
- android - 有没有办法通过使用无障碍服务来检测屏幕/声音何时投射到另一台设备?
- github - github上的文件系统问题 - Discord.Js
- python - 从python中的列表中打印行空间
- java - 如何正确使用导入构建文件的目标?
- ios - Apple Catalyst 是否支持 iPhone
- python - 选择三个国家。对于每个国家/地区,找出最致命的日期(以最高新死亡人数衡量)。需要帮助用 Python 编写逻辑