ppad-fixed-0.1.0: Large fixed-width words and constant-time arithmetic.
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellNone
LanguageHaskell2010

Data.Choice

Description

Constant-time choice.

Synopsis

Choice

data Choice :: TYPE 'WordRep Source #

Constant-time choice, encoded as a mask.

Note that Choice is defined as an unboxed newtype, and so a Choice value cannot be bound at the top level. You should work with it locally in the context of a computation.

It's safe to decide a choice, reducing it to a Bool, at any time, but the general encouraged pattern is to do that only at the end of a computation.

>>> decide (or# (false# ()) (true# ()))
True

true# :: () -> Choice Source #

Construct the truthy value.

>>> decide (true# ())
True

false# :: () -> Choice Source #

Construct the falsy value.

>>> decide (false# ())
False

decide :: Choice -> Bool Source #

Decide a Choice by reducing it to a Bool.

>>> decide (true# ())
True

to_word# :: Choice -> Word# Source #

Convert a Choice to an unboxed Word#.

MaybeWord#

newtype MaybeWord# :: TYPE ('TupleRep '['WordRep, 'WordRep]) Source #

Constructors

MaybeWord# (# Word#, Choice #) 

MaybeWide#

newtype MaybeWide# :: TYPE ('TupleRep '['TupleRep '['WordRep, 'WordRep], 'WordRep]) Source #

Constructors

MaybeWide# (# (# Word#, Word# #), Choice #) 

Construction

from_word_mask# :: Word# -> Choice Source #

Construct a Choice from an unboxed mask.

The input is not checked.

>>> decide (from_word_mask# 0##)
False
>>> decide (from_word_mask# 0xFFFFFFFFF_FFFFFFFF##)
True

from_word# :: Word# -> Choice Source #

Construct a Choice from an unboxed word, which should be either 0## or 1##.

The input is not checked.

>>> decide (from_word# 1##)
True

from_word_nonzero# :: Word# -> Choice Source #

Construct a Choice from a nonzero unboxed word.

The input is not checked.

>>> decide (from_word_nonzero# 2##)
True

from_word_eq# :: Word# -> Word# -> Choice Source #

Construct a Choice from an equality comparison.

>>> decide (from_word_eq# 0## 1##)
False
decide (from_word_eq# 1## 1##)
True

from_word_le# :: Word# -> Word# -> Choice Source #

Construct a 'Choice from an at most comparison.

>>> decide (from_word_le# 0## 1##)
True
>>> decide (from_word_le# 1## 1##)
True

from_word_lt# :: Word# -> Word# -> Choice Source #

Construct a Choice from a less-than comparison.

>>> decide (from_word_lt# 0## 1##)
True
>>> decide (from_word_lt# 1## 1##)
False

from_word_gt# :: Word# -> Word# -> Choice Source #

Construct a Choice from a greater-than comparison.

>>> decide (from_word_gt# 0## 1##)
False
>>> decide (from_word_gt# 1## 1##)
False

from_wide# :: (# Word#, Word# #) -> Choice Source #

Construct a Choice from a two-limb word, constructing a mask from the lower limb, which should be 0## or 1##.

The input is not checked.

>>> decide (from_wide# (# 0##, 1## #))
False

from_wide_le# :: (# Word#, Word# #) -> (# Word#, Word# #) -> Choice Source #

Construct a Choice from an at most comparison on a two-limb unboxed word.

>>> decide (from_wide_le# (# 0##, 0## #) (# 1##, 0## #))
True
>>> decide (from_wide_le# (# 1##, 0## #) (# 1##, 0## #))
True

Manipulation

or# :: Choice -> Choice -> Choice Source #

Logical disjunction on Choice values.

and# :: Choice -> Choice -> Choice Source #

Logical conjunction on Choice values.

xor# :: Choice -> Choice -> Choice Source #

Logical inequality on Choice values.

not# :: Choice -> Choice Source #

Logically negate a Choice.

ne# :: Choice -> Choice -> Choice Source #

Logical inequality on Choice values.

eq# :: Choice -> Choice -> Choice Source #

Logical equality on Choice values.

Constant-time Selection

select_word# :: Word# -> Word# -> Choice -> Word# Source #

Select an unboxed word, given a Choice.

select_wide# :: (# Word#, Word# #) -> (# Word#, Word# #) -> Choice -> (# Word#, Word# #) Source #

Select an unboxed two-limb word, given a Choice.

select_wider# :: (# Word#, Word#, Word#, Word# #) -> (# Word#, Word#, Word#, Word# #) -> Choice -> (# Word#, Word#, Word#, Word# #) Source #

Select an unboxed four-limb word, given a Choice.

Constant-time Equality

eq_word# :: Word# -> Word# -> Choice Source #

Compare unboxed words for equality in constant time.

>>> decide (eq_word# 0## 1##)
False

eq_wide# :: (# Word#, Word# #) -> (# Word#, Word# #) -> Choice Source #

Compare unboxed two-limb words for equality in constant time.

>>> decide (eq_wide (# 0##, 0## #) (# 0##, 0## #))
True

eq_wider# :: (# Word#, Word#, Word#, Word# #) -> (# Word#, Word#, Word#, Word# #) -> Choice Source #

Compare unboxed four-limb words for equality in constant time.

>>> let zero = (# 0##, 0##, 0##, 0## #) in decide (eq_wider# zero zero)
True