racklog icon indicating copy to clipboard operation
racklog copied to clipboard

racklog fails to unify two hash tables

Open arsdragonfly opened this issue 5 years ago • 1 comments

(%which (d) (%= (hash 1 1) (hash 1 d)))

returns '((d . 1)). However,

(%which (d) (%= (hash 1 1) (hash d 1)))

returns #f.

arsdragonfly avatar Jan 02 '21 13:01 arsdragonfly

I'll explain why it works this way and we can figure out how to change it.

Hash table unification --- https://github.com/racket/racklog/blob/master/unify.rkt#L492-L504 --- assumes that the only unifiable things are the values, not the keys. That's why the first example works and the second doesn't.

Unifying the keys is hard because if you have something like (%= (hash a b c d) (hash u v x y)) then we have to try (%or (%and (%= a u) (%= c x) ....) (%and (%= a x) (%= c u) ....). I think the correct thing to do would be implement something like:

(%= (? hash? x) (? hash? y) :=
  (try-all-permutations (hash->list x)
    (lambda (xl)
      (try-all-permutations (hash->list y)
        (lambda (yl) (%= xl yl)))))

This will be very expensive though, so maybe should only be used if a key is found to be a logic variable.

What do you think?

jeapostrophe avatar Jan 02 '21 15:01 jeapostrophe