Domain Specific Language (æãã¯ã³ã³ããã¼ã¿è¨èª)
ã¨ããããã§ãååã®ç¶ãã§ããã
ç¶ãã§ãããã¨ã¯ãã£ã¦ããååã®ãããªãããããªã話ã§ã¯ãªãã
(â¦ããããªããããªãã ããã ãã ã§ä¸éå端ãªè©±ã«â¦)ã
åçªã ããä½ããå®ç¾ããã®ã«å°è¦æ¨¡ãªè¨èªãå®è£
ããã®ã
é©å½ã ã¨å¤æãããå ´åããããªã解決æ³ãåãã®ãè¯ãã ãããã
ä¾ãã°ãã²ã¼ã ã®ã·ããªãªã ã£ãããã¾ãä¾ãã°ã
ä»åã®ICFPã®ããã«è»ã³ã¼ããçæãããããªå ´åã ã£ããã
ç§ãè¡ã£ãã®ã¯ããããããã«ãæ®éã®ããæ¹ã ã¨æã£ã¦ããã®ã ãã
å°è¦æ¨¡ãªã¹ã¯ãªããè¨èªã³ã³ãã¤ã©ãå®è£
ããã¨ããææ³ã§ãã£ãã
è¦ããã«ããã°ã©ã ã¨ãã¦ã¯ãStringâStringãªãé¢æ°ã§ããã
ãããæ®éã«åå¥è§£æãæ§æ解æãâ¦ã¨ãã£ã¦é©å½ã«ä½ã£ãã
ããLispçéã§ã¯ããç¨ãããã¦ãã(?)ãã£ã¨å¥ãªè§£æ±ºæ³ãæãã
ããã表é¡ã«æ¸ããDSLã¨å¼ã°ããææ³ãªã®ã§ãããã
(Domain Specific Languageã¯ãã¡ã¤ã³ç¹åè¨èªâ¦ãã®ãã®ã®æå³ã§ã¯
å¥æ®µå°è¦æ¨¡ã¹ã¯ãªããè¨èªã¨ãã¾ãéãããªãã®ã ãã
ããã§ã¯è¨èªå
ã«ãã®ãããªè¨èªæ§ã®ç©ãæ§æãããã¨ãèãã)
è»è¨èªçæã«é¢ãã¦å
·ä½çã«è¿°ã¹ãã¨ãåºæ¬çã«ç®çã®è»è¨èªãçæãã
ããã°ã©ã ãæ¸ãããã®éã«æ¸ãããããªããããªã©ã¤ãã©ãªãä½ãã
ã¨ãããã¨ã«ãªãã®ã ãããã
(Haskellã§ãHtmlã®ã©ã¤ãã©ãªã¯ãã®ãããªæ¹æ³ã«ãã£ã¦ããã)
ã¾ãããã£ã¨ãããªããã£ããªãã®ãèããã
ã¿ã¼ã²ããè¨èªã¯ã¾ããä¸å¿è»ã¢ã»ã³ããªã¨ãããã¨ã«ãã¦ããã
å½ä»¤ã®è©³ç´°ã¯ICFPã®ãã¼ã¸ãè¦ã¦ããã ããããªããã
(http://www.cis.upenn.edu/proj/plclub/contest/ants.html ããã®2.8ç¯ããª)
ã¾ããç¥ããªãã¦ã大ä½ã¯åããã®ã§ã¯ãªããã¨ã
ç§èªèº«ããã£ã±ãè¦ãã¦ããªãã®ã§ã
Sense LeftAhead 518 510 Home Turn Left 57 Move 512 518 PickUp 826 518 Sense RightAhead 514 518 Food
âã¯ããã¡ã®è»ããé©å½ã«å¼ã£ãæãã¦ããããã°ã©ã ã§ããã
ä¾ãã°ããã®ãããª"ããã°ã©ã "ãçæããããã°ã©ã ã¯ã
main = mapM_ putStrLn ["Sense LeftAhead 518 510 Home" ,"Turn Left 57" ,"Move 512 518" ,"PickUp 826 518" ,"Sense RightAhead 514 518 Food"]
ãã®ããã«æ¸ããã
ãªãããããã¯â¦?ã¨æããããããããªããã
Perlã§CGIã¨ãæ¸ãããä¼¼ããããªã³ã¼ãã«ãªãã®ã§ã¯ãªããããã
ãã®ãããªããã°ã©ã ã®çæãã¨ãã¨ãã¾ã§ã«è£ä½ãããã®ã
ä½æãã¤ã¤ããã°ã©ã çæããã°ã©ã ãæ¸ãã®ãã
ãã®ææ³ã®ä¸»ãªã¢ã¤ãã¢ã§ããã
ã¾ããä¸ã®ã³ã¼ãã®ã©ããã¾ããã®ãã
ã¾ããæããã«ä¸ã®ãããªã³ã¼ãã¯æ¸ãæå³ããç¡ããããªã³ã¼ããªã®ã§ãããã
ããã¯ããã¦é ã追ã£ã¦èãã¦ãããã
ã¾ãä¸ã¤ç®ã«ã表示ãã¦ããã³ã¼ããééã£ã¦ãã¦ããããç¥ãæ段ãç¡ããã¨ã
ã¤ã¾ãã
main = putStrLn "Tuen Loft 57"
ã¨ãã«ãªã£ã¦ãã¾ãã®ãã³ã³ãã¤ã«æã«ãã§ãã¯ã§ããªãã¨ãããã¨ã
ããã§ä¸ã¤æ½è±¡åã®åº¦åããä¸ããã
ãã®æç¹ã§"è»ããã°ã©ã "ã¨ãããã®ã¯åãªã"æåå"ã«ããã¦ããã
åãªãæååã§ã¯ãªãã¦"è»ããã°ã©ã "ã¨ãããã®ãå®ç¾©ãããããç¨ãããã¨ã«ããã
data Mnemonic = Sense Direction Int Int Condition | Mark Int Int | Unmark Int Int | PickUp Int Int | Drop Int | Turn Direction Int | Move Int Int | Flip Int Int Int data Direction = ... ... type Program = [Mnemonic]
ãã¦ãä»åº¦ã¯ããã°ã©ã ãåã«æååã§ã¯ãªãã®ã§ã
表示ããéã«æååã«å¤æããå¿
è¦ãããã
compile :: Program -> String
ãªãé¢æ°ãä½æããã°è¯ãã ããã
ã¡ãªã¿ã«ããã®ç¨åº¦ã®ãã¼ã¿æ§é ã§ããã°
deriving(Show)ãã¦ãcompile = concat . intersperse "\n" . map show
ã§ã¡ããã¨è¡¨ç¤ºã§ãã¦ãã¾ããããªæ°ãããªãã§ããªãâ¦ã
ããã§å®ç¾©ãããã®ãç¨ããã¨å
ã®ä¾ã¯
main = putStrLn $ compile progn progn = [ Sense LeftAhead 518 510 , Turn Left 57 , Move 512 518 , PickUp 826 518 ]
çã¨åºæ¥ããããã«ã¦ãã¹ã¿ã¤ããæ¤åºã§ãããã
ããããå°ãã ãã¾ã¨ãã«ãªã£ã¦ããã
次ã«ãå¥æ¹åã«ãããæ¡å¼µããã
ä»åãç´æ¸ããè¯ããªãæ大ã®çç±ã¯èªåã§è¡çªå·ã
æ¸ããªããã°ãªããªããã¨ã ã£ãããã«æãã
â¦ã®ã§ãä¸ããã°ã©ã ä¸ã518ã¨ãã510ã¨ãã
ã©ãã§ãããæ°åãèªåã§ç®¡çããã®ã¯ãããªã®ã§ããã
(ããã¨ããããã»ã¨ãã©ä¸å¯è½ã ããâ¦)
ããã§ãä»åã«ãªã¹ãã«ãªã£ã¦ããããã°ã©ã ã«æ§é ãä¸ãã¦
ãã®è¾ºã®ç®¡çãèªååããããã¨ã«ããã
è»ã¢ã»ã³ããªãå度è¦ã¦ã¿ãã¨ã
åå²ããå½ä»¤ ï¼Sense PickUp Move Flip
åå²ããªãå½ä»¤ï¼Mark Unmark Drop Turn
åå²ãããã®ã¨åå²ããªããã®ããããã¨ãåããã
ãã®ãã¡åå²ããªããã®ã¯ãã¸ã£ã³ãã¢ãã¬ã¹ã«
常ã«æ¬¡ã®å½ä»¤ã®ã¢ãã¬ã¹ã§ãããã¦ããã°ããã
åå²ããå½ä»¤ããçæ¹ã¯ãããã¦ããã°ããã
ããããã¨ãåå²ããå½ä»¤ã®çæ¹ããªãã¨ãæå®ã§ããã°ã
ãã以å¤ã®ã¢ãã¬ã¹ã¯çµæ§èªæã«ç®¡çã§ããã¨ãããã¨ã«ãªãã
data Fragment = (ã³ã¼ãç) mark :: Int -> Fragment unmark :: Int -> Fragment drop :: Fragment -- Preludeã¨ååã被ã⦠turn :: LeftOrRight -> Fragment -- 失ææã®åå²å ã渡ãã sense :: Direction -> Condition -> Fragment -> Fragment pickup :: Fragment -> Fragment move :: Fragment -> Fragment flip :: Int -> Fragment -> Fragment -- ãããååã被ã⦠makeProgram :: Fragment -> Program compile :: Program -> String
ããã°ã©ã ã®ä¸é¨ãã³ã¼ãçã¨ãã¦è¡¨ç¾ãã
ããããçµã¿ç«ã¦ã¦ããã°ã©ã ãä½ã£ã¦ããã
-- é次å®è¡ seq :: Fragment -> Fragment -> Fragment
ãã®ãããªãã®ãä½ãã¨
clearAndSet a b = unmark a `seq` mark b
é©å½ã«ã¤ãªããããããã«ãªã£ã¦çµã¿åããã®å¹
ãåºããã
ããã«ãFragmentã«é©å½ã«æååãè¨å®ãã¦ã©ãã«ä»ãã
åºæ¥ãããã«ãªãã¨ãããããããªãã
label :: String -> Fragment -> Fragment goto :: String -> Fragment
å
¨ãå®è£
ã®ç¡ãã³ã¼ããç¶ãã¦ãã¾ã£ããâ¦
次ã®ãããªä½¿ãæ¹ããããã®ã¨èãã¦ããã
(ã¡ãªã¿ã«ãä¸è¨ã®ã³ã¼ãã¯é©å½ã«ã§ã£ã¡ä¸ããã®ã§ã
ã©ãå®è£
ãã¦ããã®ãåãããªã)
main = putStr $ compile start start = forever nop forever f = label "forever" $ f (goto "forever") nop next = flip 1 next
ä½ãããªãè»â¦ã®ã¤ããã
ãªãããããç¬ãããããªã³ã¼ãã«ãªã£ã¦ãã¾ã£ã¦ãããããª?æ°ãããã
Fragmentã®å®è£
ãã©ãããã®ãâ¦?ã¨ããéè¦ãªåé¡ãã²ã¨ã¾ãæ¾ç½®ãã¦ãããã
åèã«ããã¦ããã£ã 'Team Dunkosmiloolump'
(http://urchin.earth.li/icfpcontest/2004/Introduction)
ã®ã³ã¼ããã¾ããããã®ã¢ããã¼ããªã®ã§ãç§ã®ããã¼ãå®è£
ããã¯
ãããåç
§ãã¦ãããã»ããé¥ãã«è¯ãã¨æãã
(ã©ã¤ãã©ãªé¨åã¯é£ããã¦ããåãããªãã£ããã ãã©â¦)
http://urchin.earth.li/icfpcontest/2004/sub/tools/compiler/Test.hs
æçµçã«ã¯ããã®ããã«è»ããã°ã©ã ã表ç¾ã§ããããã§ããã
ã¨ã説æã¯ãã®è¾ºã«ãã¦ããã¦ã
ã³ã³ãã¤ã©ãå®è£
ããã®ã§ã¯ãªãã¦è¨èªå
ã«DSLãä½ãã¡ãªããã¯ä½ã ãããã
ããã¯ãããããç¨ããè¨èªã®ãã¯ã¼ããããããã¨ãªã
ç®çã®ããã°ã©ã ã®è¨è¿°ã«ä½¿ããã¨ãã§ãããã¨ãããã¨ã ããã
ç§ã®ä½ã£ãã¹ã¯ãªããè¨èªã§ãè¦ã¦ãããã°åããã§ãããã
åã«ç¹°ãè¿ãã¨ãã§ãããããããå®è£
ããªãéã使ããªãã®ã§ã
ä»åã®ããã«3æ¥éå®çã¨ããã³ã³ãã¹ãã§ã¯Haskellãªã¿ã«å¼·åãª
ã³ã³ãã¤ã©ãåºæ¥ä¸ãããããç¡ãã
"the language of choice for discriminating hackers!"
ã§ããHaskellãè»ããã°ã©ã ã®è¨è¿°ã«ã使ããã¨ãããã¨ã¯
ãã®æç¹ã§ããªãæå©ã§ããã¨ãããã®ã ããã
åé¢ããã¡ãªããã¯ã¨ããã¨ã
ã³ã¼ãã®çæã«Haskellå¦çç³»ãå¿
è¦ãªãã¨ããããã§ããããã
ããã¯ããããã²ã¼ã ã®ã·ããªãªã®è¨è¿°ã«ãã®ææ³ã使ã£ã¦ãã¾ã£ããããå ´åã
ãã®å®è¡ã«Haskellå¦çç³»ãå¿
è¦ã«ãªã£ã¦ãã¾ããããªããããããªãã
ã¾ããä»åã¯Haskellã§çµå§éçºãè¡ããã®ãªã®ã§ããªããåé¡ã¯ç¡ãã ããã
ãããã¾ããå
¬éããã¦ãã½ã¼ã¹ã¨ããè¦ã¦ããã¨ã
DSLçãªææ³ãç¨ãã¦ãããã¼ã ãçµæ§ãããããªæãã ã
ãããªãã¨ã«ä»æ´é©ããªããã¨ããæ¹ããICFPã«åå ããã®ã¯
Haskellãæ¬æ ¼çã«åå¼·ãããã¨æã£ã¦ããæ¥ãç«ããªãããã ã£ãã®ã§ã
温ããè¦å®ã£ã¦ããã ããã¨å¹¸ãã ã£ããããâ¦ã
ã¨ãããããä»å¾ãããããã®ãæ¸ããªããã°ãªããªã
ã±ã¼ã¹ãåºã¦ããã¨ãã«ä½¿ã£ã¦ã¿ãããã¨ã