{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
module BNFC.Check.EmptyTypes (emptyData) where
import Data.Maybe
import Data.Map (Map)
import qualified Data.List as List
import qualified Data.Map as Map
import BNFC.CF
emptyData :: forall f. (IsFun f) => [Rul f] -> [RCat]
emptyData :: forall f. IsFun f => [Rul f] -> [RCat]
emptyData [Rul f]
rs =
[ RCat
pc
| Rule f
_ RCat
pc SentForm
_ InternalRule
_ <- [Rul f]
rs
, let c :: Cat
c = RCat -> Cat
forall a. WithPosition a -> a
wpThing RCat
pc
, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Cat -> Bool
isList Cat
c
, Left BaseCat
x <- [Cat -> Either BaseCat BaseCat
baseCat Cat
c]
, Bool -> ([f] -> Bool) -> Maybe [f] -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True [f] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
List.null (Maybe [f] -> Bool) -> Maybe [f] -> Bool
forall a b. (a -> b) -> a -> b
$ BaseCat -> Map BaseCat [f] -> Maybe [f]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup BaseCat
x Map BaseCat [f]
ruleMap
]
where
ruleMap :: Map BaseCat [f]
ruleMap :: Map BaseCat [f]
ruleMap = ([f] -> [f] -> [f]) -> [Map BaseCat [f]] -> Map BaseCat [f]
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith [f] -> [f] -> [f]
forall a. [a] -> [a] -> [a]
(++) ([Map BaseCat [f]] -> Map BaseCat [f])
-> [Map BaseCat [f]] -> Map BaseCat [f]
forall a b. (a -> b) -> a -> b
$ ((Rul f -> Maybe (Map BaseCat [f])) -> [Rul f] -> [Map BaseCat [f]]
forall a b. (a -> Maybe b) -> [a] -> [b]
`mapMaybe` [Rul f]
rs) ((Rul f -> Maybe (Map BaseCat [f])) -> [Map BaseCat [f]])
-> (Rul f -> Maybe (Map BaseCat [f])) -> [Map BaseCat [f]]
forall a b. (a -> b) -> a -> b
$ \case
Rule f
f RCat
pc SentForm
_ InternalRule
_
| Bool -> Bool
not (f -> Bool
forall a. IsFun a => a -> Bool
isCoercion f
f), Left BaseCat
x <- Cat -> Either BaseCat BaseCat
baseCat (RCat -> Cat
forall a. WithPosition a -> a
wpThing RCat
pc)
-> Map BaseCat [f] -> Maybe (Map BaseCat [f])
forall a. a -> Maybe a
Just (Map BaseCat [f] -> Maybe (Map BaseCat [f]))
-> Map BaseCat [f] -> Maybe (Map BaseCat [f])
forall a b. (a -> b) -> a -> b
$ BaseCat -> [f] -> Map BaseCat [f]
forall k a. k -> a -> Map k a
Map.singleton BaseCat
x [f
f]
| Bool
otherwise
-> Maybe (Map BaseCat [f])
forall a. Maybe a
Nothing