Say that I want to encode cons as:
def cons(a, b):
return lambda f : f(a, b)
I would like to add typing to the cons above. The obvious typing would be:
T = TypeVar('T')
U = TypeVar('U')
V = TypeVar('V')
def cons(a: T, b: U) -> Callable[[Callable[[T, U], V]], V]:
return lambda f : f(a, b)
However, the above doesn't type check in pyre which gives:
cons.py:7:24 Invalid type variable [34]: The type variable `Variable[V]` isn't present in the function's parameters.
I assume that what I need is a universal quantification of the return type of the function.
Is there a way to express this in the Python type-system?
Turns out that you can get enough type-safety for this on Pyre by creating a protocol to describe the type of the continuation produced by
cons:This hides the
Vfrom the type signature and allows us to use it as the return type ofcons:We can then type
carandcdras well (or anything that passes a lambda to the returnedConsProtocol):Note that we still need to explicitly add a type annotation to the
contas it cannot be inferred correctly by Pyre. Revealing the type gives:Issue where this was explained on GitHub: https://github.com/facebook/pyre-check/issues/771