RubyããHaskellãå¼ã³åºãããã¦Hubrisãã¤ã³ã¹ãã¼ã«ãã
Hubrisã¨ãããRubyããHaskellã®ã³ã¼ããå¼ã¶ããã®ããªãã¸ãããã¾ããHaskellパッケージã¨Rubyライブラリã§æ§æããã¦ãããç¾å¨ã®ãã¼ã¸ã§ã³ã¯0.0.3ã§ãã
ã¨ããçç±ããããã®Hubrisã§éãã§ã¿ããã¨æã£ãã®ã§ãããHaskellããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«ã«ããªãæããã£ãããããã¤ã³ããã¾ã¨ãã¦ããã¾ããã¡ãªã¿ã«Rubyã©ã¤ãã©ãªã®ã»ãã¯ãgem install hubrisãã§ç°¡åã«å ¥ãã¾ããã
ä»åã¾ã¨ããå 容ã¯ãããã¾ã§åã®ç°å¢ï¼CentOS 5ï¼ã§ãã¾ããã£ããã®ã§ãä»ã®ç°å¢ã§ã¯ã©ãã ãåããã¾ããã誤ã£ãè¨è¿°ãããããããã¾ããã®ã§ããæ°ã¥ãã®éã¯ãã²ãç¥ãããã ããã
Hubrisã§éã³ãããªã£ãçç±ã«ã¤ãã¦ã¯ãå¾æ¥æ¸ãã¤ããã§ãã
ãã£ããããæµã
$ wget http://hackage.haskell.org/packages/archive/hubris/0.0.3/hubris-0.0.3.tar.gz $ tar xvzf hubris-0.0.3.tar.gz $ cd hubris-0.0.3 $ runhaskell Setup.hs configure ï¼ãã㧠dist/build/autogen/Includes.hs ãç·¨éï¼ $ cabal install --enable-shared \ > --extra-include-dirs=/home/iwamot/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/i686-linux \ > --extra-lib-dirs=/home/iwamot/.rvm/rubies/ruby-1.9.1-p378/lib \ > --extra-lib-dirs=/home/iwamot/.rvm/rubies/ruby-1.8.7-p302/lib
ãrunhaskell Setup.hs configureãããã¨ããã«ãã«å¿ è¦ãª dist/build/autogen/Includes.hs ãçæããã¾ãããã ããçæãããå 容ã®ã¾ã¾ã§ãã«ãããã¨ãHubrisãæ£å¸¸åä½ããªããããæãå ããå¿ è¦ãããã¾ãã
cabal installã®ãªãã·ã§ã³ã§ã¯ãRuby 1.8ã®ããããã¡ã¤ã«æ ¼ç´å ã¨ãRuby 1.8ããã³1.9ã®å ±æã©ã¤ãã©ãªã®æ ¼ç´å ã渡ãã¦ãã¾ãã
dist/build/autogen/Includes.hs ã®ç·¨é
åã®ç°å¢*1ã§ã¯ãçæããã dist/build/autogen/Includes.hs ã®å 容ã¯ä¸è¨ã®éãã§ããã
module Includes where extraIncludeDirs=["/home/iwamot/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/i686-linux"]
ãã®ã¾ã¾ã§ããã«ãã¯ã§ããã®ã§ãããextraIncludeDirsã§æå®ããããã£ã¬ã¯ããªã«ããããã¡ã¤ã«ãæ ¼ç´ããã¦ããªããããHubrisã®å®è¡æã«ã¨ã©ã¼ãçºçãã¦ãã¾ãã¾ãã
ã¨ã©ã¼å 容ãåèã«ãä¸è¨ã®ããã«ç·¨éããã¨ããã¾ãåãããã«ãªãã¾ããã
module Includes where extraIncludeDirs=["/home/iwamot/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1", "/home/iwamot/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/i686-linux"]
cabal installã®ãªãã·ã§ã³
cabal installé¨åãåæ²ãã¾ãã
$ cabal install --enable-shared \ > --extra-include-dirs=/home/iwamot/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/i686-linux \ > --extra-lib-dirs=/home/iwamot/.rvm/rubies/ruby-1.9.1-p378/lib \ > --extra-lib-dirs=/home/iwamot/.rvm/rubies/ruby-1.8.7-p302/lib
ã--extra-include-dirsãã¯åé¡ãªãã§ããããRuby 1.8ã®ããããã¡ã¤ã«æ ¼ç´å ã渡ãã¾ãã
åé¡ã¯ã--extra-lib-dirsãã§ãä¸è¨ã®ããã«æå®ããã¨ãä¸è¨ã©ã¤ãã©ãªãã¡ã¤ã«ã®åç §ãã¤ã³ã¹ãã¼ã©ã«æ示ãããã¨ã«ãªãã¾ãã
- /home/iwamot/.rvm/rubies/ruby-1.9.1-p378/lib/libruby.so
- /home/iwamot/.rvm/rubies/ruby-1.8.7-p302/lib/libruby1.8.so
注æãã¹ããã¤ã³ãã¯2ç¹ã§ãã
ã¾ã£ãããªGHCç°å¢ããå§ã
Hubrisã®Haskellããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããã¨ãä¸è¨ã®ä¾åããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããã¾ãã
ãããããã--enable-sharedããªãã§ã¤ã³ã¹ãã¼ã«ããã¦ããå ´åãHubrisã®Haskellããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«ã«å¤±æãã¾ããåé¿ããã«ã¯ãäºåã«åé¤ï¼cabal unregisterï¼ãã¦ããããã--enable-sharedãä»ãã§åã¤ã³ã¹ãã¼ã«ï¼cabal install --reinstall --enable-sharedï¼ãã¦ãããªããã°ãªãã¾ããã
åã¯å½åãHaskell Platformã使ã£ã¦ããã®ã§ãããä¾åé¢ä¿ã®ç¸ãã§ãåé¤ãåã¤ã³ã¹ãã¼ã«ãã§ãã¾ããã§ãããããã§Haskell Platformãã¢ã³ã¤ã³ã¹ãã¼ã«ããGHC 6.12.3ãå ¥ãç´ã*2ãã¤ã³ã¹ãã¼ã«æ¸ã¿ã®ããã±ã¼ã¸ãå ¨åé¤ãã¾ããã
Hubrisãå®è¡ããåã«
ã¤ã³ã¹ãã¼ã«ãçµããã°Hubrisãå®è¡ã§ããã®ã§ãããäºåã«ç¢ºèªãã¦ãããªããã°ãªããªããã¨ãããã¾ãã
å ±æã©ã¤ãã©ãªã®ãã¼ã
ã¾ããRuby 1.8ã®å ±æã©ã¤ãã©ãªããã¼ããã¦ãããªããã°ãªãã¾ããã
$ echo /home/iwamot/.rvm/rubies/ruby-1.8.7-p302/lib > /etc/ld.so.conf.d/hubris.conf $ sudo /sbin/ldconfig
Hubriså®è¡æã«æ¤ç´¢ãããã©ã¤ãã©ãªãã¡ã¤ã«åã¯ãlibruby.so.1.8ãã§ããcabal installæã®ãlibruby1.8.soãã¨ã¯ç°ãªãããã注æãå¿ è¦ã§ãã
ãã£ã¬ã¯ããªã®ã¢ã¯ã»ã¹æ¨©ç¢ºèª
ã¾ããHubrisãå®è¡ããã¨ãã/var/hubris/cacheãã/var/hubris/sourceããã£ã¬ã¯ããªãä½æããããã£ãã·ã¥ãã¡ã¤ã«ãªã©ãé ç½®ããã¦ããã¾ããå¿ è¦ã«å¿ãã¦ããã£ã¬ã¯ããªã¸ã®ã¢ã¯ã»ã¹æ¨©ãè¨å®ãã¦ãããªããã°ãªãã¾ãããåã¯ä¸è¨ã®ããã«ã/var/hubrisããä½ãã¾ããã
$ sudo mkdir /var/hubris $ sudo chown iwamot /var/hubris
ãµã³ãã«ã®å®è¡
ã¤ã³ã¹ãã¼ã«ã¡ã¢ã¯ä»¥ä¸ã§çµããã§ããããã£ãããªã®ã§ãµã³ãã«ãåããã¦ã¿ã¾ãããã
/home/iwamot/.rvm/gems/ruby-1.9.1-p378/gems/hubris-0.0.3/sample ã«ãµã³ãã«ã¹ã¯ãªãããé ç½®ããã¦ãã¾ããå®ã¯ããã®ãµã³ãã«ãä¸é¨ä¿®æ£ãå¿ è¦ã§ããã
config.ruï¼ä¸é¨ä¿®æ£ï¼
use Rack::Reloader, 0 use Rack::ContentLength require 'pp' require 'hubris' class Fibonacci hubris :source => 'Fibonacci.hs' end def arg_from env # 岩æ¬ä¿®æ£ãããã #env['REQUEST_URI'] ? env['REQUEST_URI'].to_s.sub(/^\//, '').to_i : 0 env['PATH_INFO'] ? env['PATH_INFO'].to_s.sub(/^\//, '').to_i : 0 # ããã¾ã§ end app = proc do |env| value = Fibonacci.new.fibonacci( arg_from env ) [ 200, {'Content-Type' => 'text/plain'}, "The fib number is #{value }" ] end run app
Fibonacci.hsï¼ä¿®æ£ãªããã³ã¡ã³ãè¡ã¯çç¥ï¼
module Fibonacci where import Foreign.C.Types import Maybe fibonacci :: Int -> Int fibonacci n = fibs !! n where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
å®è¡
$ cd /home/iwamot/.rvm/gems/ruby-1.9.1-p378/gems/hubris-0.0.3/sample $ rackup config.ru
http://localhost:8765/10 ã«ã¢ã¯ã»ã¹ããã¨ãç¡äºãThe fib number is 55ãã¨è¡¨ç¤ºããã¾ããã
ä»å¾ã®äºå®
åé ã§æ¸ããã¨ãããHubrisã§éã³ãããªã£ãçç±ã次å以éã®æ¥è¨ã«æ¸ãäºå®ã§ãã
ã¾ããå½ç¶ãªãããèªä½ã®Haskellã³ã¼ããRubyããå¼ã³åºãã¦ã¿ããã¨æã£ã¦ãã¾ãããhttp://www.engineyard.com/blog/2010/a-hint-of-hubris/ããåèã«ãªãããã§ãã
*1:RVMã§Ruby 1.9.1ãã¤ã³ã¹ãã¼ã«ãã¦ãã¾ã
*2:ã¤ã³ã¹ãã¼ã«æé ã¯ãHaskell初心者の僕が試したことをまとめてみたãã«æ¸ããã¨ããã§ã