I have the following defined:
(struct type (parent dirty) #:mutable #:transparent)
(define types (make-hash))
(define (add-key predicate parent)
(begin
(hash-ref! types parent (type empty #t)) ;;if the parent doesn't exist, is created with no parent.
(let([node (hash-ref types predicate #f)])
(if(or (boolean? node) ;;the node is not on the list
(not(equal? (type-parent node) parent))) ;;the node has a different parent
(hash-set! types predicate (type parent #t))
(printf "nothing to do\n")
))))
(define (ancestor? predicate1 predicate2)
(let ([node (hash-ref types predicate2 #f)])
(cond [(false? node)(error "following predicate is not in types: " predicate2)]
[(empty? (type-parent node)) #f]
[(equal? (type-parent node) predicate1) #t]
[else (ancestor? predicate1 (type-parent node))])))
It seems to work great, and I can do stuff like:
> (ancestor? integer? even?)
#t
> (ancestor? list? even?)
#f
> (ancestor? integer? odd?)
#t
>
I only seem to have an issue with sort
as (sort '(integer? odd? number? list? even?) ancestor?)
throws the following error: following predicate is not in types: integer?
which is, of course, defined in my implementation. The thing is that I am sure that the key-value pair exists, i can manipulate it, i can manually run every line of code of ancestor
... I'am really puzzled to what could be causing this... Any idea?