@@ -22,6 +22,7 @@ import qualified Data.Data as D
23import Control.Applicative
23import Control.Applicative
24import Control.Monad
24import Control.Monad
25+import Control.Arrow ((***), (+++))
26import Language.PureScript.Types
27import Language.PureScript.Types
27import Language.PureScript.Names
28import Language.PureScript.Names
@@ -154,7 +155,7 @@ data Declaration
155 -- A value declaration (name, top-level binders, optional guard, value)
156 -- A value declaration (name, top-level binders, optional guard, value)
157− | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Expr
158+ | ValueDeclaration Ident NameKind [Binder] (Either [(Guard, Expr)] Expr)
159 -- A minimal mutually recursive set of value declarations
160 -- A minimal mutually recursive set of value declarations
@@ -379,13 +380,9 @@ data CaseAlternative = CaseAlternative
380 caseAlternativeBinders :: [Binder]
381 caseAlternativeBinders :: [Binder]
382− -- An optional guard
383+ -- The result expression or a collect of guarded expressions
384− , caseAlternativeGuard :: Maybe Guard
385+ , caseAlternativeResult :: Either [(Guard, Expr)] Expr
386− -- The result expression
388− , caseAlternativeResult :: Expr
389 } deriving (Show, D.Data, D.Typeable)
386 } deriving (Show, D.Data, D.Typeable)
@@ -492,7 +489,7 @@ everywhereOnValues f g h = (f', g', h')
493 f' :: Declaration -> Declaration
490 f' :: Declaration -> Declaration
494 f' (DataBindingGroupDeclaration ds) = f (DataBindingGroupDeclaration (map f' ds))
491 f' (DataBindingGroupDeclaration ds) = f (DataBindingGroupDeclaration (map f' ds))
495− f' (ValueDeclaration name nameKind bs grd val) = f (ValueDeclaration name nameKind (map h' bs) (fmap g' grd) (g' val))
492+ f' (ValueDeclaration name nameKind bs val) = f (ValueDeclaration name nameKind (map h' bs) ((map (g' *** g') +++ g') val))
496 f' (BindingGroupDeclaration ds) = f (BindingGroupDeclaration (map (\(name, nameKind, val) -> (name, nameKind, g' val)) ds))
493 f' (BindingGroupDeclaration ds) = f (BindingGroupDeclaration (map (\(name, nameKind, val) -> (name, nameKind, g' val)) ds))
497 f' (TypeClassDeclaration name args implies ds) = f (TypeClassDeclaration name args implies (map f' ds))
494 f' (TypeClassDeclaration name args implies ds) = f (TypeClassDeclaration name args implies (map f' ds))
498 f' (TypeInstanceDeclaration name cs className args ds) = f (TypeInstanceDeclaration name cs className args (map f' ds))
495 f' (TypeInstanceDeclaration name cs className args ds) = f (TypeInstanceDeclaration name cs className args (map f' ds))
@@ -530,8 +527,7 @@ everywhereOnValues f g h = (f', g', h')
530 handleCaseAlternative :: CaseAlternative -> CaseAlternative
527 handleCaseAlternative :: CaseAlternative -> CaseAlternative
531 handleCaseAlternative ca =
528 handleCaseAlternative ca =
532 ca { caseAlternativeBinders = map h' (caseAlternativeBinders ca)
529 ca { caseAlternativeBinders = map h' (caseAlternativeBinders ca)
533− , caseAlternativeGuard = fmap g' (caseAlternativeGuard ca)
534− , caseAlternativeResult = g' (caseAlternativeResult ca)
530+ , caseAlternativeResult = (map (g' *** g') +++ g') (caseAlternativeResult ca)
537 handleDoNotationElement :: DoNotationElement -> DoNotationElement
533 handleDoNotationElement :: DoNotationElement -> DoNotationElement
@@ -549,7 +545,7 @@ everywhereOnValuesTopDownM :: (Functor m, Applicative m, Monad m) =>
549everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
545everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
551 f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f' <=< f) ds
547 f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f' <=< f) ds
552− f' (ValueDeclaration name nameKind bs grd val) = ValueDeclaration name nameKind <$> mapM (h' <=< h) bs <*> maybeM (g' <=< g) grd <*> (g val >>= g')
548+ f' (ValueDeclaration name nameKind bs val) = ValueDeclaration name nameKind <$> mapM (h' <=< h) bs <*> eitherM (mapM (pairM (g' <=< g) (g' <=< g))) (g' <=< g) val
553 f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
549 f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
554 f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f' <=< f) ds
550 f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f' <=< f) ds
555 f' (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f' <=< f) ds
551 f' (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f' <=< f) ds
@@ -582,9 +578,8 @@ everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
582 h' (PositionedBinder pos b) = PositionedBinder pos <$> (h b >>= h')
578 h' (PositionedBinder pos b) = PositionedBinder pos <$> (h b >>= h')
583 h' other = h other
579 h' other = h other
585− handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM (h' <=< h) bs
581+ handleCaseAlternative (CaseAlternative bs val) = CaseAlternative <$> mapM (h' <=< h) bs
586− <*> maybeM (g' <=< g) grd
582+ <*> eitherM (mapM (pairM (g' <=< g) (g' <=< g))) (g' <=< g) val
589 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> (g' <=< g) v
584 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> (g' <=< g) v
590 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> (h' <=< h) b <*> (g' <=< g) v
585 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> (h' <=< h) b <*> (g' <=< g) v
@@ -599,7 +594,7 @@ everywhereOnValuesM :: (Functor m, Applicative m, Monad m) =>
599everywhereOnValuesM f g h = (f' <=< f, g' <=< g, h' <=< h)
594everywhereOnValuesM f g h = (f' <=< f, g' <=< g, h' <=< h)
601 f' (DataBindingGroupDeclaration ds) = (DataBindingGroupDeclaration <$> mapM f' ds) >>= f
596 f' (DataBindingGroupDeclaration ds) = (DataBindingGroupDeclaration <$> mapM f' ds) >>= f
602− f' (ValueDeclaration name nameKind bs grd val) = (ValueDeclaration name nameKind <$> mapM h' bs <*> maybeM g' grd <*> g' val) >>= f
597+ f' (ValueDeclaration name nameKind bs val) = (ValueDeclaration name nameKind <$> mapM h' bs <*> eitherM (mapM (pairM g' g')) g' val) >>= f
603 f' (BindingGroupDeclaration ds) = (BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> g' val) ds) >>= f
598 f' (BindingGroupDeclaration ds) = (BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> g' val) ds) >>= f
604 f' (TypeClassDeclaration name args implies ds) = (TypeClassDeclaration name args implies <$> mapM f' ds) >>= f
599 f' (TypeClassDeclaration name args implies ds) = (TypeClassDeclaration name args implies <$> mapM f' ds) >>= f
605 f' (TypeInstanceDeclaration name cs className args ds) = (TypeInstanceDeclaration name cs className args <$> mapM f' ds) >>= f
600 f' (TypeInstanceDeclaration name cs className args ds) = (TypeInstanceDeclaration name cs className args <$> mapM f' ds) >>= f
@@ -632,9 +627,8 @@ everywhereOnValuesM f g h = (f' <=< f, g' <=< g, h' <=< h)
632 h' (PositionedBinder pos b) = (PositionedBinder pos <$> h' b) >>= h
627 h' (PositionedBinder pos b) = (PositionedBinder pos <$> h' b) >>= h
633 h' other = h other
628 h' other = h other
635− handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM h' bs
630+ handleCaseAlternative (CaseAlternative bs val) = CaseAlternative <$> mapM h' bs
636− <*> maybeM g' grd
631+ <*> eitherM (mapM (pairM g' g')) g' val
639 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> g' v
633 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> g' v
640 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> h' b <*> g' v
634 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> h' b <*> g' v
@@ -651,8 +645,8 @@ everythingOnValues :: (r -> r -> r) ->
651everythingOnValues (<>) f g h i j = (f', g', h', i', j')
645everythingOnValues (<>) f g h i j = (f', g', h', i', j')
653 f' d@(DataBindingGroupDeclaration ds) = foldl (<>) (f d) (map f' ds)
647 f' d@(DataBindingGroupDeclaration ds) = foldl (<>) (f d) (map f' ds)
654− f' d@(ValueDeclaration _ _ bs Nothing val) = foldl (<>) (f d) (map h' bs) <> g' val
648+ f' d@(ValueDeclaration _ _ bs (Right val)) = foldl (<>) (f d) (map h' bs) <> g' val
655− f' d@(ValueDeclaration _ _ bs (Just grd) val) = foldl (<>) (f d) (map h' bs) <> g' grd <> g' val
649+ f' d@(ValueDeclaration _ _ bs (Left gs)) = foldl (<>) (f d) (map h' bs ++ concatMap (\(grd, val) -> [g' grd, g' val]) gs)
656 f' d@(BindingGroupDeclaration ds) = foldl (<>) (f d) (map (\(_, _, val) -> g' val) ds)
650 f' d@(BindingGroupDeclaration ds) = foldl (<>) (f d) (map (\(_, _, val) -> g' val) ds)
657 f' d@(TypeClassDeclaration _ _ _ ds) = foldl (<>) (f d) (map f' ds)
651 f' d@(TypeClassDeclaration _ _ _ ds) = foldl (<>) (f d) (map f' ds)
658 f' d@(TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) (f d) (map f' ds)
652 f' d@(TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) (f d) (map f' ds)
@@ -685,9 +679,8 @@ everythingOnValues (<>) f g h i j = (f', g', h', i', j')
685 h' b@(PositionedBinder _ b1) = h b <> h' b1
679 h' b@(PositionedBinder _ b1) = h b <> h' b1
686 h' b = h b
680 h' b = h b
688− i' ca = case caseAlternativeGuard ca of
682+ i' ca@(CaseAlternative bs (Right val)) = foldl (<>) (i ca) (map h' bs) <> g' val
689− Nothing -> foldl (<>) (i ca) (map h' (caseAlternativeBinders ca)) <> g' (caseAlternativeResult ca)
683+ i' ca@(CaseAlternative bs (Left gs)) = foldl (<>) (i ca) (map h' bs ++ concatMap (\(grd, val) -> [g' grd, g' val]) gs)
690− Just grd -> foldl (<>) (i ca) (map h' (caseAlternativeBinders ca)) <> g' grd <> g' (caseAlternativeResult ca)
692 j' e@(DoNotationValue v) = j e <> g' v
685 j' e@(DoNotationValue v) = j e <> g' v
693 j' e@(DoNotationBind b v) = j e <> h' b <> g' v
686 j' e@(DoNotationBind b v) = j e <> h' b <> g' v
@@ -713,8 +706,8 @@ everythingWithContextOnValues s0 r0 (<>) f g h i j = (f'' s0, g'' s0, h'' s0, i'
713 f'' s d = let (s', r) = f s d in r <> f' s' d
706 f'' s d = let (s', r) = f s d in r <> f' s' d
715 f' s (DataBindingGroupDeclaration ds) = foldl (<>) r0 (map (f'' s) ds)
708 f' s (DataBindingGroupDeclaration ds) = foldl (<>) r0 (map (f'' s) ds)
716− f' s (ValueDeclaration _ _ bs Nothing val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) val
709+ f' s (ValueDeclaration _ _ bs (Right val)) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) val
717− f' s (ValueDeclaration _ _ bs (Just grd) val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) grd <> (g'' s) val
710+ f' s (ValueDeclaration _ _ bs (Left gs)) = foldl (<>) r0 (map (h'' s) bs ++ concatMap (\(grd, val) -> [g'' s grd, g'' s val]) gs)
718 f' s (BindingGroupDeclaration ds) = foldl (<>) r0 (map (\(_, _, val) -> (g'' s) val) ds)
711 f' s (BindingGroupDeclaration ds) = foldl (<>) r0 (map (\(_, _, val) -> (g'' s) val) ds)
719 f' s (TypeClassDeclaration _ _ _ ds) = foldl (<>) r0 (map (f'' s) ds)
712 f' s (TypeClassDeclaration _ _ _ ds) = foldl (<>) r0 (map (f'' s) ds)
720 f' s (TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) r0 (map (f'' s) ds)
713 f' s (TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) r0 (map (f'' s) ds)
@@ -753,8 +746,8 @@ everythingWithContextOnValues s0 r0 (<>) f g h i j = (f'' s0, g'' s0, h'' s0, i'
754 i'' s ca = let (s', r) = i s ca in r <> i' s' ca
747 i'' s ca = let (s', r) = i s ca in r <> i' s' ca
756− i' s (CaseAlternative bs Nothing val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) val
749+ i' s (CaseAlternative bs (Right val)) = foldl (<>) r0 (map (h'' s) bs) <> g'' s val
757− i' s (CaseAlternative bs (Just grd) val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) grd <> (g'' s) val
750+ i' s (CaseAlternative bs (Left gs)) = foldl (<>) r0 (map (h'' s) bs ++ concatMap (\(grd, val) -> [g'' s grd, g'' s val]) gs)
759 j'' s e = let (s', r) = j s e in r <> j' s' e
752 j'' s e = let (s', r) = j s e in r <> j' s' e
@@ -780,7 +773,7 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
780 f'' s = uncurry f' <=< f s
773 f'' s = uncurry f' <=< f s
782 f' s (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f'' s) ds
775 f' s (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f'' s) ds
783− f' s (ValueDeclaration name nameKind bs grd val) = ValueDeclaration name nameKind <$> mapM (h'' s) bs <*> maybeM (g'' s) grd <*> g'' s val
776+ f' s (ValueDeclaration name nameKind bs val) = ValueDeclaration name nameKind <$> mapM (h'' s) bs <*> eitherM (mapM (pairM (g'' s) (g'' s))) (g'' s) val
784 f' s (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (thirdM (g'' s)) ds
777 f' s (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (thirdM (g'' s)) ds
785 f' s (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f'' s) ds
778 f' s (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f'' s) ds
786 f' s (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f'' s) ds
779 f' s (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f'' s) ds
@@ -819,7 +812,7 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
820 i'' s = uncurry i' <=< i s
813 i'' s = uncurry i' <=< i s
822− i' s (CaseAlternative bs grd val) = CaseAlternative <$> mapM (h'' s) bs <*> maybeM (g'' s) grd <*> g'' s val
815+ i' s (CaseAlternative bs val) = CaseAlternative <$> mapM (h'' s) bs <*> eitherM (mapM (pairM (g'' s) (g'' s))) (g'' s) val
824 j'' s = uncurry j' <=< j s
817 j'' s = uncurry j' <=< j s