Should Purescript's Partial instead be called Unsafe?
Purescript's Partial typeclass is an interesting thing,
but you may be wondering where the
forall a. ((Partial => a) -> Maybe a) lurks?
Partiality of partial functions are thought to be observable.
We think that partial function
a → b
can be written as
(a → Maybe b)
Purescript's partial functions aren't observable in this way though,
they may become preconditions that aren't encoded in the type at the moment.
unsafeIndex is allowed to return
undefined if it accesses outside the index.
unsafeIndex :: forall a. Partial => Array a -> Int -> a
Partial here is less of
and more of a warning: It may blow on your face.
If you want to catch it, just use the
Perhaps if we just checked the unsafety,
like TheMatten proposed.
But then it'd be similar to using the
index in the first place then.
Lot of the names here are like
unsafe this and that.
Partial have been named
Well there's a likely reason why it wasn't.
Although it would be the exact correct definition,
it could be confused to Haskell's most common way to be labeled unsafe
which is performing I/O, rather than breaking the type system.
Partial be renamed to
Unsafe immediately then?
Nope. The main thing would be,
do you expect it to matter that it has that name,
once you've been explained that it means for more of
an unsafe implementation-leaking partial function
rather than a nice mathematical partial function.
It matters if there would be a feature that would prefer
reassigning the name
Partial to the more nice mathematical partial functions.
But it might not matter if
Partial functions become more partial
and allow their partiality to be observed in the future.
Typeclass assisted transformations to whole programs
There's an one potential feature that might interfere, explained by Conal Elliott in his "Compiling to categories" paper. It allows giving functional programs varying interpretations by transforming from an one category to an another.
You see, category can enclose ideas such as pairs, being a function or what a natural number is. These notions can be transformed into a different category while preserving their overall structure. In the paper Conal proposes how to overload concepts such as a function or a pair, in order to vary interpretation of programs and then make them, for instance, compute incrementally or differentiate an arbitrary function.
One of the potential "categorical transforms" to a program,
is the interpretation of partially defined functions
as morphisms in a Kleisli category of the Maybe -monad.
Because of thoughts like this I've been associating
mathematical functions to
(a → Maybe b) quite strongly.
I was very surprised to see that Purescript's Partial
weren't the partial that I expected.
TL;DR: It probably should be called Unsafe, but there are problems in that approach as well, and it's likely good for now as long as you know what kind of Partial is being meant here.