Skip to content

Remove specification of Prelude from Chapter 9 - #115

Open
BinderDavid wants to merge 3 commits into
mainfrom
rewrite-section-9
Open

BinderDavid wants to merge 3 commits into
mainfrom
rewrite-section-9

Conversation

@BinderDavid

Copy link
Copy Markdown
Collaborator

Section 9 is currently the location where the public interface and the behaviour of all entities of the Prelude module are documented. Section 9 has the following major issues that this PR addresses:

  • The module Prelude is defined in terms of three other modules PreludeIO, PreludeText and PreludeList as an organizing principle. This organization dates back to Haskell98 which only had a flat module namespace, and does not correspond to how functions and types exposed by the Prelude are nowadays grouped. Modern Haskell and base use modules Control.Monad.IO, Data.List, Text.Read, Text.Show etc. instead. In Expose additional modules from the standard library #111 we already took the steps to adapt to this new organizational principle by exposing these modules.
  • The code used in chapter 9 to specify the Prelude is not valid Haskell but only a kind of PseudoHaskell. This makes it impossible to check the specification for consistency by running it through a compiler when we expand or modify it.
  • PRs such as add Applicative and MonadFail to monad hierarchy #79 or Foldable-Traversable hierarchy #97 which modify the public interface of the Prelude have to apply every change twice: Once to the code in chapter 9, and once in the modules in the stdlib directory. This is extremely error prone and not fun to do :)

The solution that this PR proposes is to repurpose chapter 9 to describe the relationship between the language report and the library report, which is provided as a separate document in the revised report. This entails the following changes:

  • Chapter 9 has been renamed from "Standard Prelude" to "Standard Libraries", with a subsection dedicated to Prelude.
  • Chapter 9 describes that the library report provides an interface specification for every module, and a behavioural specification for a subset of the standard libraries. These behavioural specifications are provided in documentation comments.
  • All code snippets which specify the Prelude have been removed from section 9.

In order to not make this PR too complicated I have left several TODOs which we have to tackle afterwards.

  • The specifications which are now missing in the stdlib have to be added back later. This should look similar in content to what Restore Data.Array specification #40 does.
  • The outcome of the discussion Relationship between language report and library report #50 which is about the relationship between the library report and its implementations should inform the content of chapter 9. I.e. we should specify what it means for an implementation like base to be conforming to the library report.

Comment on lines +13 to +15
These behavioural specifications are given in the form of documentation comments which provide a reference implementation.
Many of these reference implementations are written with clarity rather than efficiency in mind,
and it is not required that the specification be implemented as shown.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, it would be good to spell out how closely implementations are expected to conform to the reference implementation. What changes for efficiency are permitted? Non-strict semantics makes this a bit tricky, because often "as defined as possible" (avoiding unnecessary strictness) may not be quite the same as "most efficient in normal use". Presumably the implementation of a function is required to at least agree with the reference function on all inputs in normal form, but is it allowed to change strictness properties? What about memory usage?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I will add this to the text.

My opinion is that the behavioural specification should not put any restrictions on time or memory behaviour. The reason is simply that the Haskell report currently doesn't specify a call-by-need evaluation strategy, and it therefore isn't possible to do time or space reasoning based solely on the spec.

I think implementations should implement functions with the same strictness behaviour, and the GHC user guide follows this interpretation. In the bugs and infelicities section it says, for example:

splitAt semantics
Data.List.splitAt is more strict than specified in the Report. Specifically, the Report specifies that
splitAt n xs = (take n xs, drop n xs)
which implies that
splitAt undefined undefined = (undefined, undefined)
but GHC’s implementation is strict in its first argument, so
splitAt undefined [] = undefined

of reference implementation. Implementations are bound by strictness
of reference implementation.
@exaexa exaexa added the feature: language report Concerns the language specification in the report label Nov 24, 2026
Many of these reference implementations are written with clarity rather than efficiency in mind,
and it is not required that the specification be implemented as shown.

Implementations must have the same strictness behaviour as the reference implementation.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuing on what @adamgundry commented above -- this certainly sounds somewhat dangerous (as with splitAt above). It might be the case that this clause would imply standardizing inefficient implementations.

What if we allow the implementations to be less strict than the report (thus "less dangerous"), and make sure that everything specified in the library report is at least as strict as in current compilers?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(in the meantime I'd go for temporarily removing the clause so that this can get voted in ;) )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's certainly a tension here, although splitAt is not a very compelling example, because it's not actually clear GHC's version is more efficient than the Report version (#92 (comment)).

My view is that:

  • A library that aims to be a compliant implementation of the library report should agree on strictness with the report.
  • A compiler that aims to be a compliant implementation of the language report may support other base libraries than the one specified in the library report, and ideally should allow users to choose their base libraries, with at least one option being the report standard library.

In practice, I expect this to mean that:

  • base is not completely report-compliant (e.g. splitAt, other strictness changes, and because it exposes additional modules) but its divergences are documented as properties of base rather than GHC;
  • we can provide a haskell2010 library that is more closely report-compliant and is more stable/limited than base; and
  • users of GHC have the option to depend on base or haskell2010.

This means base is free to diverge from the report on strictness (or otherwise) where its maintainers believe that to be a good idea, but users who want to nail down the libraries completely can still do so.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature: language report Concerns the language specification in the report

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants