Skip to content

Commit 5dc6deb

Browse files
committed
fix the bug of tuple pattern matching
1 parent 7b3094b commit 5dc6deb

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

hs-src/Language/Egison/Core.hs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1293,11 +1293,20 @@ processMState' (MState mode env loops bindings ((MAtom pattern target matcher):t
12931293
_ ->
12941294
case matcher of
12951295
UserMatcher _ _ _ -> do
1296-
(patterns, targetss, matchers) <- inductiveMatch env' pattern target matcher
1297-
mfor targetss $ \ref -> do
1298-
targets <- evalRef ref >>= fromTupleWHNF
1299-
let trees' = zipWith3 MAtom patterns targets matchers ++ trees
1300-
return $ MState mode env loops bindings trees'
1296+
case pattern of
1297+
_ -> do
1298+
(patterns, targetss, matchers) <- inductiveMatch env' pattern target matcher
1299+
case (length patterns, length matchers) of
1300+
(1,1) -> do
1301+
mfor targetss $ \ref -> do
1302+
targets <- evalRef ref >>= (\x -> return [x])
1303+
let trees' = zipWith3 MAtom patterns targets matchers ++ trees
1304+
return $ MState mode env loops bindings trees'
1305+
_ -> do
1306+
mfor targetss $ \ref -> do
1307+
targets <- evalRef ref >>= fromTupleWHNF
1308+
let trees' = zipWith3 MAtom patterns targets matchers ++ trees
1309+
return $ MState mode env loops bindings trees'
13011310

13021311
Tuple matchers ->
13031312
case pattern of

sample/n-queen.egi

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
<nil>)>
5656
a])))
5757

58+
(test (n-queen 4))
59+
(test (n-queen 5))
60+
(test (n-queen 6))
61+
(test (n-queen 7))
5862
(test (n-queen 8))
5963
(test (n-queen 9))
6064
(test (n-queen 10))

sample/rosetta/lcs.egi

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1-
(define $common-seqs
2-
(lambda [$xs $ys]
3-
(match-all [xs ys] [(list char) (list char)]
4-
[[(loop $i [1 $n] <join _ <cons $c_i ...>> _)
5-
(loop $i [1 n] <join _ <cons ,c_i ...>> _)]
6-
(map (lambda [$i] c_i) (between 1 n))])))
1+
(define $double-list
2+
(lambda [$a]
3+
(matcher
4+
{
5+
[<cons $ $> [[a a] (double-list a)]
6+
{[[$xs $ys] (match-all [xs ys] [(list a) (list a)]
7+
[[<cons $x $rs1> <cons $y $rs2>]
8+
[[x y] [rs1 rs2]]])]}]
9+
[<join $ $> [[(list a) (list a)] (double-list a)]
10+
{[[$xs $ys] (match-all [xs ys] [(list a) (list a)]
11+
[[<join $hs1 $ts1> <join $hs2 $ts2>]
12+
[[hs1 hs2] [ts1 ts2]]])]}]
13+
[<c-cons $ $> [a (double-list a)]
14+
{[[$xs $ys] (match-all [xs ys] [(list a) (list a)]
15+
[[<cons $x $rs1> <cons ,x $rs2>]
16+
[x [rs1 rs2]]])]}]
17+
[$ something
18+
{[$tgt {tgt}]}]
19+
})))
720

8-
(define $lcs (compose common-seqs rac))
21+
(define $lcs
22+
(lambda [$xs $ys]
23+
(match-all [(unpack "thisisatest") (unpack "testing123testing")] (double-list char)
24+
[(loop $i [1 $n] <join _ <c-cons $c_i ...>> _)
25+
[n (pack (map (lambda [$i] c_i) (between 1 n)))]])))
926

10-
(test (lcs "thisisatest" "testing123testing"))
27+
(sort/fn 2#(compare (2#%1 %1) (2#%1 %2)) (lcs "thisisatest" "testing123testing"))

0 commit comments

Comments
 (0)