如何解决OCaml 中的简单多态类型注释
在 ocaml 中进行此类型检查是否需要一些注释?
library(data.table)
library(janitor)
library(purrr)
filenames <- list.files(pattern = "*.txt",recursive = TRUE,full.names = TRUE)
# read the first row of data to capture and clean the column names
clean_col_names <- function(filename){
colnames(janitor::clean_names(fread(filename,nrow = 1)))
}
clean_column_names <- map(.x = filenames,.f = clean_col_names)
# clean the colSelect vector
colSelect <- janitor::make_clean_names(c("Date","Time","Timestamp","PM2_5(ug/m3)","PM10(ug/m3)","PM01(ug/m3)","Temperature(C)","Humidity(%RH)","CO2(ppm)"))
# match each set of column names against the clean colSelect
select_indices <- map(.x = clean_column_names,.f = function(cols) match(colSelect,cols))
# use map2 to read only the matched indexes for each column
data <- purrr::map2(.x = filenames,.y = select_indices,~fread(input = .x,select = .y))
Haskell 中同样的代码需要类型注解
type ('a,'b) roll = Roll of (('b,'a) roll -> 'b)
let unroll (Roll f) = f
(* nope-
This definition has type ('b,'b) roll -> ('b,'b) roll
which is less general than 'c. ('b,'c) roll -> ('b,'c) roll *)
let rec (-:) : 'c. ('b,'c) roll -> ('a,'b) roll -> ('a,'c) roll = fun f g ->
Roll (fun k -> unroll f (g -: k))
(* nope -- unifies to 1 type !
let rec (-:) = fun f g ->
Roll (fun k -> unroll f (g -: k))
*)
解决方法
通用量化所有类型变量,就像在等效的 Haskell 代码中一样,似乎工作正常:
let rec (-:) : 'a 'b 'c. ('b,'c) roll -> ('a,'b) roll -> ('a,'c) roll = fun f g ->
Roll (fun k -> unroll f (g -: k))
val ( -: ) : ('b,'c) roll = <fun>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。