Explicit type recursion in F#

我的梦境 提交于 2019-12-06 01:00:16

问题


Inspired by this question:

Is explicit type recursion possible in F#?

type 'a Mu = In of 'a Mu 'a

let unIn (In x) = x

This code unfortunatly gives "Type parameter cannot be used as type constructor.

Remarks: This construct is used in the paper Functional Programming with Overloading and Higher-Order Polymorphism, for example.

Example of usage (taken from here):

type ('a, 'b) ListX =
    | Nil
    | Cons of 'a * 'b

type 'a List = ListX Mu

回答1:


No, this is not possible. Specifically, generics in F# have the same limitation as the CLR, namely a <T> or an <'a> must have kind " * ". This same limitation is what means you cannot author "type classes" directly in F#, since e.g. "Monad m" would take a higher-kinded argument 'm' (e.g. "* -> *", where e.g. 'list' and 'option' could be instances, those each themselves being generic type constructors), but this is not allowed.



来源:https://stackoverflow.com/questions/1253374/explicit-type-recursion-in-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!