Rough Book

random musings of just another computer nerd

Category: Projects

Regula: An annotation-based form-validator written in Javascript

Regula is an annotation-based form-validation framework written in Javascript. There already exist a few frameworks that address form-validation in Javascript, but I have found them to be somewhat lacking. I have thought about writing one of my own for some time, but I honestly had no idea what form it would or should take. I knew that I wanted to make one that was easy to use, flexible, and easily extensible (custom validation rules). I finally got an idea as to the form my framework should take, when I was looking at Hibernate bean-validation. I like the fact that you can set constraints by using annotations like @NotNull or @NotEmpty. That way, when you look at the bean, you are immediately aware of the constraints attached to it. I wanted to do something similar in HTML.
Read the rest of this entry »

A brainfuck interpreter in bAdkOde

A few days before I left India, I started writing a brainfuck interpreter in bAdkOde. I finished implemented all the instructions, excepting for looping. I actually finished the code (and fixed all bugs) while I was in the air, flying from Dubai to Los Angeles. Emirates Airlines has power-plugs for your laptop on the seat. It’s pretty sweet!

An interesting thing I noticed was that I couldn’t perfectly emulate the input instruction. I’m feeding the brainfuck code to the interpreter from STDIN and so that might be the problem. I’ve noticed that brainfuck interpreters written in brainfuck have the same problem. You have to specify program input before hand. This is what I’ve decided to do. You write brainfuck code, and then mark the end of program code by an exclamation mark. After the exclamation mark, you provide any input, and then mark the end of input by another exclamation mark. Programs that do not have any input end with two exclamation marks. After I finished writing the interpreter, I commented it. While I was doing this, I noticed a lack of labels in bAdkOde. So, I decided to update the interpreter to include them. Speaking of which, I really ought to rewrite the interpreter sometime…

Anyway, here is the code to the interpreter. I’m providing a link to it, because the commented version is rather large. But here’s the expanded (without labels or macros), unformatted version:

>3a>1b{!b?b>b[a)b-91b{=b+1[b>1b}+91b-93b{=b-1[b>1b}+93b(b+1a-33b}>0[a+1a>2b>a[b>1b{!b?b>b[a+1a-33b}>1b>a[b>0a>[aa>ab{=a>1a>[ab>3a{![a)a>[aa-62a{=a+1b-30000b)a>ba{+b>1b>[bb)b>0b-1b>0a}{-a+30000b)b>0a}(b(a+62a}+62a-60a{=a-1b)a>1a>[aa-ab>ba{-b>1a>[aa+30000a>ab)b>0b>0a-1a}{+a>1a>[aa+ab)b>0a-1a}(b(a+60a}+60a-43a{=a+1[b+43a}+43a-45a{=a-1[b+45a}+45a-46a{=a"[b+46a}+46a-44a{=a)a>2a>[aa>[a[b>2a+1[a(a+44a}+44a-91a{=a(a)b)a>[bb{=b>0b>1[b{![b(a+1a)a>[aa)a-91a{=a+1[a>1a}+91a-93a{=a-1[a>1a}+93a(a}(a-1a)a>1b}(a(b)a+91a}+91a-93a{=a(a)b)a>[bb{!b>0b>0[b-1[b{![b(a-1a)a>[aa)a-91a{=a+1[a>1a}+91a-93a{=a-1[a>1a}+93a(a}>0b}(a(b)a+93a}+93a(a+1a}>1a>0b}{!b"85"110"109"97"116"99"104"101"100"32"98"114"97"99"107"101"116"115>0b}

Also, this is additional proof for Turing Completeness. Yes, I’m a nerd! 🙂

bAdkOde is Turing Complete

In my previous post, I suggested that bAdkOde might be Turing Complete by writing a quine. One of the ways to actually prove Turing Completeness is to try and write an interpreter for another Turing-Complete language in bAdkOde. Another approach involves providing a direct translation from another Turing-Complete language into bAdkOde. Here, I prove the Turing Completeness of bAdkOde by providing a direct translation from Brainfuck into bAdkOde.
Read the rest of this entry »

A bAdkOde quine (which suggests that bAdkOde is Turing Complete)

On the bAdkOde project page, I mentioned that I didn’t know whether bAdkOde is Turing Complete (although I suspected it). I also mentioned that if I was able to write a quine in bAdkOde, then it would probably mean that it is Turing Complete. I was able to write one after going through this excellent quine tutorial by Dave Cope.
Read the rest of this entry »

if-else in bAdkOde

I was talking to my CSE 200 (my first CS class at ASU) professor Richard Whitehouse about bAdkOde and he (rightly) pointed out that it didn’t have an explicit selection statement. He also said that unless I wanted it to be really ugly I’d need to have a selection statement. However, since I was going for ugly I figured that I’d just emulate the operation of an if and an if-else with the existing while statement.

If you know assembly, then you know that a while is simply a set of statements wrapped with a conditional branch at the top and a backwards branch at the bottom (or in other words, an if with a goto at the end. A do-while is simply a set of statements with a conditional branch (at the bottom) that branches to the top of the loop. In fact, in assembly programming there really aren’t any for loops or while loops. These keywords are simply abstractions and syntactic sugar. In bAdkOde, you can implement an if with the existing while statement if you explicitly make it break out of the loop. For example, let’s say that we want to check whether the user entered the character “0”:

?a
-48a
# print a line break
"10
# if a is zero
{=a
 # print the string "zero"
 "122"101"114"111"10
 # set the a register to a non-zero value so that we can break out of the loop
 >1a
}

I knew there was a way to implement an if-else with just while statements but I didn’t remember exactly how. Then my friend and co-worker Juan reminded me that I needed two variables. In our case, we need to use two registers:

?a
-48a
# copy the value of a into b
>ab
"10
# if a is zero
{=a
 # print the string "zero"
 "122"101"114"111"10
 # set the a register to a non-zero value so that we can break out of the loop
 >1a
}
# if the top loop failed, it means that b (which holds the same value as a) is 
# non-zero and so we can enter this block.
# if the top loop was successful, it means that b (which holds the same value
# as a) is zero and so we won't enter this block. Hence, the second block acts
# as an 'else'
{!b
 # print the string "non-zero"
 "110"111"110"45"122"101"114"111"10
 # zero out the b register
 >0b
}

Yes, quite ugly. But that’s what I’m going for! 🙂

bAdkOde: An Esoteric Language

I’ve added another project to the projects page. It’s called bAdkOde, an interpreter for an esoteric language that I designed. The very first incarnation of bAdkOde was written in Java and I actually posted it (or made a blog entry about it) over 8 years ago. For some reason I took it down. Probably because I stopped working on it. Anyway, I redesigned the language and wrote an interpreter for it in Perl about 4 or 5 years ago. I finally got around to posting it. Check out the project page for more details. Let me know what you think.

Sulekha: A text-based Markov-chain generator

I’ve finished work on the projects page. Sometimes I’ll have a passing interest in something that leads me to write some code for fun. Yes, you read right. I like to code… for fun. Anyway, this is something I’ve done ever since I started programming way back in 1991. I’ve written a bunch of cool little programs over the years. Most of these have been throwaway scripts that I wrote just to “see what would happen”. A few of these, however, have had some serious effort put into them. I’ve decided to showcase these on my site. Hopefully people will find them interesting :).

The project I’ve just uploaded is called Sulekha, and is a text-based Markov-chain generator. You can check it out here. The comments for this journal entry are mirrored on the Sulekha project page, so anything you post here will be visible there (and vice versa). Let me know what you think. Oh, and it’s interactive so you can generate your own text-based Markov-chains too!

Artificial Life – some preliminary findings

I fnished my artificial life simulator over the weekend, and I have been running simulations on it. There were a few bugs initially that I quickly fixed. My initial simulations gave me some inexplicable “population explosion”. For example, in one cycle I would have 31 creatures and in the next cycle I would suddenly have around 9,000. It didn’t make very much sense until I analyzed the log files.

If any of you remember, I mentioned earlier that my “creatures” are nothing more than assembly programs. Each of these instructions (and parameters) translate into a bit pattern. Changes in the bit patterns lead to changes in the behaviour of the creature.

One of the instructions is a “reproduce instruction”. This prompts the creature to create a copy of itself, with possible mutations. The instruction has a parameter that tells the creature how many copies to create. During the course of the simulation, certain creatures evolved with the reproduce instruction as their first instruction.

My supervisor program (God program) runs one instruction of every creature during one cycle (sort of like an pre-emptive multitasking operating system). If a creature happens to reproduce during the current cycle, then the supervisor program will execute the child’s instructions as well in that current cycle. This led to an interesting situation. When the mutant that had the reproduce instruction as the first instruction reproduced, its child (if it didn’t have a mutation that changed the instruction) also had the reproduce instruction as the first instruction. So the supervisor program would run the child, and the child would reproduce with a similar offspring. This went on until a mutant that did not have a “reproduce instruction” as the first instruction, evolved. As you can see, this is what led to the population explosion. I corrected this problem by adding “sexual maturity” and “reproductive energy threshold” parameters to the creatures. Basically, a creature can reproduce only if it has been in existence for a certain number of cycles, and if its current energy is above its reproductive energy threshold. This got rid of the population explosion problem.

My simulations gave me some other interesting results. One of the instructions that I have is a MOV instruction. This instruction moves a creature from one cell in the two-dimensional array to another cell. There is a variant of this instruction, known as the MOVA instruction. In the MOV instruction, if the creature tries to move to an occupied cell, it will retreat to its original position. However, in the MOVA instruction the creature will try to kill (and subsequently eat) the occupant if the destination cell is occupied. I had created a creature that simply ate, reproduced, and moved to different cells. I noted that after many generations, this creature had evolved into an aggressive one, that would eat, reproduce, and move aggressively (MOVA) into other cells. That was rather interesting!

I also saw natural selection at work. When I corrected my population explosion problem, I made it so that the reproductive threshold of a creature was higher than its initial (starting) energy. Over the course of many cycles, I saw that the creatures evolved such that the initial energy would be very high, and that their reproductive threshold would be very low. In additon, their reproductive age was lowered. I thought that my creatures were having it too easy, so I modified my code to make reproduction a costly instruction. After running my simulation, I noticed the opposite! Now the creatures evolved such that the reproductive energy threshold would be much higher than their initial energy. It would seem that the creatures wanted to build up enough energy to where they would be able to reproduce successfuly.

There is also one very interesting behaviour that I noticed. I provide branch instructions in my instruction set. A creature can branch to any part of its code. In addition, I also have a Decrement and Branch instruction that is useful for iterative loops. One of my creatures evolved a clever strategy. It would decrement and branch into the middle of the branch instruction. When I analyzed the code, I noticed in doing so, the creature was performing a MOVA instruction in fewer bytes than writing an explicit MOVA instruction. It also had the added benefit of performing the MOVA instruction in an iterative fashion so that it could travel around the two-dimensional array. There was a reason for this kind of behaviour though. A creature consumes energy when it performs an instruction, and the energy consumption is proportional to its code size, so it is amazing, but not surprising that such behaviour could arise.

There is still more work that I need to do. I am thinking of giving my creatures some “memory” in the form of stack and RAM. They will have some Store, Load, Push and Pop instructions whereby they can write to and read from memory. Also, I am thinking of adding some more “registers”. Right now, the creatures have two count registers for performing iterative loops. My reason for adding more is this – I want the creatures to be able to act on the data in memory. For example, I can provide instructions that allow the creature to check the status of a particular cell, like if it is occupied, how much food it has, and so on. The creature can load data into the registers, and then use these registers as arguments for the cell checking instructions. This way I am hoping that my creatures will be able to evolve some rudimentary form of “intelligence”, or “intelligent” behavior. This will also increase my instruction set. Right now, if a mutation results in an invalid instruction, the creature dies when it tries to execute it. Actually, that reminds me – I had creatures that evolved with invalid instructions, but they had also evolved branch instructions that would jump over the invalid section of code!

Most of these complex behaviours aren’t that surprising when you consider natural selection… it is only err… natural that such behaviour should arise!

Well I guess that’s all I have for now. I might make a separate page for this project where I can describe this project in more detail. I’ll post more updates here after I run more simulations.

Intel and Artificial Life

This is my fourth day at Intel. I think I will enjoy working here. I haven’t done much yet though, and I still don’t know exactly what it is I am supposed to be doing. I tested some 140 chips yesterday, but that wasn’t too much work. My supervisor is out until Tuesday, so until then I am doing some odd jobs and reading up some documentation about the platforms and tools we use. I am excited to be working here. It looks like it’s going to be hard work, but I think it will also be fun.

Now about the Artificial Life. I have written up some specs on an Artificial Life program. This is different from earlier ones. I am implementing the “creatures” like assembly programs. This code is their “DNA”. It describes how they behave. A “God Program” executes one instruction of each “creature” and moves on to the next. Sort of like a pre-emptive multitasking operating system. The creatures all live in a two-dimensional area of memory. They can reproduce, and there is chance of error during reproduction.

I think I was going about it the wrong way earlier. I was trying to describe behaviour through variables and through discrete quantities. Now, I represent each creature as a bit-pattern – exactly like an assembly program. I have instructions for moving, eating, moving and attacking, reproducing, and sleeping. In addition, I have some very basic branches and loops. So based on the bit pattern, the creature has a certain behaviour. Also, changes in the bit-pattern can cause changes in behaviour. They can be subtle, or drastic. At least, that is what I believe. I need to actually implement my idea before I can find out! My God program is essentially an operating system, and the “creatures” are tasks. There’s a little more to all of this, but I can’t write it all here. If and when I finish the program, I will make a separate page for it and describe it in detail.

I am thinking of calling this program “Eden”. Anyway, that’s all for now!

Ecological Simulators and Photo Albums

I finished the “Delete Photos” page on the Photo Album Tools page… which of course, none of you can see, since it’s in the admin section. But anyway… It took me this long, because I had other stuff to take care of… namely CSE 421. I only have the “Update Photos” tool to finish… after that, I should start working on the actual photo album display page. In other news, I’m working on a JavaScript version of my Perl Ecological simulator (the one that ran wild on ASU’s general server… ohhh I’m so proud of myself). Except in this case, it’s objects that are killing each other… it should give me a lot more flexibility. My creatures even have randomly generated names like “zexoyiwub” and “xorigewor”. It’s going to be fun… once/if I finish it.

All original content on these pages is fingerprinted and certified by Digiprove
%d bloggers like this: