Algebraic structure and programming
Asked Answered
L

6

7

May anyone give me an example how we can improve our code reusability using algebraic structures like groups, monoids and rings? (or how can i make use of these kind of structures in programming, knowing at least that i didn't learn all that theory in highschool for nothing).

I heard this is possible but i can't figure out a way applying them in programming and genereally applying hardcore mathematics in programming.

Landlocked answered 19/1, 2011 at 20:16 Comment(2)
What about those lonely isomorphisms?Fibrinogen
Anyone else thinking of monads (and then "wait, who am I to suggest that? I barely get the programming concept, much less the math stuff behind it - I don't even know if it's rightfully called a 'Monad'").Myrtamyrtaceous
S
2

It is not really the mathematical stuff that helps as is the mathematical thinking. Abstraction is the key in programming. Transforming real live concepts into numbers and relations is what we do every day. Algebra is the mother of all, algebra is the set of rules that defines correctness, it is the highest level of abstraction, so, understanding algebra means you can think more clear, more faster, more efficient. Commencing from Sets theory to Category Theory, Domain Theory etc everything comes from practical challenges, abstraction and generalization requirements. In common practice you will not need to actually know these, although if you are thinking of developing stuff like AI Agents, programming languages, fundamental concepts and tools then they are a must.

Sheets answered 21/1, 2011 at 12:57 Comment(0)
S
0

In functional programming, esp. Haskell, it's common to structure programs that transform states as monads. Doing so means you can reuse generic algorithms on monads in very different programs.

The C++ standard template library features the concept of a monoid. The idea is again that generic algorithms may require an operation to satisfies the axioms of monoids for their correctness.

E.g., if we can prove the type T we're operating on (numbers, string, whatever) is closed under the operation, we know we won't have to check for certain errors; we always get a valid T back. If we can prove that the operation is associative (x * (y * z) = (x * y) * z), then we can reuse the fork-join architecture; a simple but way of parallel programming implemented in various libraries.

Sulcus answered 19/1, 2011 at 20:30 Comment(0)
A
0

Computer science seems to get a lot of milage out of category theory these days. You get monads, monoids, functors -- an entire bestiary of mathematical entities that are being used to improve code reusability, harnessing the abstraction of abstract mathematics.

Amphi answered 19/1, 2011 at 22:15 Comment(0)
S
0

Lists are free monoids with one generator, binary trees are groups. You have either the finite or infinite variant.

Starting points:

You may want to learn category theory, and the way category theory approaches algebraic structures: it is exactly the way functional programming languages approach data structures, at least shapewise.

Example: the type Tree A is

Tree A = () | Tree A | Tree A * Tree A

which reads as the existence of a isomorphism (*) (I set G = Tree A)

1 + G + G x G -> G

which is the same as a group structure

phi : 1 + G + G x G -> G
() € 1         -> e
x € G          -> x^(-1)
(x, y) € G x G -> x * y

Indeed, binary trees can represent expressions, and they form an algebraic structure. An element of G reads as either the identity, an inverse of an element or the product of two elements. A binary tree is either a leaf, a single tree or a pair of trees. Note the similarity in shape.

(*) as well as a universal property, but they are two of them (finite trees or infinite lazy trees) so I won't dwelve into details.

Seward answered 20/1, 2011 at 9:55 Comment(2)
Except, a tree isn't associative, is it? (unfortunately, I don't actually know category theory, so I don't know how it views these things...)Amphi
@comingstorm: good remark. For groups, there is a commutative diagram describing how the phi application behaves with respect to associativity. If you want to translate this to trees, you will write a predicate telling you when two trees are equal. Trees represent expressions (ie. syntax), associativity of the group law comes into play when giving semantics to the tree.Seward
B
0

Monoids are ubiquitous in programming. In some programming languages, eg. Haskell, we can make monoids explicit http://blog.sigfpe.com/2009/01/haskell-monoids-and-their-uses.html

Bunghole answered 26/8, 2011 at 21:40 Comment(0)
F
-1

As I had no idea this stuff existed in the computer science world, please disregard this answer ;)


I don't think the two fields (no pun intended) have any overlap. Rings/fields/groups deal with mathematical objects. Consider a part of the definition of a field:

For every a in F, there exists an element −a in F, such that a + (−a) = 0. Similarly, for any a in F other than 0, there exists an element a^−1 in F, such that a · a^−1 = 1. (The elements a + (−b) and a · b^−1 are also denoted a − b and a/b, respectively.) In other words, subtraction and division operations exist.

What the heck does this mean in terms of programming? I surely can't have an additive inverse of a list object in Python (well, I could just destroy the object, but that is like the multiplicative inverse. I guess you could get somewhere trying to define a Python-ring, but it just won't work out in the end). Don't even think about dividing lists...

As for code readability, I have absolutely no idea how this can even be applied, so this application is irrelevant.

This is my interpretation, but being a mathematics major probably makes me blind to other terminology from different fields (you know which one I'm talking about).

Fibrinogen answered 19/1, 2011 at 20:24 Comment(2)
"What the heck ... programming?": an algebraic structure definition is a function with a particular type. For a monoid, it is a function from () | G * G -> G, ie. a function returning the identity element or the multiplication. This is quite similar to a function evaluating an expression in binary tree form, isn't it ? In fact binary trees and monoids are similar concepts. This is why we have the term algebraic data type in functional programming.Seward
Heh, it shows that I don't have much computer science in my bag. I'll have to look more into binary trees and those monoid things. Thanks for the info!Fibrinogen

© 2022 - 2024 — McMap. All rights reserved.