Hi there,
I have a part of my code that doesn’t type-check and that I cannot
understand
why it doesn’t type-check. I would appreciate it if you could help me find
the
problem:
Here are the datatype definitions:
datatype Answer(int, int) =
| True(0, 0) of ()
| False(0, 1) of ()
| Unknown(0, 2) of ()
| {x:nat} {k:nat} Decision(x+1, k) of
[x_l:nat | x_l <= x] [k_l:nat]
[x_r:nat | x_r <= x+1] [k_r:nat | (k_r > k && x_r == x+1) || (x_r <= x
)]
(int(x), int(k), Answer(x_l, k_l), Answer(x_r, k_r))
// if x_n <= v then Answer(n_l, v_l) else Answer(n_r, v_r)
typedef Answer0 = [n:int] [v:int] Answer(n, v)
typedef NodeKey = (int, int, Answer0, Answer0) // Variable + Value +
LEqAnswer + GTAnswer
And here is the code that that doesn’t type-check:
fun GetNode
{x:nat} {k:nat} {x_l:nat | x_l <= x} {k_l:nat}
{x_r:nat | x_r <= x + 1} {k_r:nat | (x_r <= x) || ((x_r == x + 1) &&k_r
k)}
(x:int(x), k:int(k),
lte_answer:Answer(x_l, k_l), gt_answer:Answer(x_r, k_r),
nodes: &map(NodeKey, Answer0)) : Answer0 =
let
val key = (x, k, lte_answer, gt_answer)
val cachedNode = funmap_search_opt(nodes, key)
in
case+ cachedNode of
| ~Some_vt(node) => node
| ~None_vt() =>
let
val new_node = Decision(x, k, lte_answer, gt_answer)
in
nodes.insert(key, new_node) ; new_node
end
end
The position of error is on the line “val new_node = Decision(…)” where
it essentially says that Decision(…) cannot be type-checked. However,
given the conditions on x, k, x_l, k_l, x_r and k_r guaranteed by function
GetNode, I cannot see why there is an error.
Please let me know what I have missed
Thanks,
Shahab