18 Includes

Let's say we want to abstract out OCaml's arithmetic operators for ints and floats. Abstract algebra (the mathematical concept) allows us to do this using structures called Rings) and Fields). We can define structures for Ring and Field:

( ~- ) is unary negation (for example, to multiplying by -1).

Now let's see how this works:

Let's do some math!

Now we can implement a ring for floating point numbers:

See, we have 0. and so on, so we know it's a float!

What if we wanted to provide division? Well, division is actually part of a field, not a ring. What if we wanted to implement a field?

~We could copy code~ no wait don't! That's never a good idea. We can use an include to include all the contents of another signature or structure in the current one:

Now, if someone ever changes the implantation of rings, our changes would be duplicated. Let's do the same for our floating point numbers:

Uh oh... what happened there? Well, we need to use FloatFieldRep instead of FloatField so that our types are correct: