Haskell中StateEither类型等价性的替换步骤问询
StateEither类型的转换步骤
我们基于给定的State定义,一步步展开StateEither的类型:
已知定义
首先明确原始的State新类型定义:
newtype State s a = State (s -> (s, a))
初始的StateEither定义为:
newtype StateEither s e a = StateEither (State s (Either e a))
具体替换步骤
步骤1:匹配
State的类型参数State定义中的类型变量a是占位符,可被任意类型替换。在State s (Either e a)里,我们把State的a替换成了Either e a。步骤2:展开
State的实例
根据State的定义,将State s (Either e a)中的State构造器展开——把State定义里的a全部替换为Either e a,得到:State (s -> (s, Either e a))步骤3:替换
StateEither中的内容
将初始StateEither定义里的State s (Either e a),替换为上面展开后的底层函数类型(去掉外层的State构造器),最终得到:newtype StateEither s e a = StateEither (s -> (s, Either e a))
内容的提问来源于stack exchange,提问作者Ben Weaver
相关产品推荐
相关产品推荐

