[Haskell-cafe] NumberTheory library

Bo Herlin bo at gcab.net
Tue May 10 04:14:48 EDT 2005

 > Well, part of what I was doing was experimenting with what a library like
 > this should look like, even more than what it should do.  For some 
 > I kind of like writing this:
 >     *Math.Prime> is Prime 42
 >     False
 > instead of this:
 >     *Math.Prime> isPrime 42
 >     False

Great! I like this a LOT.
Im working on a framework for ranking and unranking things where primes 
are just a tiny part:

 > data DCountable
 >   = Countable Integer
 >   | Uncountable
 >     deriving (Eq,Show)
 > class CRankable a b where
 >   rank   :: a -> b -> Maybe Integer
 >   unrank :: a -> Integer -> Maybe b
 >   count  :: a -> b -> DCountable

So using the instance for Prime on Integer looks like:

Cafe> unrank Prime 23 ::Maybe Integer
Just 89
Cafe> rank Prime (89::Integer)
Just 23

I have also for trees and lists and other things with defined parameters 
like length, or not.
Using my framework they are made quite simple.

RankIL is for IntegerList with any length
(the bool is if the integers start on 1 or not, might look irrelevant here):

Cafe> map (unrank (RankIL True)) [0..10] ::[Maybe [Integer]]
[Just [1],Just [2],Just [1,1],Just [3],Just [1,2],Just [2,1],Just 
[1,1,1],Just [4],Just [1,3],Just [2,2],Just [3,1]]

RankIL is for IntegerTree

Cafe> map (unrank RankIT) [0..3] ::[Maybe (Tree Integer)]
[Just Node {rootLabel = 1, subForest = []},Just Node {rootLabel = 1, 
subForest = [Node {rootLabel = 1, subForest = []}]}...

..and so on..

Its not very pretty yet since ive only been a Haskeller for some weeks 
now, and I dont know about darcs (i run cvs as i have the book), but 
maybe my framework could be included once a Haskell-guru brushes it up a 

More information about the Haskell-Cafe mailing list