haskell-issues icon indicating copy to clipboard operation
haskell-issues copied to clipboard

Improve the documentation of partial functions in base

Open sjakobi opened this issue 8 years ago • 9 comments

  • Emphasize in which cases the function will result in an error.

  • Point at total alternatives, e.g. head's documentation should mention listToMaybe.

sjakobi avatar Dec 29 '16 02:12 sjakobi

I could do it (I recently made a patch to base and it wasn't hard) but first we should find a list of partial functions in base. Is there one somewhere?

neongreen avatar Jan 01 '17 19:01 neongreen

fromJust and head are eventually implemented using errorWithoutStackTrace, so you could search its call history in base source.

Gurkenglas avatar Jan 01 '17 20:01 Gurkenglas

Makes sense, thanks.

neongreen avatar Jan 01 '17 20:01 neongreen

I could do it (I recently made a patch to base and it wasn't hard)

Sounds great! I think it's quite a bit of work, but definitely worth it!

There is also the issue of type class instances with partial methods. For example succ :: Integer -> Integer is for most purposes quite total while succ :: Bool -> Bool isn't.

Instances can have their own haddocks like the Read instance here but individual instance methods can apparently not.

sjakobi avatar Jan 02 '17 15:01 sjakobi

I'm sleepy so I'll just dump the list here:

errorWithoutStackTrace:
  • printf
  • read
  • gather
  • showInt, showIntAtBase
  • intToDigit, digitToInt
  • mallocForeignPtr, mallocForeignPtrBytes, mallocForeignPtrAlignedBytes, 
    mallocPlainForeignPtr, mallocPlainForeignPtrBytes, 
    mallocPlainForeignPtrAlignedBytes, addForeignPtrFinalizer
  • (!!)
  • (^)
  • chr
  • NonEmpty: fromList, (!!)
  • stimes, stimesMonoid, stimesIdempotentMonoid, stimesIdempotent
  • bitSize @Integer, @Natural
  • complement @Natural, pred @Natural
  • fromJust
  • the
  • registerDelay
  • maximumBy, minimumBy, genericIndex, foldr1, foldl1, minimum, maximum
  • succ, pred, toEnum, fromEnum
  • repConstr, dataTypeConstrs, indexConstr, constrIndex, 
    maxConstrIndex, mkIntegralConstr, mkRealConstr, mkCharConstr
  • gunfold for various types (?)
  • readControlMessage, sendMessage
  • newDefaultBackend
  • dynApp
  • threadWaitRead, threadWaitWrite, threadWaitReadSTM, 
    threadWaitWriteSTM on Windows
  • mfix @Maybe, @Either

errorEmptyList:
  • head, tail, init, last, foldl1, foldl1', foldr1, maximum, minimum, cycle

error:
  • lots of functions in Data.Bifoldable

Probably missed something. Also some of those already have warnings, though it might be nice to make them more unified.

neongreen avatar Jan 02 '17 23:01 neongreen

https://hackage.haskell.org/package/base-4.9.0.0/docs/src/Data.Foldable.html#minimum

I see no errorEmptyList.

Gurkenglas avatar Jan 02 '17 23:01 Gurkenglas

The Foldable [] instance uses maximum from GHC.List, which uses errorEmptyList.

neongreen avatar Jan 02 '17 23:01 neongreen

FWIW the changes can now also be submitted via https://github.com/ghc/ghc/pulls.

sjakobi avatar Jan 24 '17 06:01 sjakobi

How about adding Liquid Haskell type signatures to constrain the domain of the partial functions and make them total?

I believe that Liquid type specifications

  • are compact forma of documentation and also
  • are machine checked.

We have already specified many Base functions in here

https://github.com/ucsd-progsys/liquidhaskell/tree/develop/include

nikivazou avatar Feb 07 '17 17:02 nikivazou