Check-in [88827b8336]
Not logged in
Overview
SHA1 Hash:88827b833669fcab08ae1f8d663bb2a067245ea2
Date: 2010-12-02 19:30:24
User: kinaba
Comment:sample clean-up
Timelines: family | ancestors | descendants | both | trunk | release
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified doc/index.html from [a25f52da129efe47] to [8335b49cb250b861].

431 431 <pre> 432 432 &gt;&gt; @value( 1 + 2 ) 433 433 3 434 434 </pre> 435 435 他のレイヤで動かしてみましょう。適当に。「<tt>@hoge</tt> レイヤ」で。 436 436 <pre> 437 437 &gt;&gt; @hoge( 3 ) 438 - polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(138): 438 + polemy.failure.RuntimeException@polemy\eval.d(138): 439 439 [<REPL>:4:8] lift function for @hoge is not registered 440 440 </pre> 441 441 <p> 442 442 エラーになりました。Polemy のインタプリタは、起動時には、<tt>@value</tt> 443 443 レイヤでのコードの意味しか知りません。<tt>@hoge</tt> レイヤでは <tt>3</tt> 444 444 というのがどんな意味なのか、わかりません!というエラーが出ています。 445 445 </p> ................................................................................ 463 463 6 464 464 </pre> 465 465 <p> 466 466 では、1+2 を <tt>@hoge</tt> レイヤで動かしてみましょう。 467 467 </p> 468 468 <pre> 469 469 &gt;&gt; @hoge( 1 + 2 ) 470 - polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(466): 470 + polemy.failure.RuntimeException@polemy\eval.d(466): 471 471 [<REPL>:3:7] only @value layer can call native function: + 472 472 [<REPL>:3:7] + 473 473 </pre> 474 474 <p> 475 475 まだエラーですね。これは要するに "+" の意味がわからない、と言っています。 476 476 <font color=red><b>レイヤ指定変数定義式</b></font> で、"+" の意味を教えてあげます。 477 477 </p> 478 478 <pre> 479 - &gt;&gt; @hoge "+" = fun(x, y) {x} 479 + &gt;&gt; @hoge + = fun(x, y) {x} 480 480 (function:182eca0:18435e0) 481 481 &gt;&gt; @hoge( 3 + 4 ) 482 482 6 483 483 </pre> 484 484 <p> 485 485 できました。 486 486 </p> 487 487 <p> 488 488 他の組み込み関数の意味も決めてみましょう。この <tt>@hoge</tt> レイヤでは、 489 489 引き算のつもりで書いたコードが、掛け算になってしまうのだ! 490 490 </p> 491 491 <pre> 492 - &gt;&gt; @hoge "-" = fun(x, y) {x * y} 492 + &gt;&gt; @hoge - = fun(x, y) {x * y} 493 493 (function:1b4c6a0:1b4fbe0) 494 494 &gt;&gt; @hoge( 5 - 6 ) 495 - polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(469): 495 + polemy.failure.RuntimeException@polemy\eval.d(469): 496 496 [<REPL>:3:24] only @value layer can call native function: * 497 497 [<REPL>:3:24] * 498 498 [<REPL>:4:8] - 499 499 </pre> 500 500 <p> 501 501 5、の意味は 10 で 6 の意味は 12 なので、10 - 12 と見せかけて掛け算して 120 が返るのだ! 502 502 と思いきや、エラーになってしまいました。なぜでしょう。それは、この "-" の定義、 ................................................................................ 505 505 </p> 506 506 <p> 507 507 ここは、「普通の」意味の掛け算を使いたいのです。 508 508 この部分については、<tt>@value</tt> レイヤで計算して欲しい。 509 509 そんなときは、レイヤ指定式を使います。 510 510 </p> 511 511 <pre> 512 - &gt;&gt; @hoge "-" = fun(x, y) {<b>@value(@hoge(x) * @hoge(y))</b>} 512 + &gt;&gt; @hoge - = fun(x, y) {<b>@value(@hoge(x) * @hoge(y))</b>} 513 513 (function:1b086c0:1b4fbe0) 514 514 &gt;&gt; @hoge( 5 - 6 ) 515 515 120 516 516 </pre> 517 517 <p> 518 518 できました。掛け算は、<tt>@value</tt> レイヤの意味で実行します。 519 519 各変数は、<tt>@hoge</tt> レイヤで計算された意味を使います、という意味になります。 ................................................................................ 706 706 var ty = @type(y); 707 707 if tx=="runtime error" then ty 708 708 else if ty=="runtime error" then tx 709 709 else if tx=="int" &amp;&amp; ty=="int" then "int" 710 710 else "type error" 711 711 )}; 712 712 713 - @type "+" = int_int_int; 714 - @type "-" = int_int_int; 715 - @type "&lt;" = int_int_int; 713 + @type + = int_int_int; 714 + @type - = int_int_int; 715 + @type &lt; = int_int_int; 716 716 </pre> 717 717 <pre> 718 718 &gt;&gt; @type( 1 + 2 ) 719 719 int 720 720 &gt;&gt; @type( 1 + "foo" ) 721 721 type error 722 722 </pre> 723 723 <p> 724 724 「実行時エラーについては、それが起きなければ返すはずの型」を計算するという定義に、 725 725 ここではしています。さらに(ちょっと手抜きで int 以外を考えていない)if の型定義を考えると、 726 726 こんな雰囲気。 727 727 </p> 728 728 <pre> 729 - @type "if" (c, t, e) {@value( 729 + @type if (c, t, e) {@value( 730 730 if( @type(c)=="int" || @type(c)=="runtime error" ) then 731 731 @type( int_int_int(t(), e()) ) 732 732 else 733 733 "type error" 734 734 )}; 735 735 </pre> 736 736 <p> ................................................................................ 1009 1009 <script>explorer.outline.writeEnabled = false;</script> 1010 1010 1011 1011 <dd><p> 1012 1012 これはエラーになります。 1013 1013 </p> 1014 1014 <pre> 1015 1015 &gt;&gt; let _ = (@macro twice(x) {x;x} in twice(print("Hello"))) 1016 - polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\value.d(109): 1016 + polemy.failure.RuntimeException@polemy\value.d(109): 1017 1017 [<REPL>:2:35] 'twice' is not set in @value layer 1018 1018 </pre> 1019 1019 <p> 1020 1020 どういうことかというと、<tt>@macro</tt> で定義したマクロはいつから使えるようになるかという話で、 1021 1021 この <tt>@macro twice(x) {x;x} in ...</tt> の部分は <tt>@value</tt> レイヤの式なので、 1022 1022 まずこの式全体のマクロ展開が終わったあとにしか実行されないのです。<tt>twice</tt> 1023 1023 がマクロと見なされはじめるのは、<tt>@macro</tt> 実行が終わった後。 ................................................................................ 1158 1158 <script>explorer.outline.decSymbolLevel();</script> 1159 1159 1160 1160 1161 1161 </td></tr> 1162 1162 <tr><td id="docfooter"> 1163 1163 Page was generated with 1164 1164 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 1165 - on Tue Nov 30 10:23:12 2010 1165 + on Thu Dec 2 09:14:39 2010 1166 1166 1167 1167 </td></tr> 1168 1168 </table> 1169 1169 </div> 1170 1170 <script> 1171 1171 explorer.packageExplorer.addModule("index"); 1172 1172 explorer.packageExplorer.addModule("main");

Modified index.dd from [c411cf72312ff15e] to [b0a85a436fa9ea4b].

318 318 <pre> 319 319 &gt;&gt; @value( 1 + 2 ) 320 320 3 321 321 </pre> 322 322 他のレイヤで動かしてみましょう。適当に。「<tt>@hoge</tt> レイヤ」で。 323 323 <pre> 324 324 &gt;&gt; @hoge( 3 ) 325 - polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(138): 325 + polemy.failure.RuntimeException@polemy\eval.d(138): 326 326 [<REPL>:4:8] lift function for @hoge is not registered 327 327 </pre> 328 328 <p> 329 329 エラーになりました。Polemy のインタプリタは、起動時には、<tt>@value</tt> 330 330 レイヤでのコードの意味しか知りません。<tt>@hoge</tt> レイヤでは <tt>3</tt> 331 331 というのがどんな意味なのか、わかりません!というエラーが出ています。 332 332 </p> ................................................................................ 350 350 6 351 351 </pre> 352 352 <p> 353 353 では、1+2 を <tt>@hoge</tt> レイヤで動かしてみましょう。 354 354 </p> 355 355 <pre> 356 356 &gt;&gt; @hoge( 1 + 2 ) 357 - polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(466): 357 + polemy.failure.RuntimeException@polemy\eval.d(466): 358 358 [<REPL>:3:7] only @value layer can call native function: + 359 359 [<REPL>:3:7] + 360 360 </pre> 361 361 <p> 362 362 まだエラーですね。これは要するに "+" の意味がわからない、と言っています。 363 363 $(RED $(B レイヤ指定変数定義式)) で、"+" の意味を教えてあげます。 364 364 </p> 365 365 <pre> 366 - &gt;&gt; @hoge "+" = fun(x, y) {x} 366 + &gt;&gt; @hoge + = fun(x, y) {x} 367 367 (function:182eca0:18435e0) 368 368 &gt;&gt; @hoge( 3 + 4 ) 369 369 6 370 370 </pre> 371 371 <p> 372 372 できました。 373 373 </p> 374 374 <p> 375 375 他の組み込み関数の意味も決めてみましょう。この <tt>@hoge</tt> レイヤでは、 376 376 引き算のつもりで書いたコードが、掛け算になってしまうのだ! 377 377 </p> 378 378 <pre> 379 - &gt;&gt; @hoge "-" = fun(x, y) {x * y} 379 + &gt;&gt; @hoge - = fun(x, y) {x * y} 380 380 (function:1b4c6a0:1b4fbe0) 381 381 &gt;&gt; @hoge( 5 - 6 ) 382 - polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(469): 382 + polemy.failure.RuntimeException@polemy\eval.d(469): 383 383 [<REPL>:3:24] only @value layer can call native function: * 384 384 [<REPL>:3:24] * 385 385 [<REPL>:4:8] - 386 386 </pre> 387 387 <p> 388 388 5、の意味は 10 で 6 の意味は 12 なので、10 - 12 と見せかけて掛け算して 120 が返るのだ! 389 389 と思いきや、エラーになってしまいました。なぜでしょう。それは、この "-" の定義、 ................................................................................ 392 392 </p> 393 393 <p> 394 394 ここは、「普通の」意味の掛け算を使いたいのです。 395 395 この部分については、<tt>@value</tt> レイヤで計算して欲しい。 396 396 そんなときは、レイヤ指定式を使います。 397 397 </p> 398 398 <pre> 399 - &gt;&gt; @hoge "-" = fun(x, y) {$(B @value(@hoge(x) * @hoge(y)))} 399 + &gt;&gt; @hoge - = fun(x, y) {$(B @value(@hoge(x) * @hoge(y)))} 400 400 (function:1b086c0:1b4fbe0) 401 401 &gt;&gt; @hoge( 5 - 6 ) 402 402 120 403 403 </pre> 404 404 <p> 405 405 できました。掛け算は、<tt>@value</tt> レイヤの意味で実行します。 406 406 各変数は、<tt>@hoge</tt> レイヤで計算された意味を使います、という意味になります。 ................................................................................ 553 553 var ty = @type(y); 554 554 if tx=="runtime error" then ty 555 555 else if ty=="runtime error" then tx 556 556 else if tx=="int" && ty=="int" then "int" 557 557 else "type error" 558 558 )}; 559 559 560 - @type "+" = int_int_int; 561 - @type "-" = int_int_int; 562 - @type "<" = int_int_int; 560 + @type + = int_int_int; 561 + @type - = int_int_int; 562 + @type &lt; = int_int_int; 563 563 </pre> 564 564 <pre> 565 565 &gt;&gt; @type( 1 + 2 ) 566 566 int 567 567 &gt;&gt; @type( 1 + "foo" ) 568 568 type error 569 569 </pre> 570 570 <p> 571 571 「実行時エラーについては、それが起きなければ返すはずの型」を計算するという定義に、 572 572 ここではしています。さらに(ちょっと手抜きで int 以外を考えていない)if の型定義を考えると、 573 573 こんな雰囲気。 574 574 </p> 575 575 <pre> 576 - @type "if" (c, t, e) {@value( 576 + @type if (c, t, e) {@value( 577 577 if( @type(c)=="int" || @type(c)=="runtime error" ) then 578 578 @type( int_int_int(t(), e()) ) 579 579 else 580 580 "type error" 581 581 )}; 582 582 </pre> 583 583 <p> ................................................................................ 796 796 )) 797 797 $(SECTION 微妙なところ3, $(SECBODY 798 798 <p> 799 799 これはエラーになります。 800 800 </p> 801 801 <pre> 802 802 &gt;&gt; let _ = (@macro twice(x) {x;x} in twice(print("Hello"))) 803 - polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\value.d(109): 803 + polemy.failure.RuntimeException@polemy\value.d(109): 804 804 [<REPL>:2:35] 'twice' is not set in @value layer 805 805 </pre> 806 806 <p> 807 807 どういうことかというと、<tt>@macro</tt> で定義したマクロはいつから使えるようになるかという話で、 808 808 この <tt>@macro twice(x) {x;x} in ...</tt> の部分は <tt>@value</tt> レイヤの式なので、 809 809 まずこの式全体のマクロ展開が終わったあとにしか実行されないのです。<tt>twice</tt> 810 810 がマクロと見なされはじめるのは、<tt>@macro</tt> 実行が終わった後。

Modified readme.txt from [d03287608e84fd60] to [366a1df4067e228e].

1 1 ----------------------------------------------------------------------------- 2 2 Polemy 0.1.0 3 3 by k.inaba (www.kmonos.net) 4 - Nov 25, 2010 4 + Dec 4, 2010 5 5 ----------------------------------------------------------------------------- 6 6 7 7 [How to Build] 8 8 9 9 - Install DMD 10 10 http://www.digitalmars.com/d/2.0/changelog.html 11 11 Version 2.050 is recommended. Older or newer version may not work.

Modified sample/fib.pmy from [4438bc82733dcb16] to [fd8a6f88bc7a9f4a].

6 6 7 7 let upto = fun(n, f){ 8 8 if n > 0: upto(n-1,f); 9 9 f(n) 10 10 }; 11 11 12 12 var compose = fun(f,g){ fun(x){f(g(x))} }; 13 -var "<<" = compose; 13 +var << = compose; 14 14 15 15 upto(18, print<<fib); 16 16 17 17 18 18 ###################################### 19 19 # Omake. Feel the automatic memoization! 20 20 21 21 # Set up a mirror layer (do-the-same-thing layer) of @value 22 22 @@m(x){x}; 23 -@m "+" (x,y) { @value(@m(x)+@m(y)) }; 24 -@m "-" (x,y) { @value(@m(x)-@m(y)) }; 25 -@m "<" (x,y) { @value(@m(x)<@m(y)) }; 26 -@m "if" (c,t,e) { @value(if @m(c) then @m(t()) else @m(e())) }; 23 +@m + (x,y) { @value(@m(x)+@m(y)) }; 24 +@m - (x,y) { @value(@m(x)-@m(y)) }; 25 +@m < (x,y) { @value(@m(x)<@m(y)) }; 26 +@m if (c,t,e) { @value(if @m(c) then @m(t()) else @m(e())) }; 27 27 28 28 # Helper function to call fib in layer @m 29 29 def fibm(x @value) { @m(fib(@value(x))) }; 30 30 31 31 # User defined layers are automatically memoized. 32 32 # So this is much faster. 33 33 upto(18, print<<fibm);

Deleted sample/fizzbuzz.pmy version [8920e9fd1d398081]

1 -# 2 -# Not at all a good example of the usage of layers, but anyway... 3 -# 4 - 5 -@@3(x){x}; 6 -@@5(x){x}; 7 -@@15(x){x}; 8 -def incr(x) { x+1 }; 9 -@ 3 incr(x) {@value( if(@ 3(x)+1< 3)then@ 3(x)+1 else 0 )}; 10 -@ 5 incr(x) {@value( if(@ 5(x)+1< 5)then@ 5(x)+1 else 0 )}; 11 -@15 incr(x) {@value( if(@15(x)+1<15)then@15(x)+1 else 0 )}; 12 - 13 -def fb(n @value @3 @5 @15) { 14 - print( if @15(n): if @5(n): if @3(n): n else: "Fizz" else "Buzz" else "FizzBuzz" ); 15 - fb(incr(n)) 16 -}; 17 - 18 -fb(0)

Added sample/lazy.pmy version [4af3c4ccb9eff42b]

1 +# I'm lazy. So I define only the primitives needed for tarai. 2 +@@lazy(x) { fun(){x} }; 3 +@lazy - (x,y) { fun(){@value(@lazy(x)() - @lazy(y)()) } }; 4 +@lazy <= (x,y) { fun(){@value(@lazy(x)() <= @lazy(y)()) } }; 5 +@lazy if (c,t,e) { fun(){@value(if @lazy(c)() then @lazy(t())() else @lazy(e())()) } }; 6 + 7 +def tarai(x,y,z) 8 +{ 9 + if x<=y then 10 + y 11 + else 12 + tarai( tarai(x-1,y,z), tarai(y-1,z,x), tarai(z-1,x,y) ) 13 +}; 14 + 15 +# print( tarai(12, 6, 0) ); 16 +print( @lazy( tarai(12, 6, 0) )() ); 17 + 18 + 19 +#### Omake: only Z is made lazy. 20 +def taraiZ(x,y,z @lazy) 21 +{ 22 + if x<=y then 23 + y 24 + else 25 + taraiZ( taraiZ(x-1,y,z), taraiZ(y-1,@lazy(z)(),x), taraiZ(@lazy(z)()-1,x,y) ) 26 +}; 27 +print( taraiZ(12,6,0) );

Modified sample/type.pmy from [a04d420215bb851f] to [5f72922a0ac6e212].

61 61 if @type(x)=="RE" || @type(y)=="RE": "RE" 62 62 else if @type(x)=="TE" || @type(y)=="TE": "TE" 63 63 else t0 64 64 )} 65 65 }; 66 66 67 67 # type annotation for built-in ops 68 -@type "+" = Tbin("int", "int", "int"); 69 -@type "-" = Tbin("int", "int", "int"); 70 -@type "*" = Tbin("int", "int", "int"); 71 -@type "/" = Tbin("int", "int", "int"); 72 -@type "%" = Tbin("int", "int", "int"); 73 -@type "&&" = Tbin("int", "int", "int"); 74 -@type "||" = Tbin("int", "int", "int"); 68 +@type + = Tbin("int", "int", "int"); 69 +@type - = Tbin("int", "int", "int"); 70 +@type * = Tbin("int", "int", "int"); 71 +@type / = Tbin("int", "int", "int"); 72 +@type % = Tbin("int", "int", "int"); 73 +@type && = Tbin("int", "int", "int"); 74 +@type || = Tbin("int", "int", "int"); 75 75 @type print = fun(x){x}; 76 76 @type gensym = fun(){"str"}; 77 77 @type argv = {list: "str"}; 78 78 @type rand = Tuni("int","int"); 79 -@type "~" = Tbinany("str"); 80 -@type "<" = Tbinany("int"); 81 -@type "<=" = Tbinany("int"); 82 -@type ">" = Tbinany("int"); 83 -@type ">=" = Tbinany("int"); 84 -@type "==" = Tbinany("int"); 85 -@type "!=" = Tbinany("int"); 86 -@type "if" (c,t,e) {@value( 79 +@type ~ = Tbinany("str"); 80 +@type < = Tbinany("int"); 81 +@type <= = Tbinany("int"); 82 +@type > = Tbinany("int"); 83 +@type >= = Tbinany("int"); 84 +@type == = Tbinany("int"); 85 +@type != = Tbinany("int"); 86 +@type if (c,t,e) {@value( 87 87 if @type(c)=="RE": "RE" 88 88 else if @type(c)!="int": "TE" 89 89 else mergeType( @type(t()), @type(e()) ); 90 90 )}; 91 91 @type _isint = Tuniany("int"); 92 92 @type _isstr = Tuniany("int"); 93 93 @type _isfun = Tuniany("int"); ................................................................................ 94 94 @type _istbl = Tuniany("int"); 95 95 @type _isbot = Tuniany("int"); 96 96 97 97 ################################### 98 98 99 99 # for lists 100 100 @type "{}"() {@value( {list: "RE"} )}; 101 -@type ".?"(t, s) {@value( 101 +@type .? (t, s) {@value( 102 102 if @type(t)=="RE": "RE" 103 103 else if @type(t)=="TE": "TE" 104 104 else if _istbl( @type(t) ): "int" 105 105 else "TE" 106 106 )}; 107 -@type ".="(t, s@value, v) {@value( 107 +@type .= (t, s@value, v) {@value( 108 108 var tt = @type(t); 109 109 if tt == "TE": "TE" 110 110 else if tt == "RE": "RE" 111 111 else if _istbl(tt) && tt.?list: 112 112 if s == "car": 113 113 mergeType(tt, {list: @type(v)}) 114 114 else if s == "cdr": 115 115 mergeType(tt, @type(v)) 116 116 else: 117 117 tt 118 118 else: 119 119 "TE" 120 120 )}; 121 -@type "."(t, s@value) {@value( 121 +@type . (t, s@value) {@value( 122 122 var tt = @type(t); 123 123 if tt == "TE": "TE" 124 124 else if tt == "RE": "RE" 125 125 else if _istbl(tt) && tt.?list: 126 126 if s == "car": 127 127 tt.list 128 128 else if s == "cdr":