MyNixOS website logo
Description

A type level predicate ranging over strict types.

A type class for types T where forall x :: T . rnf x = ⊥ <=> rwhnf x = ⊥

Travis Build Status Hackage Stackage Nightly

strict-types

This package provides two pattern synonyms Strict and Rnf to constrain value strictness.

Use Strict when you can and Rnf when you must.

The Rnf pattern

The Rnf pattern matches every value of a type with an NFData instance, forcing it to rigid normal form before binding it.

> let !(Rnf x) = [trace "One" 1, trace "Two" 2]
One
Two

The bang pattern is needed to force the Rnf x closure to weak head normal form (whnf).

Rnf is very handy to avoid space leaks when working with non-strict data, but forcing structured data with rnf has a cost even if the data is already forced, and it should be avoided in inner loops. Too few Rnf patterns and one risks a space leak, too many and one ends up with squared complexity factors.

The Strict pattern

If our datatypes are strict, then their weak head normal form is already fully evaluated and Rnf is morally just seq. In practice, it depends on what the NFData instance does. One would hope it would be implemented as a noop, but in practice this is not always the case for first order types, and can never be for higher kinded types which cannot make any assumptions about the strictness of their type parameters.

But not all is lost! We can ask the typechecker to inspect the GHC Generics representation for a type to check if it is strict, and avoid calling rnf if that is the case. This is what the Strict pattern synonym does:

> let !(Strict x) = [1, 2]
<interactive>:1:7: error:
    • [Int] has an unnamed lazy field in constructor :
    • In the pattern: Strict y
      In the pattern: !(Strict y)
      In a pattern binding:
        !(Strict y) = [trace "1" 1, trace "2" (2 :: Int)]

Lists are not strict, so the expression above does not type check.

If we define our own strict list datatype with a Generic instance, the type checker can certify the property that !(Strict x) is equivalent to !(Rnf x):

> data StrictList a = Nil | Cons !a (StrictList a) deriving Generic ; infixr :!
> let !(Strict y) = trace "one" 1 :! trace "two" 2 :! Nil
two
one

The StrictType class

Not all strict types derive Generic. For such cases where the type checker is unable to see the strictness information we can "promise" that a type is deep strict by adding an instance of the StrictType class.

Caveats

Non regular recursive types, also known as nested datatypes, will cause the type checker to loop and run out of fuel when trying to prove deep strictness.

FAQ

  • What about the Strict and StrictData pragmas ?

The Strict pragma adds an implicit bang pattern on every binding, but it doesn't force values to normal form.

The StrictData pragma adds an implicit bang pattern on every field of a data type guaranteeing that first order types are strict, but does not help with higher kinded types.

Metadata

Version

0.1.3

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows