Entries Tagged as ''

Got some super power?

You got some super power within you ……. Control it and you gonna get a million dollor… check this out.

Prolog and Definite Clause Grammar

Learning prolog is much more fascinating than what I thought it would be.I just studied how to create a CFG (Context Free Grammar) recognizer in prolog. Even though, you could do it in various methods, the best way is to use DFG(Definite Clause Grammar). DFG can be considered as a simple tool provided by Prolog for encoding CFG. But DFG’s go much beyond that. It is possible to write recognizers for non context free grammars in DFG. In fact DFG’s are syntactic sugar provided by prolog.A recognizer for CFG is a machine (program) which accept all the strings defined by the grammar.

Let’s write the grammar for accepting infix expression with operators +,x and numbers 0,1 ie., 1+0, 1*1+0, etc belongs to the language. The grammar can be written as :-

E-> T+E|T

T-> F*T|F

F-> 0,1.

So now let us convert this to a DFG. First of all let us define all lexicons ie., +,*,0,1

lex(+,add).
lex(*,mul).
lex(’0′,f).
lex(’1′,f).

This means that the symbol + belongs to the category add. If we want to add another symbol - to the grammar which has the same priority of +, we just have to add lex(-,add).

Now let us write a simple grammar that would go with the lexicon.

f –> [W],{lex(W,f)}.
a –> [W],{lex(W,add)}.
m –> [W],{lex(W,mul)}.

The new rule ‘f’ says that it contain a list with a W and an extra constrain that “It’s ok as long as W matches with something defined as lexicon f”. Similar is the case with a and m.

Now it is time to define the rules..

e –> t.
e –> t,a,e.
t –> f.
t –> f,m,t.

Copy all those things written in green into a file ‘infix.pl’. Now reconsult the file in prolog interpreter ie., take your prolog interpreter and type

|?-[infix].

Now it is time for some action. Let’s check if a sentence belong to the grammar. Type as follows.

!?-e([’1′,*,’1′,+,’0′],[]).

and you will get an answer ‘yes’, this shows that the sentence belong to the grammar. Quering e(x,[]), gives all values X can take(This will be an infinite one).

Look how simple is the code when compared to the C or Java program for the same.

Another interesting thing we could do is to create the DCG such that we get the parse tree for a sentence. Will be writing about it soon.

Prolog programming…

Just  wrote a prolog program to flatten a list. Even though we had done the program in Lisp and Python it was a wonderful experience doing the same in Prolog. Spend about an hour for the program. Really excited at this point… The following is the program.

app([], Y, Y).
app([H|X], Y, [H|Z]) :- app(X, Y ,Z).

flat([], []).
flat([H|T],Y) :- flat(H,K), flat(T,L), app(K,L,Y), !.
flat(H, [H]) :- not(H = [K]).

I am using a gprolog interpreter. The ‘not’ function is not working in my system (any idea why?) and hence wrote my own not function.

my_not(X) :- X, !, fail.
my_not(_).

For those who dont know what flatten is:

a = [ 1, [ 2, 3 ], [ [ 4 ], 5 ], 6 ]
flat(a) = [1, 2, 3, 4, 5, 6]

I am sure that this is not the best solution.  Let me know if you got a much better solutionor any other solution.

Common Sense deviates rreality?

Have you ever felt common sense as an important factor for success. Teachers used to say ‘answer using your common sense.’; does this seem logical. Does our common sense lead to right solutions? In this session let us criticize common sense.

Most people have a view of the world derived from the ‘common’ senses ie., they view objects out there in the world by the properties that they appear to us to have. This is known as Naive realism (the world we perceive is the world actually is). Naive realists are the outcome of the resistance of the people to think philosophically. Have you ever thought that the common sense are out there to cheat us? Are these senses misleading us from truth? Are we not perceiving a world which is not the exact one out there? Have you ever felt like all these things out there exist just to make us believe that they exist?. Lets take an example…

Have you ever felt the wheel of a fast moving car rotating backwards. Is the wheel really rotating backwards. If no then how do you feel so… Here is the explanation. Lets take the case that the rotating wheel is being video taped and we are watching the movie. The movie tape stores a series of picture frames and we get the impression of motion due to persistance of vision and image blending property of our eye. Let the rim rotates at a frequency of 60 cycles per second and the video camera samples picture at the rate of 1/60 of a second, then whenever the camera capture the image the rim will be in the same position and hence when the movie is played we feel like the rim is stationary. Now consider the sampling time of the camera as 1/59, then at each sampling the rim will be a position backward from the previous position and playing the tape gives us an impression of wheel rotating backwards. This is known as aliasing.

So when we see something, or when we sense something it doesnt mean that it is true ie., the pen I am looking at, to me have a particular shape due to my angle of vision and due to bouncing of light from that object in accordance to my position. A person near to me may not get a same perception. What we see is an appearance of the original pen and not the original pen. We percieve our personal interpretation of the object through sense data. This is known as Representational theory of perception largely devoloped by Bertrand Russell.

Thus the rate of perception can vary from one person to another and hence some people could hear and sense something which others cant. Consider the case of BAT, they do hear ultrasonics and they perceive objects in terms of sound. The fact is you never perceive the external world directly, you only perceive second-hand “messages” sent via photons and sound waves that have already struck your sensory apparatus and sent information about the external world into your brain where it is processed into the three-dimensional model of the real world that you see. Mind that the sound and visual images are not the only way of perception, there may exist other senses which common man is unaware of. The things that you perceive as being the genuine articles “out there” are actually simulations of them inside your brain.

It may seem amazing, that what we think of ourself, our body and the world is just a simulation around our brain’s model of the world. As long as the internal model is exact as people say or as long as the internal model matches with what we say ‘exact’ we can interact with the so called ‘real world’ without problem. Due to some unknown factors when this internal world model get compromised, paranormal, insane experiences occur. It is pity to say insane, isnt it? What if this changed world reflect the real world.
 
I am really confused about how to end this article. The net provides more and more information and maybe I’ll write a second part of this later.