prelude.rly 7.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
19
v0.0.4
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
// TODO(weberlo): should we add sugar for scalar types (e.g., `int32` => `Tensor[(), int32]`)?

def @id[A](%x: A) -> A {
  %x
}

def @compose[A, B, C](%f: fn(B) -> C, %g: fn(A) -> B) {
  fn (%x: A) -> C {
    %f(%g(%x))
  }
}

def @flip[A, B, C](%f: fn(A, B) -> C) -> fn(B, A) -> C {
  fn(%b: B, %a: A) -> C {
    %f(%a, %b)
  }
}

/*
 * A LISP-style list ADT. An empty list is represented by `Nil`, and a member
 * `x` can be appended to the front of a list `l` via the constructor `Cons(x, l)`.
 */
type List[A] {
  Cons(A, List[A]),
  Nil,
}

/*
 * Get the head of a list. Assume the list has at least one element.
 */
def @hd[A](%xs: List[A]) -> A {
  match? (%xs) {
    Cons(%x, _) => %x,
  }
}

/*
 * Get the tail of a list.
 */
def @tl[A](%xs: List[A]) -> List[A] {
  match? (%xs) {
    Cons(_, %rest) => %rest,
  }
}

/*
 * Get the `n`th element of a list.
 */
def @nth[A](%xs: List[A], %n: Tensor[(), int32]) -> A {
  if (%n == 0) {
    @hd(%xs)
  } else {
    @nth(@tl(%xs), %n - 1)
  }
}

/*
 * Return the length of a list.
 */
def @length[A](%xs: List[A]) -> Tensor[(), int32] {
  match (%xs) {
    Cons(_, %rest) => 1 + @length(%rest),
    Nil => 0,
  }
}

/*
 * Update the `n`th element of a list and return the updated list.
 */
def @update[A](%xs: List[A], %n: Tensor[(), int32], %v: A) -> List[A] {
  if (%n == 0) {
    Cons(%v, @tl(%xs))
  } else {
    Cons(@hd(%xs), @update(@tl(%xs), %n - 1, %v))
  }
}

/*
 * Map a function over a list's elements. That is, `map(f, xs)` returns a new
 * list where the `i`th member is `f` applied to the `i`th member of `xs`.
 */
def @map[A, B](%f: fn(A) -> B, %xs: List[A]) -> List[B] {
  match (%xs) {
    Cons(%x, %rest) => Cons(%f(%x), @map(%f, %rest)),
    Nil => Nil,
  }
}

/*
 * A left-way fold over a list.
 *
 * `foldl(f, z, cons(a1, cons(a2, cons(a3, cons(..., nil)))))`
 * evaluates to `f(...f(f(f(z, a1), a2), a3)...)`.
 */
def @foldl[A, B](%f: fn(A, B) -> A, %acc: A, %xs: List[B]) -> A {
  match (%xs) {
    Cons(%x, %rest) => @foldl(%f, %f(%acc, %x), %rest),
    Nil => %acc,
  }
120 121
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
/*
 * A right-way fold over a list.
 *
 * `foldr(f, z, cons(a1, cons(a2, cons(..., cons(an, nil)))))`
 * evaluates to `f(a1, f(a2, f(..., f(an, z)))...)`.
 */
def @foldr[A, B](%f: fn(A, B) -> B, %acc: B, %xs: List[A]) -> B {
  match (%xs) {
    Cons(%x, %rest) => %f(%x, @foldr(%f, %acc, %rest)),
    Nil => %acc,
  }
}

/*
 * A right-way fold over a nonempty list.
 *
 * `foldr1(f, cons(a1, cons(a2, cons(..., cons(an, nil)))))`
 * evaluates to `f(a1, f(a2, f(..., f(an-1, an)))...)`
 */
def @foldr1[A](%f: fn(A, A) -> A, %xs: List[A]) -> A {
  match? (%xs) {
    Cons(%x, Nil) => %x,
    Cons(%x, %rest) => %f(%x, @foldr1(%f, %rest)),
  }
}

/*
 * Computes the sum of a list of integer scalars.
 */
def @sum(%xs: List[Tensor[(), int32]]) {
  let %add_f = fn(%x: Tensor[(), int32], %y: Tensor[(), int32]) -> Tensor[(), int32] {
    %x + %y
  };
  @foldl(%add_f, 0, %xs)
}

/*
 * Concatenates two lists.
 */
def @concat[A](%xs: List[A], %ys: List[A]) -> List[A] {
  let %updater = fn(%x: A, %xss: List[A]) -> List[A] {
    Cons(%x, %xss)
  };
  @foldr(%updater, %ys, %xs)
  // TODO(weberlo): write it like below, once VM constructor compilation is fixed
  // @foldr(Cons, %ys, %xs)
}

/*
 * Filters a list, returning a sublist of only the values which satisfy the given predicate.
 */
def @filter[A](%f: fn(A) -> Tensor[(), bool], %xs: List[A]) -> List[A] {
  match (%xs) {
    Cons(%x, %rest) => {
      if (%f(%x)) {
        Cons(%x, @filter(%f, %rest))
      } else {
        @filter(%f, %rest)
      }
    },
    Nil => Nil,
  }
}

/*
 * Combines two lists into a list of tuples of their elements.
 *
 * The zipped list will be the length of the shorter list.
 */
def @zip[A, B](%xs: List[A], %ys: List[B]) -> List[(A, B)] {
  match (%xs, %ys) {
    (Cons(%x, %x_rest), Cons(%y, %y_rest)) => Cons((%x, %y), @zip(%x_rest, %y_rest)),
    _ => Nil,
  }
}

/*
 * Reverses a list.
 */
def @rev[A](%xs: List[A]) -> List[A] {
  let %updater = fn(%xss: List[A], %x: A) -> List[A] {
    Cons(%x, %xss)
  };
  @foldl(%updater, Nil, %xs)
  // TODO(weberlo): write it like below, once VM constructor compilation is fixed
  // @foldl(@flip(Cons), Nil, %xs)
}

/*
 * An accumulative map, which is a fold that simulataneously updates an
 * accumulator value and a list of results.
 *
 * This map proceeds through the list from right to left.
 */
def @map_accumr[A, B, C](%f: fn(A, B) -> (A, C), %init: A, %xs: List[B]) -> (A, List[C]) {
  let %updater = fn(%x: B, %acc: (A, List[C])) -> (A, List[C]) {
    let %f_out = %f(%acc.0, %x);
    (%f_out.0, Cons(%f_out.1, %acc.1))
  };
  @foldr(%updater, (%init, Nil), %xs)
}

/*
 * an accumulative map, which is a fold that simulataneously updates an
 * accumulator value and a list of results.
 *
 * This map proceeds through the list from left to right.
 */
def @map_accuml[A, B, C](%f: fn(A, B) -> (A, C), %init: A, %xs: List[B]) -> (A, List[C]) {
  let %updater = fn(%acc: (A, List[C]), %x: B) -> (A, List[C]) {
    let %f_out = %f(%acc.0, %x);
    (%f_out.0, Cons(%f_out.1, %acc.1))
  };
  @foldl(%updater, (%init, Nil), %xs)
}

/*
 * An optional ADT, which can either contain some other type or nothing at all.
 */
type Option[A] {
  Some(A),
  None,
}

/*
 * Builds up a list starting from a seed value.
 *
 * `f` returns an option containing a new seed and an output value. `f` will
 * continue to be called on the new seeds until it returns `None`. All the output
 * values will be combined into a list, right to left.
 */
def @unfoldr[A, B](%f: fn(A) -> Option[(A, B)], %seed: A) -> List[B] {
  match (%f(%seed)) {
    Some(%val) => Cons(%val.1, @unfoldr(%f, %val.0)),
    None => Nil,
  }
}

/*
 * Builds up a list starting from a seed value.
 *
 * `f` returns an option containing a new seed and an output value. `f` will
 * continue to be called on the new seeds until it returns `None`. All the
 * output values will be combined into a list, left to right.
 */
def @unfoldl[A, B](%f: fn(A) -> Option[(A, B)], %seed: A) -> List[B] {
  @rev(@unfoldr(%f, %seed))
}

/*
 * A tree ADT. A tree can contain any type. It has only one
 * constructor, rose(x, l), where x is the content of that point of the tree
 * and l is a list of more trees of the same type. A leaf is thus rose(x,
 * nil()).
 */
type Tree[A] {
  Rose(A, List[Tree[A]]),
}

/*
 * Maps over a tree. The function is applied to each subtree's contents.
 */
def @tmap[A, B](%f: fn(A) -> B, %t: Tree[A]) -> Tree[B] {
  match(%t) {
    Rose(%v, %sub_trees) => {
      let %list_f = fn(%tt: Tree[A]) -> Tree[B] {
        @tmap(%f, %tt)
      };
      Rose(%f(%v), @map(%list_f, %sub_trees))
    },
  }
}

/*
 * Computes the size of a tree.
 */
def @size[A](%t: Tree[A]) -> Tensor[(), int32] {
  match(%t) {
    Rose(_, %sub_trees) => {
      1 + @sum(@map(@size, %sub_trees))
    },
  }
}

/*
 * Takes a number n and a function f; returns a closure that takes an argument
 * and applies f n times to its argument.
 */
def @iterate[A](%f: fn(A) -> A, %n: Tensor[(), int32]) -> (fn(A) -> A) {
  if (%n == 0) {
    @id
  } else {
    @compose(%f, @iterate(%f, %n - 1))
  }
316
}