packages feed

papa-0.7.0: test/Inspection.hs

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall -O -fplugin Test.Inspection.Plugin #-}

{- HLINT ignore "Eta reduce" -}
-- Reason: each function is applied to all its arguments, as at a call site,
-- so that GHC inlines the total function into it.

-- | The cost of totality, checked in the optimised Core of the total
-- functions. The module compiles only if every obligation holds, so the test
-- is its compilation. The functions are exported, so that GHC keeps them for
-- the inspection.
--
-- At 'Int' and 'Word', each 'NonZero' division function has no type class
-- dictionaries left (it is specialised) and mentions no 'Integer' (the test
-- of the divisor for @-1@ allocates nothing). The same holds for 'pow',
-- 'intCast', and the total conversions from 'Double'; and the 'NonZero'
-- functions at 'Double' and 'Rational' are specialised.
--
-- Equality of Core with the same function written by hand with
-- "Papa.Unsafe.Numeric" (@===@) is not checked: at 'Int' the two differ only
-- in where GHC places "Prelude"'s unreachable error branches
-- (@overflowError@, @divZeroError@), which differs between the library and
-- this module; at 'Double', 'divideNonZero' compiles to @GHC.Float.divideDouble@
-- under a coercion, which a hand-written version cannot state, since the
-- 'NonZero' constructor is not exported. The benchmarks measure the cost.
module Main (
  main,
  divInt,
  modInt,
  quotInt,
  remInt,
  divModInt,
  quotRemInt,
  divWord,
  modWord,
  quotWord,
  remWord,
  divModWord,
  quotRemWord,
  powInt,
  powWord,
  ratioInteger,
  divideDouble,
  recipDouble,
  powNonZeroDouble,
  truncateDoubleInt,
  roundDoubleInt,
  floorDoubleInt,
  ceilingDoubleInt,
  intCastInt32Int64,
) where

import Papa
import Test.Inspection (hasNoType, hasNoTypeClasses, inspect)

divInt :: Int -> NonZero Int -> Int
divInt x y =
  divNonZero x y

modInt :: Int -> NonZero Int -> Int
modInt x y =
  modNonZero x y

quotInt :: Int -> NonZero Int -> Int
quotInt x y =
  quotNonZero x y

remInt :: Int -> NonZero Int -> Int
remInt x y =
  remNonZero x y

divModInt :: Int -> NonZero Int -> (Int, Int)
divModInt x y =
  divModNonZero x y

quotRemInt :: Int -> NonZero Int -> (Int, Int)
quotRemInt x y =
  quotRemNonZero x y

divWord :: Word -> NonZero Word -> Word
divWord x y =
  divNonZero x y

modWord :: Word -> NonZero Word -> Word
modWord x y =
  modNonZero x y

quotWord :: Word -> NonZero Word -> Word
quotWord x y =
  quotNonZero x y

remWord :: Word -> NonZero Word -> Word
remWord x y =
  remNonZero x y

divModWord :: Word -> NonZero Word -> (Word, Word)
divModWord x y =
  divModNonZero x y

quotRemWord :: Word -> NonZero Word -> (Word, Word)
quotRemWord x y =
  quotRemNonZero x y

powInt :: Int -> Word -> Int
powInt x y =
  pow x y

powWord :: Word -> Word -> Word
powWord x y =
  pow x y

ratioInteger :: Integer -> NonZero Integer -> Rational
ratioInteger x y =
  ratioNonZero x y

divideDouble :: Double -> NonZero Double -> Double
divideDouble x y =
  divideNonZero x y

recipDouble :: NonZero Double -> Double
recipDouble x =
  recipNonZero x

powNonZeroDouble :: NonZero Double -> Int -> Double
powNonZeroDouble x y =
  powNonZero x y

truncateDoubleInt :: Double -> Maybe Int
truncateDoubleInt x =
  truncateMaybe x

roundDoubleInt :: Double -> Maybe Int
roundDoubleInt x =
  roundMaybe x

floorDoubleInt :: Double -> Maybe Int
floorDoubleInt x =
  floorMaybe x

ceilingDoubleInt :: Double -> Maybe Int
ceilingDoubleInt x =
  ceilingMaybe x

intCastInt32Int64 :: Int32 -> Int64
intCastInt32Int64 x =
  intCast x

-- Int: no dictionaries, no Integer.
$(inspect (hasNoTypeClasses 'divInt))
$(inspect ('divInt `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'modInt))
$(inspect ('modInt `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'quotInt))
$(inspect ('quotInt `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'remInt))
$(inspect ('remInt `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'divModInt))
$(inspect ('divModInt `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'quotRemInt))
$(inspect ('quotRemInt `hasNoType` ''Integer))

-- Word: no dictionaries, no Integer.
$(inspect (hasNoTypeClasses 'divWord))
$(inspect ('divWord `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'modWord))
$(inspect ('modWord `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'quotWord))
$(inspect ('quotWord `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'remWord))
$(inspect ('remWord `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'divModWord))
$(inspect ('divModWord `hasNoType` ''Integer))
$(inspect (hasNoTypeClasses 'quotRemWord))
$(inspect ('quotRemWord `hasNoType` ''Integer))

-- pow and ratioNonZero: specialised.
$(inspect (hasNoTypeClasses 'powInt))
$(inspect (hasNoTypeClasses 'powWord))
$(inspect (hasNoTypeClasses 'ratioInteger))

-- Double: specialised.
$(inspect (hasNoTypeClasses 'divideDouble))
$(inspect (hasNoTypeClasses 'recipDouble))
$(inspect (hasNoTypeClasses 'powNonZeroDouble))

-- The total conversions from Double: specialised.
$(inspect (hasNoTypeClasses 'truncateDoubleInt))
$(inspect (hasNoTypeClasses 'roundDoubleInt))
$(inspect (hasNoTypeClasses 'floorDoubleInt))
$(inspect (hasNoTypeClasses 'ceilingDoubleInt))

-- intCast: Prelude's fromIntegral, specialised, through no Integer.
$(inspect (hasNoTypeClasses 'intCastInt32Int64))
$(inspect ('intCastInt32Int64 `hasNoType` ''Integer))

-- | The obligations are checked when this module compiles.
main :: IO ()
main =
  pure ()