开发者

Algebraic structure and programming

开发者 https://www.devze.com 2023-02-05 20:02 出处:网络
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 stru开发者_如何学Goctures in pr

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 stru开发者_如何学Goctures 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.


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.


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.


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.


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

Starting points:

  • http://en.wikipedia.org/wiki/Algebraic_data_type
  • http://en.wikipedia.org/wiki/Initial_algebra
  • http://en.wikipedia.org/wiki/F-algebra

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.


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消