0

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?

  • Note: there is a very cute algorithm that allows you to do queries in constant time, if you do a linear pre-processing of the tree before the queries. See: http://stackoverflow.com/questions/10310809/check-if-2-tree-nodes-are-related-ancestor-descendant-in-o1-with-pre-process – dyoo May 11 '13 at 03:14
  • Reading your question a bit more closely: when you say `(sort '(integer? odd? number? list? even?))`, do you really mean `(sort (list integer? odd? number? list? even?))`? – dyoo May 11 '13 at 03:18

1 Answers1

2
  1. I took your code as-is and dropped it in a file.

  2. Added a trace line for ancestor?: either add a first line with (displayln `(ancestor? ,predicate1 ,predicate2)) or add (trace ancestor?) (after a (require racket/trace)).

  3. This shows the offending call that is broken in your code: (ancestor? 'odd? 'integer?) leads to that exact error.

(I have no idea what your code is doing: the idea is that it's easy to derive the problem mechanically.)

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110