Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1.  
  2. (defn- uvar?
  3. [v]
  4. (and (symbol? v) (string/has-prefix? "?" v)))
  5.  
  6. (defn- has [ds k] (not (nil? (in ds k))))
  7.  
  8. (var unify2 nil)
  9.  
  10. (defn- occurs-check
  11. [v term subst]
  12. (cond
  13. (= v term)
  14. true
  15. (and (uvar? term) (has subst term))
  16. (occurs-check v (subst term) subst)
  17. (or (tuple? term) (struct? term) (table? term) (array? term))
  18. (loop [k :keys term]
  19. (occurs-check v (term k) subst))
  20. false))
  21.  
  22. (defn- unify-var
  23. [v x subst]
  24. (cond
  25. (has subst v)
  26. (unify2 (get subst v) x subst)
  27. (and (uvar? x) (has subst v))
  28. (unify2 v (subst v))
  29. (occurs-check v x subst)
  30. nil
  31. (table/to-struct (merge subst {v x}))))
  32.  
  33. (varfn unify2
  34. [x y subst]
  35. (var subst subst)
  36. (cond
  37. (nil? subst)
  38. nil
  39. (deep= x y)
  40. subst
  41. (uvar? x)
  42. (unify-var x y subst)
  43. (uvar? y)
  44. (unify-var y x subst)
  45. (or (and (tuple? x) (tuple? y))
  46. (and (struct? x) (struct? y))
  47. (and (table? x) (table? y))
  48. (and (array? x) (array? y)))
  49. (when (= (length x) (length y))
  50. (do
  51. (loop [k :keys x :when subst]
  52. (set subst (unify2 (x k) (y k) subst)))
  53. subst))
  54. nil))
  55.  
  56. (defn unify
  57. [x y &opt subst]
  58. (default subst {})
  59. (unify2 x y subst))
  60.  
  61. (pp (unify '[?a ?a] '[?b 2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement