<< Back to previous view

[MATCH-42] quoted symbols should be treated as literals Created: 30/Nov/11  Updated: 28/Jul/13  Resolved: 30/Nov/11

Status: Closed
Project: core.match
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Major
Reporter: Steve Miner Assignee: David Nolen
Resolution: Completed Votes: 0
Labels: None

Attachments: Text File match-42-quoted-symbol.patch    

 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.



 Comments   
Comment by Steve Miner [ 30/Nov/11 4:56 PM ]

Skip anything that's quoted when looking for duplicate symbol names. Added a test for the reported case.

Comment by Steve Miner [ 30/Nov/11 4:57 PM ]

patch attached

Comment by David Nolen [ 30/Nov/11 6:52 PM ]

Fixed, https://github.com/clojure/core.match/commit/6721be4fba74561038539e12667bc04cc5fc94cc

Generated at Mon Sep 22 17:28:31 CDT 2014 using JIRA 4.4#649-r158309.