I would like to suggest adding a typeof
functionality to type definitions.
Example:
Here we use the same function type twice. (function bodies can be ignored).
f :: Int -> Int -> Int
f _ _ = 1
collectFs :: Array (Int -> Int -> Int) -> Int
collectFs _ = 1
With the proposal:
f :: Int -> Int -> Int
f _ _ = 1
collectFs :: Array (typeof f) -> Int
collectFs _ = 1
This could be syntactic sugar that resolves to:
type Typeof_F = Int -> Int -> Int
f :: Typeof_F
f _ _ = 1
collectFs :: Array Typeof_F -> Int
collectFs _ = 1
Motivation:
One advantage would be that you could take function types from packages without having to write them explicitly
import Some.Package ( f )
collectFs :: Array (typeof f) -> Int
collectFs _ = 1
myF :: typeof f
myF a b = 1
polymorphic types:
See this function
f2 :: forall a. a -> a -> Int
f2 x y = 1
collectF2s :: forall b. Array (b -> b -> Int) -> Int
collectF2s xs = 1
with the proposal:
f2 :: forall a. a -> a -> Int
f2 x y = 1
collectF2s :: forall b. Array (typeof f2 b) -> Int
collectF2s xs = 1
could be resolved to
type Typeof_F2 a = a -> a -> Int
f2 :: forall a. Typeof_F2 a
f2 x y = 1
collectF2s :: forall b. Array (Typeof_F2 b) -> Int
collectF2s xs = 1
Of course, it saves writing work. But above all, it saves me having to invent names for things like ‘Typeof_X’.
Furthermore, in the example with imported function:
import Some.Package ( f )
collectFs :: Array (typeof f) -> Int
collectFs _ = 1
If the package has been updated with a breaking change for f
, an error occurs within collectFs
because the type acts as a dependency.
Further variants of the wording
like
collectFs :: Array (like f) -> Int
see
collectFs :: Array (see f) -> Int
Thank you for reading.