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. For example, unsafeIndex is allowed to return Javascript's undefined if it accesses outside the index.

unsafeIndex :: forall a. Partial => Array a -> Int -> a

The Partial here is less of Maybe and more of a warning: It may blow on your face. If you want to catch it, just use the index instead.

Perhaps if we just checked the unsafety, we could use Javascript's exception mechanism to catch Maybe 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 crash, unsafe this and that. Should Partial have been named Unsafe? 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.

Should the 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.

How likely is it that Purescript's compiler would implement Conal's paper? Well one obstacle is that Purescript is doing this thing of constructing Javascript modules, and you can use these modules as long as there is a boundary that only gives you the type and doesn't require you to identify which program is inside the module. These kind of program transformations do force you to identify the program. But you can achieve that by just keeping the modules there but sidestepping them when they need to be provided explicitly.

How about the problem of Purescript's Partiality being inobservable? You've probably heard Javascript's JIT is pretty amazing and one really neat thing there are the fast guard clauses that do cost very little if they pass. There are several ways how this might resolve.

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.

Similar posts