-
Notifications
You must be signed in to change notification settings - Fork 10
/
map.ks
71 lines (60 loc) · 2.83 KB
/
map.ks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(def square Float (x : Float) (mul x x))
(gdef suffwdpass [square Float])
(gdef sufrevpass [square Float])
(gdef sufrev [square Float])
(def squareall (Vec Float) (v : Vec Float) (map (lam (x : Float) (square x)) v))
(gdef suffwdpass [squareall (Vec Float)])
(gdef sufrevpass [squareall (Vec Float)])
(gdef sufrev [squareall (Vec Float)])
(def mulvec (Vec Float) ((v : Vec Float) (x : Float))
(map (lam (y : Float) (mul y x)) v))
(gdef suffwdpass [mulvec (Tuple (Vec Float) (Float))])
(gdef sufrevpass [mulvec (Tuple (Vec Float) (Float))])
(gdef sufrev [mulvec (Tuple (Vec Float) (Float))])
(def squaretensor (Tensor 2 Float) (t : Tensor 2 Float)
(map (lam (x : Float) (square x)) t))
(gdef suffwdpass [squaretensor (Tensor 2 Float)])
(gdef sufrevpass [squaretensor (Tensor 2 Float)])
(gdef sufrev [squaretensor (Tensor 2 Float)])
(def vec_length_zero (Vec Float) () (build 0 (lam (i : Integer) 0.0)))
(gdef suffwdpass [vec_length_zero (Tuple)])
(gdef sufrevpass [vec_length_zero (Tuple)])
(gdef sufrev [vec_length_zero (Tuple)])
(def unused_argument (Vec Float) (unused : Vec Float)
(let (v (build 0 (lam (i : Integer) 0.0)))
(map (lam (vi : Float) (index 0 unused)) (vec_length_zero))))
(gdef suffwdpass [unused_argument (Vec Float)])
(gdef sufrevpass [unused_argument (Vec Float)])
(gdef sufrev [unused_argument (Vec Float)])
(def map_tuple (Vec Float) (x : Vec (Tuple Float Float))
(map (lam (t : Tuple Float Float)
(let ((t1 t2) t) (add t1 t2)))
x))
(gdef suffwdpass [map_tuple (Vec (Tuple Float Float))])
(gdef sufrevpass [map_tuple (Vec (Tuple Float Float))])
(gdef sufrev [map_tuple (Vec (Tuple Float Float))])
(def map2_example (Vec Float) ((t1 : Vec Float) (t2 : Vec Float))
(map2 (lam (x : Tuple Float Float) (add x)) t1 t2))
(def main Integer ()
(let (unused_input (Vec_init 1.0 2.0))
(print (map (lam (x : Float) (mul x 2.0))
(Vec_init 1.0 2.0 3.0 4.0))
"\n"
([sufrev square] 3.0 1.0)
"\n"
(squareall (Vec_init 1.0 2.0 3.0 4.0))
"\n"
([sufrev squareall] (Vec_init 1.0 2.0 3.0 4.0) (Vec_init 1.0 1.0 1.0 1.0))
"\n"
([sufrev mulvec] (tuple (Vec_init 1.0 2.0 3.0 4.0) 3.0) (Vec_init 1.0 1.0 1.0 1.0))
"\n"
([sufrev squaretensor] (build (tuple 5 5)
(lam (ij : Tuple Integer Integer)
(let ((i j) ij) (add (mul (to_float i) 10.0) (to_float j)))))
(build (tuple 5 5)
(lam (ij : Tuple Integer Integer) 1.0)))
"\nShould print "
(size unused_input)
", to reflect length of input vector, even though it's unused:\n"
(size ([sufrev unused_argument] unused_input (vec_length_zero)))
)))