With hammar's help I have made a template Haskell bit which compiles
$(zModP 5)
to
newtype Z5 = Z5 Int
instance Additive.C Z5 where
(Z5 x) + (Z5 y) = Z5 $ (x + y) `mod` 5
...
I'm now facing a problem that I don't think I can solve this way.
A remarkable fact about polynomials is that they are irreducible in the rationals if they are irreducible modulo some prime p
. I already have a method which brute-force attempts to factor polynomials over a given (finite) field.
I want to try running this function for multiple fields. Here's kind of what I want:
isIrreducible :: (FiniteField.C a) => Poly.T a -> Bool
isIrreducible p = ...
intPolyIrreducible :: Poly.T Int -> Bool
intPolyIrreducible p = isIrreducible (p :: Poly.T Z2) ||
isIrreducible (p :: Poly.T Z3) ||
isIrreducible (p :: Poly.T Z5) ||
...
Basically I want to try running my factoring algorithm for a large number of definitions of "division".
I think this is possible to do with TH, but it seems like it would take forever. I'm wondering if it would be easier to just pass my arithmetical operations in as a parameter to isIrreducible
?
Alternatively it seems like this might be something the Newtype module could help with, but I can't think of how it would work without using TH in a way which would be just as hard...
Anyone have any thoughts on how best to accomplish this?
You can do computations in finite fields using type-level numerics, for example with the
type-level
package:This approach has the advantage of not requiring a new template haskell instance for each numeric type. The disadvantage is that it's probably slower than the template haskell solution, since each operation passes the size of the finite field around via a class dictionary.