Ocaml不正确的类型推断
我正在尝试使用Ocaml制作React的类型化版本作为练习。 为了使它更具功能性,我将一条记录作为参数传递给renderOcaml不正确的类型推断,ocaml,type-inference,Ocaml,Type Inference,我正在尝试使用Ocaml制作React的类型化版本作为练习。 为了使它更具功能性,我将一条记录作为参数传递给render type ('props,'state) reactInstance = { props: 'props; state: 'state; updater: 'a . ('props,'state) reactInstance -> 'a -> 'state -> unit;} and ('props,'state) reactClass =
type ('props,'state) reactInstance =
{
props: 'props;
state: 'state;
updater: 'a . ('props,'state) reactInstance -> 'a -> 'state -> unit;}
and ('props,'state) reactClass =
{
getInitialState: unit -> 'state;
render: ('props,'state) reactInstance -> element;}
module type ComponentBluePrint =
sig
type props
type state
val getInitialState : unit -> state
val render : (props,state) reactInstance -> element
end
module type ReactClass =
sig
type props
type state
val mkUpdater :
props ->
((props,state) reactInstance -> 'e -> state) ->
(props,state) reactInstance -> 'e -> unit
val reactClass : (props,state) reactClass
end
module CreateComponent(M:ComponentBluePrint) =
(struct
include M
let rec mkUpdater props f i e =
let nextState = f i e in
let newInstance =
{ props; state = nextState; updater = (mkUpdater props) } in
()
let reactClass =
{ render = M.render; getInitialState = M.getInitialState }
end : (ReactClass with type props = M.props and type state = M.state))
有一件事我不明白,为什么编译器不能在let newInstance={props;state=nextState;updater=(mkUpdater props)}
中推断updater=(mkUpdater props)
的类型
Error: Signature mismatch:
Values do not match:
let mkUpdater :
props =>
(reactInstance props state => '_a => '_b) =>
reactInstance props state => '_a => unit
is not included in
let mkUpdater :
props =>
(reactInstance props state => 'e => state) =>
reactInstance props state => 'e => unit
“a”和“e”有什么区别?
在我看来完全一样。如何进行此类型检查?类型变量
“\u A
(实际字母不重要,关键是下划线)是所谓的弱类型变量。这是一个无法概括的变量,即只能用一种具体类型替换。它类似于一个可变值,但属于类型领域
用弱类型变量表示的类型不包括在用泛型类型变量表示的类型中。此外,它甚至无法逃避编译单元,应该隐藏或具体化
弱类型变量是在表达式不是纯值(按语法定义)时创建的。通常,它要么是一个函数应用程序,要么是一个抽象。当您通过枚举所有函数参数将部分应用的函数替换为普通函数应用程序时,通常可以通过进行所谓的eta扩展来消除弱类型变量,例如,updater=(fun props f i e->mkUpdater props f i e)
您给出的错误不是文件产生的实际错误。正确的错误是“error:此表达式的类型为('a->'b->'c)->'a->'b->'d,但表达式的类型应为('e,'c)reactInstance->'f->'c->unit type'a->'b->'c与类型('e,'c)reactInstance)不兼容文件中有多个类似错误的内容,但是,如果不知道操作的假定语义是什么,就很难解决这些问题。不过,我首先想到的是更新程序字段中的'a.
,您确定要在此处使用forall吗?它似乎不是很有用。