HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Moduleprettyprinter-configurable-1.36.0.0Haskell2010

Text.Fixity

Machinery for deciding whether an expression needs to be wrapped in parentheses or not.

  • 7 types
  • 7 values
typetype Precedence = Double
#

Fractional precedence, so that it's always possible to squeeze an operator precedence between two existing precedences. Ranges over [-20, 120]. A normal operator should have a precedence within [0, 100). It might be useful to have a negative precedence if you're trying to model some already existing system, for example in Haskell ($) has precedence 0, but clearly if b then y else f $ x should be rendered without any parens, hence the precedence of if_then_else_ is less than 0, i.e. negative.

The precedence of juxtaposition is 100. Normally you want juxtaposition to have the highest precedence, but some languages have operators that bind tighter than juxtaposition, e.g. Haskell's postfix _{_}: f z { x = y } means f (z {x = y}).

datadata FixityOver prec
#

Fixity of an operator.

We allow unary operators to have associativity, because it's useful to distinguish between an expression like -(-x) (unary minus, left-associative) and ~~b (boolean NOT, right-associative).

Associativity of unary operators also matters when pretty-printing expressions like (-x) + y, which is pretty-printed as -x + y, assuming unary minus has the same fixity as + (and both the operators are left-associative). I.e. unary minus is handled just like the binary one: (0 - x) + y is pretty-printed as 0 - x + y.

Postfix operators are handled similarly. E.g. if ! is left-associative, then (x!)! is pretty-printed as x!! and if it's right-associative -- (x!)!.

The data type is parameterized, so that the user can choose precedence to be integer/fractional, bounded/unbounded, etc (we could also allows operators to be partially or totally ordered, but at the moment prec is required to implement Ord, i.e. it has to be totally ordered). By default we go with bounded fractional precedence, see the main Text.Fixity module.

Instances2Eq, Show
  • Eq prec => Eq (FixityOver prec)Defined in prettyprinter-configurable-1.36.0.0 · Text.Fixity.Internal
  • Show prec => Show (FixityOver prec)Defined in prettyprinter-configurable-1.36.0.0 · Text.Fixity.Internal
datadata Direction
#

Direction in which pretty-printing goes. For example in x + y x is pretty-printed to the left of + and y is pretty-printed to the right of +.

Instances2Eq, Show
  • Eq DirectionDefined in prettyprinter-configurable-1.36.0.0 · Text.Fixity.Internal
  • Show DirectionDefined in prettyprinter-configurable-1.36.0.0 · Text.Fixity.Internal
datadata RenderContextOver prec
#

A context that an expression is being rendered in.

Instances3HasRenderContext, Eq, Show
valueencloseIn
  1. :: Ord prec
  2. => (a -> a)

    Enclose a value of type a in parens.

  3. -> RenderContextOver prec

    An outer context.

  4. -> FixityOver prec

    An inner fixity.

  5. -> a
  6. -> a
#

Enclose an a (using the provided function) if required or leave it as is. The need for enclosing is determined from an outer RenderContext and the inner fixity.

valuebotFixity :: Fixity
#

A fixity with the lowest precedence. When used as a part of an outer context, never causes addition of parens.

valueunitFixity :: Fixity
#

The fixity of a unitary expression which is safe to render without parens in any context.

valuetopFixity :: Fixity
#

A fixity with the highest precedence. When used as a part of an outer context, always causes addition of parens.