2010年11月27日 星期六

[Hom] Few exercises of homomorphism - sumcap

這裡我只想專注在CS-generalization上面。所以,為了強調我們不太需要知道sumcap細節上是什麼,這邊故意不去解釋相關的函數。有關steep、rcap和sumcap,請參考 Determining List Steepness in a Homomorphism

上一篇一樣,這裡也使用 T 表示 term,同時繼續使用那四種箭頭。


根據sumcap的定義(我們不考慮empty-list的情況):
sumcap [x] = ↔ Tb = (x,x)

For original definition of leftwards(foldr) and rightwards(foldl):
⟨sum, rcap⟩ = foldr step (0, ∞) where step x (s, y) = (x + s, (x - s) ↓ y)
⟨sum, rcap⟩ = foldl step (0, ∞) where step (s, y) x = (s + x, (y - x) ↓ x)
we can obtain Tc and Ts as follows:
Tc ↔ sumcap (x << xs) = (x + (fst.sumcap xs), (x - (fst.sumcap xs)) ↓ (snd.sumcap xs))
Ts ↔ sumcap (ys >> y) = ((fst.sumcap ys) + y, ((snd.sumcap ys) - y) ↓ y)

Rb is defined as follows:
Rb = { a ↓ a → a }

GO!!!
σ1 = {
X0 ↦ (x,x) ⊛ (sumcap xs) ⇀ (x + (fst.sumcap xs), (x - (fst.sumcap xs)) ↓ (snd.sumcap xs))
}
σ2 = {
X0 ↦ (sumcap ys) ⊛ (y, y) ⇀ ((fst.sumcap ys) + y, ((snd.sumcap xs) - y) ↓ y)
}
Tg = X0
decomposition: ⊛, ⇀, (,) and ↓
σ1 = {
X1 ↦ (x,x)
X2 ↦ (sumcap xs)
X3 ↦ x + (fst.sumcap xs)
X4 ↦ x - (fst.sumcap xs)
X5 ↦ snd.sumcap xs
}
σ2 = {
X1 ↦ (sumcap ys)
X2 ↦ (y,y)
X3 ↦ (fst.sumcap ys) + y
X4 ↦ (snd.sumcap xs) - y
X5 ↦ y
}
Tg = X1 ⊛ X2 ⇀ (X3, X4 ↓ X5)
decomposition (+)
這邊特別標記一下,我在這邊用了一個rule term,
sumcap as ↔ ((fst.sumcap) as, (snd.sumcap) as)
但是這個rule term其實並沒有出現在任何一個關於Rc,Rs,Rb甚至是R++的論述裡面。
充其量應該只能說這是從semantic equality來的..
這可能要找時間好好想想是怎回事 @@
σ1 = {
X1 ↦ (x,x)
X2 ↦ (sumcap xs)
↦ (fst.sumcap xs, snd.sumcap xs)
X4 ↦ x - (fst.sumcap xs)
X5 ↦ snd.sumcap xs
X6 ↦ x
X7 ↦ fst.sumcap xs
}
σ2 = {
X1 ↦ (sumcap ys)
↦ (fst.sumcap ys, snd.sumcap ys)
X2 ↦ (y,y)
X4 ↦ (snd.sumcap xs) - y
X5 ↦ y
X6 ↦ fst.sumcap ys
X7 ↦ y
}
Tg = X1 ⊛ X2 ⇀ (X6 + X7, X4 ↓ X5)
docomposition: (,) in left hand side
σ1 = {
X4 ↦ x - (fst.sumcap xs)
X5 ↦ snd.sumcap xs
X6 ↦ x
X7 ↦ fst.sumcap xs
X8 ↦ x
X9 ↦ x
X10 ↦ fst.sumcap xs
X11 ↦ snd.sumcap xs
}
σ2 = {
X4 ↦ (snd.sumcap ys) - y
X5 ↦ y
X6 ↦ fst.sumcap ys
X7 ↦ y
X8 ↦ fst.sumcap ys
X9 ↦ snd.sumcap ys
X10 ↦ y
X11 ↦ y
}
Tg = (X8,X9) ⊛ (X10,X11) ⇀ (X6 + X7, X4 ↓ X5)
decomposition: (-)
finding out agreements
σ1 = {
X5 ↦ snd.sumcap xs (agree with X11)
X6 ↦ x (agree with X8)
X7 ↦ fst.sumcap xs (agree with X10)
X8 ↦ x
X9 ↦ x
X10 ↦ fst.sumcap xs
X11 ↦ snd.sumcap xs
X12 ↦ x (agree with X9)
X13 ↦ fst.sumcap xs (agree with X10)
}
σ2 = {
X5 ↦ y (agree with X11)
X6 ↦ fst.sumcap ys (agree with X8)
X7 ↦ y (agree with X10)
X8 ↦ fst.sumcap ys
X9 ↦ snd.sumcap ys
X10 ↦ y
X11 ↦ y
X12 ↦ snd.sumcap ys (agree with X9)
X13 ↦ y (agree with X10)
}
Tg = (X8,X9) ⊛ (X10,X11) ⇀ (X6 + X7, (X12 - X13) ↓ X5)
agreement!
σ1 = {
X8 ↦ x
X9 ↦ x
X10 ↦ fst.sumcap xs
X11 ↦ snd.sumcap xs
}
σ2 = {
X8 ↦ fst.sumcap ys
X9 ↦ snd.sumcap ys
X10 ↦ y
X11 ↦ y
}
Tg = (X8,X9) ⊛ (X10,X11) ⇀ (X8 + X10, (X9 - X10) ↓ X11)
There is no any free variable!!
Tg = (X8,X9) ⊛ (X10,X11) ⇀ (X8 + X10, (X9 - X10) ↓ X11) .

2010年11月26日 星期五

[Hom] Few exercises of homomorphism - max and scanr

為了方便, 下面用 T 表示 term,
Tb = base term
Tc = cons term
Ts = snoc term
Tg = generator term
箭頭方面,
↦ : variable-term binding
↔ : conversion relation
→ : rewriting relation
⇀ : some "arrow" operator
(for example, ⇀ could be ↔ or →)
另外,也使用下面的一些 function
(<<) : cons
(>>) : snoc
(↑) : max
(↓) : min

example 01 - max

Step 1 - To find out the base, cons and snoc term
max [a] ↔ Tb = a
Tc = x ↑ (max xs) ↔ Tb ⊛ (max xs) = x ⊛ (max xs)
Ts = (max ys) ↑ y ↔ (max ys) ⊛ Tb' = (max ys) ⊛ y

Step 2 - To find out base rule term, Rb
這個case因為用不到,剛好免了。

Step 3 - Initializing substitutions and generator term
σ1 = {X0 ↦ x ↑ (max xs) ⇀ x ⊛ (max xs)}
σ2 = {X0 ↦ (max ys) ↑ y ⇀ (max ys) ⊛ y}
Tg = X0
其實這邊看也看得出來 ⊛ 就是 ↑
不過我還是套了三次 decomposition (for ↔, ⊛ and ↑)
還有兩次的 agreement (x=x且max ys=max ys; max xs = max xs且y=y)
最後就會得到 Tg = x3 ⊛ x4 ⇀ x3 ↑ x4 .

example 02 - scanr

For simply, there fix the higher-order function argument of scanr, i.e. scr = scanr ⊕, mp a xs = map (⊕ a) xs.

Step 1 - To find out the base, cons and snoc term

For given definitions
scr (x << xs) = (x ⊕ (head (scr xs))) << (scr xs)
scr (ys >> y) = (mp y (scr ys)) >> y
we can obtain following rule terms:
scr [a] ↔ Tb = [a]
[x] ⊛ (scr xs) ↔ Tc = [x ⊕ (head (scr xs))] ++ (scr xs)
(scr ys) ⊛ [y] ↔ Ts = (mp y (scr ys)) ++ [y]

Step 2 - To find out base rule term, Rb
Rb = {
mp a [b] → [a ⊕ b]
head [a] → a
}

Step 3 - Initializing substitutions and generator term, and, run!!
σ1 = {X0 ↦ [x] ⊛ (scr xs) ⇀ [x ⊕ (head (scr xs))] ++ (scr xs)}
σ2 = {X0 ↦ (scr ys) ⊛ [y] ⇀ (mp y (scr ys)) ++ [y]}
Tg = X0
decomposition: ⊛, ⇀ and (++)
σ1 = {
X1 ↦ [x]
X2 ↦ (scr xs)
X3 ↦ [x ⊕ (head (scr xs))]
X4 ↦ (scr xs)
}
σ2 = {
X1 ↦ (scr ys)
X2 ↦ [y]
X3 ↦ (mp y (scr ys))
X4 ↦ [y]
}
Tg = X1 ⊛ X2 ⇀ X3 ++ X4
agreement: X4-X2
σ1 = {
X1 ↦ [x]
X2 ↦ (scr xs)
X3 ↦ [x ⊕ (head (scr xs))]
}
σ2 = {
X1 ↦ (scr ys)
X2 ↦ [y]
X3 ↦ (mp y (scr ys))
}
Tg = X1 ⊛ X2 ⇀ X3 ++ X2
decomposition: mp
σ1 = {
X1 ↦ [x]
X2 ↦ (scr xs)
X3 ↦ [x ⊕ (head (scr xs))] ↦ mp (head (scr xs)) [x] {by "mp a [b] → [a ⊕ b]"}
X4 ↦ head (scr xs)
X5 ↦ [x]
}
σ2 = {
X1 ↦ (scr ys)
X2 ↦ [y]
X3 ↦ (mp y (scr ys))
X4 ↦ y
X5 ↦ (scr ys)
}
Tg = X1 ⊛ X2 ⇀ (mp X4 X5) ++ X2
agreement: X5-X1
σ1 = {
X1 ↦ [x]
X2 ↦ (scr xs)
X4 ↦ head (scr xs)
}
σ2 = {
X1 ↦ (scr ys)
X2 ↦ [y]
X4 ↦ y ↦ head [y]
}
Tg = X1 ⊛ X2 ⇀ (mp X4 X1) ++ X2
decomposition: head
σ1 = {
X1 ↦ [x]
X2 ↦ (scr xs)
X4 ↦ (scr xs)
}
σ2 = {
X1 ↦ (scr ys)
X2 ↦ [y]
X4 ↦ [y]
}
Tg = X1 ⊛ X2 ⇀ (mp (head X4) X1) ++ X2
agreement: X4-X2
σ1 = {
X1 ↦ [x]
X2 ↦ (scr xs)
}
σ2 = {
X1 ↦ (scr ys)
X2 ↦ [y]
}
Tg = X1 ⊛ X2 ⇀ (mp (head X2) X1) ++ X2
There is no any free variable!!
Tg = X1 ⊛ X2 ⇀ (mp (head X2) X1) ++ X2 .

這兩個例子中,做 CS-generalization 的過程確實相當的機械化。任何需要的 term rewriting 都是直接套用Rb裡面的就好。
另外,過程中我發現幾個hint:
  1. 一旦產生出 ⇀ 之後,一概都以右手邊的free variable優先處理
  2. 需要term rewriting時,都是在Rb裡面找到一個合適的rule term。並且,很多時候都是先從rule term的右手邊去match一個合適的。
  3. 做decomposition的時候不要太貪心,有時候會因為一次decompose太多root function,而使的某一個free varable錯過一個好的binding時機。
  4. term rewriting會很直接的影響到agreement。例如說scanr的例子,[x ⊕ (head (scr xs))]要寫成mp (⊕ x) [head (scr xs)]還是mp (⊕ (head (scr xs))) [x],這會對後面造成很大的影響。而且這又取決於⊕是不是commutative之類的前提/假設。另外,在sumcap的例子裡面(請參考下一篇),對(+)做decomposition的過程中,要不要把 a+b 換成 b+a 也會是個需要都嘗試看看的部份。因為和mp的情況一樣,有可能需要交換一下順序才做的下去,也有可能不交換才可以,當然也可能不管怎樣都可以做下去。所以,我想這裡是個比較難說「不需要人的輔助」的部份。

2010年11月22日 星期一

M$ Font Sucks!!

前天吧,因故灌了M$ Office。Office本身難用與否就不提了,我倒是發現電腦裡面某些地方看起來怪怪的。直到今天突然在看新聞才發現問題是什麼。原來是字體啦,我的字體之前沒那麼難看呀,怎麼變成讓人看起來不太舒服的字體呢?隨手擷個圖:
真是難看死了,後來想說該不會是被M$偷灌了什麼字體進來所以被弄成難看的樣子。所以去 /Library/Fonts下面看一下。果然,多了一個什麼Microsoft的folder。砍掉以後就恢復到原本的樣子惹。

2010年11月17日 星期三

[筆記]Haskell X Fractal

想做Fractal
但是仔細想想
Parallel Haskell不柔
Fractal沒真的仔細看過
GUI(現在只有wxHaskell可以選)只跑過demo
慘絕人寰之餘
紀錄一下還沒時間看的網頁吧

http://haskell.org/haskellwiki/WxHaskell/Quickstart#Painting

http://gregheartsfield.com/fractal-hs/

http://hackage.haskell.org/packages/archive/haskore/0.1.0.4/doc/html/src/Haskore-Example-Fractal.html

http://boson4.phys.tku.edu.tw/high_school/unit_fractals.htm

http://classes.yale.edu/Fractals/MandelSet/MandelDef/Definition/Definition.html

http://en.wikipedia.org/wiki/Mandelbrot_set

2010年11月1日 星期一

[Note] The GUI Issue

gtk+,之前在Mac OSX上面跑太多次有問題,已經有點怕了說,不過這次用brew灌就可以用了!不過compile的時候要給的參數好多,照說應該link和load的路徑都不需要再給,改下次有空再來試好了。



裝qt,其實nokia往站上有binary可以裝,可是跑起來覺得怪彆扭的。重點是這樣就打壞了原本弄好的lib系統。所以還是看看homebrew有沒有,果然有,那就灌吧!
> brew install qt
然後,等超過一個半小時了還沒好,所以下次再寫qt筆記吧 XD



這兩天在弄gtk+,莫名其妙的弄半天以後發現,brew灌起來的gtk+應該是可以用。雖說會噴一個奇怪的error出來,但是至少有東西了(那個error好像和顯示裝置的驅動還是設定有關)。gtk可以跑,我當然一如往常的想去試一下多年的夢想「gtk2hs」!他官網晃一圈以後就想起一個慘忍的事實:gtk2hs在ghc 6.12上面有bug。Orz

好吧,只好忍痛和gtk2hs說掰掰。不甘心之餘又回頭去看看現在有什麼其他的GUI package可以用用看。東看西看,還是只有gtk2hs和wxHaskell比較大宗。Orz

無奈之下,試試看wxHaskell吧:
在開始裝wxhaskell之前,要先把mac os x內建的wxWidgets換掉:
> brew install wxmac
如果沒有做這一步,之後執行macosx-app出來的.app會不能執行(mac會該說這個東西格式不對)。

wxmac要build好一會兒(10~20分鐘左右)。然後就可以用cabal來install正主兒:
> cabal install wxcore
build的時候,有滿坑滿谷的cpp deprecated warning Orz。為保安全,紀錄下g++的version:
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
Copyright (C) 2007 Free Software Foundation, Inc.
然後 > cabal install wx
我也不知道我幹麼分兩段裝,一定是白爛病發作了XD。裝好以後確定一下東西有進去ghc-pkg。
> ghc-pkg list
/usr/local/Cellar/ghc/6.12.3/lib/ghc/package.conf.d
..[略]..
/Users/Jaiyalas/.ghc/i386-darwin-6.12.3/package.conf.d
wx-0.12.1.6
wxcore-0.12.1.6
wxdirect-0.12.1.3

接下來才是問題所在,gui的東西似乎在mac上面都特別刁鑽。wx同樣也不是可以讓你乖乖跑一下就有結果可以開開心心用的。用ghc compile以後直接執行他會噴error如下,並且會跑出一個只能看不能用的視窗。
10:46:15: Debug: wxColour::Set - couldn't set to colour string 'GREY'
[Debug] 10:46:15: Adding duplicate image handler for 'PNG file'
[Debug] 10:46:15: Adding duplicate image handler for 'JPEG file'
[Debug] 10:46:15: Adding duplicate image handler for 'TIFF file'
[Debug] 10:46:15: Adding duplicate image handler for 'GIF file'
[Debug] 10:46:15: Adding duplicate image handler for 'PNM file'
[Debug] 10:46:15: Adding duplicate image handler for 'PCX file'
[Debug] 10:46:15: Adding duplicate image handler for 'Windows icon file'
[Debug] 10:46:15: Adding duplicate image handler for 'Windows cursor file'
[Debug] 10:46:15: Adding duplicate image handler for 'Windows animated cursor file'
[Debug] 10:46:15: Adding duplicate image handler for 'TGA file'
[Debug] 10:46:15: Adding duplicate image handler for 'XPM file'

解決的辦法是把原本的executable file包成.app再去打開它。不過這之前就需要macosx-app來作這件事。細節可以參考stackoverflow上的說明。這邊作個簡單的描述:
抓下macosx-app的模板順便改名字和操作模式
> wget http://code.haskell.org/wxhaskell/bin/macosx-app-template
> mv macosx-app-template macosx-app
> chmod a+x macosx-app
然後開個順手的editor把下面這段加到macosx-app的最上面:
#!/bin/sh

libdir=""

wxrezcomp="`wx-config --rezflags`"
wxrezfile=""
if test "$wxrezcomp"; then
for word in $wxrezcomp; do
temp="`echo $word | grep '[^_]*_mac-[^r]*r'`"
if test "$temp"; then
wxrezfile="$temp"
fi
done
fi

if test "$wxrezfile"; then
wxrezdir="`echo $wxrezfile | sed -e 's|\(.*\)/libwx_mac.*|\1|'`"
wxinstallrezcomp="`echo \"${wxrezcomp}\" | sed -e \"s|${wxrezdir}|${libdir}|g\"`"
wxinstallrezfile="`echo \"${wxrezfile}\" | sed -e \"s|${wxrezdir}|${libdir}|g\"`"
fi

rezcomp="$wxinstallrezcomp"
rezfile="$wxinstallrezfile"

直接執行macosx-app的話會看到他噴出一些warning
> ./macosx-app
Warning: --rezflags, along with Mac OS classic resource building is deprecated. You should remove this from your Makefile and build .app bundles instead.
error: you need to specify a program. Use "--help" to show valid options.
這邊可以不用理他(其實是我也不知道怎要怎麼解決,反正可以用 Orz)

以上都準備好以後就可以直接
> ghc -package wx -o helloworld HelloWorld.hs
> macosx-app -v helloworld
> open helloworld.app
感人的小視窗就會跳出來惹 OvQ