有一些主函数的某些参数只在let-in表达式所定义的子函数使用,然后被主函数间接使用。今天修了一个bug支持了这种函数的类型推导。例子如下:
首先有函数:
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
然后得到结果:
1 func find T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> (system.list system.int))) codefrom 25
昨天的问题也修正了。虽然说昨天“未解决”的函数本身有错,但是后来修改了之后,其中一个函数仍然不能得到正确结果。结合上面的情况进行修正然后给test case加入了点新函数:
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 {将列表通过映射函数转换为另一个列表}
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 {将列表反转}
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 {将列表应用到一个左结合操作符上}
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 {重复无穷列表}
96 def repeat x = list x (repeat x)
97
98 {循环无穷列表}
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
结果如下:
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
大部分情况都覆盖到了,但是还剩下一些情况。譬如说,某个函数被大量重载。虽然大量重载的函数的一些类型可能是有共性,而另一些没有:
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{这个函数的类型模式特殊}
这种情况还无法处理。而且不是所有的重载函数都会在需要重载函数的模板函数的单元看到,这个时候可能需要加一个语法来解决。虽然不需要像Haskell的Monad那样那么复杂,但是他那玩意儿就解决了这个问题……
posted on 2008-10-08 08:19
陈梓瀚(vczh) 阅读(1351)
评论(0) 编辑 收藏 引用 所属分类:
脚本技术