From 635845eed8acf476621bd0d01a85146fb19693a6 Mon Sep 17 00:00:00 2001 From: bringert Date: Thu, 1 Dec 2005 15:37:47 +0000 Subject: Transfer: added support for disjunctive patterns. --- src/Transfer/SyntaxToCore.hs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/Transfer/SyntaxToCore.hs') diff --git a/src/Transfer/SyntaxToCore.hs b/src/Transfer/SyntaxToCore.hs index f3e7f828d..586160ebe 100644 --- a/src/Transfer/SyntaxToCore.hs +++ b/src/Transfer/SyntaxToCore.hs @@ -33,6 +33,7 @@ declsToCore_ = desugar >>> deriveDecls >>> replaceCons >>> compilePattDecls + >>> expandOrPatts >>> optimize optimize :: [Decl] -> C [Decl] @@ -343,6 +344,34 @@ onlyBindsFieldToVariable _ _ = False fieldPatternVars :: Ident -> [FieldPattern] -> [Ident] fieldPatternVars f fps = [p | FieldPattern f' (PVar p) <- fps, f == f'] +-- +-- * Expand disjunctive patterns. +-- + +expandOrPatts :: [Decl] -> C [Decl] +expandOrPatts = return . map f + where + f :: Tree a -> Tree a + f x = case x of + ECase e cs -> ECase (f e) (concatMap (expandCase . f) cs) + _ -> composOp f x + +expandCase :: Case -> [Case] +expandCase (Case p e) = [ Case p' e | p' <- expandPatt p ] + +expandPatt :: Pattern -> [Pattern] +expandPatt p = case p of + POr p1 p2 -> expandPatt p1 ++ expandPatt p2 + PCons i ps -> map (PCons i) $ expandPatts ps + PRec fps -> let (fs,ps) = unzip $ fromPRec fps + fpss = map (zip fs) (expandPatts ps) + in map (PRec . toPRec) fpss + _ -> [p] + +expandPatts :: [Pattern] -> [[Pattern]] +expandPatts [] = [[]] +expandPatts (p:ps) = [ p':ps' | p' <- expandPatt p, ps' <- expandPatts ps] + -- -- * Remove simple syntactic sugar. -- @@ -549,6 +578,12 @@ isValueDecl :: Ident -> Decl -> Bool isValueDecl x (ValueDecl y _ _) = x == y isValueDecl _ _ = False +fromPRec :: [FieldPattern] -> [(Ident,Pattern)] +fromPRec fps = [ (l,p) | FieldPattern l p <- fps ] + +toPRec :: [(Ident,Pattern)] -> [FieldPattern] +toPRec = map (uncurry FieldPattern) + -- -- * Data types -- -- cgit v1.2.3