So, I have been trying to encode the property of being a Binary Search Tree (instead of just a binary tree) in a new data type in Idris.
It seems to work reasonably well if just the data type is used (e.g. in the repl) to create new trees.
This is the data type as of now, where it receives a type and a Maybe of that type, that is basically the maximum value of this tree. The functions isLMaybe and isGMaybe are used to check whether the values of the subtrees are greater or lesser than the root value.
isLMaybe: Ord a => Maybe a -> a -> Bool
isLMaybe Nothing _ = True
isLMaybe (Just x) y = x < y
isGMaybe: Ord a => Maybe a -> a -> Bool
isGMaybe Nothing _ = True
isGMaybe (Just x) y = x > y
data BST: (x: Type) -> Maybe x -> Type where
EmptyBST: Ord a => BST a Nothing
NodeBST: Ord a => (l: BST a b) -> (v: a) -> (r: BST a c) -> {auto leftValues: So (isLMaybe b v)} -> {auto rightValues: So (isGMaybe c v)} -> BST a (Just v)
The problem is, once I try to create an insert function, like the one below, it throws the following error:
insertTest : (a: Type, c: Maybe a, d: Maybe a) => (x: a) -> BST a c -> BST a d
insertTest x EmptyBST = NodeBST EmptyBST x EmptyBST
insertTest x (NodeBST l v r) = case lessEqualGreater x v of
L => NodeBST (insertTest x l) v r
E => NodeBST l v r
G => NodeBST l v (insertTest x r)
N => NodeBST l v r
When checking right hand side of insertTest with expected type
BST a d
Type mismatch between
BST a (Just x) (Type of NodeBST EmptyBST x EmptyBST)
and
BST a d (Expected type)
Specifically:
Type mismatch between
Just x
and
d
I can not understand why it thinks d
is not an instance of Maybe a
, since after I got the error, I tried to explicitly put it in the function, yet I still get the same error.