bns.fun 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #import std
  2. #import nat
  3. #comment -[
  4. This module contains some fast operations on subsets of a finite
  5. universe represented as the type %bN of boolean a-trees. E.g., a
  6. universe {`a,`b,`c,`d} could have a subset {`b,`d} which would be
  7. represented by the tree [2:1: true,2:3: true].
  8. Copyright (C) 2007 Dennis Furey]-
  9. ---------------------------------------------------- conversion functions --------------------------------------------------
  10. #library+
  11. #optimize+
  12. representation = union:-0++ *+ -$^/~& addresses # inverse semantics
  13. rtree = ~&iNH+ :=^|(~&:-0,:-0! ^)+ ^\!* addresses # takes a list to an a-tree
  14. semantics = # takes a universe to a function that converts a %bN tree to a subset of the universe in the usual representation
  15. ~&iNX+rtree; (; \/~&H) ; ~&?\0!! ~+ --<0,0>+ ~&iNX; ~&al^?(~&ar?/~&WliNXSPrNiXSPXlrT ~&falPRiNXS,~&ar?\~&aNC ~&farPRNiXS)
  16. leaves = # takes a non-full a-tree to a list
  17. ~&iNC; ^= ||~& ~&YgiB+ ~&a^& ~&at?\~&ahlrlrNCClNCQrNCQ ~&ahl?\~&ahr2fatPRCtiBP ~&ahr?/~&ahl2ahr2fatPRCCttPiB ~&ahl2fatPRCtiB
  18. ----------------------------------------------------- binary operators -----------------------------------------------------
  19. union = ~&alParPfabbIPWalPQarPq
  20. subtraction = ~&alParPfabbIPWlrYiBPalPQNq
  21. contains = ~&alParPfabbIPWlrBPNNXQarZPq
  22. intersecting = ~&alrEPNNXalParPfabbIPWlrYPNQNQq # returns a boolean
  23. intersection = ~&alrEPalPalParPfabbIPWlrYiBPNQNQq # returns a set
  24. ----------------------------------------------- curried binary operators ---------------------------------------------------
  25. (# These work like the corresponding binary operators but with one of the arguments fixed in advance (i.e., being invoked as
  26. (f x) y instead of f(x,y)). If the same set is to be combined with many others, it's usually much quicker to use one of
  27. these. #)
  28. contains_element = ~&al^?(~&i&&+ ~&l;+ ~&falPR,~&ar?\~&! ~&i&&+ ~&r;+ ~&farPR)
  29. contains_set = # also works on a single element
  30. ~&/&; ((&!,0!)?=r/~&l ?)^: ^/~+~&al ~&\0!+ ~&arl?(
  31. ~&arr?((?^/~&l ~&\0!+ ~&r)^W/~&f ^(^\~&arl dot_l+ ~&al,^\~&arr dot_r+ ~&al),^R/~&f ^\~&arl dot_l+ ~&al),
  32. ~&arr?\&!! ^R/~&f ^\~&arr dot_r+ ~&al)
  33. union_with = # takes s to a function equivalent to union/s but faster
  34. ~&/&; ==?r(~&rl,?)^: ^/~+~&al ^\!+~&ar ~&arl?(
  35. ~&arr?(
  36. (both~&rZllZPB?\^ !+ ~&blr)^W/~&f ^(^\~&arl dot_l+ ~&al,^\~&arr dot_r+ ~&al),
  37. ^^(^R/~&f ^\~&arl dot_l+ ~&al,~+ dot_r+ ~&al)),
  38. ~&arr?\&!! ^^(~+ dot_l+ ~&al,^R/~&f ^\~&arr dot_r+ ~&al))
  39. subtractor_of = # takes a tree or a single address
  40. ~&/&; ==?r(~&rl,?)^: ^/~+~&al ~&\0!+ ~&arl?(
  41. ~&arr?(
  42. (0!?=l(0!?r/0!! ^riB,0!?=r/^liB ^YiB))^W/~&f ^(^\~&arl dot_l+ ~&al,^\~&arr dot_r+ ~&al),
  43. (0!?=l/^riB ^YiB)^\~+dot_r+~&al ^R/~&f ^\~&arl dot_l+ ~&al),
  44. ~&arr?\0!! (0!?=r/^liB ^YiB)^/~+dot_l+~&al ^R/~&f ^\~&arr dot_r+ ~&al)
  45. remover_of = # takes a single address
  46. ~&/&; ==?r(~&rl,?)^: ^/~+~&al ~&\0!+ ~&arl?(
  47. (0!?=l/^riB ^YiB)^\~+dot_r+~&al ^R/~&f ^\~&arl dot_l+ ~&al,
  48. ~&arr?\0!! (0!?=r/^liB ^YiB)^/~+dot_l+~&al ^R/~&f ^\~&arr dot_r+ ~&al)
  49. intersection_with =
  50. ~&/&; ==?lrlPX(~&l,?)^: ^/~+~&al ~&\0!+ ~&arl?(
  51. ~&arr?(
  52. (0!?=l(0!?r/0!! ^riB,0!?=r/^liB ^YiB))^W/~&f ^(^\~&arl dot_l+ ~&al,^\~&arr dot_r+ ~&al),
  53. (0!?=l/~&r ^liB)^\0!! ^R/~&f ^\~&arl dot_l+ ~&al),
  54. ~&arr?\~+~&al (0!?=r/~&l ^riB)^/0!! ^R/~&f ^\~&arr dot_r+ ~&al)
  55. --------------------------------------------------- other functions --------------------------------------------------------
  56. dot_l = ~&alPfalPRNXarPNfarPRXNNXNXQq # equivalent to .\&l for a singly branched argument
  57. dot_r = ~&alPfalPRNXarPNfarPRXNNNXXQq # equivalent to .\&r for a singly branched argument
  58. cardinality = length+ ~&a^& ||&! --+ ~&W # takes a set in %bN form to its cardinality
  59. addresses = ~&rSxaahPNfatPRXfatPRNXQaaXq2S+ iol; zipp0^*D\~& 0!*+ ~&z # takes a list to a list of addresses