top recent

プログラミング:企画もの

関連ページ

プログラミング プログラミング:企画もの:お題1 プログラミング:企画もの:お題2 プログラミング:企画もの:お題3

目次

  1. お題4:お前らfactorialのソースとコンパイル結果を拝見させて下さい...
  2. Emacs Lisp...
  3. Squeak...
  4. Squeak (非再帰版)...
  5. Squeak (無理矢理再帰(?)版)...
  6. Squeak (Code Complete 推奨版を Smalltalk に書き直したもの。Squeak に吐きださせた C コード付きw)...
  7. VisualWorks 7nc (祝公開開始)...
  8. gofer (hugs の前身) G-Code (VM は G-Machine)...
  9. Scheme48: simple recursive version...
  10. Scheme48: tail recursive version...
  11. GikoForth(スタックのみ使用版)...
  12. GikoForth(ローカル変数使用版)...
  13. GikoForth(メソッド版)...
  14. bbbb版...
  15. お前ら張り切りすぎでホント頭が下がります...
  16. DelはVMじゃないし…...
  17. Scheme (Gauche) : simple recursive version...
  18. Scheme (Gauche) : tail recursive version...
  19. Haskell (ソースだけ)...
  20. Haskell: most simple version...
  21. Concurrent Clean 版...
  22. Prolog (Jun439 版)...

お題4:お前らfactorialのソースとコンパイル結果を拝見させて下さい

VM系はバイトコード、ネイティブ系はアセンブラ。

[[id:1240]] 2002-09-19 15:07:49


Emacs Lisp

(defun factorial (integer)
"Compute factorial of INTEGER."
(if (= 1 integer) 1
(* integer (factorial (1- integer)))))
(byte-compile 'factorial)
(factorial 10)
=> 3628800

(disassemble 'factorial)
nil
byte code for factorial:
doc: Compute factorial of INTEGER.
args: (integer)
0 varref integer
1 constant 1
2 eqlsign
3 goto-if-nil 1
6 constant 1
7 return
8:1 varref integer
9 constant factorial
10 varref integer
11 sub1
12 call 1
13 mult
14 return

# emacs lisp リファレンスマニュアルより

[[id:1241]] 2002-09-19 14:57:30


Squeak

MyFunctor class>>factorial: anInteger
"Compute factorial of INTEGER."
anInteger = 1
ifTrue: [^ 1]
ifFalse: [^ anInteger * (self factorial: anInteger - 1)]

MyFunctor factorial: 10
=> 3628800

(MyFunctor class compiledMethodAt: #factorial:) symbolic
=> '9 <10> pushTemp: 0
10 <76> pushConstant: 1
11 <B6> send: =
12 <99> jumpFalse: 15
13 <76> pushConstant: 1
14 <7C> returnTop
15 <10> pushTemp: 0
16 <70> self
17 <10> pushTemp: 0
18 <76> pushConstant: 1
19 <B1> send: -
20 <E0> send: factorial:
21 <B8> send: *
22 <7C> returnTop
'

[[id:1242]] 2002-09-19 15:04:47


Squeak (非再帰版)


(Integer >> #factorial2) getSourceFromFile
"=> '
factorial2
"Answer the factorial of the receiver."
self < 0 ifTrue: [self error: 'Not valid for negative integers'].
self < 2 ifTrue: [^ 1].
^ (2 to: self) inject: 1 into: [:fact :int | fact * int]
' "

10 factorial2
"=> 3628800"

(Integer >> #factorial2) symbolic "意味なさげ…ですが(^_^;)"
"=> '
21 <70> self
22 <75> pushConstant: 0
23 <B2> send: <
24 <9B> jumpFalse: 29
25 <70> self
26 <21> pushConstant: ''Not valid for negative integers''
27 <E0> send: error:
28 <87> pop
29 <70> self
30 <77> pushConstant: 2
31 <B2> send: <
32 <99> jumpFalse: 35
33 <76> pushConstant: 1
34 <7C> returnTop
35 <77> pushConstant: 2
36 <70> self
37 <E3> send: to:
38 <76> pushConstant: 1
39 <89> pushThisContext:
40 <77> pushConstant: 2
41 <C8> send: blockCopy:
42 <A4 06> jumpTo: 50
44 <69> popIntoTemp: 1
45 <68> popIntoTemp: 0
46 <10> pushTemp: 0
47 <11> pushTemp: 1
48 <B8> send: *
49 <7D> blockReturn
50 <F2> send: inject:into:
51 <7C> returnTop
' "
----
(Integer >> #factorial3) getSourceFromFile
"=> '
factorial3
"Answer the factorial of the receiver."
| fact int |
self < 0 ifTrue: [self error: ''Not valid for negative integers''].
self < 2 ifTrue: [^ 1].
fact _ int _ self.
[ (int _ int - 1) > 1 ] whileTrue: [fact _ fact * int].
^ fact
' "

10 factorial3
"=> 3628800"


(Integer >> #factorial3) symbolic
"=> '
13 <70> self
14 <75> pushConstant: 0
15 <B2> send: <
16 <9B> jumpFalse: 21
17 <70> self
18 <21> pushConstant: ''Not valid for negative integers''
19 <E0> send: error:
20 <87> pop
21 <70> self
22 <77> pushConstant: 2
23 <B2> send: <
24 <99> jumpFalse: 27
25 <76> pushConstant: 1
26 <7C> returnTop
27 <70> self
28 <81 41> storeIntoTemp: 1
30 <68> popIntoTemp: 0
31 <11> pushTemp: 1
32 <76> pushConstant: 1
33 <B1> send: -
34 <81 41> storeIntoTemp: 1
36 <76> pushConstant: 1
37 <B3> send: >
38 <9D> jumpFalse: 45
39 <10> pushTemp: 0
40 <11> pushTemp: 1
41 <B8> send: *
42 <68> popIntoTemp: 0
43 <A3 F2> jumpTo: 31
45 <10> pushTemp: 0
46 <7C> returnTop
' "
----
Time millisecondsToRun: [10000 factorial2]
"=> 1950"

Time millisecondsToRun: [10000 factorial3]
"=> 2316"

Time millisecondsToRun: [10000 factorial] "original version"
"=> 3435"

#sumim

[[id:1250]] 2002-09-19 18:27:32


Squeak (無理矢理再帰(?)版)


(Compiler evaluate: '
[ :int |
int < 2
ifTrue: [1]
ifFalse: [
int * ((Compiler evaluate: (thisContext home method decompileString allButFirst: 14)) value: int - 1)]]
') value: 10
" => 3628800"

(Compiler evaluate: '
[ :int |
int < 2
ifTrue: [1]
ifFalse: [
int * ((Compiler evaluate: (thisContext home method decompileString allButFirst: 14)) value: int - 1)]]
') home method symbolic
" => '
33 <89> pushThisContext:
34 <76> pushConstant: 1
35 <C8> send: blockCopy:
36 <A4 17> jumpTo: 61
38 <68> popIntoTemp: 0
39 <10> pushTemp: 0
40 <77> pushConstant: 2
41 <B2> send: <
42 <9A> jumpFalse: 46
43 <76> pushConstant: 1
44 <A4 0E> jumpTo: 60
46 <10> pushTemp: 0
47 <41> pushLit: Compiler
48 <89> pushThisContext:
49 <D5> send: home
50 <D4> send: method
51 <D3> send: decompileString
52 <26> pushConstant: 14
53 <E2> send: allButFirst:
54 <E0> send: evaluate:
55 <10> pushTemp: 0
56 <76> pushConstant: 1
57 <B1> send: -
58 <CA> send: value:
59 <B8> send: *
60 <7D> blockReturn
61 <7C> returnTop
' "
----
若干、高速化版

Smalltalk at: #FACTORIAL put: '
[ :int |
int < 2
ifTrue: [1]
ifFalse: [int * ((Compiler evaluate: FACTORIAL) value: int - 1)]]
'.
(Compiler evaluate: FACTORIAL) value: 10
" => 3628800"

Time millisecondsToRun: [(Compiler evaluate: FACTORIAL) value: 100]
" => 577"

Time millisecondsToRun: [(Compiler evaluate: '
[ :int |
int < 2
ifTrue: [1]
ifFalse: [
int * ((Compiler evaluate: (thisContext home method decompileString allButFirst: 14)) value: int - 1)]]
') value: 100]
" => 12032"

[[id:1265]] 2002-09-20 14:11:46


Squeak (Code Complete 推奨版を Smalltalk に書き直したもの。Squeak に吐きださせた C コード付きw)

ひどいコードの例 (本棚:CodeComplete)

(Functor class >> #factorial:) getSourceFromFile
" => '
factorial: number
| intermediateResult |
intermediateResult _ 1.
2 to: number do: [ :factor |
intermediateResult _ intermediateResult * factor ].
^ intermediateResult
' "

Functor factorial: 10
" => 3628800"

(Functor class >> #factorial:) symbolic
" => '
5 <76> pushConstant: 1
6 <69> popIntoTemp: 1
7 <77> pushConstant: 2
8 <6A> popIntoTemp: 2
9 <12> pushTemp: 2
10 <10> pushTemp: 0
11 <B4> send: <=
12 <AC 0A> jumpFalse: 24
14 <11> pushTemp: 1
15 <12> pushTemp: 2
16 <B8> send: *
17 <69> popIntoTemp: 1
18 <12> pushTemp: 2
19 <76> pushConstant: 1
20 <B0> send: +
21 <6A> popIntoTemp: 2
22 <A3 F1> jumpTo: 9
24 <11> pushTemp: 1
25 <7C> returnTop
' "

((CCodeGenerator new initialize addClass: Functor class; prepareMethods) cCodeForMethod: #factorial:)
" => '
int factorial(int number) {
int factor;
int intermediateResult;

intermediateResult = 1;
for (factor = 2; factor <= number; factor += 1) {
intermediateResult = intermediateResult * factor;
}
return intermediateResult;
}
' "

[[id:1266]] 2002-09-20 15:47:16


VisualWorks 7nc (祝公開開始)


| factorial |
factorial := [ :int |
int < 0 ifTrue: [self error: 'Not valid for negative integers'].
int < 2
ifTrue: [1]
ifFalse: [int * (factorial value: int - 1)] ].
^ factorial value: 10
=> 3628800

1 <CB 02> push 2 copied values
3 <10> push local 0
4 <49> push 0
5 <A2> send <
6 <C4> jump false 12
7 <11> push local 1
8 <1C> push 'Not valid for negative integers'
9 <CE 2D> non-immediate send error:
11 <66> pop
12 <10> push local 0
13 <4B> push 2
14 <A2> send <
15 <C1> jump false 18
16 <4A> push 1
17 <65> return
18 <10> push local 0
19 <D9 20> push local 2 at 0
21 <10> push local 0
22 <4A> push 1
23 <A1> send -
24 <BA> send value:
25 <A8> send *
26 <65> return

#sumim
----
http://www.cincom.com/scripts/smalltalk.dll/downloads/index.ssp?content=smalltalk

[[id:1251]] 2002-09-19 19:05:22


gofer (hugs の前身) G-Code (VM は G-Machine)


ソースコード(fact.gs)

fact 0 = 1
fact n = n * fact (n-1)


? :l fact.gs ← インタープリタへロード
Reading script file "fact.gs":
Compilingname=fact
0x17CA LOAD 1
0x17CC EVAL
0x17CD INTEQ 0 0x17D5
0x17D0 INT 1
0x17D2 UPDATE 0
0x17D4 RETURN
0x17D5 SETSTK 1
0x17D7 INT 1
0x17D9 LOAD 1
0x17DB CELL $-13001
0x17DD DICT 2
0x17DF MKAP 2
0x17E1 CELL fact
0x17E3 MKAP 1
0x17E5 LOAD 1
0x17E7 CELL $-13001
0x17E9 DICT 3
0x17EB MKAP 1
0x17ED UPDAP 0
0x17EF RETURN
------------------

Gofer session for:
standard.prelude
fact.gs
? fact 10 ← fact 10 の評価
0x17F0 INT 10
0x17F2 CELL fact
0x17F4 MKAP 1
0x17F6 RETURN
------------------
3628800 ← fact 10 の評価結果
(32 reductions, 52 cells)

[[id:1246]] 2002-09-19 16:19:37


Scheme48: simple recursive version

> (define (fact n)
(if (> n 1) (* n (fact (- n 1))) 1))
; no values returned
> (fact 10)
3628800
> ,dis fact
fact
0 (protocol 1)
2 (make-env 1)
5 (make-cont (=> 21) 0)
9 (local0-push 1)
12 (small-literal '1)
15 (push)
16 (global >)
19 (call 2)
21 (protocol 1)
23 (jump-if-false (=> 65))
26 (local0-push 1)
29 (make-cont (=> 57) 1)
33 (make-cont (=> 49) 0)
37 (local0-push 1)
40 (small-literal '1)
43 (push)
44 (global -)
47 (call 2)
49 (protocol 1)
51 (push)
52 (global fact)
55 (call 1)
57 (protocol 1)
59 (push)
60 (global *)
63 (call 2)
65 (small-literal '1)
68 (return)

[[id:1247]] 2002-09-19 16:33:13


Scheme48: tail recursive version

- Scheme は入れ子の define 認めてたような気がするんだけどなぁ.
- ちゃんと tail-recursion を最適化してるのかどうかはぱっと見ではわからないな.

> (define (facti n)
(letrec ((fact0 (lambda (n a)
(if (> n 1) (fact0 (- n 1) (* n a)) a))))
(fact0 n 1)))
; no values returned
> (facti 10)
3628800
> ,dis facti
facti
0 (protocol 1)
2 (make-env 1)
5 (small-literal '173)
8 (make-stored-object 1 vector)
11 (letrec-closures
fact0
0 (protocol 2)
2 (make-env 2)
5 (make-cont (=> 21) 0)
9 (local0-push 2)
12 (small-literal '1)
15 (push)
16 (global >)
19 (call 2)
21 (protocol 1)
23 (jump-if-false (=> 67))
26 (make-cont (=> 42) 0)
30 (local0-push 2)
33 (small-literal '1)
36 (push)
37 (global -)
40 (call 2)
42 (protocol 1)
44 (push)
45 (make-cont (=> 60) 1)
49 (local0-push 2)
52 (local0-push 1)
55 (global *)
58 (call 2)
60 (protocol 1)
62 (push)
63 (local2 1)
65 (call 2)
67 (local0 1)
69 (return))
16 (local1 1)
18 (push)
19 (small-literal '1)
22 (push-local0 1)
25 (call 2)

[[id:1248]] 2002-09-19 16:44:32


GikoForth(スタックのみ使用版)


しまった。出遅れた!(^^;

: fact ( n -- r )
?dup IF dup 1- recurse * ELSE 1 THEN ;

10 fact .
3628800 ok

0 ?dup
4 0branch +32
12 dup
16 1-
20 fact
24 *
28 branch +16
36 literal 1
44 ;

[[id:1253]] 2002-09-19 21:55:04


GikoForth(ローカル変数使用版)


: fact2 { n -- r }
n 0> IF n n 1- recurse * ELSE 1 THEN ;

10 fact2 .
3628800 ok

0 literal 0
8 (flocal.prepare)
12 literal 1
20 (local.entry)
24 (1_local@)
28 0>
32 0branch +36
40 (1_local@)
44 (1_local@)
48 1-
52 fact2
56 *
60 branch +16
68 literal 1
76 (local.exit)
80 ;

[[id:1254]] 2002-09-19 21:55:32


GikoForth(メソッド版)


:category var
:m fact: ( -- r )
get: self
:[ ?dup IF dup 1- <recurse * ELSE 1 THEN ]: :.
;m
;category

var n
10 put: n
fact: n .
3628800 ok


0 ^base
4 @
8 set^lexbase
24 set^lexlocal
36 savelp
48 branch +144
56 literal ????
64 (to^base)
68 literal ????
76 localsptr!
80 ?dup
84 0branch +84
92 dup
96 1-
100 call/obj/lp blkrcXT
156 *
160 branch +12
168 literal 1
172 return
192 inlineObject (block)
??0 literal thisObject
??8 (setlexlvptr)
?12 :.
?16 ;

- サイズでかっ 

[[id:1256]] 2002-09-19 23:31:45


bbbb版

足りないOPを急遽でっちあげて

/fact {
dup
1 .eq {
}{
dup
1 exch .sub
% pstack
fact
.mul
% pstack
} if
} def

#計算これで良かったっけ?
-02/09/20 -1する場所が間違ってることに気づいた(ぉ)ので修正。1少ない 9 fact を計算しちまてーたよ。

def を == に交換する(^^;と、VM(??)に仕込まれた物が見える…といえば聞こえが良いが、

Cell[0xa025030]:type=asArray:size=6:v=[0xa029f80] -> {
Cell[0xa042fe0]:type=asSymbol:id/name/scope/literal=11/[dup]/Context/false
Cell[0xa042fb0]:type=asInt:v=1
Cell[0xa042f90]:type=asSymbol:id/name/scope/literal=33/[.eq]/Message/false
Cell[0xa042f60]:type=asArray:size=0:v=[0x0] -> {
}
Cell[0xa042f20]:type=asArray:size=6:v=[0xa029fa0] -> {
Cell[0xa042f00]:type=asSymbol:id/name/scope/literal=11/[dup]/Context/false
Cell[0xa042ee0]:type=asInt:v=1
Cell[0xa042ec0]:type=asSymbol:id/name/scope/literal=10/[exch]/Context/false
Cell[0xa042ea0]:type=asSymbol:id/name/scope/literal=31/[.sub]/Message/false
Cell[0xa042e80]:type=asSymbol:id/name/scope/literal=35/[fact]/Context/false
Cell[0xa042e60]:type=asSymbol:id/name/scope/literal=32/[.mul]/Message/false
}
Cell[0xa042e30]:type=asSymbol:id/name/scope/literal=23/[if]/Context/false
}

面白くもなんともありませんでした。シンボルの展開は実行時だもんな。実行可能配列のTreeが有るだけ。
-ヤパーリこういうのは"コンパイル"結果とは言わないんだろうな。あえて言えば実行可能配列を生成したという部分だけがCompileっすかね?

[[id:1257]] 2002-09-20 14:38:27


お前ら張り切りすぎでホント頭が下がります

出題したその日に10本とは…

しかしなぜかCとかDelとか*Javaとかは出てこないのは普通すぎるから?

[[id:1258]] 2002-09-20 16:58:35


DelはVMじゃないし…

お前ら張り切りすぎでホント頭が下がります (#15)

ソースの様態はCと同じようなものにしかならない(のが判りきってる)し、
Codeと言われてi386の逆汗コードを提出するのも気が引けるし。(ダンプリスト?)

関数の返し値のあたりでCとちょっと違う技が使えるかも知れないけど、本質的じゃないし。
-関数と同じ名の変数(またはBorland拡張では"Result")が、返し値を代入「しておく」変数、ということになっている。
-でもCでもresultというLocal変数を作れば済むし

[[id:1267]] 2002-09-20 17:18:49


Scheme (Gauche) : simple recursive version


もうScheme48が出ているのであんまり新鮮味はないなあ。Gaucheは有向グラフ+なんちゃってCPSなので、関数呼び出しのところだけ普通のバイトコードとちょっと違います。RETは疑似インストラクションで、実際にはグラフの終端に達したことを表現しています。グラフの終端に達したらアクティブな継続へと制御が移ります(戻って来ません)。

gosh> (use gauche.vm.disasm)
(#<module gauche.vm.disasm> #<module gauche.interactive>)
gosh> (define (fact n) (if (> n 1) (* n (fact (- n 1))) 1))
fact
gosh> (disasm fact)
LREF0-PUSH ;; n
1
NUMGT2 ;; (> n 1)
IF
PRE-TAIL(2) ;; (* n (fact (- n 1)))
LREF0-PUSH ;; n
PRE-CALL ;; (fact (- n 1))
LREF0 ;; n
NUMADDI(-1) ;; (- n 1)
PUSH
GREF #<id 0x81ebfa0 user::fact>
CALL(1)
RET
PUSH
GREF #<id 0x81ea0c0 user::*>
TAIL-CALL(2)
RET
1
RET

[[id:1260]] 2002-09-20 05:08:44


Scheme (Gauche) : tail recursive version


gosh> (define (fact n) (define (rec n r) (if (> n 1) r (rec (- n 1) (* n r)))) (rec n 1))
fact
gosh> (disasm fact)
LET(1)

LAMBDA(2 0)
LREF1-PUSH ;; n
1
NUMGT2 ;; (> n 1)
IF
LREF0 ;; r
RET
PRE-TAIL(2) ;; (rec (- n 1) (* n r))
LREF1 ;; n
NUMADDI(-1) ;; (- n 1)
PUSH
PRE-CALL ;; (* n r)
LREF1-PUSH ;; n
LREF0-PUSH ;; r
GREF #<id 0x8205470 user::*>
CALL(2)
RET
PUSH
LREF10 ;; rec
TAIL-CALL(2)
RET
LSET0
PRE-TAIL(2) ;; (rec n 1)
LREF10-PUSH ;; n
PUSHI(1)
LREF0 ;; rec
TAIL-CALL(2)
RET
RET

[[id:1261]] 2002-09-20 05:04:07


Haskell (ソースだけ)


ソースコードだけだけど、以下のURLを紹介しておきます。 -- Haskeller

http://www.willamette.edu/~fruehr/haskell/evolution.html

これで大笑いできるのは相当の通です。たぶん。
- 大笑いできるほど通の域に達し得るべくもありませんが、右寄り、左寄りのあたりは「?」の後に思わず笑みが。 オチが素晴らしいですね。オリジナルページよりずっと面白い!と思うのは Haskell のポテンシャルの高さから?--sumim

[[id:1262]] 2002-09-20 10:56:10


Haskell: most simple version

- nhc98 on SunOS 5.6 sun4u, gcc version 2.8.1.
- module 宣言をつけないと Main モジュールに main 関数が無いって怒るんでこんな.
- どうもアセンブラ出力はバイトコードをデータにしてるだけのようなので省略.
- バイトコードはそのままコンパイルできる C ソースとして出て来るようだ.

----
module Fact (fact) where

import Prelude

fact 0 = 1
fact n = n * fact (n - 1)

----
#include "newmacros.h"
#include "runtime.h"

#define v177 ((void*)startLabel+55)
#define CT_v181 ((void*)startLabel+120)
extern Node FN_Prelude_46fromInteger[];
extern Node FN_Prelude_46_95apply1[];
extern Node FN_Prelude_46Num_46Prelude_46Eq[];
extern Node FN_Prelude_46_61_61[];
extern Node FN_Prelude_46_45[];
extern Node FN_Prelude_46_95apply2[];
extern Node FN_Prelude_46_42[];

static Node startLabel[] = {
bytes2word(0,0,2,0)
, bytes2word(1,1,0,2)
, useLabel(CT_v181)
,};
Node FN_Fact_46fact[] = {
bytes2word(NEEDHEAP_I32,HEAP_CVAL_I3,HEAP_ARG,1)
, bytes2word(PUSH_HEAP,HEAP_CVAL_I4,HEAP_OFF_N1,3)
, bytes2word(HEAP_CADR_N1,1,PUSH_P1,0)
, bytes2word(PUSH_ARG_I2,HEAP_CVAL_I5,HEAP_ARG,1)
, bytes2word(PUSH_HEAP,HEAP_CVAL_P1,6,HEAP_OFF_N1)
, bytes2word(3,EVAL,NEEDHEAP_I32,APPLY)
, bytes2word(2,SLIDE_P1,1,EVAL)
, bytes2word(NEEDHEAP_I32,JUMPFALSE,13,0)
, bytes2word(PUSH_CADR_N1,3,PUSH_HEAP,HEAP_CVAL_I3)
, bytes2word(HEAP_ARG,1,EVAL,NEEDHEAP_I32)
, /* v177: (byte 3) */
bytes2word(APPLY,1,RETURN_EVAL,HEAP_CVAL_P1)
, bytes2word(7,HEAP_ARG,1,HEAP_CVAL_I3)
, bytes2word(HEAP_ARG,1,HEAP_CVAL_I4,HEAP_OFF_N1)
, bytes2word(3,HEAP_CADR_N1,3,HEAP_CVAL_P1)
, bytes2word(8,HEAP_OFF_N1,8,HEAP_ARG)
, bytes2word(2,HEAP_OFF_N1,6,PUSH_HEAP)
, bytes2word(HEAP_CVAL_P1,9,HEAP_ARG,1)
, bytes2word(HEAP_OFF_N1,6,PUSH_P1,0)
, bytes2word(PUSH_ARG_I2,PUSH_HEAP,HEAP_CVAL_P1,10)
, bytes2word(HEAP_ARG,1,ZAP_ARG_I1,ZAP_ARG_I2)
, bytes2word(ZAP_STACK_P1,3,EVAL,NEEDHEAP_I32)
, bytes2word(APPLY,2,RETURN_EVAL,ENDCODE)
, bytes2word(0,0,0,0)
, 0
, CONSTRW(1,0)
, 1
, CONSTRW(0,0)
, /* CT_v181: (byte 0) */
HW(8,2)
, 0
,};
Node F0_Fact_46fact[] = {
CAPTAG(useLabel(FN_Fact_46fact),2)
, VAPTAG(useLabel(FN_Prelude_46fromInteger))
, VAPTAG(useLabel(FN_Prelude_46_95apply1))
, VAPTAG(useLabel(FN_Prelude_46Num_46Prelude_46Eq))
, VAPTAG(useLabel(FN_Prelude_46_61_61))
, VAPTAG(useLabel(FN_Prelude_46_45))
, VAPTAG(useLabel(FN_Prelude_46_95apply2))
, VAPTAG(useLabel(FN_Fact_46fact))
, VAPTAG(useLabel(FN_Prelude_46_42))
,};

[[id:1263]] 2002-09-20 10:11:26


Concurrent Clean 版

module fact

import StdClass, StdInt

Fact 0 = 1
Fact n = n * Fact (n - 1)

Start::Int
Start = Fact 10
----
サブディレクトリにこんなのあったけど、これがアセンブラ出力ということでいいのか?<よく分かってない。

.comp 916 000111010
.code 0 0 0
.start __fact_Start
.depend "StdInt" "20020805104816"
.depend "StdBool" "20020805104816"
.depend "StdOverloaded" "20020805104816"
.depend "StdClass" "20020805104816"
.module m_fact "fact" "20020925180630"
.endinfo
.implab _cycle_in_spine
.implab _type_error
.implab _hnf
.impdesc _ind
.implab _indirection _eaind
.impdesc e_system_dif
.implab e_system_nif e_system_eaif
.implab e_system_sif
.impdesc e_system_dAP
.implab e_system_nAP e_system_eaAP
.implab e_system_sAP
.impdesc _Nil
.impdesc _Cons
.impdesc _Conss
.implab n_Conss ea_Conss
.impdesc _Consts
.implab n_Consts ea_Consts
.impdesc _Conssts
.implab n_Conssts ea_Conssts
.impdesc _Tuple
.impdesc d_S.1
.implab n_S.1 ea_S.1
.impdesc d_S.2
.implab n_S.2 ea_S.2
.impdesc d_S.3
.implab n_S.3 ea_S.3
.impdesc d_S.4
.implab n_S.4 ea_S.4
.impdesc d_S.5
.implab n_S.5 ea_S.5
.impdesc d_S.6
.implab n_S.6 ea_S.6
.implab _driver
.impdesc e_StdInt_d-;7
.implab e_StdInt_s-;7
.implab e_StdInt_n-;7
.impdesc e_StdInt_d*;9
.implab e_StdInt_s*;9
.implab e_StdInt_n*;9
.desc d6 _hnf _hnf 0 0 "__Module"
.export __fact_Start
.o 0 0
__fact_Start
build _ 0 n2
.d 1 0
jmp _driver
.n 0 _
.o 1 0
n2
push_node _cycle_in_spine 0
.d 0 0
jsr ea2
.o 0 1 i
fillI_b 0 0
pop_b 1
.d 1 0
rtn
.o 0 0
ea2
.o 0 0
s2
pushI 10
.d 0 1 i
jmp s1
.o 0 1 i
s1
eqI_b 0 0
jmp_true case.1
jmp case.2
case.1
pop_b 1
pushI 1
.d 0 1 i
rtn
case.2
pushI 1
push_b 1
subI
.d 0 1 i
jsr s1
.o 0 1 i
push_b 1
update_b 1 2
updatepop_b 0 1
mulI
.d 0 1 i
rtn

[[id:1303]] 2002-09-25 18:04:28


Prolog (Jun439 版)


factorial(0, 1) :- !.
factorial(X, Y) :- is(X1, -(X, 1)), factorial(X1, Y1), is(Y, *(X, Y1)).
?-factorial(10, Result).
Result = 3628800
yes

[[id:1306]] 2002-09-26 00:58:53


top recent

HashedWiki version 3 beta
SHIMADA Keiki