Skip to content

Commit 9cad798

Browse files
committed
enable to use match-all including multi clause
2 parents 594e85a + 13bcc2a commit 9cad798

File tree

7 files changed

+30
-14
lines changed

7 files changed

+30
-14
lines changed

hs-src/Interpreter/egison.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ printHelp = do
221221
putStrLn ""
222222
putStrLn "Options to change input or output format:"
223223
putStrLn " --tsv, -T Input and output in tsv format"
224-
putStrLn " --field, -F int Specify a field type of input tsv"
224+
putStrLn " --field, -F field Specify a field type of input tsv"
225225
putStrLn " --math, -M (asciimath|latex|mathematica)"
226226
putStrLn " Output in AsciiMath, LaTeX, or Mathematica format (only for interpreter)"
227227
exitWith ExitSuccess

hs-src/Language/Egison/Core.hs

+7-3
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,10 @@ evalExpr env (IoExpr expr) = do
545545
Tuple [_, val'] -> return $ Value val'
546546
_ -> throwError $ TypeMismatch "io" io
547547

548-
evalExpr env (MatchAllExpr target matcher (pattern, expr)) = do
548+
evalExpr env (MatchAllExpr target matcher clauses) = do
549549
target <- evalExpr env target
550550
matcher <- evalExpr env matcher >>= evalMatcherWHNF
551-
result <- patternMatch env pattern target matcher
552-
mmap (flip evalExpr expr . extendEnv env) result >>= fromMList
551+
f matcher target >>= fromMList
553552
where
554553
fromMList :: MList EgisonM WHNFData -> EgisonM WHNFData
555554
fromMList MNil = return . Value $ Collection Sq.empty
@@ -558,6 +557,11 @@ evalExpr env (MatchAllExpr target matcher (pattern, expr)) = do
558557
tail <- ISubCollection <$> (liftIO . newIORef . Thunk $ m >>= fromMList)
559558
seqRef <- liftIO . newIORef $ Sq.fromList [head, tail]
560559
return . Intermediate $ ICollection $ seqRef
560+
f matcher target = do
561+
let tryMatchClause (pattern, expr) results = do
562+
result <- patternMatch env pattern target matcher
563+
mmap (flip evalExpr expr . extendEnv env) result >>= flip mappend results
564+
mfoldr tryMatchClause (return MNil) (fromList clauses)
561565

562566
evalExpr env (MatchExpr target matcher clauses) = do
563567
target <- evalExpr env target

hs-src/Language/Egison/Desugar.hs

+5-5
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ desugar (AlgebraicDataMatcherExpr patterns) = do
128128
matchingFailure :: EgisonExpr
129129
matchingFailure = CollectionExpr []
130130

131-
desugar (MatchAllLambdaExpr matcher clause) = do
131+
desugar (MatchAllLambdaExpr matcher clauses) = do
132132
name <- fresh
133-
desugar $ LambdaExpr [TensorArg name] (MatchAllExpr (VarExpr $ stringToVar name) matcher clause)
133+
desugar $ LambdaExpr [TensorArg name] (MatchAllExpr (VarExpr $ stringToVar name) matcher clauses)
134134

135135
desugar (MatchLambdaExpr matcher clauses) = do
136136
name <- fresh
@@ -304,11 +304,11 @@ desugar (MatchExpr expr0 expr1 clauses) = do
304304
clauses' <- desugarMatchClauses clauses
305305
return (MatchExpr expr0' expr1' clauses')
306306

307-
desugar (MatchAllExpr expr0 expr1 clause) = do
307+
desugar (MatchAllExpr expr0 expr1 clauses) = do
308308
expr0' <- desugar expr0
309309
expr1' <- desugar expr1
310-
clause' <- desugarMatchClause clause
311-
return $ MatchAllExpr expr0' expr1' clause'
310+
clauses' <- desugarMatchClauses clauses
311+
return $ MatchAllExpr expr0' expr1' clauses'
312312

313313
desugar (DoExpr binds expr) = do
314314
binds' <- desugarBindings binds

hs-src/Language/Egison/Parser.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,13 @@ quoteSymbolExpr :: Parser EgisonExpr
318318
quoteSymbolExpr = char '`' >> QuoteSymbolExpr <$> expr
319319

320320
matchAllExpr :: Parser EgisonExpr
321-
matchAllExpr = keywordMatchAll >> MatchAllExpr <$> expr <*> expr <*> matchClause
321+
matchAllExpr = keywordMatchAll >> MatchAllExpr <$> expr <*> expr <*> (((flip (:) []) <$> matchClause) <|> matchClauses)
322322

323323
matchExpr :: Parser EgisonExpr
324324
matchExpr = keywordMatch >> MatchExpr <$> expr <*> expr <*> matchClauses
325325

326326
matchAllLambdaExpr :: Parser EgisonExpr
327-
matchAllLambdaExpr = keywordMatchAllLambda >> MatchAllLambdaExpr <$> expr <*> matchClause
327+
matchAllLambdaExpr = keywordMatchAllLambda >> MatchAllLambdaExpr <$> expr <*> (((flip (:) []) <$> matchClause) <|> matchClauses)
328328

329329
matchLambdaExpr :: Parser EgisonExpr
330330
matchLambdaExpr = keywordMatchLambda >> MatchLambdaExpr <$> expr <*> matchClauses

hs-src/Language/Egison/ParserNonS.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ quoteSymbolExpr :: Parser EgisonExpr
318318
quoteSymbolExpr = char '`' >> QuoteSymbolExpr <$> expr
319319

320320
matchAllExpr :: Parser EgisonExpr
321-
matchAllExpr = keywordMatchAll >> MatchAllExpr <$> expr <* (inSpaces $ string "as") <*> expr <*> matchClause
321+
matchAllExpr = keywordMatchAll >> MatchAllExpr <$> expr <* (inSpaces $ string "as") <*> expr <*> matchClauses
322322

323323
matchExpr :: Parser EgisonExpr
324324
matchExpr = keywordMatch >> MatchExpr <$> expr <* (inSpaces $ string "as") <*> expr <*> matchClauses

hs-src/Language/Egison/Types.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ data EgisonExpr =
251251
| WithSymbolsExpr [String] EgisonExpr
252252

253253
| MatchExpr EgisonExpr EgisonExpr [MatchClause]
254-
| MatchAllExpr EgisonExpr EgisonExpr MatchClause
254+
| MatchAllExpr EgisonExpr EgisonExpr [MatchClause]
255255
| MatchLambdaExpr EgisonExpr [MatchClause]
256-
| MatchAllLambdaExpr EgisonExpr MatchClause
256+
| MatchAllLambdaExpr EgisonExpr [MatchClause]
257257

258258
| NextMatchExpr EgisonExpr EgisonExpr [MatchClause]
259259
| NextMatchAllExpr EgisonExpr EgisonExpr MatchClause

test/syntax.egi

+12
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@
110110
[<cons $x $xs> [x xs]])
111111
{[1 {2 3}]})
112112

113+
(assert-equal "match-all-multi"
114+
(match-all {1 2 3} (multiset integer)
115+
{[<cons $x <cons ,(+ x 1) _>> [x (+ x 1)]]
116+
[<cons $x <cons ,(+ x 2) _>> [x (+ x 2)]]})
117+
{[1 2] [2 3] [1 3]})
118+
113119
(assert-equal "match-lambda"
114120
(letrec {[$count (match-lambda (list something)
115121
{[<nil> 0]
@@ -121,6 +127,12 @@
121127
((match-all-lambda (list something) [<join _ <cons $x _>> x]) {1 2 3})
122128
{1 2 3})
123129

130+
(assert-equal "match-all-lambda-multi"
131+
((match-all-lambda (multiset something)
132+
{[<cons $x <cons ,(+ x 1) _>> [x (+ x 1)]]
133+
[<cons $x <cons ,(+ x 2) _>> [x (+ x 2)]]}) {1 2 3})
134+
{[1 2] [2 3] [1 3]})
135+
124136
(assert-equal "pattern variable"
125137
(match 1 something
126138
{[$x x]})

0 commit comments

Comments
 (0)