[Haskell-cafe] Re: [Haskell-beginners] Just how unsafe is unsafe

Colin Paul Adams colin at colina.demon.co.uk
Fri Feb 6 10:10:11 EST 2009


>>>>> "Roel" == Roel van Dijk <vandijk.roel at gmail.com> writes:

    Roel> On Fri, Feb 6, 2009 at 1:00 PM, Antoine Latter <aslatter at gmail.com> wrote:
    >> Tangential to all of this - sometimes my unsafeXXX functions
    >> are pure, but partial.  So I'll have:
    >> 
    >> foo :: a -> b -> Maybe c
    >> 
    >> and
    >> 
    >> unsafeFoo :: a -> b -> c

    Roel> I use the "unsafe" prefix in the same way. For me it means
    Roel> 'assume that preconditions hold'. If the preconditions do
    Roel> not hold and you evaluate an unsafe function anyway I would
    Roel> expect an error, as opposed to an exception. I have done
    Roel> that in my (tiny) roman numerals package.

    Roel> -- simplified toRoman :: Int -> Either String Int
    Roel> unsafeToRoman :: Int -> String

    Roel> The first function is very clear about the fact that
    Roel> something can go wrong. If you provide it with a value of
    Roel> (-3) it will (hopefully) produce something like 'Left "no
    Roel> negative numbers allowed"'. The second function hides this
    Roel> fact and will result in a (uncatchable) runtime error. It is
    Roel> still a pure function, but preventing errors is now the
    Roel> responsibility of whoever evaluates it.

Do you document the preconditions?

It seems to me that this is more useful than naming a function
unsafeXXX.

I was using comments to document the contracts on my functions, but I
have just found about about ESC/Haskell, so I am now using the
contract notation of that (not yet released) tool.

See http://www.cl.cam.ac.uk/~nx200/
-- 
Colin Adams
Preston Lancashire


More information about the Beginners mailing list