I am doing an assignment for my class on ProofPad which is a web-based IDE for ACL2.
I have already been given two functions:
;;; BEGIN boilerplate code -- ignore :-)
(in-package "ACL2")
(include-book "testing" :dir :teachpacks)
(include-book "doublecheck" :dir :teachpacks)
(include-book "arithmetic-5/top" :dir :system)
;;; END boilerplate code
(defun tree-max (tree)
(if (consp tree)
(if (consp (third tree))
(if (consp (fourth tree))
(max-<< (first tree)
(max-<< (tree-max (third tree))
(tree-max (fourth tree))))
(max-<< (first tree)
(tree-max (third tree))))
(if (consp (fourth tree))
(max-<< (first tree)
(tree-max (fourth tree)))
(first tree)))
'MIN-SYMBOL))
;;; This function finds the minimum key in a tree
(defun tree-min (tree)
(if (consp tree)
(if (consp (third tree))
(if (consp (fourth tree))
(min-<< (first tree)
(min-<< (tree-min (third tree))
(tree-min (fourth tree))))
(min-<< (first tree)
(tree-min (third tree))))
(if (consp (fourth tree))
(min-<< (first tree)
(tree-min (fourth tree)))
(first tree)))
'MAX-SYMBOL)
)
The conditions are these:
;;; This function checks whether the tree is a valid search tree
;;; The conditions are that:
;;; 1. The tree is NIL, OR
;;; 2.1. The tree is a list with 4 elements (key value left right), AND
;;; 2.2. The key is a valid symbol, AND
;;; 2.3. The value is a rational number, AND
;;; 2.4. The left tree is NIL, OR the max of the left tree is << the key, AND
;;; 2.5. The right tree is NIL, OR the key is << the min of the right, AND
;;; 2.6. The left tree is a valid search tree, AND
;;; 2.7. The right tree is a valid search tree
This is what I have so far:
;;; TODO-5: Implement this function using the strategy above.
(defun search-treep (tree)
(if (consp tree) ;;if list or tree is non-0
(if (= (LEN '(tree)) 4)
(if (symbolp (first tree)) ;;if the first item in list or "key" is a symbol
(if(rationalp (second tree)) ;;if the second item for value is a number
(if(AND ;;if both
(OR (= (third tree) nil) (tree-max (third tree))) ;;The left tree is NIL, OR the max of
(OR (= (fourth tree) nil) (tree-min(fourth tree)))) ;;The right tree is NIL, OR the key
(if (search-treep (third tree)) ;he left tree is a valid search tree
(if (search-treep (fourth tree)) t ;The right tree is a valid search tree
nil)
nil)
nil)
nil)
nil)
nil)
nil))
I was expecting:
(check-expect (search-treep '(x 1 nil (z 3 (y 2 nil nil) nil))) t) - this one to be true
(check-expect (search-treep '(y 1 (z 3 (x 2 nil nil) nil) nil)) nil) - this one to be nil
However both are Nil