core.match

quoted symbols should be treated as literals

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Reported on the Clojure mailing list:

From: Alex Miller <alex@puredanger.com>
Subject: Symbol matching in core.match
Date: November 30, 2011 2:49:43 PM EST
To: Clojure <clojure@googlegroups.com>
Reply-To: clojure@googlegroups.com

I've been working with core.match some this week and finding it pretty
nice. However, I have a common case (for me) that is not well handled
right now via core.match: matching symbols. Say that I wrote a match
like this:

;; translate (+ x (+ y z)) to (+ x y z)
(let [e '(+ 1 (+ 2 3))]
(match [e]
[([+ x ([+ y z] :seq)] :seq)] (+ x y z)))

You will see this error:
Pattern row 1: Pattern row reuses wildcards in [([+ x ([+ y
z] :seq)] :seq)]. The following wildcards are ambiguous: +. There's
no guarantee that the matched values will be same. Rename the
occurrences uniquely.

Any symbol inside a pattern row is treated as a bind variable. + is a
symbol. You can achieve this with guards:

(defn +? [s] (= '+ s))

(let [e '(+ 1 (+ 2 3))]
(match ['(+ 1 (+ 2 3))]
[([(_o1 :when +?) x ([(_o2 :when +?) y z] :seq)] :seq)] (list
'+ x y z)))

but, yuck. I can imagine using the reserved ()'s with additional keys
(:symbol or :sym) to do symbol matching like (:symbol +) but also,
yuck. The simplest idea I came up with was:

(let [e '(+ 1 (+ 2 3))]
(match [e]
[(['+ x (['+ y z] :seq)] :seq)] ('+ x y z)))

These come through as (quote x) although the error reporting goes a
little off the rails:
Pattern row 1: Pattern row reuses wildcards in [([(quote +) x ([(quote
+) y z] :seq)] :seq)]. The following wildcards are ambiguous: quote.
There's no guarantee that the matched values will be same. Rename the
occurrences uniquely.

However, that seems fixable and you could then use (quote x) as a
signal to do symbol matching. If I can figure out what the hell I'm
doing in core.match then I'd be happy to work on a patch.

Activity

Steve Miner made changes -
Field Original Value New Value
Attachment match-42-quoted-symbol.patch [ 10728 ]
Steve Miner made changes -
Status Open [ 1 ] In Progress [ 3 ]
Steve Miner made changes -
Assignee Steve Miner [ steveminer@gmail.com ] David Nolen [ dnolen ]
David Nolen made changes -
Resolution Completed [ 1 ]
Status In Progress [ 3 ] Resolved [ 5 ]
David Nolen made changes -
Status Resolved [ 5 ] Closed [ 6 ]

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: