Wednesday, December 10, 2008

v8 not build on x86_64

Stupid Google, why would your v8 not build on 64bit system?

scons mode=release library=shared prefix=/usr wordsize=64 || return 1

g++ -o obj/release/third_party/jscre/pcre_compile.os -c -march=x86-64 -mtune=generic -O2 -pipe -ansi -w -O3 -fomit-frame-pointer -m32 -fno-rtti -fno-exceptions -ansi -w -O3 -fomit-frame-pointer -m32 -fPIC -DENABLE_LOGGING_AND_PROFILING -DSUPPORT_UTF8 -DNO_RECURSE -DSUPPORT_UCP

Look at that dreadful -m32 ...

Monday, December 8, 2008

back to archlinux

stupid windows it doesnt' let me fucking build llvm and hssdl doesn't work well on fucking windows fuck fuck fuck.
so, pacman -Syu then
pacman -Sf zsh #for http://www.archlinux.org/news/421/
rm /usr/lib/klibc/include/asm #for http://www.archlinux.org/news/411/
Then,
Section "ServerFlags"
 Option  "AllowEmptyInput" "false"
EndSection
for http://www.archlinux.org/news/424/

Saturday, December 6, 2008

todo

http://alien.luaforge.net/
http://luaforge.net/projects/luaffi/
http://cython.org/
http://code.google.com/apis/v8/embed.html

installing cabal packages on windows vista

http://www.haskell.org/ghc/docs/latest/html/Cabal/builders.html
By default on windows, --datadir=C:\Program Files\Haskell. And vista complains.
So,

runghc Setup.lhs configure --prefix=C:\Users\sam\haskell --datadir=$prefix\share

Thursday, April 17, 2008

Niffy Template Haskell trick for better trace

This trick is by EvilTerran from #haskell freenode IRC channel.

A quick and dirty way to debug Haskell code is to use trace function from Debug.Trace module.

For example,

f (trace ("arg1: " ++ show arg1) arg1) arg2

would print

arg1: <actual value of arg1>

Usung Template Haskell, above code can be shortened.

> {-# LANGUAGE TemplateHaskell #-}

This means this code uses TemplateHaskell extension.

> module Trace where

Let's call the module Trace.

> import Language.Haskell.TH
> import Debug.Trace (trace)

And, import some modules.

> t name = [| trace ($(litE . StringL $ nameBase name)
>    ++ ": " ++ show $(varE name)) $(varE name) |]

Then, define the macro t that can be called as:

$(t 'varName)

And it'll be expanded to:

trace ("varName" ++ ": " ++ show varName) varName

Let's actually use Trace.t in Main module.

> {-# LANGUAGE TemplateHaskell #-}

Main module also needs to use TemplateHaskell extension because it'll include macro call like $(t 'a).

> module Main where
> import Trace (t)
> import Debug.Trace (trace)

> main = do
>     let a = 2
>     let bravo = 40
>     putStrLn $ show ((trace ("a: " ++ show a) a) + bravo)

compare this with

>     putStrLn $ show ($(t 'bravo) + $(t 'a))

>     return ()

Shorter.

Now, I need vim keyboard shortcut that'll replace the word under cursor with $(t 'wordUnderCuror). And, another keyboard shortcut that'll turn $(t 'word) into word.

Edit: Alok commented with vim function below:

" word <===> $(t 'word)
" by Alok
function! ToggleTrace()
    call searchpos('\|)')
    let b = searchpos('\<', 'b')
    let s = searchpos("\$(t '", 'b')
    if s[0] == b[0] && b[1] - s[1] == len("$(t '")
        " remove trace
        norm df'f)x
    else
        " add trace
        call insert (b, 0, 0)
        call add (b, 0)
        call setpos('.', b)
        let @z = "$(t '"
        norm "zP
        let @z = ")"
        norm f "zP
    endif
endfunction

nmap <leader>t :call ToggleTrace()<cr>

Thank you Alok.

Monday, March 10, 2008

factor listner font size

in ~/.factor-rc
for 0.91
IN:  ui.freetype
: dpi 120 ;

for git version
USE: namespaces
USE: ui.freetype
120 dpi set-global

Thursday, March 6, 2008

Staque

Accompanying presentation

I was thinking about how to let users mix up prefix, infix, and postfix syntax.

For example, + (+ 1 2) 3, 1 + 2 + 3, and 1 2 + 3 + would all be grammatical.

Using a stack and a queue, it might be possible:

  • Each token (value) is pushed on to the stack.
  • If the value is a function, pop from the stack to get arguments.
  • If stack doesn't have enough arguments, pop from the queue.

Basically, it is stack based evaluation (postfix) with look ahead to give users illusion of prefix or infix.

1 + 2 + 3, for example, would be evaluated as follows:

Queue: 1 + 2 + 3
Stack:
--
Queue: + 2 + 3
Stack: 1
--
Queue: + 3
Stack: 3
--
Queue:
Stack: 6

I name the language Staque and here is prototype:

> module Main where

Import stuff for parser.

> import qualified Text.ParserCombinators.Parsec as P
> import Text.ParserCombinators.Parsec ( (<|>) )

Import stuff for printing.

> import qualified Text.PrettyPrint.HughesPJ as PP

Import stuff for repl.

> import System.IO ( stdout, hFlush )

Let's define values for the language.

> data Val = Int Integer
>     | Ident String
>     | Expr [Val]

Let Val to be showable.

> ppVal (Ident s) = PP.text s
> ppVal (Int i) = PP.integer i
> ppVal (Expr xs) = PP.parens (PP.hsep $ map ppVal xs)
> instance Show Val where show = PP.render . ppVal

Let's write a parser. Expression is just a list of tokens separated by whitespaces.

> parseExpr = do
>     toks <- P.sepEndBy parseToken ws
>     return $ Expr toks
> ws = P.skipMany1 P.space

Expression can be parenthesized.

> parseParenExpr = do
>     lparen
>     e <- P.sepEndBy parseToken ws
>     rparen
>     return $ Expr e
>     where
>         lparen = P.char '(' >> P.spaces
>         rparen = P.spaces >> P.char ')'

A token can be an integer literal, an identifier, or a parenthesized expression.

> parseToken = do
>     P.try parseParenExpr
>     <|> P.try parseInt
>     <|> P.try parseIdent

Let's parse integer literal. An integer literal can start with -.

> parseInt = do
>     sign <- P.string "-" <|> return ""
>     val <- nat
>     return $ Int (read $ sign ++ val)
> nat = P.many1 P.digit

Let's parse identifier. Identifier can be operator or name.

> parseIdent = do
>     ident <- parseOp <|> parseName
>     return $ Ident ident
>     where
>         parseOp = parseHeadBody opChar opChar
>         parseName = parseHeadBody nameChar nameChar
>         parseHeadBody hChar bChar = do
>             h <- hChar
>             b <- P.many bChar
>             return (h : b)
>         opChar = P.oneOf ":!#$%&*+./<=>?@\\^|-~"
>         nameChar = P.alphaNum <|> P.oneOf "_-'"

Now, onto actual evaluation.

Let's define stack and implement push and pop:

> type Stack = [Val]
> push v s = v : s
> pop (x:xs) = (x, xs)

Here is queue. We only consume a queue. Never push element to the queue.

> type Queue = [Val]
> front (x:xs) = (x, xs)

Evaluation function. Finally!

> eval :: Stack -> Queue -> Val

When the queue is empty, evaluation is done. Make sure the stack has only 1 element and return the element as the result of evaluation.

> eval s [] | length s == 1 = (fst . pop) s

When the queue contains an expression, evaluate the expression.

> eval s [Expr q] = eval s q

Now, the queue's front is an identifier. Let's look it up and call the function bound to the identifier. Also, we make sure the rest of the queue is evaluated with the updated stack.

> eval s (Ident fname : args) = let
>     (s', q) = funcall fname s args
>     in
>         eval s' q

The queue's front is not an identifier. Assume it's a literal and push it to the stack and evaluate the rest of the queue.

> eval s (x : xs) = let
>     s' = push x s
>     in
>         eval s' xs

Funcall just looks up a function. If found, it calls the function with stack and queue. The called function returns updated (Stack, Queue).

> funcall fname s q = case lookup fname primitives of
>     Nothing -> error $ fname ++ " not defined"
>     Just f -> f s q

Funcall looks up this map.

> primitives = [
>     ("+", binNumOp (+))
>     , ("-", binNumOp (-))
>     , ("/", binNumOp div)
>     , ("*", binNumOp (*))
>     ]

Before we define a function that uses stack and queue to evaluate binary numeric operations, let's define helper functions.

To unpack and pack integers from and to Val.

> fromVal (Int a) = a
> toVal a = Int a

To evaluate values popped from stack or queue. If the popped value is an expression, evaluate the expression using a new stack. Otherwise, just return the popped value.

> evalVal val = case val of
>     Expr q -> eval [] q
>     otherwise -> val

Now, onto evaluation of binary numeric operation.

First, stack has 2 elements. So, both arguments to the binary operation can be popped from the stack. The arguments popped are evaluated because they can be nested expressions. Then push the result of operation to the stack and return it with queue.

> binNumOp op s q | length s >= 2 = let
>     (b, s') = pop s
>     (a, s'') = pop s'
>     a' = evalVal a
>     b' = evalVal b
>     result = toVal $ fromVal a' `op` fromVal b'
>     in
>         (push result s'', q)

When stack has only 1 element, we should pop from the queue, too.

> binNumOp op s q | length s >= 1 = let
>     (a, s') = pop s
>     (b, q') = front q
>     a' = evalVal a
>     b' = evalVal b
>     result = toVal $ fromVal a' `op` fromVal b'
>     in
>         (push result s', q')

Stack is empty. So, pop 2 arguments from the queue.

> binNumOp op s q = let
>     (a, q') = front q
>     (b, q'') = front q'
>     a' = evalVal a
>     b' = evalVal b
>     result = toVal $ fromVal a' `op` fromVal b'
>     in
>         (push result s, q'')

Now, let's make a repl.

> repl = do
>     input <- prompt "staque> "
>     if input == ":q"
>         then putStrLn "bye"
>         else do
>             putStrLn $ evaluate input
>             repl
>     where
>         prompt p = do
>             putStr p
>             hFlush stdout
>             getLine

Actual evaluate function that transforms user input to string.

> evaluate s = case P.parse parseExpr "staque" s of
>     Left err -> show err
>     Right (Expr q) -> show $ eval [] q

Finally, main function.

> main = repl

Let's run it!

$ runhaskell staque.lhs
staque> 1 + 2
3
staque> + 1 2
3
staque> 1 2 +
3
staque> 1 + 2 + ((1 - -2) * 3) 3 / (+ 1 2) *
12
staque> :q
bye

For exercises:

  • Add more types like string, float, list, char, bool... to the language.
  • Add function definition and lambda abstraction.
  • Add static type checking with user defined types.
  • Get fancier with type system.
  • Add foreign function interface.
  • Add OpenGL binding.
  • Make a first point shooter.

Wednesday, January 16, 2008

GLFW Archlinux package.

I bought SOE. And here is GLFW PKGBUILD for Archlinux:

pkgname=glfw2hs
pkgver=0.3
pkgrel=1
pkgdesc="A Haskell module for GLFW OpenGL framework. It provides an alternative to GLUT for OpenGL based Haskell programs."
url="http://haskell.org/haskellwiki/GLFW"
license=('GPL')
depends=('gcc' 'ghc' 'xorg-server')
arch=('i686')
source=("GLFW-$pkgver.tar.gz")
install=('glfw2hs.install')
md5sums=('c1cefce0573dd0237031fc3d28b4514d')
build() {
    cd "$startdir/src/GLFW-$pkgver"
    runhaskell Setup.hs configure --ghc --prefix=/usr
    runhaskell Setup.hs build || return 1
    runhaskell Setup.hs register --gen-script
    runhaskell Setup.hs unregister --gen-script
    install -D -m744 register.sh "$startdir/pkg/usr/share/haskell/$pkgname/register.sh"
    install -m744 unregister.sh "$startdir/pkg/usr/share/haskell/$pkgname/unregister.sh"
    runhaskell Setup.hs copy --destdir="$startdir/pkg"
}

This is glfw2hs.install:

HS_DIR=/usr/share/haskell/glfw2hs

post_install() {
    ${HS_DIR}/register.sh
    echo "GLFW registered"
}

pre_upgrade() {
    ${HS_DIR}/unregister.sh
}

post_upgrade() {
    ${HS_DIR}/register.sh
}

pre_remove() {
    ${HS_DIR}/unregister.sh
}
op=$1
shift

$op $*

Tuesday, January 15, 2008

Sambja: the song

Ok. Writing a song in 30 minutes.

Melody and chords: easy.

   | G/B    | Bm7    | C/B     | Bm7    |
a b  a b a e  a d a g  f# g a d  a d d b

C       | Bm/C    | Cm     | Eb  F
f# d d g  f# g a b  c g a b  c g d

mp3

Coming up with lyrics is hard: :)

Sam, be joyous again. Your precious days are near here across the nations. Rejoice now. Hail through the night. Get some sleep now. Wipe your tears away.

Wednesday, January 9, 2008

Epic year

Epoch time starts from 1970. Given a date in YYYY-MM-DD, list all dates that evaluates to 1970. For example, 2005-11-24 = 1970.

In python:
[(yyyy,mm,dd) 
    for dd in range(1,31) 
    for mm in range(1,12) 
    for yyyy in range(1970+1+1,1970+12+31) 
        if time.localtime(time.mktime((yyyy,mm,dd,0,0,0,0,0,0)))[:3] == (yyyy,mm,dd) 
            and yyyy-mm-dd == 1970]

So, 1970+12+31 = 2013

Friday, January 4, 2008

Some type system jargons

Type system jargons:

----------------------+------------------------------+-----------------------
Yes                   |                              |  No
----------------------+------------------------------+-----------------------
Manifest              |                              |  Latent
                      | explicit type declaration    |
`int a = 1`           |                              |  `a = 1`
                      |                              |
----------------------+------------------------------+-----------------------
Static                |                              |  Dynamic
                      | AST node has type            |
                      | (note AST is built during    |
                      |  compilation)                |
`a + 2`               |                              |  `a + 2`
already is Int        |                              |  is evaluated to 3
during compilation    |                              |  during runtime
                      |                              |  and gets type Int
                      |                              |
----------------------+------------------------------+-----------------------
Weak                  |                              |  Strong
                      | type coercion                |
`print 2`             |                              |  `print (String)2`
Int is automatically  |                              |  Int is explicitly
coerced to String     |                              |  converted to String
                      |                              |
----------------------+------------------------------+-----------------------
Nominative            |                              |  Structural
                      | type compatibility decleared |
                      | explicitly                   |
`class C extends B`   |                              |  `class C { x, y }`
C is compatible to B  |                              |  `class B { x }`
because it is         |                              |  C is compatible
explicitly            |                              |  to B because it
declared to extend B  |                              |  has similar
                      |                              |  structure
----------------------+------------------------------+-----------------------

Tuesday, January 1, 2008

Fate

Romans 9:11-12 The Lord said this to show that he makes his own choices and that it wasn't because of anything either of them had done.

Romans 9:14-16 Are we saying that God is unfair? Certainly not! The Lord told Moses that he has pity and mercy on anyone he wants to. Everything then depends on God's mercy and not on what people want or do.

Romans 9:18 Everything depends on what God decides to do, and he can either have pity on people or make them stubborn.

Romans 9:19-20 Someone may ask, "How can God blame us, if he makes us behave in the way he wants us to?" But, my friend, I ask, "Who do you think you are to question God? Does the clay have the right to ask the potter why he shaped it the way he did?

So, there seem to be fate and destiny, and they govern my life. I hope my fate is not to be stubborn, but to receive pity from above.