summaryrefslogtreecommitdiff
path: root/test/mono/fnreduce.sail
blob: f39fb87d8640babf93c6bd00178e9632cdfa877b (plain)
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
(* Test constant propagation part of monomorphisation involving
   functions.  We should reduce a function application when the
   arguments are suitable values, the function is pure and the result
   is a value.
 *)

typedef AnEnum = enumerate { One; Two; Three }

typedef EnumlikeUnion = const union { First; Second }

typedef ProperUnion = const union {
  (AnEnum) Stuff;
  (EnumlikeUnion) Nonsense;
}

function AnEnum canReduce ((AnEnum) x) = {
  switch (x) {
  case One -> Two
  case x -> x
  }
}

function nat cannotReduce ((AnEnum) x) = {
  let (nat) y = switch (x) { case One -> 1 case _ -> 5 } in
  2 + y
}

function AnEnum effect {rreg} fakeUnpure ((AnEnum) x) = {
  switch (x) {
  case One -> Two
  case x -> x
  }
}

function EnumlikeUnion canReduceUnion ((AnEnum) x) = {
  switch (x) {
  case One -> First
  case _ -> Second
  }
}

function ProperUnion canReduceUnion2 ((AnEnum) x) = {
  switch (x) {
  case One -> Nonsense(First)
  case y -> Stuff(y)
  }
}

(* FIXME LATER: once effect handling is in place we should get an error
   because this isn't pure *)

val AnEnum -> (AnEnum,nat,AnEnum,EnumlikeUnion,ProperUnion) effect pure test

function test (x) = {
  let a = canReduce(x) in
  let b = cannotReduce(x) in
  let c = fakeUnpure(x) in
  let d = canReduceUnion(x) in
  let e = canReduceUnion2(x) in
  (a,b,c,d,e)
}

val unit -> bool effect pure run

function run () = {
  test(One) == (Two,3,Two,First,Nonsense(First)) &
  test(Two) == (Two,7,Two,Second,Stuff(Two)) &
  test(Three) == (Three,7,Three,Second,Stuff(Three))
}