-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recursive instances? #32
Comments
The workaround for this case is to use a non-recursive functor with a recursive module as the body: implicit functor C_list(A:C) : C with type a = A.a list =
struct
module rec S : C with type a = A.a list =
struct
type a = A.a list
let f (g : ((implicit B:C) -> B.a -> B.a)) =
function
[] -> []
| x :: xs -> g (implicit A) x :: g (implicit S) xs
end
include S
end |
A slightly lighter version, without explicitly-instantiated type f_arg = (implicit B:C) -> B.a -> B.a
implicit functor C_list(A:C) : C with type a = A.a list = struct
module rec S : C with type a = A.a list = struct
implicit module T = S
type a = A.a list
let f (g : f_arg) : a -> a = function
[] -> []
| x :: xs -> g x :: g xs
end
include S
end |
This is an interesting case. I hadn't really considered wanting to recursively use an instance within itself. It seems that recursive functors should provide support for this, but recursive functors are very restricted in how they can be used -- you basically can't call them from within themselves. They are also not well specified. If recursive functors were improved/fixed then I think this example would just work naturally. So I think that this example provides a use case and some motivation for fixing recursive functors, but that we shouldn't worry about it too much as part of the implicits work. |
I'd like to write something analogous to the following Haskell code
but I don't see how to make recursive instance resolution work. I suppose it should look something like this
but that code doesn't work, of course, because
C_list
is not visible in the callg xs
.Is there some way to bring the implicit C_list instance into scope within its own body?
The text was updated successfully, but these errors were encountered: