Safe Haskell | None |
---|
Libraries.Synthesis.Ring
Contents
- Rings
- Rings with particular elements
- Rings with particular automorphisms
- Normed rings
- Floor and ceiling
- Particular rings
- The ring ℤ₂ of integers modulo 2
- The ring D of dyadic rationals
- The ring ℚ of rational numbers
- The ring R[√2]
- The ring ℤ[√2]
- The ring ℤ[1/√2]
- The field ℚ[√2]
- The ring R[i]
- The ring ℤ[i] of Gaussian integers
- The ring ℤ[1/√2, i]
- The ring ℚ[√2, i]
- The ring ℂ of complex numbers
- The ring R[ω]
- The ring ℤ[ω]
- The ring D[ω]
- The field ℚ[ω]
- Conversion to dyadic
- Real part
- Rings of integers
- Common denominators
- Conversion to EComplex
- Parity
- Auxiliary functions
Description
This module provides type classes for rings. It also provides several specific instances of rings, such as the ring ℤ₂ of integers modulo 2, the ring ℚ of rational numbers, the ring ℤ[½] of dyadic fractions, the ring ℤ[i] of Gaussian integers, the ring ℤ[√2] of quadratic integers with radix 2, and the ring ℤ[ω] of cyclotomic integers of degree 8.
- class Num a => Ring a
- class Ring a => HalfRing a where
- half :: a
- class Ring a => RootTwoRing a where
- roottwo :: a
- class (HalfRing a, RootTwoRing a) => RootHalfRing a where
- roothalf :: a
- class Ring a => ComplexRing a where
- i :: a
- class Ring a => OmegaRing a where
- omega :: a
- class Adjoint a where
- adj :: a -> a
- class Adjoint2 a where
- adj2 :: a -> a
- class Ring r => NormedRing r where
- class Ring r => Floor r where
- floor_of :: r -> Integer
- ceiling_of :: r -> Integer
- data Z2
- data Dyadic = Dyadic !Integer !Integer
- decompose_dyadic :: Dyadic -> (Integer, Integer)
- integer_of_dyadic :: Dyadic -> Integer -> Integer
- fromDyadic :: HalfRing a => Dyadic -> a
- newtype Rationals = ToRationals {}
- showsPrec_rational :: (Show a, Integral a) => Int -> Ratio a -> ShowS
- fromRationals :: Fractional a => Rationals -> a
- data RootTwo a = RootTwo !a !a
- type DInteger = RootTwo Integer
- fromDInteger :: RootTwoRing a => DInteger -> a
- type DReal = RootTwo Dyadic
- fromDReal :: RootHalfRing a => DReal -> a
- type EReal = RootTwo Rationals
- fromEReal :: (RootTwoRing a, Fractional a) => EReal -> a
- data Cplx a = Cplx !a !a
- type ZComplex = Cplx Integer
- fromZComplex :: ComplexRing a => ZComplex -> a
- type DComplex = Cplx DReal
- fromDComplex :: (RootHalfRing a, ComplexRing a) => DComplex -> a
- type EComplex = Cplx EReal
- fromEComplex :: (RootTwoRing a, ComplexRing a, Fractional a) => EComplex -> a
- type CDouble = Cplx Double
- type CFloat = Cplx Float
- data Omega a = Omega !a !a !a !a
- omega_real :: Omega a -> a
- type ZOmega = Omega Integer
- fromZOmega :: OmegaRing a => ZOmega -> a
- dinteger_of_zomega :: ZOmega -> DInteger
- type DOmega = Omega Dyadic
- fromDOmega :: (RootHalfRing a, ComplexRing a) => DOmega -> a
- type QOmega = Omega Rationals
- class ToDyadic a b | a -> b where
- maybe_dyadic :: a -> Maybe b
- to_dyadic :: ToDyadic a b => a -> b
- class RealPart a b | a -> b where
- real :: a -> b
- class WholePart a b | a -> b where
- from_whole :: b -> a
- to_whole :: a -> b
- class DenomExp a where
- denomexp :: a -> Integer
- denomexp_factor :: a -> Integer -> a
- denomexp_decompose :: (WholePart a b, DenomExp a) => a -> (b, Integer)
- showsPrec_DenomExp :: (WholePart a b, Show b, DenomExp a) => Int -> a -> ShowS
- class ToEComplex a where
- toEComplex :: a -> EComplex
- class Parity a where
- lobit :: Integer -> (Integer, Integer)
- log2 :: Integer -> Maybe Integer
- intsqrt :: Integral n => n -> n
Rings
A type class to denote rings. We make Ring
a synonym of
Haskell's Num
type class, so that we can use the usual notation
+
, -
, *
for the ring operations. This is not a perfect fit,
because Haskell's Num
class also contains two non-ring operations
abs
and signum
. By convention, for rings where these notions
don't make sense (or are inconvenient to define), we set abs
x
= x and signum
x = 1.
Rings with particular elements
We define several classes of rings with special elements.
Rings with ½
class Ring a => HalfRing a whereSource
A type class for rings that contain ½.
Instances
HalfRing Double | |
HalfRing Float | |
HalfRing Rational | |
HalfRing Rationals | |
HalfRing Dyadic | |
(HalfRing a, RealFloat a) => HalfRing (Complex a) | |
Precision e => HalfRing (FixedPrec e) | |
HalfRing a => HalfRing (Omega a) | |
HalfRing a => HalfRing (Cplx a) | |
(Eq a, HalfRing a) => HalfRing (RootTwo a) | |
(HalfRing a, Nat n) => HalfRing (Matrix n n a) |
Rings with √2
class Ring a => RootTwoRing a whereSource
A type class for rings that contain √2.
Instances
RootTwoRing Double | |
RootTwoRing Float | |
(RootTwoRing a, RealFloat a) => RootTwoRing (Complex a) | |
Precision e => RootTwoRing (FixedPrec e) | |
Ring a => RootTwoRing (Omega a) | |
RootTwoRing a => RootTwoRing (Cplx a) | |
(Eq a, Ring a) => RootTwoRing (RootTwo a) | |
(RootTwoRing a, Nat n) => RootTwoRing (Matrix n n a) |
Rings with 1/√2
class (HalfRing a, RootTwoRing a) => RootHalfRing a whereSource
A type class for rings that contain 1/√2.
Instances
RootHalfRing Double | |
RootHalfRing Float | |
(RootHalfRing a, RealFloat a) => RootHalfRing (Complex a) | |
Precision e => RootHalfRing (FixedPrec e) | |
HalfRing a => RootHalfRing (Omega a) | |
RootHalfRing a => RootHalfRing (Cplx a) | |
(Eq a, HalfRing a) => RootHalfRing (RootTwo a) | |
(RootHalfRing a, Nat n) => RootHalfRing (Matrix n n a) |
Rings with i
class Ring a => ComplexRing a whereSource
A type class for rings that contain a square root of -1.
Instances
(Ring a, RealFloat a) => ComplexRing (Complex a) | |
Ring a => ComplexRing (Omega a) | |
Ring a => ComplexRing (Cplx a) | |
(Eq a, ComplexRing a) => ComplexRing (RootTwo a) | |
(ComplexRing a, Nat n) => ComplexRing (Matrix n n a) |
Rings with ω
class Ring a => OmegaRing a whereSource
A type class for rings that contain a square root of i, or equivalently, a fourth root of −1.
Instances
(ComplexRing a, RootHalfRing a) => OmegaRing a | |
OmegaRing ZOmega | |
OmegaRing (Omega Z2) |
Rings with particular automorphisms
Rings with complex conjugation
A type class for rings with complex conjugation, i.e., an automorphism mapping i to −i.
When instances of this type class are vectors or matrices, the conjugation also exchanges the roles of rows and columns (in other words, it is the adjoint).
For rings that are not complex, the conjugation can be defined to be the identity function.
Instances
Adjoint Double | |
Adjoint Float | |
Adjoint Int | |
Adjoint Integer | |
Adjoint Rational | |
Adjoint Rationals | |
Adjoint Dyadic | |
Adjoint Z2 | |
(Adjoint a, Ring a) => Adjoint (Complex a) | |
Precision e => Adjoint (FixedPrec e) | |
(Adjoint a, Ring a) => Adjoint (Omega a) | |
(Adjoint a, Ring a) => Adjoint (Cplx a) | |
Adjoint a => Adjoint (RootTwo a) | |
(Nat n, Adjoint a) => Adjoint (Matrix n n a) |
Rings with √2-conjugation
A type class for rings with a √2-conjugation, i.e., an automorphism mapping √2 to −√2.
When instances of this type class are vectors or matrices, the √2-conjugation does not exchange the roles of rows and columns.
For rings that have no √2, the conjugation can be defined to be the identity function.
Instances
Adjoint2 Double | |
Adjoint2 Float | |
Adjoint2 Int | |
Adjoint2 Integer | |
Adjoint2 Rational | |
Adjoint2 Rationals | |
Adjoint2 Dyadic | |
Adjoint2 Z2 | |
Precision e => Adjoint2 (FixedPrec e) | |
(Adjoint2 a, Ring a) => Adjoint2 (Omega a) | |
(Adjoint2 a, Ring a) => Adjoint2 (Cplx a) | |
(Adjoint2 a, Num a) => Adjoint2 (RootTwo a) | |
(Nat n, Adjoint2 a) => Adjoint2 (Matrix n n a) |
Normed rings
class Ring r => NormedRing r whereSource
A (number-theoretic) norm on a ring R is a function N : R → ℤ such that N(rs) = N(r)N(s), for all r, s ∈ R. The norm also satisfies N(r) = 0 iff r = 0, and N(r) = ±1 iff r is a unit of the ring.
Instances
NormedRing Integer | |
NormedRing a => NormedRing (Omega a) | |
NormedRing a => NormedRing (Cplx a) | |
(Eq a, NormedRing a) => NormedRing (RootTwo a) |
Floor and ceiling
class Ring r => Floor r whereSource
The floor
and ceiling
functions provided by the standard
Haskell libraries are predicated on many unnecessary assumptions.
This type class provides an alternative.
Minimal complete definition: floor_of
or ceiling_of
.
Particular rings
The ring ℤ₂ of integers modulo 2
The ring ℤ₂ of integers modulo 2.
The ring D of dyadic rationals
A dyadic rational is a rational number whose denominator is a power of 2. We denote the dyadic rationals by D = ℤ[½].
We internally represent a dyadic rational a/2n as a pair (a,n). Note that this representation is not unique. When it is necessary to choose a canonical representative, we choose the least possible n≥0.
Instances
Eq Dyadic | |
Num Dyadic | |
Ord Dyadic | |
Real Dyadic | |
Show DOmega | |
Show DComplex | |
Show Dyadic | |
ToEComplex Dyadic | |
DenomExp DOmega | |
DenomExp DReal | |
Adjoint2 Dyadic | |
Adjoint Dyadic | |
HalfRing Dyadic | |
ShowLaTeX Dyadic | |
WholePart DOmega ZOmega | |
WholePart DReal DInteger | |
WholePart Dyadic Integer | |
ToDyadic Rational Dyadic | |
ToDyadic Rationals Dyadic | |
ToDyadic Dyadic Dyadic | |
Nat m => Show (Matrix m n DReal) | |
Nat m => Show (Matrix m n DComplex) | |
Nat m => Show (Matrix m n DOmega) | |
Nat n => ShowLaTeX (Matrix n m DComplex) | |
Nat n => ShowLaTeX (Matrix n m DOmega) |
decompose_dyadic :: Dyadic -> (Integer, Integer)Source
Given a dyadic rational r, return (a,n) such that r = a/2n, where n≥0 is chosen as small as possible.
integer_of_dyadic :: Dyadic -> Integer -> IntegerSource
Given a dyadic rational r and an integer k≥0, such that a = r2k is an integer, return a. If a is not an integer, the behavior is undefined.
fromDyadic :: HalfRing a => Dyadic -> aSource
The unique ring homomorphism from ℤ[½] to any ring containing ½. This exists because ℤ[½] is the free such ring.
The ring ℚ of rational numbers
We define our own variant of the rational numbers, which is an
identical copy of the type Rational
from the standard Haskell
library, except that it has a more sensible Show
instance.
Constructors
ToRationals | |
Fields |
showsPrec_rational :: (Show a, Integral a) => Int -> Ratio a -> ShowSSource
An auxiliary function for printing rational numbers, using correct precedences, and omitting denominators of 1.
fromRationals :: Fractional a => Rationals -> aSource
Conversion from Rationals
to any Fractional
type.
The ring R[√2]
The ring R[√2], where R is any ring. The value RootTwo
a
b represents a + b √2.
Constructors
RootTwo !a !a |
Instances
Show DComplex | |
Parity DInteger | |
DenomExp DReal | |
Floor EReal | |
EuclideanDomain DInteger | |
WholePart DReal DInteger | |
Eq a => Eq (RootTwo a) | |
(Eq a, Adjoint2 a, Fractional a) => Fractional (RootTwo a) | |
(Eq a, Num a) => Num (RootTwo a) | |
(Eq a, Ring a) => Ord (RootTwo a) | |
(Show a, Eq a, Ring a) => Show (RootTwo a) | |
ToEComplex a => ToEComplex (RootTwo a) | |
(Eq a, NormedRing a) => NormedRing (RootTwo a) | |
(Adjoint2 a, Num a) => Adjoint2 (RootTwo a) | |
Adjoint a => Adjoint (RootTwo a) | |
(Eq a, ComplexRing a) => ComplexRing (RootTwo a) | |
(Eq a, HalfRing a) => RootHalfRing (RootTwo a) | |
(Eq a, Ring a) => RootTwoRing (RootTwo a) | |
(Eq a, HalfRing a) => HalfRing (RootTwo a) | |
(ShowLaTeX a, Eq a, Ring a) => ShowLaTeX (RootTwo a) | |
HalfRing a => RealPart (Omega a) (RootTwo a) | |
ToDyadic a b => ToDyadic (RootTwo a) (RootTwo b) | |
Residue a b => Residue (RootTwo a) (RootTwo b) | |
Nat m => Show (Matrix m n DReal) | |
Nat m => Show (Matrix m n DComplex) | |
Nat n => ShowLaTeX (Matrix n m DComplex) |
The ring ℤ[√2]
fromDInteger :: RootTwoRing a => DInteger -> aSource
The unique ring homomorphism from ℤ[√2] to any ring containing √2. This exists because ℤ[√2] is the free such ring.
The ring ℤ[1/√2]
type DReal = RootTwo DyadicSource
The ring ℤ[1/√2]. We think of the elements of ℤ[1/√2] as (certain) exact real numbers. The "D" stands for "dyadic".
fromDReal :: RootHalfRing a => DReal -> aSource
The unique ring homomorphism from ℤ[1/√2] to any ring containing 1/√2. This exists because ℤ[1/√2] is the free such ring.
The field ℚ[√2]
type EReal = RootTwo RationalsSource
The field ℚ[√2]. We think of the elements of ℚ[√2] as (certain) exact real numbers, so the "E" stands for "exact".
fromEReal :: (RootTwoRing a, Fractional a) => EReal -> aSource
The unique ring homomorphism from ℚ[√2] to any ring containing the rational numbers and √2. This exists because ℚ[√2] is the free such ring.
The ring R[i]
The ring R[i], where R is any ring. The reason we do not
use the Complex
a type from the standard Haskell libraries is
that it assumes too much, for example, it assumes a is a member
of the RealFloat
class. Also, this allows us to define a more
sensible Show
instance.
Constructors
Cplx !a !a |
Instances
Show DComplex | |
EuclideanDomain ZComplex | |
Eq a => Eq (Cplx a) | |
Fractional a => Fractional (Cplx a) | |
Num a => Num (Cplx a) | |
(Eq a, Show a, Num a) => Show (Cplx a) | |
ToEComplex a => ToEComplex (Cplx a) | |
DenomExp a => DenomExp (Cplx a) | |
NormedRing a => NormedRing (Cplx a) | |
(Adjoint2 a, Ring a) => Adjoint2 (Cplx a) | |
(Adjoint a, Ring a) => Adjoint (Cplx a) | |
Ring a => ComplexRing (Cplx a) | |
RootHalfRing a => RootHalfRing (Cplx a) | |
RootTwoRing a => RootTwoRing (Cplx a) | |
HalfRing a => HalfRing (Cplx a) | |
(ShowLaTeX a, Ring a, Ord a) => ShowLaTeX (Cplx a) | |
RealPart (Cplx a) a | |
WholePart a b => WholePart (Cplx a) (Cplx b) | |
ToDyadic a b => ToDyadic (Cplx a) (Cplx b) | |
Residue a b => Residue (Cplx a) (Cplx b) | |
Nat m => Show (Matrix m n DComplex) | |
Nat n => ShowLaTeX (Matrix n m DComplex) |
The ring ℤ[i] of Gaussian integers
fromZComplex :: ComplexRing a => ZComplex -> aSource
The unique ring homomorphism from ℤ[i] to any ring containing i. This exists because ℤ[i] is the free such ring.
The ring ℤ[1/√2, i]
type DComplex = Cplx DRealSource
The ring ℤ[1/√2, i]. We think of the elements as (certain) exact complex numbers. The "D" stands for "dyadic".
fromDComplex :: (RootHalfRing a, ComplexRing a) => DComplex -> aSource
The unique ring homomorphism from ℤ[1/√2, i] to any ring containing 1/√2 and i. This exists because ℤ[1/√2, i] is the free such ring.
The ring ℚ[√2, i]
type EComplex = Cplx ERealSource
The field ℚ[√2, i]. We think of the elements as (certain) exact complex numbers, hence the "E" in the name.
fromEComplex :: (RootTwoRing a, ComplexRing a, Fractional a) => EComplex -> aSource
The unique ring homomorphism from ℚ[√2, i] to any ring containing the rational numbers, √2, and i. This exists because ℚ[√2, i] is the free such ring.
The ring ℂ of complex numbers
We provide two versions of the complex numbers using floating point arithmetic.
The ring R[ω]
The ring R[ω], where R is any ring, and ω is an 8th root of
unity. The value Omega
a b c d represents
aω3+bω2+cω+d.
Constructors
Omega !a !a !a !a |
Instances
Show DOmega | |
DenomExp DOmega | |
OmegaRing ZOmega | |
EuclideanDomain ZOmega | |
ShowLaTeX ZOmega | |
WholePart DOmega ZOmega | |
Eq a => Eq (Omega a) | |
Num a => Num (Omega a) | |
(Show a, Ring a) => Show (Omega a) | |
ToEComplex a => ToEComplex (Omega a) | |
NormedRing a => NormedRing (Omega a) | |
(Adjoint2 a, Ring a) => Adjoint2 (Omega a) | |
(Adjoint a, Ring a) => Adjoint (Omega a) | |
OmegaRing (Omega Z2) | |
Ring a => ComplexRing (Omega a) | |
HalfRing a => RootHalfRing (Omega a) | |
Ring a => RootTwoRing (Omega a) | |
HalfRing a => HalfRing (Omega a) | |
ShowLaTeX (Omega Z2) | |
HalfRing a => RealPart (Omega a) (RootTwo a) | |
ToDyadic a b => ToDyadic (Omega a) (Omega b) | |
Residue a b => Residue (Omega a) (Omega b) | |
Nat m => Show (Matrix m n DOmega) | |
Nat n => ShowLaTeX (Matrix n m DOmega) |
omega_real :: Omega a -> aSource
An inverse to the embedding R ↦ R[ω]: return the "real rational" part.
The ring ℤ[ω]
type ZOmega = Omega IntegerSource
The ring ℤ[ω] of cyclotomic integers of degree 8. Such rings were first studied by Kummer around 1840, and used in his proof of special cases of Fermat's Last Theorem. See also:
- http://fermatslasttheorem.blogspot.com/2006/05/basic-properties-of-cyclotomic.html
- http://fermatslasttheorem.blogspot.com/2006/02/cyclotomic-integers.html
- Harold M. Edwards, "Fermat's Last Theorem: A Genetic Introduction to Algebraic Number Theory"
fromZOmega :: OmegaRing a => ZOmega -> aSource
The unique ring homomorphism from ℤ[ω] to any ring containing ω. This exists because ℤ[ω] is the free such ring.
dinteger_of_zomega :: ZOmega -> DIntegerSource
Inverse of the embedding ℤ[√2] → ℤ[ω]. Note that ℤ[√2] = ℤ[ω] ∩ ℝ. This function takes an element of ℤ[ω] that is real, and converts it to an element of ℤ[√2]. It throws an error if the input is not real.
The ring D[ω]
type DOmega = Omega DyadicSource
The ring D[ω]. Here D=ℤ[½] is the ring of dyadic
fractions. In fact, D[ω] is isomorphic to the ring ℤ[1/√2,
i], but they have different Show
instances.
fromDOmega :: (RootHalfRing a, ComplexRing a) => DOmega -> aSource
The unique ring homomorphism from D[ω] to any ring containing 1/√2 and i. This exists because D[ω] is the free such ring.
The field ℚ[ω]
Conversion to dyadic
class ToDyadic a b | a -> b whereSource
A type class relating "rational" types to their dyadic counterparts.
Methods
maybe_dyadic :: a -> Maybe bSource
Convert a "rational" value to a "dyadic" value, if the
denominator is a power of 2. Otherwise, return Nothing
.
Instances
ToDyadic Rational Dyadic | |
ToDyadic Rationals Dyadic | |
ToDyadic Dyadic Dyadic | |
ToDyadic a b => ToDyadic (Omega a) (Omega b) | |
ToDyadic a b => ToDyadic (Cplx a) (Cplx b) | |
ToDyadic a b => ToDyadic (RootTwo a) (RootTwo b) | |
ToDyadic a b => ToDyadic (Vector n a) (Vector n b) | |
ToDyadic a b => ToDyadic (Matrix m n a) (Matrix m n b) |
to_dyadic :: ToDyadic a b => a -> bSource
Convert a "rational" value to a "dyadic" value, if the denominator is a power of 2. Otherwise, throw an error.
Real part
Rings of integers
class WholePart a b | a -> b whereSource
A type class for rings that have a distinguished subring "of
integers". A typical instance is a = DReal
, which has b =
DInteger
as its ring of integers.
Methods
from_whole :: b -> aSource
The embedding of the ring of integers into the larger ring.
The inverse of from_whole
. Throws an error if the given
element is not actually an integer in the ring.
Instances
WholePart () () | |
WholePart DOmega ZOmega | |
WholePart DReal DInteger | |
WholePart Dyadic Integer | |
WholePart a b => WholePart [a] [b] | |
WholePart a b => WholePart (Cplx a) (Cplx b) | |
(WholePart a a', WholePart b b') => WholePart (a, b) (a', b') | |
WholePart a b => WholePart (Vector n a) (Vector n b) | |
WholePart a b => WholePart (Matrix m n a) (Matrix m n b) |
Common denominators
A type class for things from which a common power of 1/√2 (a
least denominator exponent) can be factored out. Typical instances
are DReal
, DComplex
, as well as tuples, lists, vectors, and
matrices thereof.
denomexp_decompose :: (WholePart a b, DenomExp a) => a -> (b, Integer)Source
Calculate and factor out the least denominator exponent k of a. Return (b,k), where a = b/(√2)k and k≥0.
showsPrec_DenomExp :: (WholePart a b, Show b, DenomExp a) => Int -> a -> ShowSSource
Generic show
-like method that factors out a common denominator
exponent.
Conversion to EComplex
EComplex
is the largest one of our "exact" arithmetic
types. We define a toEComplex
family of functions for converting
just about anything to an EComplex
.
class ToEComplex a whereSource
A type class for things that can be exactly converted to
EComplex
.
Instances
ToEComplex Integer | |
ToEComplex Rational | |
ToEComplex Rationals | |
ToEComplex Dyadic | |
ToEComplex a => ToEComplex (Omega a) | |
ToEComplex a => ToEComplex (Cplx a) | |
ToEComplex a => ToEComplex (RootTwo a) |
Parity
A type class for things that have parity.