Why do my top-level functions need signatures in Haskell?

GHC warns that I do not have function signatures at the top level. I don’t understand why they are needed. The problem with their provision is that they are quite complex, like this one (auto-generated):

applyValue :: forall t t1 t2 t3 t4. (t2 -> t) -> (t2 -> t3 -> t4 -> t1) -> t2 -> t3 -> t4 -> (t -> Bool) -> [t1] 

So why don't I add them?

function itself:

 applyValue getValueAt stitchAndMove at fabric mark matchAt = if matchAt (getValueAt at) then [stitchAndMove at fabric mark] else [] 
+6
source share
1 answer
  • As a form of documentation verified by the machine. If you think the type is the correct type by placing it there, ask the compiler to double check that you did not nest your own interface during your subsequent inevitable refactoring sessions.
  • Like human-readable documentation. Although, as you noticed, when you notice that you are writing a terrible machine type, it may be time to think about what abstractions (level type) you need to make it understandable to humans.
  • For haddock. Haddock comments are bound to signature types, not bindings, so unless you specify a type signature, your carefully written documentation will be ignored.
  • To improve the error messages and results of the ghci query: although the actual names of the type variables are irrelevant, GHC tries to save the names when provided by the user. Something like (node -> Bool) -> (edge -> Bool) -> (graph -> Bool) can be much more readable than (t1 -> Bool) -> (t2 -> Bool) -> (t3 -> Bool) , although they are equivalent.
+22
source

Source: https://habr.com/ru/post/956841/


All Articles