Fork me on GitHub
#meander
<
2022-06-11
>
Richie00:06:44

Why can’t I use & for sets?

(m/rewrite #{:one :two :three}
  #{:one & ?rest} ?rest)
gives
No method in multimethod 'ground?' for dispatch value: :amp

Richie00:06:37

I expect it to only match if :one exists and then bind the other values except :one to ?rest.

noprompt01:06:06

@UPD88PGNT You can use & for sets but it is limited because it hackily use meta.

#{1 2 ^& ?rest}

noprompt01:06:19

This is because, for sets, the elements will end up unordered so there is obvious way to know what the pretzel should apply to.

Richie01:06:57

If I can just remember this then I’ll be happy enough.

(defn step
  [[left right]]
  (m/rewrites [left right]
    [(m/and #{:you}
            (m/app #(disj % :you)
                   (m/app vec [!a ... ?x . !b ...]))) ?right]
    [[!a ... !b ...] [?x :you & ?right]]

    [?left
     (m/and #{:you}
            (m/app #(disj % :you)
                   (m/app vec [!a ... ?x . !b ...])))]
    [[?x :you & ?left] [!a ... !b ...]]))

(defn step
  [[left right]]
  (m/rewrites [left right]
    [#{:you ^& (m/app vec [!a ... ?x . !b ...])} ?right]
    [[!a ... !b ...] [?x :you & ?right]]

    [?left #{:you ^& (m/app vec [!a ... ?x . !b ...])}]
    [[?x :you & ?left] [!a ... !b ...]]))

Richie01:06:31

It is much shorter.

noprompt01:06:55

A solution to this problem would be to simply have a non-literal syntax (as is slowly being worked out on zeta but could be added to epsilon):

(m.set/union (m/hash-set p1 p2 p3) ?rest)

noprompt01:06:35

Ah, glad it worked out for you! 😅

Richie01:06:25

(defn step
  [[left right]]
  (m/rewrites [left right]
    [#{:you ^& (m/seqable !a ... ?x . !b ...)} ?right]
    [#{^& [!a ... !b ...]} #{?x :you ^& ?right}]

    [?left #{:you ^& (m/seqable !a ... ?x . !b ...)}]
    [#{?x :you ^& ?left} #{^& [!a ... !b ...]}]))
(step [#{:fox :corn :you} #{:goose}])
;; ([#{:corn} #{:you :fox :goose}] [#{:fox} #{:you :goose :corn}])

Richie02:06:54

Oh right, it actually gets cleaner than that.

(defn step
  [[left right]]
  (m/rewrites [left right]

    [#{:you ^& ?left} ?right]
    [?left #{:you ^& ?right}]

    [#{:you ?x ^& ?left} ?right]
    [?left #{?x :you ^& ?right}]

    [?left #{:you ^& ?right}]
    [#{:you ^& ?left} ?right]

    [?left #{:you ?x ^& ?right}]
    [#{?x :you ^& ?left} ?right]))

(step [#{:you :corn :fox} #{:goose}])
;; ([#{:fox :corn} #{:you :goose}] [#{:corn} #{:you :fox :goose}] [#{:fox} #{:you :goose :corn}])