有一些主函數(shù)的某些參數(shù)只在let-in表達(dá)式所定義的子函數(shù)使用,然后被主函數(shù)間接使用。今天修了一個bug支持了這種函數(shù)的類型推導(dǎo)。例子如下:
首先有函數(shù):
1 {判斷符合條件的元素在列表中的位置}
2 def find constraint xs =
3 let
4 def _find indices n xs =
5 select xs of
6 case list x tail : if (constraint x) (list n indices) (_find indices (iadd n 1) tail)
7 case empty : indices
8 end
9 in _find empty 0 xs
然后得到結(jié)果:
1 func find T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> (system.list system.int))) codefrom 25
昨天的問題也修正了。雖然說昨天“未解決”的函數(shù)本身有錯,但是后來修改了之后,其中一個函數(shù)仍然不能得到正確結(jié)果。結(jié)合上面的情況進(jìn)行修正然后給test case加入了點新函數(shù):
1 module list
2 import sysutils
3
4 {返回列表長度}
5 def length xs =
6 select xs of
7 case list x tail : iadd 1 (length tail)
8 case empty : 0
9 end
10
11 {返回列表的第一個元素}
12 def head xs =
13 select xs of
14 case list x tail : x
15 end
16
17 {返回列表的第二個元素開始的列表}
18 def tail xs =
19 select xs of
20 case list x tail : tail
21 end
22
23 {連接兩個列表}
24 def concat as bs =
25 select as of
26 case list a tail : list a (concat tail bs)
27 case empty : bs
28 end
29
30 {判讀列表是否為空}
31 def isempty xs =
32 select xs of
33 case list x tail : false
34 case empty : true
35 end
36
37 {將列表通過映射函數(shù)轉(zhuǎn)換為另一個列表}
38 def transform mapper xs =
39 select xs of
40 case list x tail : list (mapper x) (transform mapper tail)
41 case empty : empty
42 end
43
44 {將列表反轉(zhuǎn)}
45 def reverse xs =
46 let
47 def _reverse xs r =
48 select xs of
49 case list x tail : _reverse tail (list x r)
50 case empty : r
51 end
52 in _reverse xs empty
53
54 {為列表插入分隔符}
55 def intersperse spliter xs =
56 select xs of
57 case list x tail : list spliter (list x (intersperse spliter tail))
58 case empty : empty
59 end
60
61 {將“列表的列表”的所有元素連接起來成為一個長的新列表}
62 def flatten xs =
63 select xs of
64 case list x tail : concat x (flatten tail)
65 case empty : empty
66 end
67
68 {將兩個列表組合成一個pair的列表}
69 def pairlist as bs =
70 select as of
71 case list a atail :
72 select bs of
73 case list b btail : list (pair a b) (pairlist atail btail)
74 case empty : empty
75 end
76 case empty : empty
77 end
78
79 {將列表應(yīng)用到一個左結(jié)合操作符上}
80 def fold init op xs =
81 select xs of
82 case list x tail : fold (op init x) op tail
83 case empty : init
84 end
85
86 {判斷列表的所有元素是否符合某個約束}
87 def all constraint xs = fold true and (transform constraint xs)
88
89 {判斷列表的是否存在元素是否符合某個約束}
90 def any constraint xs = fold false or (transform constraint xs)
91
92 {遞歸無窮列表}
93 def iterate op init = list init (iterate op (op init))
94
95 {重復(fù)無窮列表}
96 def repeat x = list x (repeat x)
97
98 {循環(huán)無窮列表}
99 def cycle xs = concat xs (cycle xs)
100
101 {取列表前n個元素組成子列表}
102 def take n xs =
103 if (iequ n 0)
104 empty
105 select xs of
106 case list x tail : list x (take (isub n 1) tail)
107 case empty : empty
108 end
109
110 {取列表n個元素以后的字列表}
111 def drop n xs =
112 if (iequ n 0)
113 xs
114 select xs of
115 case list x tail : take (isub n 1) tail
116 case empty : empty
117 end
118
119 {取列表中符合條件的元素組成的新列表}
120 def takeif constraint xs =
121 select xs of
122 case list x tail : if (constraint x) (list x (takeif constraint tail)) (takeif constraint tail)
123 case empty : empty
124 end
125
126 {取列表中不符合條件的元素組成的新列表}
127 def dropif constraint xs =
128 select xs of
129 case list x tail : if (constraint x) (dropif constraint tail) (list x (dropif constraint tail))
130 case empty : empty
131 end
132
133 {判斷一個列表是否另一個列表的前綴}
134 def isprefix eq as bs =
135 select as of
136 case list a atail :
137 select bs of
138 case list b btail : and (eq a b) (isprefix atail btail)
139 case empty : false
140 end
141 case empty : true
142 end
143
144 {判斷一個列表是否另一個列表的后綴}
145 def ispostprefix eq as bs = isprefix eq (reverse as) (reverse bs)
146
147 {取出列表中指定位置的元素}
148 def elemof n xs = if (iequ n 0) (head xs) (elemof (isub n 1) (tail xs))
149
150 {取出列表從指定位置開始的子列表}
151 def sublistof n xs = if (iequ n 0) xs (sublistof (isub n 1) (tail xs))
152
153 {判斷符合條件的元素在列表中的位置}
154 def findfirst constraint xs =
155 let
156 def _findfirst n xs =
157 select xs of
158 case list x tail : if (constraint x) n (_findfirst (iadd n 1) tail)
159 case empty : ineg 1
160 end
161 in _findfirst 0 xs
162
163 {判斷符合條件的元素在列表中的位置}
164 def find constraint xs =
165 let
166 def _find indices n xs =
167 select xs of
168 case list x tail : if (constraint x) (list n indices) (_find indices (iadd n 1) tail)
169 case empty : indices
170 end
171 in _find empty 0 xs
結(jié)果如下:
1 【模塊:system】
2 module system::system
3 type bool
4 type char
5 type int
6 type list T
7 type void
8 ctor empty :: type list T
9 ctor false :: type bool
10 ctor list :: <T> -> (system.list <T>) -> type list T
11 ctor true :: type bool
12 func chr :: (system.int -> system.char) alias chr codefrom -1
13 func iadd :: (system.int -> (system.int -> system.int)) alias iadd codefrom -1
14 func idiv :: (system.int -> (system.int -> system.int)) alias idiv codefrom -1
15 func iequ :: (system.int -> (system.int -> system.bool)) alias iequ codefrom -1
16 func ilg :: (system.int -> (system.int -> system.bool)) alias ilg codefrom -1
17 func imod :: (system.int -> (system.int -> system.int)) alias imod codefrom -1
18 func imul :: (system.int -> (system.int -> system.int)) alias imul codefrom -1
19 func ism :: (system.int -> (system.int -> system.bool)) alias ism codefrom -1
20 func isub :: (system.int -> (system.int -> system.int)) alias isub codefrom -1
21 func ord :: (system.char -> system.int) alias ord codefrom -1
22 【模塊:sysutils】
23 module sysutils::sysutils
24 import system
25 type pair T1 T2
26 ctor pair :: <T1> -> <T2> -> type pair T1 T2
27 func and :: (system.bool -> (system.bool -> system.bool)) codefrom 4
28 func if T1 :: (system.bool -> (<T1> -> (<T1> -> <T1>))) codefrom 9
29 func ineg :: (system.int -> system.int) codefrom 11
30 func not :: (system.bool -> system.bool) codefrom 2
31 func or :: (system.bool -> (system.bool -> system.bool)) codefrom 6
32 func pairop T1 T2 T3 :: ((<T1> -> (<T2> -> <T3>)) -> ((sysutils.pair <T1> <T2>) -> <T3>)) codefrom 12
33 func xor :: (system.bool -> (system.bool -> system.bool)) codefrom 8
34 【模塊:list】
35 module list::list
36 import sysutils
37 func all T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> system.bool)) codefrom 11
38 func any T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> system.bool)) codefrom 12
39 func concat T1 :: ((system.list <T1>) -> ((system.list <T1>) -> (system.list <T1>))) codefrom 3
40 func cycle T1 :: ((system.list <T1>) -> (system.list <T1>)) codefrom 15
41 func drop T1 :: (system.int -> ((system.list <T1>) -> (system.list <T1>))) codefrom 17
42 func dropif T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> (system.list <T1>))) codefrom 19
43 func elemof T1 :: (system.int -> ((system.list <T1>) -> <T1>)) codefrom 22
44 func find T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> (system.list system.int))) codefrom 25
45 func findfirst T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> system.int)) codefrom 24
46 func flatten T1 :: ((system.list (system.list <T1>)) -> (system.list <T1>)) codefrom 8
47 func fold T1 T2 :: (<T1> -> ((<T1> -> (<T2> -> <T1>)) -> ((system.list <T2>) -> <T1>))) codefrom 10
48 func head T1 :: ((system.list <T1>) -> <T1>) codefrom 1
49 func intersperse T1 :: (<T1> -> ((system.list <T1>) -> (system.list <T1>))) codefrom 7
50 func isempty T1 :: ((system.list <T1>) -> system.bool) codefrom 4
51 func ispostprefix T1 T2 :: ((<T1> -> (<T2> -> system.bool)) -> ((system.list <T1>) -> ((system.list <T2>) -> system.bool))) codefrom 21
52 func isprefix T1 T2 :: ((<T1> -> (<T2> -> system.bool)) -> ((system.list <T1>) -> ((system.list <T2>) -> system.bool))) codefrom 20
53 func iterate T1 :: ((<T1> -> <T1>) -> (<T1> -> (system.list <T1>))) codefrom 13
54 func length T1 :: ((system.list <T1>) -> system.int) codefrom 0
55 func pairlist T1 T2 :: ((system.list <T1>) -> ((system.list <T2>) -> (system.list (sysutils.pair <T1> <T2>)))) codefrom 9
56 func repeat T1 :: (<T1> -> (system.list <T1>)) codefrom 14
57 func reverse T1 :: ((system.list <T1>) -> (system.list <T1>)) codefrom 6
58 func sublistof T1 :: (system.int -> ((system.list <T1>) -> (system.list <T1>))) codefrom 23
59 func tail T1 :: ((system.list <T1>) -> (system.list <T1>)) codefrom 2
60 func take T1 :: (system.int -> ((system.list <T1>) -> (system.list <T1>))) codefrom 16
61 func takeif T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> (system.list <T1>))) codefrom 18
62 func transform T1 T2 :: ((<T1> -> <T2>) -> ((system.list <T1>) -> (system.list <T2>))) codefrom 5
63
大部分情況都覆蓋到了,但是還剩下一些情況。譬如說,某個函數(shù)被大量重載。雖然大量重載的函數(shù)的一些類型可能是有共性,而另一些沒有:
1 func eq::int->int->bool
2 func eq::char->char->bool
3 func eq T::T->T->bool
4 func eq::list char->int->bool{這個函數(shù)的類型模式特殊}
這種情況還無法處理。而且不是所有的重載函數(shù)都會在需要重載函數(shù)的模板函數(shù)的單元看到,這個時候可能需要加一個語法來解決。雖然不需要像Haskell的Monad那樣那么復(fù)雜,但是他那玩意兒就解決了這個問題……
posted on 2008-10-08 08:19
陳梓瀚(vczh) 閱讀(1358)
評論(0) 編輯 收藏 引用 所屬分類:
腳本技術(shù)