<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Playing With Pointers</title>
	<atom:link href="http://playingwithpointers.com/feed" rel="self" type="application/rss+xml" />
	<link>http://playingwithpointers.com</link>
	<description>One step closer to reflective equilibrium.</description>
	<lastBuildDate>Thu, 02 Feb 2012 11:36:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Books, movies and music</title>
		<link>http://playingwithpointers.com/archives/669</link>
		<comments>http://playingwithpointers.com/archives/669#comments</comments>
		<pubDate>Thu, 02 Feb 2012 11:36:32 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Offline Bytes]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=669</guid>
		<description><![CDATA[I graduated out of high school an idiot. I hadn&#8217;t read anything, seen anything or heard anything (Everybody anyone?). Now that I&#8217;m becoming moderately literate in such things, I think I can make a generalization &#8212; the information content (sorry Dr. Shannon, I&#8217;m going to abuse that term here) of an art form is inversely [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I graduated out of high school an idiot.  I hadn&#8217;t read anything, seen anything or heard anything (<em>Everybody</em> anyone?).  Now that I&#8217;m becoming moderately literate in such things, I think I can make a generalization &#8212; the information content (sorry Dr. Shannon, I&#8217;m going to abuse that term here) of an art form is   inversely proportional to the effect it has on me.</p>
<p align="justify">Contrast a book with a movie, for instance.  The information a book contains is certainly more abstract than the information a movie rams into my head.  I don&#8217;t know how <em>Naoko</em> in <em>Norwegian Wood</em> looked like but <em>Martha</em> always conjures up the image of Helena Bontham Carter in a hat, smoking a freshly lit cigarette.   <em>Naoko</em>&#8216;s image is malleable and ethereal but <em>Martha</em> will always be a dark girl with curly hair (this is not to criticize <em>Fight Club</em>, I loved the movie).</p>
<p align="justify">I think this makes books more relatable, and a well written text hits me at a very base level.  Since the written word is so flexible and abstract, my mind can lift it up to a concrete representation that is very close to my sense of self.  A book can drive me to insanity, a movie cannot.</p>
<p align="justify">I think the same applies to music too &#8212; I&#8217;ve always found solos by  <em>Pink Floyd</em> their most interesting work.  When a rapper talks about how he &#8220;hit it up with a ho&#8221; (I&#8217;m not good with american street slang so I just made that up), there is only so much information my mind can add, there is only so much I can misinterpret.  When I hear the crazy slide-guitar in <em>Shine on you crazy diamond, VI-IX</em>, my mind is forced to conjure up something using the only tools it has at its disposal, my imagination and my experience.  And it always comes   up with something sinister.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/669/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Republic: Book Two</title>
		<link>http://playingwithpointers.com/archives/665</link>
		<comments>http://playingwithpointers.com/archives/665#comments</comments>
		<pubDate>Mon, 02 Jan 2012 05:22:55 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Offline Bytes]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=665</guid>
		<description><![CDATA[Glaucon comments that there are three classes of good things: things which are good in themselves (simple, harmless pleasures), things which are good because they have good consequences (exercise, timely health checkups) and things which are good for both the above reasons. Socrates says he&#8217;d like to place justice in the third category &#8212; &#8220;which [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Glaucon comments that there are three classes of good things: things which are good in themselves (simple, harmless pleasures), things which are good because they have good consequences (exercise, timely health checkups) and things which are good for <em>both</em> the above reasons.  Socrates says he&#8217;d like to place justice in the third category &#8212; &#8220;which will be valued by one who is in pursuit of true happiness, alike for their own sake and for their consequences&#8221;.  Glaucon does not concur, and wants to place it in the second category,   something that people pursue solely for favorable consequences.</p>
<p align="justify">What Glaucon says next seemed a lot like Rawls&#8217; theory of a social contract (I have not read Rawls&#8217; work in detail, and might be standing on <a href="http://2.bp.blogspot.com/-mOGZHyT5zVk/Tvsl4ajH1rI/AAAAAAAAEyE/LrBS5eEqEu4/s1600/20111228.gif">mount stupid</a>).  The case for justice, he says, is that the pain of suffering injustice outweighs the pleasure of committing it.  Hence, we &#8220;find it expedient to make a compact of mutual abstinence from injustice&#8221; and that &#8220;there is certainly no one else who is willingly just; but it is from cowardice, or age, or some other infirmity, that men condemn injustice [...] this is proved by the fact that the first of these   people who comes to power is the first to commit injustice&#8221;</p>
<p align="justify">Glaucon then goes on to compare the ideal just man and the ideal unjust man.  In fact, he makes the distinction between being just and the <em>appearance</em> of being just &#8212; if Socrates defends a just man who is known for his justice, he is not defending justice but merely its appearance.  The ideal just man has the worst reputation for injustice, he says, without being guilty of a single unjust act; and the ideal unjust man is the opposite.  It is easy to see how such an ideal unjust man will trump the ideal just man in every situation; he sums it up by saying &#8220;to be just without also thought just is no advantage to me, but only entails manifest trouble and loss, whereas if I am unjust and get myself a name for justice an   unspeakably happy life is promised me&#8221;.</p>
<p align="justify">One deterrent to injustice is the promise of afterlife &#8212; people who have lived just and honest lives spend eternity carousing with the gods while the ungodly and unjust are plunged into Hades, condemned to carry water in a sieve.  However, there is no shortage of soothsayers and quacks who claim to have powers to amend crimes committed in this world &#8212;  unjust people (who are usually rich enough to afford such luxuries) tend to hire such people to insure their afterlife.  Such practices are further encouraged by poets like Homer, Glaucon says, by spreading stories how not just individuals but whole cities can petition a god&#8217;s forgiveness   by offering the right sacrifice.  </p>
<p align="justify">It does not seem like (not to me, at least) that Socrates actually makes a proper attempt to address the concern (that the unjust man is always better off than the just man), but diverges to another direction entirely.  He starts the discussion by talking about justice in a city; hoping to get some helpful insight into the nature of justice.  He first talks about specialization.  Once the basic needs of the citizens are met, need will be felt to &#8220;cut ourselves a slice of our neighbour&#8217;s territory&#8221; for greater luxury and comfort.  This adds another specialization that a healthy city needs &#8212; warriors, or guardians.  These guardians needs to be gentle to their friends and dangerous to their enemies.  This way, I think, Socrates links the qualities of the warriors to that of a just man, as   discussed in book one.</p>
<p align="justify">I find the next argument (primarily by Socrates) very absurd.  Firstly, they somehow convince themselves that a high-spirited gentle man is a contradiction.  Socrates resolves this contradiction by comparing such a man with a dog;  and noting that a dog equates acquaintance with friendship and &#8220;How, I ask, can the creature be other than fond of learning when it makes knowledge and ignorance the criteria of the familiar and the strange?&#8221;.  This is clearly well-fermented manure &#8212; in fact, that a dog refuses to use any other (more reliable) criterion to judge animosity and friendship shows that it is <em>not</em> fond of learning and self-correction.  Even if you disregard the absurdity of this argument, there is no way such a predicate can be literally translated over to a human.  The comparison of human and animal behaviour in most contexts is dubious at best and in my opinion, breaks down completely when you try to draw such   sweeping conclusions.</p>
<p align="justify"><em>Anyway</em>, once they&#8217;ve concluded that the nature of the guardians are supposed to be &#8220;philosophical, high-spirited, swift-footed and strong&#8221;, the focus goes to their education.  Socrates has something against the poet-educated culture (he <a href="http://en.wikipedia.org/wiki/The_Clouds">definitely has reason to</a> :)) and suggests a censor on the kind of folklore a future guardian is exposed to in his childhood.  Socrates is concerned that exposure to folklore that depict gods and other respectable engage in immoral, unjust behaviour, sets a bad example for children   to aspire to.  He gives various examples I won&#8217;t repeat.</p>
<p align="justify">Book two ends with Socrates arguing that since</p>
<p align="justify">
<ul>
<li>gods are perfect beings</li>
<li>gods can only change their form only by exerting their own will</li>
<li>the human form is imperfect</li>
</ul>
<p align="justify">they never impose themselves on us by sending signs, apparitions or by appearing in our dreams.  Doing so would involve debasing themselves, which no god would ever do.  This is a clear case against poets who often use gods as characters in their storytelling.  He says when &#8220;a poet uses such language concerning the gods, we shall be angry with him and refuse him a chorus, neither shall we allow our teachers to use his writing for the instruction of the young&#8221;.  The rift between the philosophers and the poets is   clearly visible at this point.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/665/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Republic: Book One</title>
		<link>http://playingwithpointers.com/archives/648</link>
		<comments>http://playingwithpointers.com/archives/648#comments</comments>
		<pubDate>Sun, 25 Dec 2011 10:37:15 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Offline Bytes]]></category>
		<category><![CDATA[plato]]></category>
		<category><![CDATA[republic]]></category>
		<category><![CDATA[tl;dr]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=648</guid>
		<description><![CDATA[I&#8217;ve started reading Republic by Plato lately, and in order to publicly shame myself into actually reading it properly, I&#8217;ve decided to put up a blog post on each book. The post will consist of sort of a TL;DR with my opinions on the book (Republic refers to each chapter as a book, it consists [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I&#8217;ve started reading <em>Republic</em> by Plato lately, and in order to publicly shame myself into actually reading it properly, I&#8217;ve decided to put up a blog post on each book.  The post will consist of sort of a <em>TL;DR</em> with my opinions on the book (<em>Republic</em> refers to each chapter as a book, it consists of   ten such books).</p>
<p align="justify">The first book begins by Socrates talking about old age with Cephalus and whether wealth has any role in keeping one happy in old age. Cephalus says that wealth helps one remain just.  Since old people tend to worry more about afterlife (because death is now imminent) and the repercussions of being immoral play out in afterlife, wealth in general contributes to happiness in old age.  This steers the conversation towards discussing what is meant by being   just.</p>
<p align="justify">Socrates says it can&#8217;t merely be returning to people what they owe you &#8212; you would not return a knife a friend lent you when your friend is mad with rage (and likely to do something rash).  Polemarchus takes up this argument and says (quoting Simonides) that debt of friend to friend is to do good to one another and not harm.  Then Socrates goes on to conclude that justice is useless when a thing is in use, but useful when it is out of use.  I personally could not follow this argument &#8212; Socrates says, for instance, &#8220;well, in what partnership is the just man superior to the harp player?&#8221;.  The point being missed, I think, is that justness and the ability to play a harp are not exclusive properties.  You can be a just harp player (in which case you&#8217;ll wrong your enemies and help your friends) or an unjust one; and your justness has nothing to do with your harp playing, like the wetness of an apple has nothing to do with its   redness.</p>
<p align="justify">Moreover, Socrates says, a person is likely to make a mistake or two in choosing his friends.  He might end up regarding a good, just person an enemy and a thieving, malicious person as a friend.  And now if he follows the doctrine of hurting his enemies and helping his friends, he&#8217;ll end up doing something that is clearly   wicked.</p>
<p align="justify">Hearing this, Polemarchus makes a change to his definition of justice, changing it to mean hurting enemies who are bad and helping friends who are good.  Again Socrates goes into an argument which my spoonful of cognition can&#8217;t decipher.  He concludes by saying that justice, by its virtue of justness can not make other people unjust.  But, since hurting <em>anyone</em> makes them less excellent in human virtues and since justice is a human virtue; justice, but justness can not hurt anyone.  I have two problems with   this argument.</p>
<p align="justify">First is with the conclusion:  if being just implies hurting no-one, it only cements Thrasymachus&#8217;s argument that will appear later.  The second is with the actual argument:  Socrates asks, for instance, &#8220;can musicians, by their art of music, make men unmusical?&#8221;.  While Polemarchus answers no, I think <em>yes</em>.  Specifically, I think it is a musician who has the most ability to render men unmusical; by using the art of music.  The art of music itself does not have values.  In fact, it has been agreed only pages before that &#8220;a man can guard expertly whatever he can thieve expertly&#8221; and that in cases of illness, a physician is the person who can do   more harm to enemies and more good to friends.</p>
<p align="justify">After this, we see Thrasymachus in the scene.  After a brief squabble with Socrates, he gives his view of justice:  that it is the interest of the stronger.  He talks about how rulers legislate laws which are in the interest of the rulers themselves.  Socrates employs the pattern he just used, talking about how rulers are apt to make mistakes and legislate laws that are not in their favor.  Is following those laws (which are not in the interest of the stronger) also just?  Thrasymachus parries with the abstract concept of a <em>ruler</em> who does not make mistakes.  A rules is not a ruler in the time he makes a mistake, just like a cook is not a cook   at the time he forgets to season his dishes.</p>
<p align="justify">Then Socrates says that the art of ruling, in its highest form, is the art of taking care of the subjects being ruled.  This implies that the rulers will end up legislating in interest of the people being ruled and not the rulers themselves.  In fact, he says, the reason why people contest of government offices is not because they want power, but because they are averse to being ruled by an unjust man.  Thrasymachus&#8217;s response is that while a shepherd tends to his sheep and has their interests in his mind; those interests stems from how he&#8217;ll harvest their wool and eat their meat later, not   a genuine sense of service.</p>
<p align="justify">Before this argument proceeds further, Thrasymachus says something more important &#8212; that an unjust man is always better off than a just man.  He gives reasons that are essentially obvious.  Socrates has a bit of trouble arguing with this one.  He finally ends the conversation with a teleological argument, saying that it is the property of the soul to be just and an unjust person cannot be better off than a just person since he is violating the very purpose of his   soul.  This argument did not make much sense to me.</p>
<p align="justify">In my opinion, the problem in book one is that justice is being considered a property of an individual, when it is the property of social structures.  I see justice as a set of strategies which   ensures a stable society &#8212; in principle, it is always more profitable for an individual to be unjust.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/648/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>New JIT Interface for GDB</title>
		<link>http://playingwithpointers.com/archives/633</link>
		<comments>http://playingwithpointers.com/archives/633#comments</comments>
		<pubDate>Sun, 20 Nov 2011 12:51:27 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[jit]]></category>
		<category><![CDATA[jit interface]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=633</guid>
		<description><![CDATA[I worked on GDB as a part of my internship at Igalia. The work I did is finally merged, and this blog post describes the problem that I tried to solve and the solution I implemented. The Problem Let&#8217;s say A is a runtime function that sometimes gets called by JIT compiled code. When the [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I worked on GDB as a part of my internship at <a href="http://igalia.com">Igalia</a>.  The work I did is finally merged, and this blog post describes the problem that I tried to solve and the solution I implemented.</p>
<h3>The Problem</h3>
<p align="justify">Let&#8217;s say <code>A</code> is a runtime function that sometimes gets called by JIT compiled code.  When the control is   inside <code>A</code>, the stack may look something like this:</p>
<pre>
[  A  ]
[ Some JIT compiled function (X)  ]
[ Some other JIT compiled function (Y)  ]
[ Some function that launches JIT compiled code (B)  ]
[ Some function that does the JIT compilation (C)  ]
[   .... and so on ....  ]
[  First function for this thread == (D)  ]
</pre>
<p align="justify">Now, for GDB to be able to successfully unwind through the <code>X</code> and <code>Y</code> frames, it needs some extra information.  For regular functions (like <code>A</code>, <code>B</code>, <code>C</code> and <code>D</code>) this information is embedded in a section in the ELF file, using the DWARF encoding. However, <code>X</code> and <code>Y</code> don&#8217;t have any such associated information since they were generated on the fly.  On trying to <code>backtrace</code> when the stack is in the state shown above you&#8217;ll</p>
<ul>
<li>See a bunch of <code>??</code>s instead of <code>X</code> and         <code>Y</code>, since GDB does not have the debug symbols.</li>
<li>A possibly incorrect backtrace.  From what I understand, GDB can backtrace by interpreting the prologue, but only up to a point.</li>
</ul>
<p align="justify">The initial solution (still supported by GDB) was this:  whenever the JIT compiler compiles a block of code, have it emit the debug information to an  ELF file in memory.  Then have the JIT compiler notify GDB of these in-memory ELF files using a <a href="http://sourceware.org/gdb/onlinedocs/gdb/JIT-Interface.html">published     interface</a>.  This way, GDB knows how to display a pretty backtrace.</p>
<p align="justify">This solution (of generating ELF files in memory) is, however, not ideal.  ELF files are complex and hard to generate.  Generating ELF files bites even more if all that GDB needs to do is display a meaningful stack trace.  My job was to find a less complex   way to get the debugging information into GDB.</p>
<h3>The Solution</h3>
<p align="justify">It was Tom Tromey&#8217;s idea that it probably makes more sense to load and use some sort of a plugin provided by the JIT vendors.  I too thought this idea was viable and decided to   implement it and see how well it worked.</p>
<p align="justify">The problem can be divided into two parts &#8212; providing the debug symbols and providing the unwind information.  We don&#8217;t deal with providing type information and other such more sophisticated information, since we&#8217;re targeting people who just want to see a meaningful stack trace without putting in too much effort.  The ELF handling code is still in GDB and anyone wishing to do something more exciting than looking at a backtrace can generate   full-fledged ELF files.</p>
<p align="justify">For the debug symbols part, we</p>
<ol>
<li>Have the JIT compiler generate the debug symbol information in     any format it pleases.  GDB does not need to understand this format.</li>
<li>Have the plugin parse this debug information (since         it understands the format, being written by the JIT vendor) and emit the symbol tables GDB requires.</li>
</ol>
<p align="justify">In the name of a clean interface and better backward compatibility, (2) is done by passing a bunch of callback functions to the plugin, along with the data it is supposed to parse (the data that the JIT compiler passed to GDB).  The plugin then constructs the symbol tables using the callbacks it is given.  There are callbacks to create a new symbol table (<code>symtab_open</code>), to add a mapping from line numbers to PCs (<code>line_mapping_add</code>) among others.  All this is documented   in the header <code>jit-reader.h</code>.</p>
<p align="justify"><code>jit-reader.h</code> is installed in <code>{includdir}/gdb/</code>, where <code>{includedir}</code> is the system&#8217;s default include directory.  This header is the interface between the plugin and GDB, and needs to be included in the plugin source file.  Because of where it is installed by default, usually a <code>#include     &lt;gdb/jitreader.h&gt;</code> is sufficient.</p>
<p align="justify">For the unwinding part we simply have the plugin provide an unwinder.  The unwinder too gets a bunch of callbacks, one to read the value of a register in the current frame, one to tell GDB (<em>write</em>) the value of a register in the previous frame (the one that was called immediately before), one to read some data off the target process&#8217; address space, one to generate a <em>frame id</em> (described in <code>jit-reader.h</code>) and so on.  The unwinder is especially easy to write if the JIT compiled code stores interesting things at constant offsets from the base register.  Pseudo code could   be </p>
<pre>
  READ_REGISTER(cb, AMD64_RBP, FramePtr);
  PreviousFP = READ_MEMORY_AT(FramePtr)
  PreviousPC = READ_MEMORY_AT(FramePtr + 8)

  WRITE_REGISTER(AMD64_RBP, PreviousFP)
  WRITE_REGISTER(AMD64_RA, PreviousPC)
</pre>
<p align="justify">Telling GDB the values of the frame pointer and the   program counter is enough for it to display a backtrace.</p>
<p align="justify">I&#8217;ve checked in the code, and it is available in the current trunk.  This was a very general bird&#8217;s eye view of the situation, more information can be found in the GDB manual and the <code>jit-reader.h</code> file GDB installs.  Thanks to everyone on the GDB mailing list who sent in their valuable reviews.</p>
<p align="justify">Comments, suggestions and questions welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/633/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Macros In Haskell</title>
		<link>http://playingwithpointers.com/archives/615</link>
		<comments>http://playingwithpointers.com/archives/615#comments</comments>
		<pubDate>Tue, 18 Oct 2011 15:33:03 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[fp]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[template haskell]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=615</guid>
		<description><![CDATA[Haskell too has macros. The subsystem is called Template Haskell. It is a Lisp-like AST generation framework that comes as a GHC extension (that is, it isn&#8217;t a part of the language as of now). This blog post is a (hopefully) simple introduction to TH, and how it can possibly be used. As usual, it [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Haskell too has macros.  The subsystem is called <em>Template Haskell</em>.  It is a Lisp-like AST generation framework that comes as a GHC extension (that is, it isn&#8217;t a part of the language as of now).  This blog post is a (hopefully) simple introduction to TH, and how it can possibly be used.  As usual, it is less of an instructive guide, and more like a chronicle of my own personal adventure.  Moderate competence in Haskell is required.</p>
<p align="justify">Unlike Lisp&#8217;s macro system, TH is typed &#8212; like everything else in Haskell.  You have typed constructors like <code>FunD</code> (function declaration) and <code>InfixP</code> which are used the build the AST of whatever form you wish to <em>splice</em> into your regular code.  You can find the current TH user manual at [1].</p>
<p align="justify">The AST constructors are in the module <code>Language.Haskell.TH</code>, which looks quite overwhelming at the first glance, and, as such, writing TH expressions from scratch can get quite difficult.  Thankfully, we also have a nice <code>runQ</code> function which allows us to write TH expressions without really grokking everything in the module.  I&#8217;ll try to demonstrate this by getting TH to generate a small (read useless / toy) string matcher.</p>
<h3>Objective</h3>
<p align="justify">We&#8217;ll try to generate a function with the type <code>String->Bool</code>.  The function&#8217;s job will be to check if the <code>String</code> it receives belongs to a list of <code>String</code>s we provide at compile time.  To make things a little more interesting, we&#8217;ll allow the strings we have at compile time to have a regex-like <code>"?"</code> in them, which can match any character.  As an example, if the list of <code>String</code>s   is <a name="input"></a><code>["asdf", "a?foo", "P"]</code>, the generated function should match <code>"asdf"</code>, <code>"P"</code>, <code>"amfoo"</code> but not <code>"afoo"</code>.  We don&#8217;t address the issue of how to match a literal <code>"?"</code>.  Heck, out end product boils down to ten lines of code.</p>
<p align="justify">Once we&#8217;re done writing our TH code, we should have a function <code>generateMatcher</code>, which we shall invoke from   the top level of a source file, like:</p>
<pre>
  generateMatcher "generatedF" ["asdf", "a?foo", "P"]
</pre>
<p align="justify">It will then generate the matcher, with the name <code>generatedF</code> and the behaviour we&#8217;re just described.  It can then be called like a regular function.  So a source file can look like:</p>
<pre>
  generateMatcher "generatedF" ["asdf", "a?foo", "P"]

  main = do
    str < - getLine
    putStrLn $ show $ generatedF str
</pre>
<h3>Starting Off</h3>
<p align="justify">The first thing to think about is how the compiled code should look like.  Given the above problem instance, we could generate something like</p>
<p><a name="togenerate"></a>
  </pre>
<pre>
    generatedF ('a':'s':'d':'f':[]) = True
    generatedF ('a':_:'f':'o':'o':[]) = True
    generatedF ('P':[]) = True
    generatedF _ = False
  </pre>
<p align="justify">Haskell&#8217;s pattern matcher is, AFAIK, intelligent enough to convert the cases into a tree structure and quickly jump to the correct clause.  Now the second question:  how should the <em>AST</em> look like?  Here we take the help of the <code>runQ</code> function.  First we start GHCi as <code>ghci -XTemplateHaskell</code> (since TH is not enabled by default).  Once in GHCi, we import the required module by <code>:m + Language.Haskell.TH</code>.  The scene is set!  We can now see the AST for <code>generatedF   ('a':'s':'d':'f':[]) = True</code>.  For that, we evaluate <code>runQ [d|generatedF   ('a':'s':'d':'f':[]) = True|]</code>.  The <code>[d| ... |]</code> is used to quote a <em>declaration</em>.  Detailed information (about this, and generally about TH) is in [1]. We will now see some (rather incomprehensible) output:</p>
<pre>
[FunD generatedF [Clause [InfixP (LitP (CharL 'a'))
GHC.Types.: (InfixP (LitP (CharL 's')) GHC.Types.:
(InfixP (LitP (CharL 'd')) GHC.Types.:
(InfixP (LitP (CharL 'f')) GHC.Types.:
(ConP GHC.Types.[] []))))] (NormalB
(ConE GHC.Bool.True)) []]]
</pre>
<p align="justify">A little indentation, of course, goes a long way in making things clearer (when does it not? :D):</p>
<p><a name="rawast"></a></p>
<pre>
[FunD
 generatedF [Clause
             [InfixP (LitP (CharL 'a'))
              GHC.Types.:
              (InfixP (LitP (CharL 's'))
               GHC.Types.:
               (InfixP (LitP (CharL 'd'))
                GHC.Types.:
                (InfixP (LitP (CharL 'f'))
                 GHC.Types.:
                 (ConP GHC.Types.[] []))))]
             (NormalB (ConE GHC.Bool.True)) []]]
  </pre>
<p align="justify">I usually use guesswork from this point, leaving it to Haskell&#8217;s type system to correct my incompetence.  Haskell&#8217;s AST has the convention of suffixing most data constructors with a letter which hints at what type it belongs to.  We can guess (and purely guess, I have not looked up the documentation) that <code>InfixP</code> is an infix pattern, a <code>LitP</code> is a literal pattern, a <code>NormalB</code> is a normal body, a <code>ConE</code> is a constructor expression, a <code>FunD</code> is a function declaration and so forth.  The AST for the match-anything clause is simpler:</p>
<pre>
  [FunD
   generatedF [Clause [WildP]
               (NormalB (ConE GHC.Bool.False)) []]]
</pre>
<p align="justify">You can guess what a <code>WildP</code> is, especially since it derives from a <code>_</code>.</p>
<h3>The core idea</h3>
<p align="justify">The basic idea behind TH is that allowing the user to splice in a bit of custom-generated AST into an otherwise normal Haskell program can lead to all kinds of awesomeness.  A <em>splicable</em> AST has to be typed as a <code>Q Exp</code>, a <code>Q Type</code> or a <code>Q   [Dec]</code>.  The last one stands for a list of declarations, which is the one we need right now.  To splice in an expression as an AST, we place it inside a <code>$( ... )</code>.  To keep things clean, top level declarations don&#8217;t need the <code>$( ...   )</code> nesting &#8212; anything of the type <code>Q [Dec]</code> at the top level of a source file is automatically interpreted correctly.  <code>Q</code> is a <code>Monad</code>, which is sometimes needed to generate unique identifiers, like Lisp&#8217;s <code>gensym</code>.  We don&#8217;t need that right now.</p>
<p align="justify">Getting back to the point, we need a function that takes the specified <a href="#input">input</a>, and outputs the AST of the <a href="#togenerate">code we want to generate</a>.  Since we need to generate something of the type <code>Q [Dec]</code>, and we will be given the name of the function to generate, and a list of   <code>String</code>s, there is at least one line we can write:</p>
<pre>
  generateMatcher :: String -> [String] -> Q [Dec]
</pre>
<p align="justify">We already have seen the <a href="#rawast">basic structure of the AST</a>.  We now write some code to generate it. We will generate only one <code>Dec</code> (i.e. the <code>[Dec]</code> in <code>Q [Dec]</code> will only have one element), corresponding to the one function we want to declare.  The <code>Dec</code> will be a <code>FunD</code> (just like in the AST <code>runQ</code> showed us), with multiple <code>Clause</code>s, each representing one pattern (which itself corresponds to one <code>String</code> we were given at compile time).</p>
<p align="justify">One thing though &#8212; for some reason I don&#8217;t really understand, while <code>runQ</code> prints them, things in the <code>GHC</code> module (like <code>GHC.Bool.True</code>,  <code>GHC.Types.:</code>) don&#8217;t really work in a generated AST.  A <em>token</em> in TH is essentially a <code>Name</code>, which we can get using <code>mkName :: String -> Name</code> or by using the quote (<code>'</code>) operator.  So one way to get around the problem is to represent things like <code>GHC.Bool.True</code> as <code>Name</code>s &#8212; this seem to work well in practice.  The quote operator does not seem to work for symbols &#8212; we replace things like <code>GHC.Bool.True</code> with <code>'True</code> and things like <code>:</code> with <code>mkName ":"</code>.  We also need to use <code>mkName</code> for the functions&#8217; name, which needs to be a (surprise) <code>Name</code>.</p>
<p align="justify">Okay, phew!  Time to see some code that actually works.  With the little Haskell-foo I have, I wrote the following   piece of code:</p>
<pre>
generateMatcher name strs =
  return [FunD (mkName name) (map thMagic strs ++ baseCase)]
  where
    thMagic str = Clause [transform str] (NormalB (ConE 'True)) []
    baseCase = [Clause [WildP] (NormalB (ConE 'False)) []]
    transform [] = ConP (mkName "[]") []
    transform ('?':rest) =
      let colon = mkName ":" in (InfixP WildP colon $ transform rest)
    transform (x:rest) =
      InfixP (LitP (CharL x)) (mkName ":") $ transform rest
</pre>
<p align="justify">Ten lines, as promised.  I used <code>return</code> to lift the <code>[Dec]</code> into the <code>Q</code> monad.  Other than, most of it is a direct translation of the input we get into something that looks like the output <code>runQ</code> dumped on me.  The code is in <a href="https://github.com/sanjoy/Snippets/blob/master/GenerateMatcher.hs">my   snippets repo</a>, as usual.  TH does not, as of now, allow you to declare an expression and splice it in the same file.  You need to have, say, a <code>FooTH.hs</code> where you have all the functions for generating the AST and a <code>Foo.hs</code> where you actually use them.</p>
<h3>TL;DR</h3>
<p align="justify">This was a long and meandering post.  In short,   here are the two basic points I want to make:</p>
<ul>
<li>TH is awesome.  It can save a lot of time, if used properly.</li>
<li>Don&#8217;t beat yourself up trying to write ASTs from scratch, play         around with <code>runQ</code>, steal and modify.</li>
</ul>
<p>[1] <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html">http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/615/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Threading A Graph</title>
		<link>http://playingwithpointers.com/archives/604</link>
		<comments>http://playingwithpointers.com/archives/604#comments</comments>
		<pubDate>Sun, 09 Oct 2011 09:13:12 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[gc]]></category>
		<category><![CDATA[graphs]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=604</guid>
		<description><![CDATA[One operation that moving garbage collectors (among other things) have to concern themselves with is updating of references. Once you move object X from position P0 to P1, you&#8217;ll also need to update all references to X accordingly. To be able to do this, you first need to somehow maintain a list of words that [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">One operation that moving garbage collectors (among other things) have to concern themselves with is updating of references.  Once you move object <em>X</em> from position <em>P<sub>0</sub></em> to <em>P<sub>1</sub></em>, you&#8217;ll also need to update all references to X accordingly.  To be able to do this, you first need to somehow maintain a list of words that point to a specific object.  There are obvious ways of this, of course &#8212; maintaining a hash-table that maps a object to a list of words that store references to it is one.  There is a more elegant way to do this, however; if the set of objects you&#8217;re working with satisfies the following constraints:</p>
<p align="justify">
<ul>
<li>Each object has a (header or otherwise) pointer-sized field which stores some information that can be differentiated from a pointer.  On a system that is aligns words to two or four byte boundaries, we     could set the LSB of non-pointer data stored in the     field.  We&#8217;ll call this field <em>info</em>.</li>
<li>Pointers to objects always point to the beginning of some object.      No pointing to a field stored somewhere in the middle.</li>
</ul>
<p align="justify">The algorithm is extremely simple and elegant.  All you have to do is this: every time you see a slot (a <em>field</em>, if you like) <em>S</em> point to an object <em>O</em>, change <em>O</em>s <em>info</em> field to point to slot <em>S</em> and set <em>S</em> to whatever value (pointer or non-pointer) <em>info</em> held before it was set to point to <em>S</em>.  If you try this out with pen and paper (I&#8217;m too lazy to draw a diagram here), you&#8217;ll see that this simple action, when applied to every slot in the graph, transforms the predicate &#8220;Slots <em>S<sub>0</sub></em>, <em>S<sub>1</sub></em> and <em>S<sub>2</sub></em> point to object <em>O</em>&#8221; to &#8220;<em>O</em>&#8216;s <em>info</em> slot contains a pointer to <em>S<sub>2</sub></em>, <em>S<sub>2</sub></em> contains a pointer to <em>S<sub>1</sub></em>, <em>S<sub>1</sub></em> contains a pointer to <em>S<sub>0</sub></em> and <em>S<sub>0</sub></em> contains the data <em>O</em>&#8216;s <em>info</em> field originally held&#8221;.  Since we can always distinguish a data word stored in <em>info</em> from a pointer, we can traverse this list and get a list of the slots.  The slots can then be filled with new location of the object.</p>
<p align="justify">This is one of the main concepts behind Jonker&#8217;s compaction algorithm, except that it intelligently divides the compaction into two phases of updating forward pointers and updating   backward pointers while moving  objects.</p>
<p align="justify">I wrote a simple and tiny implementation of threading a graph, which you can find at <a href="https://github.com/sanjoy/Snippets/blob/master/Thread.cc">https://github.com/sanjoy/Snippets/blob/master/Thread.cc</a>.  Note how the main threading algorithm only takes thirteen lines of C++.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/604/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tree Traversal in O(1) Space</title>
		<link>http://playingwithpointers.com/archives/600</link>
		<comments>http://playingwithpointers.com/archives/600#comments</comments>
		<pubDate>Sat, 08 Oct 2011 12:52:45 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[gc]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[O(1)]]></category>
		<category><![CDATA[thorelli]]></category>
		<category><![CDATA[traversal]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=600</guid>
		<description><![CDATA[I&#8217;ve been reading some texts recently, and came across a very interesting way to traverse a graph, called pointer reversal. The idea is this &#8212; instead of maintaining an explicit stack (of the places you&#8217;ve visited), you try to store the relevant information in the nodes themselves. One approach that works on directed graphs with [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I&#8217;ve been reading some texts recently, and came across a very interesting way to traverse a graph, called pointer reversal.  The idea is this &#8212; instead of maintaining an explicit stack (of the places you&#8217;ve visited), you try to store the relevant information in the nodes themselves.  One approach that works on directed graphs with two (outgoing) arcs per node is called the Deutsch-Schorr-Waite algorithm.  This was later extended by Thorelli to work for directed graphs with an unknown number of (outgoing) arcs   per node.</p>
<p align="justify">The Deutsch-Schorr-Waite algorithm does not require adding any extra fields to the nodes.  Adding fields to the node structure sort of spoils the fun &#8212; you could, for instance, have a pointer pointing to the parent of every node making stackless traversal a cup of tea.  And you haven&#8217;t really saved any space!  Thorelli&#8217;s approach, however, requires adding a integer to each node that can hold at least the maximum   number of outgoing arcs a node can possibly have.  But it can traverse graphs with more than two outgoing arcs per node.  And you do save space here &#8212; if you have three outgoing arcs (maximum) per node, you could do with just a two bit integer.  On x86, that&#8217;s <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" target="_blank">the last two bits of a pointer</a>.</p>
<p align="justify">Note that both the approaches are quite a bit more complicated than a <em>vanilla</em> DFS.  Nevertheless, there are applications where such characteristics (of consuming only constant space) becomes important.  One of the more visible ones is a GC.  A GC has to traverse a (potentially) very large and complicated graph of objects, and it can&#8217;t use up too much memory (or call-stack space) when collecting   &#8212; a time when memory is already a scarce resource.</p>
<p align="justify">I like implementing things, and while I did not implement the full algorithm proposed by Thorelli, I did implement one which traverses a <em>tree</em> in constant space.  What more, it does   not need the extra bits Thorelli&#8217;s algorithm requires.</p>
<p align="justify">The code is here (I believe in making code talk instead of giving long, winded descriptions):   <a href="https://github.com/sanjoy/Snippets/blob/master/Traversal.cc">https://github.com/sanjoy/Snippets/blob/master/Traversal.cc</a>.</p>
<p align="justify">The snippets repo is an idea that I recently got &#8212; I often code small things when I&#8217;m bored or want to try something out.  Having a public &#8220;snippets&#8221; repo where I upload all that smelt good.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/600/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Haskell&#8217;s Fixed Point Combinator</title>
		<link>http://playingwithpointers.com/archives/590</link>
		<comments>http://playingwithpointers.com/archives/590#comments</comments>
		<pubDate>Thu, 06 Oct 2011 12:44:47 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[fixed point]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[y combinator]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=590</guid>
		<description><![CDATA[About this post This is one of those posts I write to ensure I actually understand a concept. Read at your own peril. Some knowledge of Haskell is required. The Problem One Hello World problem that is sometimes used to demonstrate a programming language is generating the Fibonacci series. The nth Fibonacci number is given [...]]]></description>
			<content:encoded><![CDATA[<h3>About this post</h3>
<p align="justify">This is one of those posts I write to ensure I actually understand a concept.  Read at your own peril.  Some   knowledge of Haskell is required.</p>
<h3>The Problem</h3>
<p align="justify">One <em>Hello World</em> problem that is sometimes used to demonstrate a programming language is generating the Fibonacci     series.  The n<sup>th</sup> Fibonacci number is given as:</p>
<pre>
fib 0 = 1
fib 1 = 1
fib x = fib (x - 1) + fib (x - 2)
</pre>
<p align="justify">This, as it turns out, is one way to define the   n<sup>th</sup> Fibonacci number in Haskell.  Save this code as     <code>Fib.hs</code>, start the GHC REPL as <code>ghci Fib.hs</code> and you&#8217;re done &#8212; <code>fib 5</code> evaluates to <code>8</code> and <code>fib 7</code> evaluates to <code>21</code>.</p>
<p align="justify">However, as cute this may be, it isn&#8217;t elegant enough.  Fibonacci is essentially a series, and while mathematicians will tell you that a series is essentially a function (and that &#8220;is essentially&#8221; is a transitive relation)  this definition does not, yet, sit quite well.  Of course, you say, something like this can be done:</p>
<pre>
fibSeries = map f [0..]
            where
              f 0 = 1
              f 1 = 1
              f x = f (x - 1) + f (x - 2)
</pre>
<p align="justify">Not only is this approach a hack but is also quite slow.  One nice, canonical, way to define fibSeries is this:</p>
<pre>
fibSeries = 1:1:zipWith (+) (tail fibSeries) fibSeries
</pre>
<p align="justify">Note the recursion here &#8212; the series of Fibonacci   numbers remains the same if we take a list of two <code>1</code>s, and append it with the elementwise sum of the list of Fibonacci numbers   and the list of Fibonacci numbers less the first element.  Let this sink in.</p>
<h3>Enter the fixed point combinator.</h3>
<p align="justify">There happens to be another, more elegant, way of expressing fibSeries, using <code>fix</code>, Haskell&#8217;s fixed point combinator.  The above definition of <code>fibSeries</code> refers to itself in its definition.  Let&#8217;s say you don&#8217;t want this, perhaps for pure intellectual pleasure (like here :P) or for deeper reasons &#8212;  untyped lambda calculus, for instance, does not have named values, and so you <em>cannot</em> write an expression that refers to itself.  In   such cases, we use a fixed point combinator.</p>
<p align="justify">The first interesting thing about <code>fix</code> is its type: <code>:t fix</code> in <code>ghci</code> (fix is in <code>Data.Function</code>) will tell you its type is <code>(a->a)->a</code>.  This should strike as counter-intuitive &#8212; how can a function <em>possibly</em> take a, say, <code>Integer->Integer</code> (let&#8217;s call this function <code>foo</code>) and return an <code>Integer</code>?  It needs some value to apply <code>foo</code> to, right?  The short answer is that <code>fix</code> calculates <code>foo</code>&#8216;s <em>fixed point</em>.  A slightly longer answer follows, which ends at TL;DR.</p>
<p align="justify">The fixed point of <code>foo</code> is the value <code>v</code> such that <code>foo v</code> equals <code>v</code>.  It is fixed in the sense that no matter how many times we apply <code>foo</code> on the fixed point of <code>foo</code>, it remains <em>fixed</em>.  One way to calculate the fixed point of <code>foo</code> is to apply it to <code>⊥</code> infinite number of times.  This works because a type is a complete partial order and that <code>foo</code> is (Scott) continuous.  In general the person defining <code>foo</code> needs to ensure this, but, thankfully, in Haskell, all functions you can <em>possibly</em> define are Scott Continuous.  Relevant theory is <a href="http://en.wikipedia.org/wiki/Complete_partial_order">here</a>.  Keeping this way of looking at <code>fix</code> in mind (infinite applications of <code>foo</code> on undefined), you could implement <code>fix</code> as:</p>
<pre>
fix x = applyInfinitely x undefined
  where
    applyInfinitely f x = f (applyInfinitely f x)
</pre>
<p align="justify">There are other ways to define <code>fix</code>, of course.  In an untyped lambda calculus, you can define it as <code>fix f = \x -> f (x x)) (\x -> f (x x)</code> (this is the famed Y combinator).  If you&#8217;re well-versed with types, you could implement the Y combinator in Haskell too; but then you&#8217;ll have to depend on recursion at the type level.  I like to think of <code>fix</code> as something that <em>encapsulates</em> recursion, allowing you to write expressions that don&#8217;t have any explicit recursion, but are recursive   nevertheless.</p>
<h3>TL;DR</h3>
<p align="justify">Coming back to the meaty aspects of <code>fix</code>, how do you write the function in whose fixed point is the value you want to compute?  In this case we want <code>fix</code> to produce a <code>[Integer]</code>.  This means the function we will write (<code>foo</code>) needs to have the type <code>[Integer]->[Integer]</code>.  Which means it will take an <code>[Integer]</code> and return an <code>[Integer]</code>.  But what <code>[Integer]</code> does it take?  The same <code>[Integer]</code> it is supposed to return!  As a first attempt, with this knowledge, you could write:</p>
<pre>
fibSeries = fix (\x->x)
</pre>
<p align="justify">I mean, since <code>fix</code>&#8216;s argument (we&#8217;ll call it <code>foo</code> consistently) is somehow magically the same <code>[Integer]</code> <code>foo</code> is supposed to return, what could be more straightforward?  However, something smells wrong.  Both because of the word <em>magically</em> and the fact that this definition of <code>fibSeries</code> simply does not contain <em>any</em> information specific to the Fibonacci series (well, except the name <code>fibSeries</code>, but I think that is expecting too much of GHC :D).  One thing I had not mentioned is this: when we say &#8220;fixed point&#8221; we really mean &#8220;<strong>least</strong> fixed point&#8221;.  Deep in his or her heart, everyone who plays with types knows that a type is a poset with a bottom (<code>⊥</code>).  The ordering in this poset is essentially information based &#8212; <code>A</code> > <code>B</code> ⇒ <code>A</code> has more information than <code>B</code>.  So, in the poset of <code>Integer</code>s, <code>1</code> and <code>2</code> are incomparable, since both of them contain an equal amount of information.  And then you have ⊥, which contains the least information of all.  What is the <em>least</em> value (in terms of the &#8220;least&#8221; defined above) that can be the fixed point of <code>\x->x</code> (the identity function)?  <code>⊥</code>, of course!  Since <code>(\x->x) ⊥</code> is <code>⊥</code> and no doubt, <code>⊥</code> contains the least information of all by definition!</p>
<p align="justify">A more intuitive way of looking at this is that when constructing <code>foo</code>, you need to make sure it <em>adds</em> some information.  You need to embed some extra information into <code>foo</code> to ensure that, given the value you&#8217;re supposed to return, you also do some identity preserving operation on it, <em>assuming that it is the same value you&#8217;re supposed to return</em>.  This is not as hard or tricky as it sounds.  For instance, calculating <code>fibSeries</code> using <code>fix</code> is a direct extension to the self referencing definition of <code>fibSeries</code>:</p>
<pre>
fibSeries = fix (\x->1:1:zipWith (+) (tail x) x)
</pre>
<p align="justify">Another cute example is using <code>fix</code> to   define a factorial function:</p>
<pre>
factorial = fix (\f x->if x == 0 then 1 else x * f (x - 1))
</pre>
<p align="justify">By the way, why won&#8217;t this</p>
<pre>
fibSeries = fix (\x->1:zipWith (+) (tail x) x)
</pre>
<p align="justify">work?</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/590/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Abstraction, Death of Nihilism and The Human Experience</title>
		<link>http://playingwithpointers.com/archives/580</link>
		<comments>http://playingwithpointers.com/archives/580#comments</comments>
		<pubDate>Sun, 18 Sep 2011 22:15:42 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Offline Bytes]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=580</guid>
		<description><![CDATA[One way of looking at how human beings behave is to talk about our basic desires (food, sex and power) and how they manifest themselves as complex behaviour. The reason the rich guy helped the poor guy is because that helps him demonstrate power, which increases his chances of getting laid. The engineer builds bridges [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">
One way of looking at how human beings behave is to talk about our basic desires (food, sex and power) and how they manifest themselves as complex behaviour.  The reason the rich guy helped the poor guy is because that helps him demonstrate power, which increases his chances of getting laid.  The engineer builds bridges to impress men (see how cleverly I spliced in that the engineer is either a woman or gay &#8212; these things help keep my blog politically correct) and the doctor refuses to perform cosmetic surgery (again, more political correctness) because he thinks when the word gets around, he&#8217;ll get some extra action.  Now, however incredulous my tone may sound, I think this way of looking at things is quite accurate; I believe we <strong>do</strong> have a certain set of basic instincts that guide our actions.  But I disagree on whether this is a useful way to reason about people.  I don&#8217;t think it is.  Not because it isn&#8217;t true (it probably is), but because more complex behaviour may exist at higher levels of abstraction.
</p>
<p align="justify">
For instance, I could argue about how we all are automatons driven solely by the desire to increase the entropy of the universe.  Not a very useful way to discuss why A&#8217;s marriage did not work out, is it?  I find it more useful to look at human beings as a reflection of their line of existence through time and place.  This way, I treat a person not as someone (or something) contained in the physical realms of his or her body; but as a context that spreads out in time and space.  The coffee he&#8217;s drinking is changing his mood, why should it not be as much &#8220;him&#8221; as his tonsils?  How about the kindergarten school she went too?  Is that not as much &#8220;her&#8221; as her retina?  How about the book she&#8217;s reading?  This way of thinking about people is very rewarding (I personally think so) &#8212; it allows me to be more compassionate, understanding and patient.  Once the object of your frustration is no longer a sharply defined physical entity, things start to diffuse and you begin thinking more clearly.
</p>
<p align="justify">
As a result of this <em>context</em> (spatial and temporal) which everyone carries around, we have certain goals, aspirations and biases.  That is, what I think, makes us human.
</p>
<p align="justify">
I have had arguments with my peers on things like whether it is better to live comfortably without free speech and liberty or to live without material comfort surrounded by intellectual freedom.  But such arguments are probably too restrictive &#8212; maybe the North Koreans are actually happy!  Perhaps they [2] really <em>do</em> think that their dear leader is divine.  Maybe is isn&#8217;t some mass cognitive dissonance after all?  I find their condition desolate and pitiful because I am carrying around this huge bag full of <strong>my own</strong> context, which I shamelessly dump on them.  What is it really, to think in someone else&#8217;s shoes?  Can I <em>possibly</em> think like a unwanted child born to a prostitute in a Bombay slum?  My personal rationality is biased, more so when applied to analyzing others.
</p>
<p align="justify">
It seems to me that everyone, on account of the biases in their rationality and the inherent context they carry, tend to distort their world in their own unique way, by reflecting themselves off a mirror that isn&#8217;t really perfect and by looking at others through tainted glass.  But then how <em>do</em> you reason about the flaws in your rationality using your flawed rationality?  Maybe this, the dents in our looking glasses, is the human experience.  Perhaps without it the world would be a very boring place.
</p>
<p align="justify">
I still maintain that our existence, at the very base level, is purposeless and incidental.  But I now have something shiny to look forward to &#8212; the human experience; something that can not be dissected, explained and thrown away.
</p>
<p align="justify">
  <em>Thanks to all those who disagreed, those who still do, chai and Albert Hofmann.</em>
</p>
<p>[1] http://www.edge.org/3rd_culture/eagleman09/eagleman09_index.html<br />
[2] I&#8217;m talking about the majority here &#8212; some people do try to escape.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/580/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Young Lust</title>
		<link>http://playingwithpointers.com/archives/557</link>
		<comments>http://playingwithpointers.com/archives/557#comments</comments>
		<pubDate>Mon, 15 Aug 2011 12:55:19 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Offline Bytes]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=557</guid>
		<description><![CDATA[One of the track in The Wall is Young Lust. Brilliant guitaring apart, the track talks about how the protagonist (Pink) wants a dirty woman and a dirty girl. On a first glance, this looks unlike most other Pink Floyd songs. Why, then, does this track exist? I think, it is connected to a previous [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">
One of the track in <em>The Wall</em> is <em>Young Lust</em>. Brilliant guitaring apart, the track talks about how the protagonist (Pink) wants a <em>dirty woman</em> and a <em>dirty girl</em>. On a first glance, this looks unlike most other Pink Floyd songs. Why, then, does this track exist?
</p>
<p align="justify">
I think, it is connected to a previous track titled <em>Mother</em>. The movie (album) is about the construction and (later) deconstruction of a metaphorical wall. The wall exists at many different levels (for instance, as the divide between the elite and the starving masses, apparent in <em>What shall we do now</em>); the most basic being the protective wall built around Pink by his mother.
</p>
<p align="justify">
<em>Mother</em> has a line on how Pink&#8217;s mother would check all of his girlfriends for him and won&#8217;t let anyone <em>dirty</em> get in. Here, Pink wanting a <em>dirty</em> girl signals the beginning of the wall&#8217;s destruction.
</p>
<p align="justify">
Pink Floyd amazes me every single time.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/557/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.853 seconds -->
<!-- Cached page served by WP-Cache -->

