summaryrefslogtreecommitdiff
path: root/test/mono/castrequnion.sail
blob: 1eefb2ad3b070a39440c08e57873335763703369 (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
default Order dec
$include <prelude.sail>

val bitvector_cast_in = "zeroExtend" : forall 'n. bits('n) -> bits('n) effect pure
val bitvector_cast_out = "zeroExtend" : forall 'n. bits('n) -> bits('n) effect pure

val foo : forall 'm 'n, 'm in {8,16} & 'n in {16,32,64}. (implicit('n), bits('m)) -> option(bits('n)) effect pure

function foo(n, x) =
  let y : bits(16) = sail_zero_extend(x,16) in
  match 'n {
    16 => None(),
    32 => Some(y@y),
    64 => let z = y@y@y@y in let dfsf = 4 in Some(z)
  }

union Result ('a : Type) = {
  Value : ('a, int),
  Complaint : string
}

/* Getting ahead of myself: the 2*'n isn't supported yet, although shouldn't it end up in the form below?
*/
val bar : forall 'n, 'n in {8,16,32}. bits('n) -> Result(bits(2*'n))

function bar(x) =
  match 'n {
    8 => Complaint("No bytes"),
   16 => Value(x@x, unsigned(x)),
   32 => Value(sail_sign_extend(x,64), unsigned(x))
  }
/*
val bar : forall 'n 'm, 'n in {8,16,32} & 'm == 2*'n. bits('n) -> Result(bits('m))

function bar(x) =
  match 'n {
    8 => Complaint("No bytes"),
   16 => Value(x@x, unsigned(x)),
   32 => Value(sail_sign_extend(x,64), unsigned(x))
  }
*/

val cmp : forall 'n. (option(bits('n)), option(bits('n))) -> bool

function cmp (None(),None()) = true
and cmp (None(),Some(_)) = false
and cmp (Some(_),None()) = false
and cmp (Some(x),Some(y)) = x == y

overload operator == = {cmp}

val run : unit -> unit effect {escape}

function run() = {
  assert((foo(0x12) : option(bits(16))) == None());
  assert((foo(0x12) : option(bits(32))) == Some(0x00120012));
  assert((foo(0x12) : option(bits(64))) == Some(0x0012001200120012));
}