Peano’s Axioms IV: Advanced Functions and Integers

So here’s the last installment, we’ll make use of all the infrastructure we’ve build up to define primality, and associated functions like, a divisor function, totient, and sigma and whatever else I can come up with.

as always this is a literate source, save to Peano3.lhs and make sure you have the previous posts, execute with:

 ghci Peano3.lhs

and have fun.

No big preambles this time, Lets go.

module Peano3 where> import Peano2> import Data.List

First, all this stuff will boil down to prime numbers. So lets come up with a way to test if a number is prime or not.

An easy method is to create a seive of all prime numbers, then our isPrime function is just a search on the list, is it the fastest method in the world? Not really, does it work? You bet it does.


> natPrimes :: [Nat] natPrimes = sieve [S(S(Z))..]
> sieve :: [Nat] -> [Nat] -> sieve [] = []
> sieve (x:xs) = x : sieve [y | y -> xs, y `mod` x /= 0]

Awesome, we all know about Haskell’s powerful List Comprehensions. Now lets implement that simple linear search on the list.

> isPrime :: Nat -> Bool
> isPrime x = isPrime' x natPrimes
>         where
>              isPrime' x (p:ps)
>                     | (p                      | otherwise = (p == x)


EDIT: augustss mentioned that my use of seq was pointless, theoretically, it would be more efficient to use strict evaluation here, but it won’t till I learn how to do that. It’s unfortunate ghc/hugs won’t recognize tail-recursion and make it strict automatically :/

Cool, all we’re doing here is building up a chain of “does element p_k equal the given number?

Now what? Well, since we’ve defined prime numbers over the naturals, we can do some handy things like create a factorization function. We’ll just use trial division to determine the factors


> factorize :: Nat -> [Nat]> factorize x 
>    -- if the number is prime, we're done.
>    | isPrime x = [x]
>    -- if not, then we know we just need to find the first
>    -- factor, and then recurse on the number `div` the factor
>    | otherwise = firstFactor 
>                : (factorize (x `div` firstFactor))
>    where
>       divides x y = (y `mod` x) == 0
>       firstFactor = head 
>                   $ dropWhile (\p -> not (p `divides` x)) natPrimes

Fast? Hell no, it takes quite some time to factorize 210, and I didn’t even bother to wait till it finished 2310, but it does work.

We know we can create a generic “closure” function, which takes a list and a operation on the elements of that list and recursively applies that function till the list is “closed” that is, applying closure again returns the same list. Lets write that quickly.


> closure :: Ord a => (a -> a -> a) -> [a] -> [a]
> closure f ls = closure' f ls []

> -- closure' is just a helper function which keeps track of the 
> -- last list for comparasion against the closed list
> closure' :: Ord a => (a -> a -> a) -> [a] -> [a] -> [a]
> closure' f ls old 
>      -- if the old list is the same as the new list, return the
>      -- list
>      | ls == old = sort ls
>      | otherwise = closure' f (performClosure f ls) ls

> performClosure :: Eq a => (a -> a -> a) -> [a] -> [a]
> performClosure _ [] = []> performClosure f (x:xs) = [x] 
>                         `union` (map (f x) xs) 
>                         `union` (performClosure f xs)

Well, okay- it wasn’t that simple. However, using this, we can write the “divisors” function, which returns a set of all numbers which divide a given number. We’re going to use a nifty trick here too, I’ll take a moment to explain it.

Normally, we see closed sets defined with a “mod” function, as in the group of integers mod 10, etc. We can define the closure operation (in Haskell’s lambda notation) as being:

\ x y -> (x + y) `mod` k

for some constant k. However, this really doesn’t work well when trying to come up with a closure operator for divisors. What we need is a function which will dynamicly limit the power to which each factor is raised. Fortunately, a nice mathematician named Euclid came up with a handy algorithm for creating a function just like this, it is called the greatest common divisor, the GCD.

Upon examination, you’ll note that the function:

\ x y -> (x * y) `gcd` k

will force the product to only contain appropriately sized factors, because if the multiplication creates a number with an factor with an exponent greater than that of the same factor in k, then it will simply return the factor to the lower of the two powers.

So now, lets create a divisors function based on that concept and previously defined functions, we need to add 1 to the list because our factorization function won’t return that.


> divisors :: Nat -> [Nat]> divisors k = S(Z) : closure closer (factorize k)
>      where closer x y = (x*y) `gcd` k

Pretty simple, just one more reason to love Haskell. Lets define sigma, which is the sum of divisors to a power function. That is


> sigma :: Nat -> Nat -> Nat
> sigma k p = sum (map (^p) (divisors k))

Hmm, lets do one more big one, how about Totient, that is, the total number of all numbers x less than k that satisfy the property gcd(x,k) == 1


> totient :: Nat -> [Nat]
> totient k = length [x | x <- [S(Z)..(k-1)], gcd x k == 1]

List comprehensions are awesome, aren’t they?

Okay, last thing on our list, Integers. So far, we’ve been dealing with Naturals so far, and as such, have not had negative numbers to deal with. What I intend create a “Smart” Datatype which can cleverly increment and decrement without much difficulty. The problem with Integers is that the naive method for creating them, using the standard data types, is that when we try to decrement a positive number (or vice versa) we have to ensure that we just remove one of the increment/decrement symbols. Rather than just add a new one.

Heres the Datatype, you’ll note its similarity to Nat.


data ZZ = Pre ZZ -- decrement an integer
	| Z      -- Zero is an integer
	| Suc ZZ -- increment an integer

Note that all we really did was relax the “0 is the successor of no number” axiom. Much of mathematics is discovered by this method of removing the restrictions imposed by some axiom, or assuming an axioms converse/inverse, etc. The most popular example is that of Geometry, for many years, Euclidean Geometry was all there was. However, in the 19th century, Janos Bolyai and Nikolai Ivanovich Lobachesevky (the same mathematician of Tom Lehrer’s “Lobachevsky”) independently published papers about Hyperbolic Geometry, which changed the infamous “parallel” postulate of Euclidean Geometry to say that, instead of only one line, that there are two lines which pass through a point P not on a line L that do not intersect L. Riemannian, or Ellipic Geometry, states that instead of two lines, that there are no lines. In fact, you can imagine an infinite number of geometries based on the number of lines that can be parallel to a given line. For more about Euclidean and Non-Euclidean Geometries, wikipedia has some very nice articles, links are at the bottom of the post.

So the real test is to create some smarter constructors than what is provided, then we already have. The first thing, really, is to note that we can, in fact, pattern match on functions, eg


> precInt :: Int -> Int -> Int> precInt (x + 1) = x

works just fine. So lets use that to create two functions, s and p, which are successor and predecessor over the Integers. We’ll start a new module for this, this should be placed in a separate .lhs file called “ZZ.lhs”


> module ZZ(ZZ(Z), s, p) where

> data ZZ = P ZZ
>         | Z
>         | S ZZ
>         deriving (Show, Eq, Ord) -- that'll allow us to skip ahead a bit

Notice how we don’t have to deal with s(Z) or p(Z) that happens automagically for us.


> s :: ZZ -> ZZ
> -- if we're incrementing a negative number, we can just eliminate a P
> s (P x) = x
> -- these take care of the normal cases, just like in the Nats
> s x = (S x)

> p :: ZZ -> ZZ
> -- Now we just do p, in a similar way.> p (S x) = x> p x = (P x)

so now we can define addition, which is all we’ll define over the Integers, most of it will be the same or similar to the Naturals, and if you’d like to see it, I encourage you to try it, I’d love to see it working.

Here it is, Addition:


> addZZ :: ZZ -> ZZ -> ZZ
> addZZ Z y = y> addZZ x y 
>     | y      | x == Z = y
>     | y == Z = x
>     | x      | x > Z  = addZZ (p(x)) (s(y))

Notably, this also defines subtraction, given the capability of negation, anyway. Hopefully you’ve enjoyed seeing the buildup from a simple concept of Z and S(x::Nat) to a powerful arithmetic. I think the next stop on my list is dealing with DFA/NFA and PDA evaluator and eventually a Regex Recognizer DFA automata, ala Thompson’s NFA, then maybe I’ll build a Turing Machine Interpreter. All plans are subject to change randomly for no reason at all, Stay Tuned!

~~Joe

Wikipedia articles about various geometry stuff.
Geometry
Euclidean Geometry
Non-Euclidean Geometry
Elliptic Geometry
Hyperbolic Geometry

EDIT: fixed some indentation errors

Advertisements
Published in: on July 28, 2007 at 10:39 pm  Comments (4)  

Peano’s Axioms Part III: Making use of Haskell’s Powerful Polymorphism

This time, we’ll be looking at making use of the polymorphic capabilties of Haskell’s type system.

In Haskell, Polymorphism is provided via type variables. That is, you may have a function



foo :: (a -> b) -> [a] -> [b]

which requires a function of any type to any other type, and a list of elements of the first type, and it returns a list of elements of the second type. Most people call this function map, though it can represent others.

So in addition to getting the functions defined in the typeclass, we get all the prelude functions with variable type signatures.

But wait- theres more. When we define certain types of functions, we often want to limit the scope of the function to only operate on certain variables. Like defining an instance of an interface in Java, we can specify a “scope” (though thats an abuse of the term) for a type variable. As in the following:



foo2 :: Ord a => a -> a -> a

Here, we state that a must be an instance of the typeclass Ord (and by inheritance, an instance of Eq too.) So now we know that foo2 takes two comparable elements and returns a comparable element.

Polymorphism is nothing new to any language. However, I think that Haskell really has an advantage not in the area of semantics, which are pretty much uniform — at least in the case of polymorphism. I think that Haskell’s advantage is in the syntax of polymorphism. Type signatures are easily wonderfully simple ways to express polymorphism. Both this basic kind of polymorphism (called rank-1 polymorphism), as well as higher order polymorphism (rank-2, rank-3, rank-n).

The point of this post is to show the rich library of polymorphic functions which become available with just a few (I think we’re up to 7, one derived, 6 implemented) type classes. This, as always, is a literate file, just cut and paste to a .lhs and run


$> ghci .lhs



> module Peano2 (Nat(..)) where
> import Peano
> import Data.Ratio

==============================================================
Continuing on which defining math in terms of Peano’s axioms
==============================================================

Last time I noted that we’d be working on exp, div, mod, and some other
higher-level functions. I also mentioned that we “sortof” got exp for free, in
that

S(S(S(Z)))^k,

where k is an integer, works fine, but what if we k be a natural. we’ll notice
readily that this will fail, with the error that theres no instance of
Integral Nat.

Why is that a problem? because (^) isn’t in Num, its defined elsewhere, its
signature is



(^) :: (Num a, Integral b) => a -> b -> a

Okay, so now what we should do is define Nat to be an Integral Type. So, lets go
for it.

=======================================

so, for Integral, we need quot, rem, and toInteger. We have the last of these, from the last time. Its quot and rem that we need. So, how do we define these?

well, we know that quot and rem are (effectively) just mod and div, in fact, not having negative numbers means that they are exactly the same. Lets then realize that mod is just repeated subtraction until we hit modulus > remnant. further, we relize that div is just the same, but the count of times we subtracted till we met that condition.



> quotNat :: Nat -> Nat -> Nat
> quotNat k m
> | k == m = 1
> | k < m = 0
> | otherwise = 1 + (quotNat (k-m) m)

> remNat :: Nat -> Nat -> Nat
> remNat k m
> | k == m = 0
> | k < m = k
> | otherwise = remNat (k-m) m

> quotRemNat :: Nat -> Nat -> (Nat, Nat)
> quotRemNat k m = ((quotNat k m), (remNat k m))


now, we just instantiate integral




> instance Integral Nat where
> toInteger = ntoi
> quotRem = quotRemNat
> -- this fixes a problem that arises from Nats not having
> -- negative numbers defined.
> divMod = quotRemNat


=======================================

but now we need to instantiate Enum and Real, oh my. Lets go for Enum first.

Enum requires just toEnum and fromEnum, thats pretty easy, to and from enum are just to and from Integer, which we have.



> instance Enum Nat where
> toEnum = integerToNat
> fromEnum = natToInteger

Real is actually relatively easy, we’re just projecting into a superset of the
Naturals, notably, the Rationals, so we do this simply by pushing the value
into a ratio of itself to 1, that is

toRational S(S(S(S(Z)))) ==> S(S(S(S(Z)))) % 1



> instance Real Nat where
> toRational k = (ntoi k) % 1

=======================================

Next time, we’ll go for primes.

oh- and by the way, pushing Nat into Integral gave us lots of neat things, notably even/odd, gcd/lcm, the ability to do ranges like [(Z)..], and all the appropriate functions that go with that.

So far, I’ve spent about 1 hour making all this work, you can imagine how this speed could be useful if you have defined your problem as having certain properties. Type classes are an extremely powerful tool, which can help make your code both clean, as well as powerful. In one hour, I’ve managed to build up a simple bit of code, based on some fairly primitive axioms, and create a huge amount of powerful math around it.

Imagine if you could define these same relations around data? What if you were able to define strings as having properties of numbers, heres an Idea:

Imagine you have some strings, you can define the gcd of two strings as the least common substring of two strings. If you can sensically define the product of two strings, then you can get a concept of lcm as well. Granted, the may not be the best example. But you can just imagine the power you can push into your data by defining an arithmetic, (not even an algebra!) on them. Imagine creating an arithmetic of music (been done, sortof, check out Haskore) or pictures? I use
arithmetic,because what I’m implementing here is only a little peice of the power you can instill. _This_ is why Haskell is powerful. Not because its purely functional, not even because its lazy. It’s the _math_ that makes Haskell capable of doing this. The type theory upon which Haskell rests makes Haskell powerful.

Remember, next time, Primes and Totients and Number Theory, and a datatype
representing the Integers,
Oh my!

Published in: on July 21, 2007 at 4:16 am  Leave a Comment  

Peano’s Axioms Part I: Haskell and Type Theory, and Object Oriented Programming

Well, the title’s a little misleading, this has very little to do with type signatures, I decided to start a little series on what I think is the most powerful aspect of Haskell. Of course, I think that the most part is Math. Specifically in the realm of Type Classes. I’ll give you a little background here.

I’m primarily familiar with Java’s class system. Notably, upon reading about and toying with other class inheritance systems, Java stood out to me as being my favorite. It feels the most algebraic. One thing that should be noted is that typically, the only “real” difference between these systems (I’m generalizing wildly.) is in the world of multiple inheritance. Java, in particular, uses interfaces, which I intend to show as being a kind of type class, in fact, it is about one step away from being a real live type class.

Now, Lets start talking about inheritance.

Assume we have an Object Joe. Joe is a “Reified” (to borrow some language from type theory) object, that is. He’s a finite, usable, object, with real methods, that do real things. Effectively, he’s material, you can “poke” him, as it were.

Lets now say Joe has some Children, Jack and Emily. These are also Reified objects. However, they have a special property, they have a “Parent” object, namely Joe. This “special property” allows them to inherit properties from Joe, (say… devilishly good looks, and powerful intellect?). By now, the female readers are probably moaning, “Those have to come from the mother.” And thats a good point, how do we get the Mom in on this? We need Multiple Inheritence. Before we get there though, lets talk about a different kind of object, an Abstract object.

An Abstract Object describes some underlying property of an object, but it does so through this Parent/Child relationship. An Abstract Object in our setting might be something like “Ancestor” or even “Parent”. Notable things about Abstract objects is that they don’t contain any “Reified” methods, that is, methods containing code. The contain stubs which describe the signature of a method, but not the method itself. So we might have a Abstract class “Parent” which contains a method “getChildren” which has a signature void -> List, its purpose is to return a list of the extending classe’s (like Joe) Children. The important thing is, it doesn’t force that to be the result, it only enforces a type of the content, not the content itself.

But what about Multiple Inheritence. Assume we have another object called “Sarah” which also extends the parent class, and also assume that Jack and Emily are Sarah’s children too. Lets assume further that Sarah implements getChildren differently than Joe does. How then do Jack and Emily access there respective parents getChildren Methods? If they are fundamentally different implementations, then how can they choose which to use? Further, what if we want to have Parents for Joe, but we also want Joe to extend the Ancestor class? Now we have a mess…

How do we fix it? Well, the answer is to create a super version of Abstract classes, lets call them interfaces.

What does an Interface do? Well– in Java, no class is allowed to inherit from more than one parent class. Abstract or Reified. Interfaces exist outside of the usual inheritance tree, and can therefore give information about the tree as a whole. For instance, we can now create our inheritance tree by saying that each ancestor class implements the “hasChildren” interface, which forces the ancestor to have the void -> List method, we can then say that the Children found in that list are instances of the Child class, rather than the class itself, this gives us a solution to the problem by forcing us to access Jack through the Sarah object.

Could we have done this with normal Abstract/Reified classes, sure. Would it work as well? Probably not.

So why are interfaces cool? Well- you can think of an Interface as a way to describe a set of classes. Classes which meet certain properties. Indeed, they use interfaces like this all the time. For instance, Java has a interface called “Comparable” which- as the name suggests, means that any class that implements Comparable can compare instances of itself in a meaninful way. Comparable doesn’t say how to do the actual comparison, it just says that there must be a method compare which has type: (using Haskell-esque syntax)


compare :: Comparable a => a -> a -> {-1,0,+1}

The Javadocs recommends that it should have 0 mean that x == y, but that its not required. The interface only defines the kind of interaction you can have.

Why is this cool? Well, consider the obvious example, what if I wanted to write quicksort over a generic list of elements? How do I ensure that I have a sortable list? Lets say we have an Object Foo, which has no ordering, and another Object Bar, which can. I have a magic method qsort which will actually do the sorting, but how do I write the method such that I know that the list is sortable? That is if I write tests:


assertFail(qsort(List = {foo1, foo2, ...}));
assertPass(qsort(List = {bar1, bar2, ...}));

How can I ensure those both succeed (with the assumption, of course, that those testing constructs exist)? Well, the easy answer is to define qsort as having the method signature:


List qsort(List);

(I think thats the right syntax, it’s been a while since I wrote Java.)

This will prevent (at compile time, no less) the use of qsort on List, because Foo doesn’t implement Comparable. Running it on List is Fine, because its comparable, like we said before.

So now how is this different from Type Classes. Well, principly, there is one difference. Type Classes can not only specify methods the Type must have defined on it, but it can also define other methods derived from those methods. A seemingly trivial extension to Interfaces. However, that one simple change gives you enormous power in creating data abstractions. For instance, given the Haskell equivalent of Comparable, which is the pair of classes Eq and Ord (for Equality and Orderability, resp.), which require only 2 definitions total (== or /=, and compare or <=, /= is not equal). We get for free, about 20-30 functions including: all your comparison and equality operators, the ability to put items implementing the class in a set, the ability to use them as a key in a finite map, sorting of implementing items, some extra list functions, and more. All that, from 2 definitions, in two type classes, along with the Prelude and Haskell’s own polymorphism capabilities.

How does that work? Well, lets dissect some code, here’s the Ord class:

ignore the ‘.”s, they’re just for spacing.

class (Eq a) => Ord a where
........compare :: a -> a -> Ordering
........(<), (=),(>) :: a -> a -> Bool
........max, min :: a -> a -> a
................--minimal complete definition:
................-- (<=) or compare
................-- using compare can be more efficient for complex types.
........compare x y
.............| x == y = EQ
.............| x <= y = LT
.............| otherwise = GT
........x <= y = compare x y /= GT
........x < y = compare x y == LT
........x >= y = compare x y /= LT
........x > y = compare x y == GT
........-- note that (min x y, max x y) = (x,y) or (y,x)
........max x y
.............| x <= y = y
.............| otherwise = x
........min x y
.............| x <= y = x
.............| otherwise = y

So, lets dissect.

First, we declare what the class implements, that is, what methods are required to exist if the class is implemented. However, this is not the same as saying that all those methods must be defined over the structure before we can say it implements the class. We instead only need the “MRD/MCD” or “Minimal Requisite/Complete Definition” that is, the minimal number of functions needed to define all the other functions the class provides. How do we know what the MRD is? Well- thats up to you to decide. We do that by defining each function of the class in terms of other functions in the class, even cyclicly if necessary. I like to call this set of cyclic definitions the “internal definitions” of the set. These internal definitions provide the real power of the type class. Also note how, in the class definition, we have an inheritance bit, in that of Eq a => Ord a. This allows us to assume that we have basic equality functions provided by Eq.

In the next few posts in this series, I’ll be implementing Peano Arithmetic in Haskell, principly by pushing one simple data type:


Nat = Z | S Nat

Into as many type classes as I can, as of this writing, I have two more posts planned, and have pushed Nat into Eq, Ord, Num, Integral, Enum, and Real, I hope to get it into Rational and Fractional which will turn it into Nat-QQ (Natural Rationals (that is, all the Positive Rationals)), and maybe then refactor Nat into ZZ (the integers), and get the Regular Rationals out of that.

Published in: on July 9, 2007 at 2:52 am  Comments (2)