青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆-341  評(píng)論-2670  文章-0  trackbacks-0
    這次終于實(shí)現(xiàn)了兩個(gè)exe,一個(gè)是編譯器,一個(gè)是提供控制臺(tái)API的虛擬機(jī)。等提供GUI的虛擬機(jī)出來之后就開放出來。

    假設(shè)有代碼Program.txt:
1 module program
2 import console
3 import list
4 
5 def main = take 10 (iterate finc 1.0||> sqr ||> ftoa ||> writeln |> ioseq

    那么提供Program.xml:
1 <kfpProject>
2   <inherit path="..\..\Include\ConsoleApplication.xml"/>
3   <output path="Executable.xml"/>
4   <report path="Report.txt"/>
5   <code>
6     <include path="Program.txt"/>
7   </code>
8 </kfpProject>

    然后執(zhí)行:
..\..\Release\KfpCompiler.exe Program.xml
..\..\Release\KfpConsole.exe Executable.xml

    就可以運(yùn)行一個(gè)程序啦!

    讓我們分析一下代碼。首先finc是一個(gè)將浮點(diǎn)數(shù)加一的函數(shù),那么iterate finc 1.0就是一個(gè)從1.0開始,每次遞增1.0的無窮數(shù)組,然后take 10返回[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0]。然后||>sqr將所有數(shù)字開方,||>ftoa將所有數(shù)字轉(zhuǎn)成字符串,然后||>writeln將所有的字符串變成10個(gè)輸出字符串的函數(shù),最后|>ioseq運(yùn)行這10個(gè)函數(shù),結(jié)果如下:


    接下來打算實(shí)現(xiàn)一個(gè)VL_CompressedStream用于壓縮產(chǎn)生的可執(zhí)行鏡像,然后再開發(fā)一個(gè)支持簡(jiǎn)單繪圖功能的虛擬機(jī),就開放出來。所需要的時(shí)間應(yīng)該不久,因?yàn)橐粋€(gè)新的虛擬機(jī)只需要實(shí)現(xiàn)API就可以了。偉大的插件系統(tǒng),滅哈哈…… 

    下面是上面MakeFile所引用到的庫(kù)文件(預(yù)定義的):

    ConsoleApplication.xml
1 <kfpProject>
2   <inherit path="Library.xml"/>
3   <code>
4     <include path="ConsoleModule.txt"/>
5   </code>
6 </kfpProject>

    Library.xml
1 <kfpProject>
2   <code>
3     <include path="SysUtils.txt"/>
4     <include path="List.txt"/>
5   </code>
6 </kfpProject>

    所需要的代碼文件:

    ConsoleModule.txt
1 module console
2 import system
3 
4 func read :: (IO string) alias "console::read"
5 
6 func write :: (string -> (IO void)) alias "console::write"
7 
8 func writeln :: (string -> (IO void)) alias "console::writeln"
9 

    SysUtils.txt
 1 module sysutils
 2 import system
 3 
 4 def (+= iadd
 5 def (+= fadd
 6 def (-= isub
 7 def (-= fsub
 8 def (*= imul
 9 def (*= fmul
10 def (/= idiv
11 def (/= fdiv
12 def (>= igt
13 def (>= fgt
14 def (>= cgt
15 def (>== iegt
16 def (>== fegt
17 def (>== cegt
18 def (<= ilt
19 def (<= flt
20 def (<= clt
21 def (<== ielt
22 def (<== felt
23 def (<== celt
24 def (=== iequ
25 def (=== fequ
26 def (=== cequ
27 def (!== ineq
28 def (!== fneq
29 def (!== cneq
30 def (&&= and
31 def (||= or
32 def (^= xor
33 def (|>) param op = op param
34 def oprev op a b = op b a
35 
36 def not a = select a of
37               case true : false
38               case false : true
39             end
40 
41 def and a b = select a of
42                 case true : b
43                 case false : false
44               end
45 
46 def or a b = select a of
47                 case true : true
48                 case false : b
49               end
50 
51 def xor a b = select a of
52                 case true : not b
53                 case false : b
54               end
55 
56 def if cond t f = select cond of
57                     case true : t
58                     case false : f
59                   end
60 
61 def ineg num = isub 0 num
62 
63 def fneg num = fsub 0.0 num
64 
65 def inc n = iadd n 1
66 
67 def dec n = isub n 1
68 
69 def finc n = fadd n 1.0
70 
71 def fdec n = fsub n 1.0
72 
73 def pairfirst p = select p of
74   case pair a b : a
75 end
76 
77 def pairsecond p = select p of
78   case pair a b : b
79 end
80 
81 def pairop op =\p->
82     select p of
83         case pair a b : op a b
84     end
85 
86 func return T :: T -> IO T
87 def return x e = success (pair x e)
88 
89 func ioerror T :: string -> IO T
90 def ioerror s = \env->fail(ioemessage s)

    List.txt
  1 module list
  2 import sysutils
  3 
  4 def (+= concat
  5 def (||>) param op = transform op param
  6 
  7 def ioseq = foldr iovoid (>>>)
  8 
  9 {返回列表長(zhǎng)度}
 10 def length xs =
 11     select xs of
 12         case list x tail : iadd 1 (length tail)
 13         case empty : 0
 14     end
 15 
 16 {返回列表的第一個(gè)元素}
 17 def head xs =
 18     select xs of
 19         case list x tail : x
 20     end
 21 
 22 {返回列表的第二個(gè)元素開始的列表}
 23 def tail xs =
 24     select xs of
 25         case list x tail : tail
 26     end
 27 
 28 {連接兩個(gè)列表}
 29 def concat as bs =
 30     select as of
 31         case list a tail : list a (concat tail bs)
 32         case empty : bs
 33     end
 34 
 35 {判讀列表是否為空}
 36 def isempty xs =
 37     select xs of
 38         case list x tail : false
 39         case empty : true
 40     end
 41 
 42 {將列表通過映射函數(shù)轉(zhuǎn)換為另一個(gè)列表}
 43 def transform mapper xs =
 44     select xs of
 45         case list x tail : list (mapper x) (transform mapper tail)
 46         case empty : empty
 47     end
 48 
 49 {將列表反轉(zhuǎn)}
 50 def reverse xs =
 51     let
 52         def _reverse xs r =
 53             select xs of
 54                 case list x tail : _reverse tail (list x r)
 55                 case empty : r
 56             end
 57     in _reverse xs empty
 58 
 59 {為列表插入分隔符}
 60 def intersperse spliter xs =
 61     select xs of
 62         case list x xtail : 
 63             select xtail of
 64                 case list y ytail : list x (list spliter (intersperse spliter xtail))
 65                 case empty : list x empty
 66             end
 67         case empty : empty
 68     end
 69 
 70 {將“列表的列表”的所有元素連接起來成為一個(gè)長(zhǎng)的新列表}
 71 def flatten xs =
 72     select xs of
 73         case list x tail : concat x (flatten tail)
 74         case empty : empty
 75     end
 76 
 77 {將兩個(gè)列表組合成一個(gè)pair的列表}
 78 def pairlist as bs =
 79     select as of
 80         case list a atail :
 81             select bs of
 82                 case list b btail : list (pair a b) (pairlist atail btail)
 83                 case empty : empty
 84             end
 85         case empty : empty
 86     end
 87 
 88 {將列表應(yīng)用到一個(gè)左結(jié)合操作符上}
 89 def foldl init op xs =
 90     select xs of
 91         case list x tail : foldl (op init x) op tail
 92         case empty : init
 93     end
 94 
 95 {將列表應(yīng)用到一個(gè)右結(jié)合操作符上}
 96 def foldr final op xs =
 97     select xs of
 98         case list x tail : op x (foldr final op tail) 
 99         case empty : final
100     end
101 
102 {判斷列表的所有元素是否符合某個(gè)約束}
103 def all constraint xs = foldl true and (transform constraint xs)
104 
105 {判斷列表的是否存在元素是否符合某個(gè)約束}
106 def any constraint xs = foldl false or (transform constraint xs)
107 
108 {遞歸無窮列表}
109 def iterate op init = list init (iterate op (op init))
110 
111 {重復(fù)無窮列表}
112 def repeat x = list x (repeat x)
113 
114 {循環(huán)無窮列表}
115 def cycle xs = concat xs (cycle xs)
116 
117 {取列表前n個(gè)元素組成子列表}
118 def take n xs =
119     if (iequ n 0)
120         empty
121         select xs of
122             case list x tail : list x (take (isub n 1) tail)
123             case empty : empty
124         end
125 
126 {取列表n個(gè)元素以后的字列表}
127 def drop n xs =
128     if (iequ n 0)
129         xs
130         select xs of
131             case list x tail : drop (isub n 1) tail
132             case empty : empty
133         end
134 
135 {取列表中符合條件的元素組成的新列表}
136 def takeif constraint xs =
137     select xs of
138         case list x tail : if (constraint x) (list x (takeif constraint tail)) (takeif constraint tail)
139         case empty : empty
140     end
141 
142 {取列表中不符合條件的元素組成的新列表}
143 def dropif constraint xs =
144     select xs of
145         case list x tail : if (constraint x) (dropif constraint tail) (list x (dropif constraint tail))
146         case empty : empty
147     end
148 
149 {判斷一個(gè)列表是否另一個(gè)列表的前綴}
150 def isprefix eq as bs =
151     select as of
152         case list a atail :
153             select bs of
154                 case list b btail : and (eq a b) (isprefix eq atail btail)
155                 case empty : false
156             end
157         case empty : true
158     end
159     
160 {判斷一個(gè)列表是否另一個(gè)列表的后綴}
161 def ispostfix eq as bs = isprefix eq (reverse as) (reverse bs)
162 
163 {取出列表中指定位置的元素}
164 def elemof n xs = if (iequ n 0) (head xs) (elemof (isub n 1) (tail xs))
165 
166 {判斷符合條件的元素在列表中的位置}
167 def findfirst constraint xs =
168     let
169         def _findfirst n xs =
170             select xs of
171                 case list x tail : if (constraint x) n (_findfirst (iadd n 1) tail)
172                 case empty : ineg 1
173             end
174     in _findfirst 0 xs
175 
176 {判斷符合條件的元素在列表中的位置}
177 def find constraint xs =
178     let
179         def _find indices n xs =
180             select xs of
181                 case list x tail : _find (if (constraint x) (list n indices) indices) (iadd n 1) tail
182                 case empty : indices
183             end
184     in reverse (_find empty 0 xs)
posted on 2008-12-26 08:07 陳梓瀚(vczh) 閱讀(2072) 評(píng)論(5)  編輯 收藏 引用 所屬分類: 腳本技術(shù)

評(píng)論:
# re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 06:36 | jge
你蠻厲害的哦,不過...代碼或是思想都挺山寨的...有好處也有壞處,但我的建議是:why not dig into something deeper? i checked out your posts before, of cource you can make your language live easily, but the result is far from satisfaction, especially when you deal with a lazy one:
intuitively a programming language is called `lazy' because it's able to reduce its equivalence relations(say function bindings) from left from right(rewrite) rather than commonly from right to left (calculate). but when we talk about`lazy programming language', we mean `a programming language with full laziness' which ensures every common expression is evaluated at most once. if a full lazy language needs to be *compiled*, the solution is mature and clearly stated in internet known as `super-combinator' which roughly means top level combinator without free variables. to compile your code to `super-combinators', you have to do `lambda-lifting' for all lambda/let/where, then `full-laziness lifting' to all super-combinators, and alpha/beta/eta/peephole reductions. with that super-combinators can be compiled to a VM called G-machine.
well, modern haskell compiler (say GHC) is much more complex from upper process, but the upper one contains *huge* stuff as well and idea is similar: `laziness' composes code node and `pattern matching' decomposes them. i don't even mention the most important stuff: type system. since most memory is located at heap, you can allow every operation that you can imagine in your language because almost everything is the same size : a pointer. like adding two functions, applying a 2-arg function with 3 args. a duck-typeing language simply allows them, but there're more sophisticated solutions. i implied you'd better read some material on Hindley–Milner type system(and System-F) in a previous comment, i don't know if you do, but i don't see much from your posts on the implementions. a flexiable polymorphic type system is the *key* of a functional programming language. if you become a guru on these, then you can contribute to a more modern compiler (say GHC), come on, do learn some lambda-calculus/SystemF first and think deeply, i'm looking forward to your new milestone.
哈哈,我是蠻無聊的,在別人博客上羅嗦,不過思考的深入一點(diǎn)對(duì)你有好處,加油。  回復(fù)  更多評(píng)論
  
# re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 06:50 | 陳梓瀚(vczh)
《The Implementation of Functional Programming Language》和類型理論都看過了,G-Machine的指令集根本不算指令,跟x86的模式相差太遠(yuǎn)了,因此根本就沒有解決問題。

不過我這次實(shí)現(xiàn)用的是最簡(jiǎn)單的辦法,反正G-Machine的指令集也要構(gòu)造樹,所以我一開始就把樹構(gòu)造好了。這就是我編譯的結(jié)果是xml的原因了……Kfp是個(gè)強(qiáng)類型語(yǔ)言,這造成了一點(diǎn)小麻煩。  回復(fù)  更多評(píng)論
  
# re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 07:28 | jge
G-machine is the only way to effectively *compile* a full-lazy programming language till now, this is also why although haskell development is leaded by MSR Cambridge, but M$ still can't bring it to VisualStudio. interpreting a tree is compilation? and i think the tree G-machine makes during runtime totally differs from your static tree. super-combinators and full-lazy lifting are really slick solutions IMO. you probably need to read the book twice.
i guess `強(qiáng)類型' you mentioned actually means `static typeing', well, according to my knowledge, i don't know any functional pl isn't static...(except LISP which is not only dynamic typeing but also dynamic scopeing, it's not a modern style we want to talk about). type-level polymophism is almost the least requirement for a lazy fp, and this is where the real fun emerges. in modern fp, a type also has `type' called `kind' (although recent haskell implement unifies it with `unsyntactic type coercion'). the book you mentioned is old and uses miranda as example, it can't touch much on type system.
  回復(fù)  更多評(píng)論
  
# re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 07:41 | jge
according to `VisualStudio' above, i mean `dotnet' strictly. 哈哈,不騷擾你了,你的代碼讓我想起了本科時(shí)同樣的山寨勁,所以打字多了點(diǎn)。  回復(fù)  更多評(píng)論
  
# re: Kernel FP編譯器工具實(shí)現(xiàn) 2009-01-19 07:49 | 陳梓瀚(vczh)
運(yùn)行時(shí)也有一棵樹的,所以跟G-Machine是一樣的。G-Machine的指令如果你仔細(xì)觀察的話,會(huì)發(fā)現(xiàn)分成了兩批。至于類型理論,實(shí)際上也有另外的兩本書講了,只不過只對(duì)語(yǔ)法的設(shè)計(jì)有指導(dǎo)作用。  回復(fù)  更多評(píng)論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            91久久嫩草影院一区二区| 在线观看欧美日韩国产| 久久久国产午夜精品| 国产九九视频一区二区三区| 亚洲欧洲av一区二区| 久久九九电影| 亚洲精品一区二区三区99| 欧美日本不卡视频| 午夜视频一区| 欧美a级片一区| 午夜精品久久久久久久久| 精品成人一区二区三区四区| 欧美精品一区二区三区在线看午夜| 在线视频你懂得一区| 久久影院亚洲| 亚洲欧美一区二区精品久久久| 日韩视频一区二区三区| 午夜精品一区二区三区四区| 激情校园亚洲| 国产精品日韩一区二区三区| 午夜欧美电影在线观看| 亚洲免费激情| 激情久久久久久| 国产欧美一区二区色老头| 欧美日韩午夜在线视频| 久久夜精品va视频免费观看| 亚洲免费影视| 亚洲欧美成人综合| 亚洲一区区二区| 激情自拍一区| 国内外成人免费激情在线视频| 国产免费成人在线视频| 国产女主播一区二区| 国产精品视频99| 国产精品国产福利国产秒拍| 国产精品久久久久久久一区探花 | 一区二区三欧美| 亚洲美女视频在线免费观看| 亚洲日本中文字幕区| 亚洲精品孕妇| 一区二区三区精品视频在线观看| 亚洲精选成人| 亚洲自拍偷拍福利| 久久久久久久一区二区三区| 亚洲一区二区三区视频| 午夜一区在线| 久久综合久色欧美综合狠狠| 老司机久久99久久精品播放免费 | 久久gogo国模裸体人体| 久久综合给合久久狠狠色| 另类欧美日韩国产在线| 亚洲国产高清高潮精品美女| 99pao成人国产永久免费视频| 亚洲黄色精品| 中文国产成人精品| 久久精品中文字幕一区| 亚洲欧洲一区二区在线观看| 亚洲欧美日韩国产一区| 蜜桃av综合| 国产精品系列在线播放| 在线观看三级视频欧美| 午夜电影亚洲| 一本大道av伊人久久综合| 欧美中文字幕视频在线观看| 欧美日韩妖精视频| 亚洲精品国精品久久99热一| 久久精品国产亚洲高清剧情介绍| 亚洲激情视频在线观看| 亚洲尤物在线视频观看| 欧美日韩在线视频首页| 亚洲日韩欧美视频| 久久亚洲二区| 午夜电影亚洲| 国产精品免费网站| 亚洲一区久久久| 日韩视频永久免费观看| 欧美剧在线观看| 亚洲人www| 欧美国产日韩一区二区| 久久综合狠狠| 91久久精品国产91久久| 麻豆精品一区二区综合av| 久久久久亚洲综合| 亚洲国产高清自拍| 国产精品一区二区你懂得 | 欧美日韩视频在线观看一区二区三区| 国内久久精品视频| 亚洲人成人一区二区在线观看| 午夜免费在线观看精品视频| 亚洲二区精品| 一本大道久久精品懂色aⅴ| 亚洲第一天堂av| 国产日韩欧美中文| 亚洲在线视频免费观看| 亚洲乱码久久| 麻豆九一精品爱看视频在线观看免费| 狠狠色狠狠色综合人人| 欧美成人dvd在线视频| 欧美日韩岛国| 欧美在线观看视频在线| 欧美成va人片在线观看| avtt综合网| 欧美一区二区三区日韩视频| 韩日视频一区| 正在播放亚洲一区| 亚洲国产精品一区制服丝袜 | 国产亚洲一区二区三区在线播放| 久久精品国产亚洲aⅴ| 欧美高清在线| 久久免费国产精品1| 久久gogo国模裸体人体| 美女福利精品视频| 亚洲已满18点击进入久久| 免费观看在线综合色| 99av国产精品欲麻豆| 欧美大秀在线观看| 91久久久久久久久| 亚洲国产乱码最新视频| 久久久噜噜噜久噜久久| 久久男人av资源网站| 国产日韩欧美日韩| 欧美亚洲一区二区在线| 久久国产精品黑丝| 国产一区二区三区久久久| 欧美一级理论片| 久久九九热re6这里有精品| 国产一区二区三区av电影| 性欧美8khd高清极品| 久久er精品视频| 激情综合久久| 欧美精品日日鲁夜夜添| 亚洲一区在线播放| 蜜臀av在线播放一区二区三区| 亚洲第一精品久久忘忧草社区| 久久精品国产69国产精品亚洲| 欧美日韩你懂的| 99精品国产一区二区青青牛奶| 亚洲伊人伊色伊影伊综合网| 国产欧美精品在线播放| 久久久久久一区二区| 亚洲激情av在线| 久久久国产精品亚洲一区| 一本久道久久综合狠狠爱| 裸体一区二区三区| 亚洲高清在线播放| 欧美午夜视频在线观看| 欧美日韩免费高清| 欧美日韩一区二区三区在线| 欧美视频在线不卡| 欧美午夜寂寞影院| 国产欧美三级| 一区二区三区自拍| 国产亚洲成av人片在线观看桃| 欧美日韩国产区一| 欧美大片一区| 久久这里只精品最新地址| 亚洲欧美日韩高清| 99视频一区二区| 在线亚洲欧美视频| 欧美国产日产韩国视频| 午夜精品免费| 欧美在线观看网站| 亚洲欧美日本国产有色| 亚洲理伦在线| 99re热这里只有精品视频| 亚洲精品一区二区三区在线观看| 国产一区二区三区免费观看| 国产精品一级二级三级| 国产精品人成在线观看免费| 欧美日韩国产精品一区| 欧美片在线观看| 欧美久久视频| 国产精品视频福利| 国产亚洲欧美日韩在线一区| 国产精品女主播| 国产视频久久久久| 亚洲国产高清一区| 亚洲欧美日韩在线综合| 蜜臀a∨国产成人精品| 免费一级欧美片在线播放| 亚洲麻豆一区| 久久综合给合久久狠狠狠97色69| 欧美色精品在线视频| 精品福利电影| 亚洲一区二区综合| 欧美暴力喷水在线| 在线亚洲免费视频| 欧美久色视频| 久久免费精品日本久久中文字幕| 久久亚洲捆绑美女| 国产精品v日韩精品| 伊人久久亚洲热| 亚洲一本视频| 亚洲高清不卡av| 欧美一区二区三区四区在线观看| 欧美岛国在线观看| 激情婷婷久久| 久久久久中文| 亚洲欧美日韩精品久久奇米色影视| 久久精品夜色噜噜亚洲a∨|