Here are a few simple functions:
f1 :: () -> ()
f1 () = ()
f2 :: a -> a
f2 a = a
f3 :: a -> (a, a)
f3 a = (a, a)
f4 :: (a, b) -> a
f4 (a, b) = a
All of f1
, f2
, and f3
are able to accept ()
as an input. On the other hand, of course, f4
can't accept ()
; f4 ()
is a type error.
Is it possible to type-theoretically characterize what f1
, f2
, and f3
have in common? Specifically, is it possible to define an acceptsUnit
function, such that acceptsUnit f1
, acceptsUnit f2
, and acceptsUnit f3
are well-typed, but acceptsUnit f4
is a type error -- and which has no other effect?
The following does part of the job, but monomorphizes its input (in Haskell, and I gather in Hindley-Milner), and hence has an effect beyond simply asserting that its input can accept ()
:
acceptsUnit :: (() -> a) -> (() -> a)
acceptsUnit = id
-- acceptsUnit f4 ~> error π
-- acceptsUnit f3 'a' ~> error βΉοΈ
The same monomorphizing, of course, happens in the following. In this case, the annotated type of acceptsUnit'
is its principal type.
acceptsUnit' :: (() -> a) -> (() -> a)
acceptsUnit' f = let x = f () in f
Typeable
on a rank-N type (?) but I'm really unsure about that. β Albert()
as an input state. Another way (seen in the other question) is to treat closed terms as ones whose value doesn't depend on the input. β Crwthcase
), the question doesn't even make sense: the only functions that can accept()
are ones whose first argument is of type()
. β SpyacceptUnit' a a
) ignoring the types. ThenacceptUnit
can take a from-unit argument as its first one and the real argument as its second. β SpyImpredicativeTypes
in GHC 9.0.1, but I don't think that helps. β Hailytakes_unit
, with a typing rule that looks like "ifΞ β’ e : t
andΞ β’ e : () -> t'
thenΞ β’ takes_unit e : t
". (Can't render premises above conclusion in markdown, hopefully you can understand what I intend here.) Of course you'd have to think about whether there's an algorithm that corresponds with that modified type system, but it seems pretty likely to me that there would be one. β Rumpf