Skip to content

Commit 15108e1

Browse files
committed
---
yaml --- r: 4939 b: refs/heads/master c: 809a833 h: refs/heads/master i: 4937: e8d7890 4935: 8fed9ee v: v3
1 parent dfd2fff commit 15108e1

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: f841e894437843c142ebd7e0b0a18ed00ed52457
2+
refs/heads/master: 809a833e34176534886abf326cefaad4c6577099
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// xfail-stage0
2+
// Uses foldl to exhibit the unchecked block syntax.
3+
use std;
4+
5+
import std::list::*;
6+
7+
// Can't easily be written as a "pure fn" because there's
8+
// no syntax for specifying that f is pure.
9+
fn pure_foldl<@T, @U>(ls: &list<T>, u: &U, f: &block(&T, &U) -> U) -> U {
10+
alt ls {
11+
nil. { u }
12+
cons(hd, tl) { f(hd, pure_foldl(*tl, f(hd, u), f)) }
13+
}
14+
}
15+
16+
// Shows how to use an "unchecked" block to call a general
17+
// fn from a pure fn
18+
pure fn pure_length<@T>(ls: &list<T>) -> uint {
19+
fn count<T>(_t: &T, u: &uint) -> uint { u + 1u }
20+
unchecked {
21+
pure_foldl(ls, 0u, count)
22+
}
23+
}
24+
25+
pure fn nonempty_list<@T>(ls: &list<T>) -> bool {
26+
pure_length(ls) > 0u
27+
}
28+
29+
// Of course, the compiler can't take advantage of the
30+
// knowledge that ls is a cons node. Future work.
31+
// Also, this is pretty contrived since nonempty_list
32+
// could be a "tag refinement", if we implement those.
33+
fn safe_head<@T>(ls: &list<T>) : nonempty_list(ls) -> T { car(ls) }
34+
35+
fn main() {
36+
let mylist = cons(@1u, @nil);
37+
// Again, a way to eliminate such "obvious" checks seems
38+
// desirable. (Tags could have postconditions.)
39+
check(nonempty_list(mylist));
40+
assert (*(safe_head(mylist)) == 1u);
41+
}

0 commit comments

Comments
 (0)