You're thinking "partially applied function", which uses very similar terms but means something completely unrelated.
A partially applied function is a function which is applied to a subset of its formal arguments, yielding a function taking the leftover arguments.
A partial function is contrasted with a total function and the term is about the relation between inputs and outputs, namely does every possible input value yield an output. The example of `first` used in the essay is pretty common because it's quite clear: given `first :: [a] -> a`, what happens if you call `first` with an empty list? Well it can't succeed, it can't just give you an `a` out of nowhere because it doesn't have anything to do that. So despite an empty array being a possible input value, there is no output for it: it is a partial function, it only partially maps its inputs to its outputs.
`first :: [a] -> Maybe a` would be total: in the case of an empty input it returns `None`, otherwise it returns `Some a`.
The downside here is that rather than blowing up your application immediately upon a bug it blows up your application somewhere else depending on your logic.
This can be a useful thing. You can write powerful and elegant algorithms that avoid error management because the bottom values never actually get used. But most people aren't doing that and instead these are time bombs.
Agreed. This would be with some escape hatch function, maybe even from Unsafe.
But having head in Prelude, without huge warning in the docs, without deprecation warnings, it just, well, not very Haskelly, I'd say.
divTenBy :: Double -> Double
divTenBy n = 10 / n
If you actually call the above function you get a runtime exception. We really don't like functions that do this; they are called partial. $ python3
Python 3.9.1 (default, Feb 3 2021, 07:04:15)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>It's "partial" because it's not defined for 0.
That would be the "right way" to fix this.
(or create a type NotNilDouble, lol)
It's clearer for partial function "head :: [a] -> a", which takes the first of a list if it exists, and explodes without dignity if the list is empty (this is what makes head partial).
A proposal is "head :: [a] -> Maybe a", so head returns Nothing when the list is empty.
https://www.idris-lang.org/docs/current/prelude_doc/docs/Pre...'
https://www.idris-lang.org/docs/current/contrib_doc/docs/Dat...
f :: Int -> Bool
f 0 = True
f 1 = False
f n = f (n + 1)
is surjective onto Bool but also partial (doesn't return for n > 1). In Haskell we say that when a function doesn't return, the output is bottom, written as ⊥.
You could say that a function f : A -> B is partial if f^{-1}(B \ ⊥) is surjective.
No; a partial function is one that isn't well-defined for the whole of its domain. So, as per the article, head is a partial function because its type signature of [a] -> a implies that all arrays have a head value. But head [] does not. It's a partial function.
The type system isn't helping you at all there.
The feeling I had was that much of the prelude stuff was there to provide for beautiful, terse examples of functional programming and less to protect a software engineer.
I've just started my Haskell journey and the undefined paths through partial function implementation caught me by surprise.
Just skimming Wikipedia it looks like I would want to use "total/strong functional programming" but apparently "total functional programming is not Turing-complete"
https://en.wikipedia.org/wiki/Total_functional_programming
Also found this on the Haskell site after some more googling:
It isn't, but usually Turing completeness isn't what you want. Take a look at Idris, where functions may explicitly be total or not-necessarily-total.
Last time I wrote any non-trivial Haskell was in 2014, so a long time ago, but I found that my biggest problem with it at the time was the really huge variety of Haskell in the wild. If you're doing simple stuff, you probably stick to the prelude and you'll be happy. But if you're doing anything that's a bit complex, you'll end up seeing hundreds of mini-dialects of Haskell in the wild, so much so that I found it really difficult as a newcomer to understand code on the net. In many cases it's almost like a different language completely, what with the user-defined infix functions, tons of currying everywhere, laziness, and the like, made it very difficult to follow code paths.