{-# language DeriveFoldable #-}
{-# language DeriveFunctor #-}
{-# language DeriveTraversable #-}
{-# language OverloadedStrings #-}
module Language.Elm.Pattern where

import Data.Text (Text)

import qualified Language.Elm.Name as Name

data Pattern v
  = Var v
  | Wildcard
  | Con Name.Qualified [Pattern v]
  | List [Pattern v]
  | String !Text
  | Int !Integer
  | Float !Double
  deriving (Pattern v -> Pattern v -> Bool
(Pattern v -> Pattern v -> Bool)
-> (Pattern v -> Pattern v -> Bool) -> Eq (Pattern v)
forall v. Eq v => Pattern v -> Pattern v -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall v. Eq v => Pattern v -> Pattern v -> Bool
== :: Pattern v -> Pattern v -> Bool
$c/= :: forall v. Eq v => Pattern v -> Pattern v -> Bool
/= :: Pattern v -> Pattern v -> Bool
Eq, Eq (Pattern v)
Eq (Pattern v) =>
(Pattern v -> Pattern v -> Ordering)
-> (Pattern v -> Pattern v -> Bool)
-> (Pattern v -> Pattern v -> Bool)
-> (Pattern v -> Pattern v -> Bool)
-> (Pattern v -> Pattern v -> Bool)
-> (Pattern v -> Pattern v -> Pattern v)
-> (Pattern v -> Pattern v -> Pattern v)
-> Ord (Pattern v)
Pattern v -> Pattern v -> Bool
Pattern v -> Pattern v -> Ordering
Pattern v -> Pattern v -> Pattern v
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall v. Ord v => Eq (Pattern v)
forall v. Ord v => Pattern v -> Pattern v -> Bool
forall v. Ord v => Pattern v -> Pattern v -> Ordering
forall v. Ord v => Pattern v -> Pattern v -> Pattern v
$ccompare :: forall v. Ord v => Pattern v -> Pattern v -> Ordering
compare :: Pattern v -> Pattern v -> Ordering
$c< :: forall v. Ord v => Pattern v -> Pattern v -> Bool
< :: Pattern v -> Pattern v -> Bool
$c<= :: forall v. Ord v => Pattern v -> Pattern v -> Bool
<= :: Pattern v -> Pattern v -> Bool
$c> :: forall v. Ord v => Pattern v -> Pattern v -> Bool
> :: Pattern v -> Pattern v -> Bool
$c>= :: forall v. Ord v => Pattern v -> Pattern v -> Bool
>= :: Pattern v -> Pattern v -> Bool
$cmax :: forall v. Ord v => Pattern v -> Pattern v -> Pattern v
max :: Pattern v -> Pattern v -> Pattern v
$cmin :: forall v. Ord v => Pattern v -> Pattern v -> Pattern v
min :: Pattern v -> Pattern v -> Pattern v
Ord, Int -> Pattern v -> ShowS
[Pattern v] -> ShowS
Pattern v -> String
(Int -> Pattern v -> ShowS)
-> (Pattern v -> String)
-> ([Pattern v] -> ShowS)
-> Show (Pattern v)
forall v. Show v => Int -> Pattern v -> ShowS
forall v. Show v => [Pattern v] -> ShowS
forall v. Show v => Pattern v -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall v. Show v => Int -> Pattern v -> ShowS
showsPrec :: Int -> Pattern v -> ShowS
$cshow :: forall v. Show v => Pattern v -> String
show :: Pattern v -> String
$cshowList :: forall v. Show v => [Pattern v] -> ShowS
showList :: [Pattern v] -> ShowS
Show, (forall a b. (a -> b) -> Pattern a -> Pattern b)
-> (forall a b. a -> Pattern b -> Pattern a) -> Functor Pattern
forall a b. a -> Pattern b -> Pattern a
forall a b. (a -> b) -> Pattern a -> Pattern b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Pattern a -> Pattern b
fmap :: forall a b. (a -> b) -> Pattern a -> Pattern b
$c<$ :: forall a b. a -> Pattern b -> Pattern a
<$ :: forall a b. a -> Pattern b -> Pattern a
Functor, (forall m. Monoid m => Pattern m -> m)
-> (forall m a. Monoid m => (a -> m) -> Pattern a -> m)
-> (forall m a. Monoid m => (a -> m) -> Pattern a -> m)
-> (forall a b. (a -> b -> b) -> b -> Pattern a -> b)
-> (forall a b. (a -> b -> b) -> b -> Pattern a -> b)
-> (forall b a. (b -> a -> b) -> b -> Pattern a -> b)
-> (forall b a. (b -> a -> b) -> b -> Pattern a -> b)
-> (forall a. (a -> a -> a) -> Pattern a -> a)
-> (forall a. (a -> a -> a) -> Pattern a -> a)
-> (forall a. Pattern a -> [a])
-> (forall a. Pattern a -> Bool)
-> (forall a. Pattern a -> Int)
-> (forall a. Eq a => a -> Pattern a -> Bool)
-> (forall a. Ord a => Pattern a -> a)
-> (forall a. Ord a => Pattern a -> a)
-> (forall a. Num a => Pattern a -> a)
-> (forall a. Num a => Pattern a -> a)
-> Foldable Pattern
forall a. Eq a => a -> Pattern a -> Bool
forall a. Num a => Pattern a -> a
forall a. Ord a => Pattern a -> a
forall m. Monoid m => Pattern m -> m
forall a. Pattern a -> Bool
forall a. Pattern a -> Int
forall a. Pattern a -> [a]
forall a. (a -> a -> a) -> Pattern a -> a
forall m a. Monoid m => (a -> m) -> Pattern a -> m
forall b a. (b -> a -> b) -> b -> Pattern a -> b
forall a b. (a -> b -> b) -> b -> Pattern a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => Pattern m -> m
fold :: forall m. Monoid m => Pattern m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Pattern a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Pattern a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Pattern a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Pattern a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Pattern a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Pattern a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Pattern a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Pattern a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Pattern a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Pattern a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Pattern a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Pattern a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Pattern a -> a
foldr1 :: forall a. (a -> a -> a) -> Pattern a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Pattern a -> a
foldl1 :: forall a. (a -> a -> a) -> Pattern a -> a
$ctoList :: forall a. Pattern a -> [a]
toList :: forall a. Pattern a -> [a]
$cnull :: forall a. Pattern a -> Bool
null :: forall a. Pattern a -> Bool
$clength :: forall a. Pattern a -> Int
length :: forall a. Pattern a -> Int
$celem :: forall a. Eq a => a -> Pattern a -> Bool
elem :: forall a. Eq a => a -> Pattern a -> Bool
$cmaximum :: forall a. Ord a => Pattern a -> a
maximum :: forall a. Ord a => Pattern a -> a
$cminimum :: forall a. Ord a => Pattern a -> a
minimum :: forall a. Ord a => Pattern a -> a
$csum :: forall a. Num a => Pattern a -> a
sum :: forall a. Num a => Pattern a -> a
$cproduct :: forall a. Num a => Pattern a -> a
product :: forall a. Num a => Pattern a -> a
Foldable, Functor Pattern
Foldable Pattern
(Functor Pattern, Foldable Pattern) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Pattern a -> f (Pattern b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Pattern (f a) -> f (Pattern a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Pattern a -> m (Pattern b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Pattern (m a) -> m (Pattern a))
-> Traversable Pattern
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Pattern (m a) -> m (Pattern a)
forall (f :: * -> *) a.
Applicative f =>
Pattern (f a) -> f (Pattern a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Pattern a -> m (Pattern b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Pattern a -> f (Pattern b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Pattern a -> f (Pattern b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Pattern a -> f (Pattern b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Pattern (f a) -> f (Pattern a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Pattern (f a) -> f (Pattern a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Pattern a -> m (Pattern b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Pattern a -> m (Pattern b)
$csequence :: forall (m :: * -> *) a. Monad m => Pattern (m a) -> m (Pattern a)
sequence :: forall (m :: * -> *) a. Monad m => Pattern (m a) -> m (Pattern a)
Traversable)

foldMapGlobals
  :: Monoid m
  => (Name.Qualified -> m)
  -> Pattern v
  -> m
foldMapGlobals :: forall m v. Monoid m => (Qualified -> m) -> Pattern v -> m
foldMapGlobals Qualified -> m
f Pattern v
pat =
  case Pattern v
pat of
    Var v
_ ->
      m
forall a. Monoid a => a
mempty

    Pattern v
Wildcard ->
      m
forall a. Monoid a => a
mempty

    Con Qualified
c [Pattern v]
pats ->
      Qualified -> m
f Qualified
c m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (Pattern v -> m) -> [Pattern v] -> m
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Qualified -> m) -> Pattern v -> m
forall m v. Monoid m => (Qualified -> m) -> Pattern v -> m
foldMapGlobals Qualified -> m
f) [Pattern v]
pats

    List [Pattern v]
pats ->
      (Pattern v -> m) -> [Pattern v] -> m
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Qualified -> m) -> Pattern v -> m
forall m v. Monoid m => (Qualified -> m) -> Pattern v -> m
foldMapGlobals Qualified -> m
f) [Pattern v]
pats

    String Text
_ ->
      m
forall a. Monoid a => a
mempty

    Int Integer
_ ->
      m
forall a. Monoid a => a
mempty

    Float Double
_ ->
      m
forall a. Monoid a => a
mempty

tuple :: Pattern v -> Pattern v -> Pattern v
tuple :: forall v. Pattern v -> Pattern v -> Pattern v
tuple Pattern v
p1 Pattern v
p2 = Qualified -> [Pattern v] -> Pattern v
forall v. Qualified -> [Pattern v] -> Pattern v
Con Qualified
"Basics.," [Pattern v
p1, Pattern v
p2]