Quantcast
Channel: Chaos Folded
Viewing all 10 articles
Browse latest View live

Obscure C from a new perspective

$
0
0

C is probably one of the most uninteresting programming languages in the world, from a language theory standpoint. It is very simple, doesn’t provide any advanced control constructs, the type system only provides most basic functionality, and its execution model is very simple and predictable. This is one reason why many people love C, including even me in the past. However, I went on to functional programming, which bent my mind a lot. In my opinion, C’s simplicity can quickly become an illusion of easiness. A lot of people think that a good reason to use C is that code written in it is easy to reason about, very predictable and also very fast.

This is not another attempt to prove the opposite. What I want instead is to experiment trying to bring concepts from functional languages into C. As it turned out, this yields very obscure code, and is mostly even impossible in C99. For example, there is no direct or indirect support for closures in C, so I had to use a GCC extension (namely lexically scoped functions) to get things to work.

Now what is the canonical first function you write in a functional language, the “hello world” in functional programming? It’s the notorious factorial function. A very integral part of my implementations is the concept of continuation-passing style (CPS). If you haven’t heard about continuations before, I recommend reading Continuations Made Simple and Illustrated written by Denys Duchier. Although it’s Python-specific, it sheds quite some light on CPS.

I have written four versions of the factorial function in C, each as a little program, which accepts a number as its first command line argument and prints its factorial in response. The codes are very obscure, even for experienced C programmers, and I admit that this was intended to some extent. It shows how incompatible concepts can turn an otherwise familiar language into a beast. However, differently from other approaches to obscure C programming, I didn’t achieve this by intentionally unbeautifying the code, because that would be boring. Anyway, here is the code:

  • Version 1 — using setjmp/longjmp continuations
  • Version 2 — using function pointer continuations
  • Version 3 — implementing algebraic lists and folds using closures
  • Version 4 — lambda calculus with fixpoint operator

Convenient XML stylesheet for articles

$
0
0

I’m currently writing a (hopefully) complete and comprehensive tutorial about monads in Haskell. I’m writing it in an XML document type I specifically made up for this kind of articles. Some of you found it very convenient, so I decided to make it available here under a BSD3 license.

The package contains a Relax NG description of the document type as well as an XSLT stylesheet for XHTML output. For your convenience, I also included a Makefile, which makes validating the XML input and creating XHTML output easy. However, you need GNU make to use it.

Download:

Side note: Some browsers, including Firefox, support viewing XML documents with an XSLT stylesheet directly, so you don’t need to process them to XHTML first. That makes them convenient to use for article authors.

Prime de illuminati

$
0
0

Recently, when someone with the nickname Pr1me came into one of the German IRC channels I’m in, I pointed out that 1 is not a prime number. He should pick the nickname Pr11me instead.

Of course, I was just joking, but out of curiousity, I looked for larger prime numbers consisting only of decimal 1s, and I found the following numbers to be prime:

  • 11 (2 digits)
  • 1111111111111111111 (19 digits)
  • 11111111111111111111111 (23 digits)

I looked further, again just out of curiosity, to find the following interesting numbers, which are not prime:

  • 11111111111111111111111111111111111111 (38 digits)
  • 111111111111111111111111111111111111111 (39 digits)
  • 1111111111111111111111111111111111111111111111 (46 digits)

The first number is interesting, because it is 38 digits long and contains the 19 (= 38/2) digits number 1111111111111111111 as a prime factor. This is interesting in that it’s a highly unlikely case (considering the random nature of prime numbers), that a prime factor consisting only of repetitions of a single digit is found in just another number of that form, and that the prime factor has exactly half the digits of the composite number.

The second number has the curious-looking prime factor 900900900900990990990991.

Finally the third number has 46 digits and is a product including the 23 digits prime factor 11111111111111111111111. The probability for that to happen for a 38 digits number is already very low. It is even orders of magnitude lower for a 46 digits number.

I’m not a fan of conspiracy theories, but this is surely curious. The 46 digits number is, by the way, the largest number of that form I have found. You can come up with many ways to relate that number to the (prime) number 23, some of which are very interesting.

Update: Robert Smith (quadricode at gmail com) took this further and observed the following curiosity: In any number base he tested, a number consisting entirely of repetitions of the digit 1 would only be prime, if the number of digits is itself prime. This may be a generalization of Mersenne primes. Let’s see if we can prove this.

Comments

Frank Dreßler:

  1. 111…111_b = Sum[b^i,{i,0,n–1}] = (b^n–1)/(b–1)
  2. (x - 1)|(x^y - 1), because of: x^y - 1 mod x - 1 = 1^y - 1 = 0
  3. n not prime => n = n1*n2
  4. b^n - 1 = b^(n1*n2) - 1 = (bn1)n2 - 1 = (bn2)n1 - 1 => (b^n1 - 1)|(b^n - 1) and (b^n2 - 1)|(b^n - 1)
  5. b^n - 1 = k1 *(b^n1 - 1) = k2 *(b^n2 - 1) = k1*k10*(b - 1) = k2*k20*(b - 1) <=> (b^n - 1)/(b - 1) = k1*k10 = k2*k20
  6. None of the k’s is 1 => (b^n - 1)/(b - 1) is a composite number, thus 111…111_b of length n is composite, too, if n is composite and b arbitrary.

Wer kann mein Freund sein, der seine Heimat im Stich lässt und in meine eindringt, denn er wird ja auch meine im Stich lassen, sind die Zustände hier eines Tages vielleicht einmal so wie sie es in seiner Heimat waren. Du sollst dich um deine wahre Heimat sorgen, denn laufen alle vor dem Bösen davon, ist irgendwann die ganze Welt böse und fängt nie eine Blume an, im Unkrautbeet zu blühen, wird das Unkrautbeet niemals ein wunderschönes Blumenmeer. Wie soll ich meinen Kindern Treue beibringen, ist das Land doch voller Leute, die Treue vergessen, sobald es irgendwo mehr zu holen gibt. Wir sind Feinde, weil ich ein soverhetzter Nazi bin, aber über Mathematik und so können wir dennoch quatschen.

Schönen Tag noch!

December 10, 2008 8:53 PM

ertes:

Thanks for your comment!

To your German comment: In einer globalisierten Welt kann man diese klare Unterscheidung, wie es sie früher gegeben hat, nicht mehr so leicht treffen. Ich bin niemals längerfristig woanders gewesen als hier. War ich es, fühlte ich mich unwohl und wollte zurück. Das liegt aber nicht daran, dass hier mehr zu holen ist. Woanders gehöre ich einfach nicht hin. Ich weiß nicht, ob du ‘Leute wie mich’ gerne loshättest. Wenn ja, dann ist das halt so, und ich könnte wahrscheinlich (zumindest über Blog-Kommentare) eh nichts daran ändern. Du solltest aber eines differenzieren: Selbst, wenn ich ungebeten bin — ein Gast bin ich nicht, und ich trage auch zu deinem Wohl bei.

Ohne dir zu nahetreten zu wollen: Du solltest deine Beurteilungen und Entscheidungen nicht (allein) auf formale Merkmale basieren. Ich bin schließlich auch nicht mit allem einverstanden, was manche Leute hier tun; und damit meine ich keineswegs die “soverhetzten Nazis”. Meine Beurteilung kommt allerdings von ihrem Tun, nicht von ihrem Reisepass.

Grüße,
Ertugrul.

December 12, 2008 1:06 AM

nbloomf:

You could probably prove that 11…1 is prime when the number of digits is prime by appealing to the irreducibility of the cyclotomic polynomials in Z[x]. For prime p, the pth cyclotomic polynomial is phi_p(x) = 1 + x + x^2 + … + x^(p–1), which evaluated at 10 is 1…1 with p digits. If phi_p were reducible we’d have a factorization of 1…1, and vice versa (i think- it’s late).

December 29, 2008 5:16 AM

Frank Dreßler:

I have found a website about those prime numbers by accident while solving another problem.

http://primes.utm.edu/glossary/page.php?sort=Repunit http://en.wikipedia.org/wiki/Repunit

Gruß,
Frank Dreßler

December 29, 2008 6:52 AM

ertes:

Indeed, these things are already known — though reinventing the wheel is probably done on a regular basis in mathematics.

About proving primality through polynomial irreducibility, I don’t know a polynomial irreducibility test that is faster than prime number tests. Also consider this: with x = 10, 15 = x + 5. Although x + 5 is irreducible, 15 is not prime.

December 29, 2008 3:55 PM

rgs26:

Let’s call n 1s 1{n}. Then:

1{38} is /obviously/ divisible by 1{19}, since it’s 1{19} * (10^19 + 1).

More generally, 1{mn} is divisible by 1{m} and 1{n} (it’s 1{m} * sum for k = 1 to n of 10^(m*k)). So 1{p} is prime => p is prime.

As a corollary, if 1{p} is prime, then 1{ap} has 1{p} as a factor for all a in N.

December 20, 2009 6:43 PM

Understanding Haskell monads

$
0
0

I’m proud to announce that I have completed my first monads tutorial for Haskell programmers. Although I’m not completely happy with a few parts (I’m a virgo after all), I’m releasing it now. I hope it will be helpful to the community.

Read the tutorial

Comments

catechu:

Coincidentally, your suggestion on Usenet today to learn more about monads brought me to your tutorial.

The phrase “category theory” is tossed around a lot, but although I find plenty of tutorials about monads, it’s not clear to me how monads connect to the underlying category theory. Understanding this connection — or, at least, pointing to references which explain it from a mathematical perspective — would probably make monads seem less arbitrary. What do you think?

December 26, 2008 10:38 PM

ertes:

Yes, in fact, Haskell monads are a special case of the monads from category theory. The good thing about Haskell monads is that they can be understood without needing to learn category theory, and this is really the preferred way to go. My experience is that category theory confuses people even more, so going into it would probably be worse than not mentioning it at all.

To someone, who is foreign in the category theory world, monads appear just as arbitrary as Peano’s idea of natural numbers appears to someone, who hasn’t yet known about numbers. A better comparison would be structures from group theory. They are similarly abstract.

The power of monads comes from this abstraction. You implement functionality at a much more general level than you would for particular monads. I hoped to have addressed this motivation well in my tutorial.

As a comparison, consider groups from group theory. You analyze the abstract structure and find theorems and corresponding proofs about it. Whenever you find that a structure is a group, those proofs apply automatically.

Trying to interpret the general monad structure is like trying to find a better name for it.

December 27, 2008 3:14 AM

catechu:

“My experience is that category theory confuses people even more, so going into it would probably be worse than not mentioning it at all.”

Thanks for pointing this out. I’ll take your word for it, and continue to climb Mount Monad by reading your tutorial.

December 27, 2008 7:33 AM

linker:

Hey, good work. I especially liked how you mentioned uniqueness typing of Clean (haven’t seen this in other tutorials).

The section on monad transformers is too short I think, but still, the examples you give are good enough to justify the terseness.

Keep it up!

December 28, 2008 5:14 PM

Benjamin:

Great tutorial that nailed it for me.
Having some background in functional programming (as well as in imperative programming), this tutorial did what other tutorials - for some reason or other - couldn’t:
It demystified the inner workings of monads.

January 21, 2009 4:23 PM

The latest in low quality

$
0
0

With the advent of the internet and sophisticated digital audio and video codecs, classic hard mediums became less and less popular. People prefer to listen to their music on their PCs. However, it was just a few years ago, when people would insist on having high quality encodings of their favorite music or movies. I observed an interesting change in this trend.

Web services like streaming music or video are of rather low quality, compared to their non-networked counterparts. But somehow more and more people tend to happily accept that. Instead of getting a high quality copy of their favorite song, they just listen to it on YouTube, silently acquiescing the horrible quality.

I think, this comes partly from laziness or unawareness, but often even simply from ignorance. People abandon quality in favor of ease of use and less management overhead. I have even seen people download music clips from YouTube and extract the MP3 track of them. Honestly, I’ve done this myself a few times, when I was too impatient to get a song through other means.

The current state of things worries me, but at least I will do my part and go back to good old high quality files. Unfortunately many things aren’t as easy to find in the music store as they are on YouTube, so I’m afraid, I might be stuck with it for now.

A reinterpretation of Python lists

$
0
0

I’m not a Python programmer and I only know the basics of the language, but what I never liked about it is its list handling. In general, just like C, Python is quite a boring language from a language-theoretic standpoint — a language to get the job done.

However, here is a supplemental piece of code implementing an alternative idea of lists for all the functional programmers of you, who are forced to use Python for whatever reason. Have fun, but beware: Your coworkers may not understand your code. ;)

Side note: A very annoying fact for me is that Python doesn’t support currying, but I found a somewhat ugly way to emulate it. Refer to the fix and range functions to see how it works.

Functional Church lists in Python

Update (Sun, 2009–02–08): I have refined the code and removed the fold function, since it’s really useless. Lists are higher order functions themselves, and the fold function was just a wrapper around them. I have also solved the currying problem, thanks to verte from #python on Freenode.

The problem with interpretations

$
0
0

I’ve had another one of those enlightenment experiences today, when I bought and read a very comprehensible book about quantum mechanics. What the author did right, and what most other authors do wrong: It disregarded all the partly quite bizarre interpretations of the theory. If you forget about all the motivations and interpretations of a theory, what remains? Equations. That’s it.

For example, I’ve learned that a quantum state is simply a unit vector V. Things such as superpositions or in more colloquial terms being in multiple states simultaneously exist only in interpretations. The state vector is a fully determined value.

Then where do superpositions come from? It’s simple. They are no fundamental feature of a quantum state, but a feature of the measuring process. Measurement is always done in a certain base described by a number of base vectors. The outcome of the measurement of V depends on the description of V as a linear combination of these base vectors. The square of the absolute value of the coefficient of a particular base vector X is the probability of the outcome being X. That’s it. Quantum states are fully determined and not superposed. Superposition is a measurement effect, because the measurement destroys information.

Example: If you run a vertically polarized photon through a polarization filter, which lets through vertically and horizontally polarized photons, then the outcome is a vertically polarized photon. If you rotate your filter by 45°, the outcome is completely random. That’s not because the photon had a superposed polarization, but because the new base of the measurement has changed in a way, which makes the outcome “vertical” impossible. In other words, in the former experiment, there was no superposition, in the latter there was, even though the initial configuration is exactly the same with the only difference being the justification of the filter. So superposition doesn’t exist. It can be interpreted as a measurement effect.

I had that same experience with other things as well, for example group theory or monads. For some reason, people seem to prefer to depict abstract or unimaginable things and concepts as something complicated or mysterious. I think, this is related to our fascination for the incomprehensible and the satisfaction of amazing other people. Who isn’t amazed by the imagination that there may be more than the three (or four) dimensions you can see, or that a single thing can be at multiple locations at the same time, let alone relativistic effects? However, if you described these things as what they really are, you wouldn’t amaze anyone. Ask a physicist about quantum physics, you’ll get a straight, but disappointingly boring answer.

Continuations for secure code

$
0
0

C is one of the most popular languages, especially for security-related applications including server programs, cryptography implementations and much more. Paradoxically it is also one of the most dangerous languages, because there are many spots where mistakes can be made, some of the most common being to forget to free allocated memory or to free it twice, to exceed a buffer (colloquially called a buffer overflow), to dereference a NULL pointer and more.

Some of you may have read my article about functional concepts in C, which I have written for fun. That article actually uses a non-standard extension to implement them. I have had a long thought about it and found that some of the concepts can be implemented without extensions easily and that they can be very useful in practice for real C code.

My focus was on continuations. They would immediately solve the problem of freeing memory and closing file handles properly in many situations. The idea is so simple and obvious that I just show you some example code of how it works:

int withFile(const char *fileName,
             const char *mode,
             int (*user)(FILE *))
{
  FILE *fh;
  int err;

  fh = fopen(fileName, mode);
  if (fh == NULL) return 1;

  err = user(fh);
  fclose(fh);
  return err;
}

The big advantage of this approach is that the responsibility for closing the file handle is moved from the programmer to the withFile function. In that sense withFile acts like a little run-time system (RTS) for the file handle. This also makes code much simpler. If you remember Linus Torvalds’ note that goto is not necessarily evil and can make writing cleanup code much easier and more beautiful, you will find the above approach even nicer, because it gets along without goto, but does essentially the same.

Of course the above version of withFile is made for a single file handle. You can write versions of it for more than one or even arbitrarily many file handles. You can also make it allocate a working buffer for you and much more.

This style of implementing managed handles through a mini-RTS can also be applied to temporarily allocated strings and buffers, which particularly suffer from buffer overflow errors, double frees, dereferencing of NULL pointers, etc. The RTS guarantees that no code ever receives a NULL pointer or an invalid handle, it helps against double frees and forgetting to free memory, because this responsibility is moved out of mainstream code. Of course this works best, if you don’t use global variables or use them sparingly.

Unfortunately C does not support closures. If it did, then you could easily pass handler functions to the user function, which obey certain rules for you like buffer boundaries. For certain kinds of managed handles you could even pass handler functions only, such that no handle is used at all. This is impossible in C, but can be very useful in languages with closures like Python or functional languages.

Comments

Anonymous:

I don’t get it. Aren’t you closing the file just afterwards you open it?? What role does the “int (*user) (FILE *)” have??

July 12, 2009 12:30 AM

ertes:

This is a function pointer user to a function, which accepts a FILE handle. You pass a file name and a function pointer to withFile. Then withFile opens the given file and calls the given function with the resulting file handle. Closing the file handle is taken care of in withFile.

July 18, 2009 10:21 AM


Back from real life

$
0
0

After a long time of silence, I’m continuing to tell my story. There have been a lot of changes since. First and foremost, I’ve got a new job and I’m finally feeling to be at the right place. My work includes managing our corporate network, coding things (unfortunately in Visual Basic and Visual C#, although I have to admit that C# is quite a good imperative language) as well as helping my coworkers with computer-related issues. My company is widely known in the driving school branch.

Also I’ve got my hairs cut a few weeks ago, because long hairs were not compatible with my new work. I’m happy with my new hair style and with the overall situation and you can expect new blog entries more frequently again.

A new final solution

$
0
0

After more than one year of silence I have once again changed my homepage completely. On my way I used many approaches to website and blog management:

  1. Static HTML pages (using WYSIWYG editors),
  2. static HTML pages (hand-written),
  3. regular expressions to generate static HTML pages,
  4. M4 to generate static HTML pages,
  5. XML + XSLT to generate static HTML pages,
  6. Drupal,
  7. my own blog software in PHP,
  8. Static HTML pages and a separate blog using Blogger.com.

Since none of these solutions (each of which I thought of as being the ultimate solution at first) really worked well for me, I abandoned all of them.

Then recently I found Hakyll, which is a static site generator. Its configuration file is an actual program written in Haskell, my favorite programming language. Using it to generate my site, this is my next attempt to make and maintain a personal homepage.

Viewing all 10 articles
Browse latest View live




Latest Images