August 24, 2010

Etudes

Hi,

I'm going to write small number of websites in order to catchup with modern web.

Each will need subject and simple composition.

Borrowed from thecprogrammer.com
Etude One  Tic-tac-toe

- There is deck of empty cards on the page
- You can take one (dragging it from the deck) to invite play
- I can take another card from deck to invite play

Two one-handed cards invite each other, thus
game begins.

There is BOT which invites one-handed card on some timeout.

All cards are visible on the screen but they repelled by card you play towards margins.

August 15, 2010

scalac phases

Scala 2.8 compiler process sources trough 27 phases.

Compiler phases

# I'll illustrate `what's going on' in `bash code
scalac -Xshow-phases

The naming of those phases defined in man page. To get to the meaning we have to detour and become compiler hacker or rediscover stuff hands on style. I'm not that much into compiler internals yet and have to choose `discovery mode.

Collecting information

man scalac
Compilation Phases

initial    - initializing compiler
parse      - parse source files
namer      - create symbols
analyze    - name and type analysis
refcheck   - reference checking
uncurry    - uncurry function types and applications
transmatch   - translate match expressions
lambdalift   - lambda lifter
typesasvalues            - represent types as values
addaccessors             - add accessors for constructor arguments
explicitouterclasses     - make links from inner classes to enclosing one explicit
addconstructors          - add explicit constructor for each class
tailcall    - add tail-calls
wholeprog   - perform whole program analysis
addinterfaces            - add one interface per class
expandmixins             - expand mixins by code copying
boxing     - makes boxing explicit
erasure    - type eraser
icode      - generate icode
codegen    - enable code generation
terminal   - compilation terminated


Lets prepare stage. 

We use simple code with `case class and `for expression.

// Scala source: Frameworks.scala

object cph { 
  case class Person(name: String, mail: Boolean, children: Person*)
  //
  val bob = Person("Bob", true)
  val aida = Person("Aida", false)
  val cipi = Person("Cipi", true, bob, aida)
  val alina = Person("Alina", false, bob, aida)
  val all = List(bob, aida, cipi, alina)
  //  println(all)
  //
  val mochi = all filter (_.mail == false) 
flatMap (p => p.children map (c => (p, c)))
  //
  mochi foreach { 
    pair =>  
      val (p: Person, c: Person) = pair
      println(p.name + " > " + c.name)
  }
  //
  val motherOf = for (m <- all;
                      c <- m.children 
                      if m.mail == false) 
                 yield (m, c)
  //
  for ((m, c) <- motherOf)
    println (m.name + " - " + c.name)
}
(1) - Code formatting made ugly on purpose. I'm working with `emacs scala-mode with `sbt replacing `scala shell. Coding paralleled with 'sbt> ~compile', which means `compile whenever source files change. Going to REPL with 'sbt> console-project' I can use select paragraph M-h then sent it to interpreter with C-c-e. That's why scala blocks formatted as emacs paragraphs.


Generate reports for all phases.

$ scalac -Xshow-phases 2>&1 |while read f; do echo ">> $f";
> scalac -Xprint:$f Frameworks.scala 2>&1 > Frameworks.$f.scala;
> done

Analyze 

We need diff's from consecutive phase. Could be done nicely with change-set manager HG designed just for this.

scalac -Xshow-phases > scala.phases
for f in `cat scala.phases `; do echo ">> $f"; 
 cp  Frameworks.$f.scala Frameworks.scala; 
 hg ci -m"$f" Frameworks.scala; 
 done
 
hg log Frameworks.scala
changeset:   34:c03403f4f0c4
summary:     namer

changeset:   33:37ed3eeb753e
summary:     parser

changeset:   32:917dc0255899
summary:     add Framework.scala

hg diff -r33:34
diff -r 37ed3eeb753e -r c03403f4f0c4 ScalaLanguage/src/main/scala/Frameworks.scala
--- a/ScalaLanguage/src/main/scala/Frameworks.scala Sun Aug 15 13:03:51 2010 +0300
+++ b/ScalaLanguage/src/main/scala/Frameworks.scala Sun Aug 15 13:03:51 2010 +0300
@@ -1,6 +1,6 @@
-[[syntax trees at end of parser]]// Scala source: Frameworks.scala
+[[syntax trees at end of namer]]// Scala source: Frameworks.scala
 package  {
-  object cph extends scala.ScalaObject {
+  final object cph extends scala.ScalaObject {
     def () = {
       super.();
       ()


Visualizing 

It shouldn't be that much console stuff, but there is nice 'unix tradition to get you trough process even if don't have installed `kdiff3 or `hgview.

hgview on scalac phases



I was looking for translation of `for expression. Surprisingly, it done at the very beginning in `parse phase.

I followed remaining phases too.  It is quite entertaining to see code morphing from Sources to JVM.  It's also much more educative that simple `scalap  -verbose or even `jd.  However good showing off need special code snippets for which I'm going to look because it worse effort.

Naming phase.

Typer phase.

Specialize to ExplicitOuter.

August 11, 2010

Lifehack, Alarm invertion

Suppose there is strong reason for you to wake up at 7h00.
Default solution is to set alarm clock at appropriate time.


This hack use Inversion of Control (IoC) principle to achieve same effect when,
instead to wake up at 7h00 you go to sleep at 23h00. Following the idea alarm should be set to 23h00 (day before). After 8 hours of sleep you up.

Hack will probably fail first time when sleep debit is big, but in the long run this it's preferable to default solution.

Novelty of the approach can be contrasted in two positive intentions: 
The early bird catches the worm.
  and
It is better to oversleep than to undereat.




This hack isn't without BUG. It missing crucial part - Asynchronous Completion Token. When you wake up you need to recall context in which you set alarm and it is not implicitly true if reason to wake up every time different.

July 31, 2010

Desktop (at) Jul 31 2010

I organize my Desktop on III trends:
  • increasing comfort;
  • decreasing effort;
  • keeping productivity constant.

Currently I sit on two screens with same X11 but no Xinerama.
All stuff orchestrated by `Ion3 Window Manager.

It feels just right despite it's 5 dimensions which I thought would be to much to use subconsciously because of my 3D habits.

My front view has Screen1 ++ Screen2,
 Each screen has Windows on `X and Desktops on `Z,
  Windows divided into Frames (`X-horizontal or `Y-vertical),
 Frame has Tabs like in Firefox which slice `X ones more.

Screen 1 Screen 2

Left monitor smaller then right. I do reading from it.

Right is for work.

Desktops organized by themes on Screen1 (Office, RTFM's etc.) and tasks on Screen 2. Tasks have usually one or two application which need full screen (Eclipse, Remote Desktop to Windows machine or remote X Session) and some small frames with terminal app's and scripts.
Fulscreeners are collapsed initially  and only when I'm focusing on the task they goes big on `Z so I still have all stuff on 2D distance. Themes also adjusted to the task to extract me required info.



Before I used Window Managers with overlapped windows. Normally it consume 5-6 desktops times 2 monitors.
Now, just 5-6 on both monitors in any time. It's contra intuitive but tiling WM save 1.2 square meters of pixels for me.

I use mouse for editing and never for navigating because I have to move it and still not able to go directly trough all Ion's dimensions. I use teleport instead which doesn't have those limits then just tasting shortcuts in app's.

July 26, 2010

There no such think as lie

My point exactly is that there no deliberate untruth.
Fist account is logical equality of `rightful `true teller and `deluded `liar.
It can be checked by logical table and all YES/NO answers will be same.

Suppose real person. She is liar. Lie is recurring, also reflective process, from it comes self delusion. If we recall that this person liar it become indistinguishable from honest and sane one.

The only difference is inside.


Geeks jokes

trait Philosophical {
    def philosophize = println ("I consume memory, therefor I'm\nQED")  
} 


Atoms dialog:
- i lost my electron
- i you sure?
- yes, i'm positive!