{- |
Module      : LHMake.Data.Feature
Description : Language features and dependencies between them.
Copyright   : (c) Hangil Kim, 2026
                  Kairon Johnson, 2026
                  Leonidas Sallos, 2026
                  Oscar Abelanet, 2026
License     : MIT
Maintainer  : kimhang@oregonstate.edu
Stability   : experimental

This module provides definitions for language features and operations on them,
such as superimposition and combination generation.
It also provides a number of definitions supporting the specification of dependencies between features.
-}
module LHMake.Data.Feature (
    -- * Features
    FeatureName, ModuleName, Feature (..), feature,
    -- ** Registries
    Registry, registry,
    -- ** Superimposing features
    FeatureSet, resolve, superimpose,
    -- ** Listing features
    list, listAll, listDeps,
    -- ** Listing feature combinations
    combos, generate, generateCombos,
    -- * Dependencies
    Dependency (..),
    -- ** Convenient synonyms
    (/\), (\/),
    -- ** Normalization
    cnf, dnf
    ) where

import Control.Monad    (forM, forM_, unless, when)
import Data.Bifunctor   (first)
import Data.Functor     (void)
import Data.List        (intercalate)
import Data.Map         (Map)
import Data.Maybe       (maybeToList)
import Data.Set         (Set)
import System.Directory (createDirectoryIfMissing, doesFileExist)
import System.Exit      (die)
import System.FilePath  (takeDirectory, (</>), (<.>))
import System.IO        (readFile')

import Happy.Frontend   (parseYFileContents)
import Happy.Frontend.AbsSyn
import qualified Language.Haskell.Exts as H

import qualified Data.Graph as Graph
import qualified Data.Map   as Map
import qualified Data.Set   as Set

import LHMake.Control.Superimposition ((-->), parseMode)
import LHMake.Data.Error              (LHsMakeError)
import LHMake.Text.Happy.Pretty       (pprHappy)
import LHMake.Text.Label              (infoLabel, errLabel)

-------------------------------------------------------------------------------
-- Features

-- | The name of a 'LH.Data.Feature.Feature'.
type FeatureName = String

-- | The name of a Haskell module or Happy grammar (which itself denotes a Haskell module).
type ModuleName = String

-- | A language feature \(f\) is represented using four components: the files holding its Haskell modules,
--   at most one Happy grammar file, and two nested sets of feature names that represent what \(f\) depends on,
--   and in what cases \(f\) is automatically enabled.
--
--   'dependsOn' and 'impliedBy' use conjunctive and disjunctive normal form respectively
--   as those choices lead to the most "natural" implementation.
--
--      ['dependsOn']: ...denotes /all/ conditions holding.
--      ['impliedBy']: ...denotes /some/ condition holding.
--
--   See 'feature'.
data Feature = Feature
    { Feature -> FeatureName
descrip   :: String                  -- ^ A short description of \(f\).
    , Feature -> [FeatureName]
modules   :: [FilePath]              -- ^ Haskell source files.
    , Feature -> Maybe FeatureName
grammar   :: Maybe FilePath          -- ^ A Happy grammar, if any.
    , Feature -> Set (Set FeatureName)
dependsOn :: Set (Set FeatureName)
        -- ^ What \(f\) depends on /in conjunctive normal form/.
        --
        --   @{{A}, {B, C}}@ means @A@ and (@B@ or @C@). See 'cnf'.
    , Feature -> Set (Set FeatureName)
impliedBy :: Set (Set FeatureName)
        -- ^ What features imply \(f\) /in disjunctive normal form/.
        --
        --   @{{A}, {B, C}}@ means @A@ or (@B@ and @C@). See 'dnf'.
    , Feature -> Bool
hidden    :: Bool
        -- ^ If 'True', \(f\) may only be enabled automatically.
    }

-- | Builds a 'Feature' \(f\) specified in terms of values of type 'Dependency'.
--   The two 'Dependency' arguments are automatically converted into the respective normal forms. See 'cnf' and 'dnf'.
--   
--   The filepaths 'feature' expects should be written relative to @LayeredHaskell-make\/src\/LH\/Feature\/@.
--
--   Note that since 'feature' takes 'Dependency' arguments, it's easier to construct features using this over the original constructor 'Feature'.
feature
    :: String            -- ^ A short description of \(f\). See 'descrip'.
    -> [FilePath]        -- ^ Haskell source files (specified /relatively/, e.g. @E/AbsSyn.hs@). See 'modules'.
    -> Maybe FilePath    -- ^ A Happy grammar, if any (specified /relatively/, e.g. @E/Parser.y@). See 'grammar'.
    -> Dependency        -- ^ What \(f\) depends on. See 'dependsOn'.
    -> Dependency        -- ^ What implies \(f\). See 'impliedBy'.
    -> Bool              -- ^ Whether \(f\) may only be enabled automatically. See 'hidden'.
    -> Feature
feature :: FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
desc [FeatureName]
modls Maybe FeatureName
gram Dependency
depends Dependency
implied Bool
hide =
    FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Set (Set FeatureName)
-> Set (Set FeatureName)
-> Bool
-> Feature
Feature FeatureName
desc ((FeatureName -> FeatureName) -> [FeatureName] -> [FeatureName]
forall a b. (a -> b) -> [a] -> [b]
map FeatureName -> FeatureName
qualify [FeatureName]
modls) ((FeatureName -> FeatureName)
-> Maybe FeatureName -> Maybe FeatureName
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FeatureName -> FeatureName
qualify Maybe FeatureName
gram) (Dependency -> Set (Set FeatureName)
cnf Dependency
depends) (Dependency -> Set (Set FeatureName)
dnf Dependency
implied) Bool
hide
    where qualify :: FeatureName -> FeatureName
qualify FeatureName
file = FeatureName
"LayeredHaskell-make" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"src" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"LHMake" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Feature" FeatureName -> FeatureName -> FeatureName
</> FeatureName
file

-------------------------------------------------------------------------------
-- Registries

-- | A registry maps names to their 'Feature's.
type Registry = Map FeatureName Feature

-- | The set of /all/ features.
--
--   ==== __Features__
--
--   Layered Haskell defines the following features.
--   Hidden features are /italicized/.
--
--      [@E@]: /Expressions/.
--             All other features, whether it be directly or indirectly, depends on @E@.
--      [@B@]: Basic types, namely @Int@ and @Bool@.
--             Depends on @E@.
--      [@D@]: (Monomorphic) algebraic datatypes.
--             Depends on @E@.
--      [@F@]: Functions and basic pattern matching.
--             Depends on @B@ or @D@.
--      [@C@]: /Case expressions/.
--             Implied by @F@ and @D@.
--      [@P@]: Prenex polymorphism (with implicit @forall@s).
--             Depends on @D@.
--      [@L@]: List and tuple syntax.
--             Depends on @F@ and @D@.
registry :: Registry
registry :: Registry
registry = [(FeatureName, Feature)] -> Registry
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
    [ (FeatureName
"E", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Expressions"
                    [FeatureName
"E" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
                    (FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"E" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
                    Dependency
None
                    Dependency
None
                    Bool
True)
    , (FeatureName
"B", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Basic types"
                    [FeatureName
"B" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
                    (FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"B" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
                    (FeatureName -> Dependency
Has FeatureName
"E")
                    Dependency
None
                    Bool
False)
    , (FeatureName
"D", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Monomorphic algebraic datatypes"
                    [FeatureName
"D" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
                    (FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"D" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
                    (FeatureName -> Dependency
Has FeatureName
"E")
                    Dependency
None
                    Bool
False)
    , (FeatureName
"F", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Functions"
                    [FeatureName
"F" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
                    (FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"F" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
                    (FeatureName -> Dependency
Has FeatureName
"B" Dependency -> Dependency -> Dependency
\/ FeatureName -> Dependency
Has FeatureName
"D")
                    Dependency
None
                    Bool
False)
    , (FeatureName
"C", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Case expressions"
                    [FeatureName
"C" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
                    (FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"C" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
                    Dependency
None
                    (FeatureName -> Dependency
Has FeatureName
"D" Dependency -> Dependency -> Dependency
/\ FeatureName -> Dependency
Has FeatureName
"F")
                    Bool
True)
    , (FeatureName
"P", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"Prenex polymorphism"
                    [FeatureName
"P" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
                    (FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"P" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
                    (FeatureName -> Dependency
Has FeatureName
"D")
                    Dependency
None
                    Bool
False)
    , (FeatureName
"L", FeatureName
-> [FeatureName]
-> Maybe FeatureName
-> Dependency
-> Dependency
-> Bool
-> Feature
feature FeatureName
"List and tuple syntax"
                    [FeatureName
"L" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"AbsSyn" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs"]
                    (FeatureName -> Maybe FeatureName
forall a. a -> Maybe a
Just (FeatureName
"L" FeatureName -> FeatureName -> FeatureName
</> FeatureName
"Parser" FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y"))
                    (FeatureName -> Dependency
Has FeatureName
"D" Dependency -> Dependency -> Dependency
/\ FeatureName -> Dependency
Has FeatureName
"F")
                    Dependency
None
                    Bool
False)
    ]

-------------------------------------------------------------------------------
-- Merging features

-- | A feature set is a set of features, which we simply identify by their names.
type FeatureSet = Set FeatureName

-- | Repeatedly adds to a set of feature names until nothing new gets added.
select :: Registry -> FeatureSet -> FeatureSet
select :: Registry -> Set FeatureName -> Set FeatureName
select Registry
reg Set FeatureName
names
    | Set FeatureName -> Int
forall a. Set a -> Int
Set.size Set FeatureName
added Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Set FeatureName -> Int
forall a. Set a -> Int
Set.size Set FeatureName
names = Set FeatureName
names
    | Bool
otherwise                        = Registry -> Set FeatureName -> Set FeatureName
select Registry
reg Set FeatureName
added
    where
        added :: Set FeatureName
added = [Set FeatureName] -> Set FeatureName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions [Set FeatureName
names, Set FeatureName
dependencies, Set FeatureName
implications]
        dependencies :: Set FeatureName
dependencies = (Feature -> Set FeatureName) -> Registry -> Set FeatureName
forall m a. Monoid m => (a -> m) -> Map FeatureName a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Set (Set FeatureName) -> Set FeatureName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (Set (Set FeatureName) -> Set FeatureName)
-> (Feature -> Set (Set FeatureName)) -> Feature -> Set FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set FeatureName -> Bool)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall a. (a -> Bool) -> Set a -> Set a
Set.filter ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1) (Int -> Bool)
-> (Set FeatureName -> Int) -> Set FeatureName -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set FeatureName -> Int
forall a. Set a -> Int
Set.size) (Set (Set FeatureName) -> Set (Set FeatureName))
-> (Feature -> Set (Set FeatureName))
-> Feature
-> Set (Set FeatureName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Set (Set FeatureName)
dependsOn)
                               (Registry -> Set FeatureName -> Registry
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Registry
reg Set FeatureName
names)
        implications :: Set FeatureName
implications = Registry -> Set FeatureName
forall k a. Map k a -> Set k
Map.keysSet ((Feature -> Bool) -> Registry -> Registry
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter ((Set FeatureName -> Bool) -> Set (Set FeatureName) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Set FeatureName -> Set FeatureName -> Bool
forall a. Ord a => Set a -> Set a -> Bool
`Set.isSubsetOf` Set FeatureName
names) (Set (Set FeatureName) -> Bool)
-> (Feature -> Set (Set FeatureName)) -> Feature -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Set (Set FeatureName)
impliedBy) Registry
reg)

-- | Attempt to produce the features in a valid order.
--   Specifically, every feature should occur before the features the 'dependsOn' and 'impliedBy' fields refer to.
order :: Registry -> FeatureSet -> LHsMakeError [FeatureName]
order :: Registry -> Set FeatureName -> LHsMakeError [FeatureName]
order Registry
reg Set FeatureName
names = ([FeatureName] -> [FeatureName])
-> LHsMakeError [FeatureName] -> LHsMakeError [FeatureName]
forall a b.
(a -> b) -> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [FeatureName] -> [FeatureName]
forall a. [a] -> [a]
reverse ((SCC FeatureName -> Either FeatureName FeatureName)
-> [SCC FeatureName] -> LHsMakeError [FeatureName]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse SCC FeatureName -> Either FeatureName FeatureName
acyclic ([(FeatureName, FeatureName, [FeatureName])] -> [SCC FeatureName]
forall key node. Ord key => [(node, key, [key])] -> [SCC node]
Graph.stronglyConnComp [(FeatureName, FeatureName, [FeatureName])]
nodes))
    where
        nodes :: [(FeatureName, FeatureName, [FeatureName])]
nodes = [ (FeatureName
name, FeatureName
name, Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList (Set (Set FeatureName) -> Set FeatureName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (Feature -> Set (Set FeatureName)
dependsOn Feature
feat Set (Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall a. Semigroup a => a -> a -> a
<> Feature -> Set (Set FeatureName)
impliedBy Feature
feat)))
                | (FeatureName
name, Feature
feat) <- Registry -> [(FeatureName, Feature)]
forall k a. Map k a -> [(k, a)]
Map.toList (Registry -> Set FeatureName -> Registry
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Registry
reg Set FeatureName
names) ]
        acyclic :: SCC FeatureName -> Either FeatureName FeatureName
acyclic (Graph.AcyclicSCC FeatureName
name)   = FeatureName -> Either FeatureName FeatureName
forall a b. b -> Either a b
Right FeatureName
name
        acyclic (Graph.CyclicSCC [FeatureName]
members) = FeatureName -> Either FeatureName FeatureName
forall a b. a -> Either a b
Left (FeatureName
"These features have cyclic dependencies: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords [FeatureName]
members)

-- | Lists every unknown feature name.
unknown :: Registry -> FeatureSet -> [String]
unknown :: Registry -> Set FeatureName -> [FeatureName]
unknown Registry
reg Set FeatureName
names =
    [ FeatureName
"Unknown feature: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
name
    | FeatureName
name <- Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList ((FeatureName -> Bool) -> Set FeatureName -> Set FeatureName
forall a. (a -> Bool) -> Set a -> Set a
Set.filter (FeatureName -> Registry -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`Map.notMember` Registry
reg) Set FeatureName
names) ]

-- | Lists every problem with a set of feature names.
--   An empty result means that there are no problems with the set.
check :: Registry -> FeatureSet -> [String]
check :: Registry -> Set FeatureName -> [FeatureName]
check Registry
reg Set FeatureName
names =
    Registry -> Set FeatureName -> [FeatureName]
unknown Registry
reg Set FeatureName
names [FeatureName] -> [FeatureName] -> [FeatureName]
forall a. [a] -> [a] -> [a]
++
    [ FeatureName
"Can't automatically resolve dependencies of " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
name FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
", explicitly enable one of: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords (Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList Set FeatureName
clause)
    | (FeatureName
name, Feature
feat) <- Registry -> [(FeatureName, Feature)]
forall k a. Map k a -> [(k, a)]
Map.toList (Registry -> Set FeatureName -> Registry
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Registry
reg Set FeatureName
names)
    , Set FeatureName
clause <- Set (Set FeatureName) -> [Set FeatureName]
forall a. Set a -> [a]
Set.toList (Feature -> Set (Set FeatureName)
dependsOn Feature
feat)
    , Set FeatureName -> Set FeatureName -> Bool
forall a. Ord a => Set a -> Set a -> Bool
Set.disjoint Set FeatureName
clause Set FeatureName
names ]

-- | Decides which features are really needed for a selection.
--  
--   'resolve' generates an error if any of the following are true.
--
--       * One or more 'hidden' features were manually enabled.
--       * The 'registry' is ill-formed in that it contains unknown features.
--       * The enabled features have dependencies that cannot be unambiguously resolved.
--
--   The last case occurs when, say, a feature @A@ depends on features @B@ or @C@, but only @A@ was manually enabled.
--   To avoid an arbitrary choice between @B@ and @C@, an error is emitted.
resolve :: Registry -> FeatureSet -> LHsMakeError [FeatureName]
resolve :: Registry -> Set FeatureName -> LHsMakeError [FeatureName]
resolve Registry
reg Set FeatureName
names = do
    let hide :: [FeatureName]
hide     = Registry -> [FeatureName]
forall k a. Map k a -> [k]
Map.keys ((Feature -> Bool) -> Registry -> Registry
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter Feature -> Bool
hidden (Registry -> Set FeatureName -> Registry
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Registry
reg Set FeatureName
names))
        selected :: Set FeatureName
selected = Registry -> Set FeatureName -> Set FeatureName
select Registry
reg Set FeatureName
names
        errs :: [FeatureName]
errs     = Registry -> Set FeatureName -> [FeatureName]
check Registry
reg Set FeatureName
selected
    Bool -> Either FeatureName () -> Either FeatureName ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FeatureName]
hide) (Either FeatureName () -> Either FeatureName ())
-> Either FeatureName () -> Either FeatureName ()
forall a b. (a -> b) -> a -> b
$
        FeatureName -> Either FeatureName ()
forall a b. a -> Either a b
Left (FeatureName
"Hidden features cannot be selected directly: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords [FeatureName]
hide)
    Bool -> Either FeatureName () -> Either FeatureName ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FeatureName]
errs) (Either FeatureName () -> Either FeatureName ())
-> Either FeatureName () -> Either FeatureName ()
forall a b. (a -> b) -> a -> b
$
        FeatureName -> Either FeatureName ()
forall a b. a -> Either a b
Left (FeatureName -> [FeatureName] -> FeatureName
forall a. [a] -> [[a]] -> [a]
intercalate FeatureName
"\n" [FeatureName]
errs)
    Registry -> Set FeatureName -> LHsMakeError [FeatureName]
order Registry
reg Set FeatureName
selected

-- | Reads and parses the files corresponding to a language feature, 
--   producing its Haskell modules and its grammar (if any) as abstract syntax.
--
--   The result of 'load' also carries around corresponding module names, 
--   since we only want to superimpose modules of the same name.
load :: Feature -> IO (LHsMakeError (Map ModuleName (H.Module ()), Map ModuleName BookendedAbsSyn))
load :: Feature
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
load Feature
feat = do
    [(FeatureName, FeatureName)]
haskellSrcs <- (FeatureName -> IO (FeatureName, FeatureName))
-> [FeatureName] -> IO [(FeatureName, FeatureName)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse FeatureName -> IO (FeatureName, FeatureName)
loadFileWithPath (Feature -> [FeatureName]
modules Feature
feat)
    [(FeatureName, FeatureName)]
happySrcs   <- (FeatureName -> IO (FeatureName, FeatureName))
-> [FeatureName] -> IO [(FeatureName, FeatureName)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse FeatureName -> IO (FeatureName, FeatureName)
loadFileWithPath (Maybe FeatureName -> [FeatureName]
forall a. Maybe a -> [a]
maybeToList (Feature -> Maybe FeatureName
grammar Feature
feat))
    LHsMakeError
  (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsMakeError
   (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
 -> IO
      (LHsMakeError
         (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)))
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a b. (a -> b) -> a -> b
$ do
        [(FeatureName, Module ())]
haskell <- [(FeatureName, FeatureName)]
-> ((FeatureName, FeatureName)
    -> Either FeatureName (FeatureName, Module ()))
-> Either FeatureName [(FeatureName, Module ())]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(FeatureName, FeatureName)]
haskellSrcs (\(FeatureName
file, FeatureName
src) -> do
            Module ()
modl <- FeatureName -> FeatureName -> Either FeatureName (Module ())
parse FeatureName
file FeatureName
src
            FeatureName
name <- FeatureName -> Module () -> Either FeatureName FeatureName
forall {l}.
FeatureName -> Module l -> Either FeatureName FeatureName
moduleName FeatureName
file Module ()
modl
            (FeatureName, Module ())
-> Either FeatureName (FeatureName, Module ())
forall a. a -> Either FeatureName a
forall (m :: * -> *) a. Monad m => a -> m a
return (FeatureName
name, Module ()
modl))
        [(FeatureName, BookendedAbsSyn)]
happy <- [(FeatureName, FeatureName)]
-> ((FeatureName, FeatureName)
    -> Either FeatureName (FeatureName, BookendedAbsSyn))
-> Either FeatureName [(FeatureName, BookendedAbsSyn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(FeatureName, FeatureName)]
happySrcs (\(FeatureName
file, FeatureName
src) -> do
            BookendedAbsSyn
gram <- (FeatureName -> FeatureName)
-> Either FeatureName BookendedAbsSyn
-> Either FeatureName BookendedAbsSyn
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((FeatureName
file FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": ") FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++) (FeatureName -> Either FeatureName BookendedAbsSyn
parseYFileContents FeatureName
src)
            FeatureName
header <- case BookendedAbsSyn
gram of
                BookendedAbsSyn Maybe FeatureName
_ (Just FeatureName
header) AbsSyn FeatureName
_ Maybe FeatureName
_ -> FeatureName -> Either FeatureName FeatureName
forall a b. b -> Either a b
Right FeatureName
header
                BookendedAbsSyn
_ -> FeatureName -> Either FeatureName FeatureName
forall a b. a -> Either a b
Left (FeatureName
file FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": The grammar header is empty, so I can't identify it")
            Module ()
modl <- FeatureName -> FeatureName -> Either FeatureName (Module ())
parse FeatureName
file FeatureName
header
            FeatureName
name <- FeatureName -> Module () -> Either FeatureName FeatureName
forall {l}.
FeatureName -> Module l -> Either FeatureName FeatureName
moduleName FeatureName
file Module ()
modl
            (FeatureName, BookendedAbsSyn)
-> Either FeatureName (FeatureName, BookendedAbsSyn)
forall a. a -> Either FeatureName a
forall (m :: * -> *) a. Monad m => a -> m a
return (FeatureName
name, BookendedAbsSyn
gram))
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a. a -> Either FeatureName a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(FeatureName, Module ())] -> Map FeatureName (Module ())
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(FeatureName, Module ())]
haskell, [(FeatureName, BookendedAbsSyn)] -> Map FeatureName BookendedAbsSyn
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(FeatureName, BookendedAbsSyn)]
happy)
    where
        loadFileWithPath :: FeatureName -> IO (FeatureName, FeatureName)
loadFileWithPath FeatureName
file = do
            FeatureName
src <- FeatureName -> IO FeatureName
readFile FeatureName
file
            (FeatureName, FeatureName) -> IO (FeatureName, FeatureName)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (FeatureName
file, FeatureName
src)
        parse :: FeatureName -> FeatureName -> Either FeatureName (Module ())
parse FeatureName
file FeatureName
src = case ParseMode -> FeatureName -> ParseResult (Module SrcSpanInfo)
H.parseModuleWithMode ParseMode
parseMode FeatureName
src of
            H.ParseOk Module SrcSpanInfo
modl -> Module () -> Either FeatureName (Module ())
forall a b. b -> Either a b
Right (Module SrcSpanInfo -> Module ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Module SrcSpanInfo
modl)
            H.ParseFailed (H.SrcLoc FeatureName
_ Int
l Int
c) FeatureName
err ->
                FeatureName -> Either FeatureName (Module ())
forall a b. a -> Either a b
Left (FeatureName
file FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
"@" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ Int -> FeatureName
forall a. Show a => a -> FeatureName
show Int
l FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
":" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ Int -> FeatureName
forall a. Show a => a -> FeatureName
show Int
c FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
err)
        moduleName :: FeatureName -> Module l -> Either FeatureName FeatureName
moduleName FeatureName
_ (H.Module l
_ (Just (H.ModuleHead l
_ (H.ModuleName l
_ FeatureName
name) Maybe (WarningText l)
_ Maybe (ExportSpecList l)
_)) [ModulePragma l]
_ [ImportDecl l]
_ [Decl l]
_) = FeatureName -> Either FeatureName FeatureName
forall a b. b -> Either a b
Right FeatureName
name
        moduleName FeatureName
file Module l
_ = FeatureName -> Either FeatureName FeatureName
forall a b. a -> Either a b
Left (FeatureName
file FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": The module has no header, so I can't identify it")

-- | Resolves the dependencies inherent to a 'FeatureSet', then superimposes modules and grammars 
--   such that a single set of Haskell modules and at most one Happy grammar is produced.
merge :: Registry -> FeatureSet -> IO (LHsMakeError (Map ModuleName (H.Module ()), Map ModuleName BookendedAbsSyn))
merge :: Registry
-> Set FeatureName
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
merge Registry
reg Set FeatureName
names = case Registry -> Set FeatureName -> LHsMakeError [FeatureName]
resolve Registry
reg Set FeatureName
names of
    Left FeatureName
err      -> LHsMakeError
  (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (FeatureName
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b. a -> Either a b
Left (FeatureName -> FeatureName
errLabel FeatureName
err))
    Right [FeatureName]
enabled -> do
        FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"These features are enabled: " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords (Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList Set FeatureName
names)
        let automatic :: [FeatureName]
automatic = (FeatureName -> Bool) -> [FeatureName] -> [FeatureName]
forall a. (a -> Bool) -> [a] -> [a]
filter (FeatureName -> Set FeatureName -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.notMember` Set FeatureName
names) [FeatureName]
enabled
        Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FeatureName]
automatic) (FeatureName -> IO ()
putStrLn
            (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$  FeatureName -> FeatureName
infoLabel FeatureName
"These features were automatically enabled: "
            FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ [FeatureName] -> FeatureName
unwords [FeatureName]
automatic)
        -- While each feature only has at most one grammar,
        -- the Map-based representation leads to a slightly simpler implementation.
        [LHsMakeError
   (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
loaded <- (FeatureName
 -> IO
      (LHsMakeError
         (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)))
-> [FeatureName]
-> IO
     [LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Feature
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
load (Feature
 -> IO
      (LHsMakeError
         (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)))
-> (FeatureName -> Feature)
-> FeatureName
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Registry
reg Registry -> FeatureName -> Feature
forall k a. Ord k => Map k a -> k -> a
Map.!)) [FeatureName]
enabled
        let mergeAll :: [Map k a] -> Either FeatureName (Map k a)
mergeAll [Map k a]
maps = Map k (Either FeatureName a) -> Either FeatureName (Map k a)
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => Map k (m a) -> m (Map k a)
sequence ((Either FeatureName a
 -> Either FeatureName a -> Either FeatureName a)
-> [Map k (Either FeatureName a)] -> Map k (Either FeatureName a)
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith (\Either FeatureName a
x Either FeatureName a
y -> Either FeatureName a
x Either FeatureName a
-> (a -> Either FeatureName a) -> Either FeatureName a
forall a b.
Either FeatureName a
-> (a -> Either FeatureName b) -> Either FeatureName b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> Either FeatureName a
y Either FeatureName a
-> (a -> Either FeatureName a) -> Either FeatureName a
forall a b.
Either FeatureName a
-> (a -> Either FeatureName b) -> Either FeatureName b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a
a a -> a -> Either FeatureName a
forall a. Superimposable a => a -> a -> LHsMakeError a
-->)) 
                                                     ((Map k a -> Map k (Either FeatureName a))
-> [Map k a] -> [Map k (Either FeatureName a)]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> Either FeatureName a)
-> Map k a -> Map k (Either FeatureName a)
forall a b. (a -> b) -> Map k a -> Map k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Either FeatureName a
forall a b. b -> Either a b
Right) [Map k a]
maps))
        LHsMakeError
  (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsMakeError
   (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
 -> IO
      (LHsMakeError
         (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)))
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall a b. (a -> b) -> a -> b
$ (FeatureName -> FeatureName)
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first FeatureName -> FeatureName
errLabel (LHsMakeError
   (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
 -> LHsMakeError
      (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b. (a -> b) -> a -> b
$ do
            ([Map FeatureName (Module ())]
modls, [Map FeatureName BookendedAbsSyn]
grams) <- ([(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
 -> ([Map FeatureName (Module ())],
     [Map FeatureName BookendedAbsSyn]))
-> Either
     FeatureName
     [(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
-> Either
     FeatureName
     ([Map FeatureName (Module ())], [Map FeatureName BookendedAbsSyn])
forall a b.
(a -> b) -> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
-> ([Map FeatureName (Module ())],
    [Map FeatureName BookendedAbsSyn])
forall a b. [(a, b)] -> ([a], [b])
unzip ([LHsMakeError
   (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
-> Either
     FeatureName
     [(Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence [LHsMakeError
   (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)]
loaded)
            (,) (Map FeatureName (Module ())
 -> Map FeatureName BookendedAbsSyn
 -> (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> Either FeatureName (Map FeatureName (Module ()))
-> Either
     FeatureName
     (Map FeatureName BookendedAbsSyn
      -> (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Map FeatureName (Module ())]
-> Either FeatureName (Map FeatureName (Module ()))
forall {k} {a}.
(Ord k, Superimposable a) =>
[Map k a] -> Either FeatureName (Map k a)
mergeAll [Map FeatureName (Module ())]
modls Either
  FeatureName
  (Map FeatureName BookendedAbsSyn
   -> (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> Either FeatureName (Map FeatureName BookendedAbsSyn)
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b.
Either FeatureName (a -> b)
-> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Map FeatureName BookendedAbsSyn]
-> Either FeatureName (Map FeatureName BookendedAbsSyn)
forall {k} {a}.
(Ord k, Superimposable a) =>
[Map k a] -> Either FeatureName (Map k a)
mergeAll [Map FeatureName BookendedAbsSyn]
grams

-- | Superimposes features (and implicit dependencies), and outputs files to a specified directory.
--   The directory should written in the format of, say, @src\/LH\/Feature@, 
--   since 'superimpose' simply appends it to the front of the filepath using '(</>)'. 
--
--   Note that 'superimpose' will write to a file /only/ if its contents have changed.
superimpose :: [FeatureName] -> FilePath -> IO ()
superimpose :: [FeatureName] -> FeatureName -> IO ()
superimpose [FeatureName]
names FeatureName
dir = do
    FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"LayeredHaskell-make is starting up..."
    (Map FeatureName (Module ())
haskell, Map FeatureName BookendedAbsSyn
happy) <- Registry
-> Set FeatureName
-> IO
     (LHsMakeError
        (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
merge Registry
registry ([FeatureName] -> Set FeatureName
forall a. Ord a => [a] -> Set a
Set.fromList [FeatureName]
names) IO
  (LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> (LHsMakeError
      (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
    -> IO
         (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> IO
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (FeatureName
 -> IO
      (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> ((Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
    -> IO
         (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn))
-> LHsMakeError
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either FeatureName
-> IO
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a. FeatureName -> IO a
die (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
-> IO
     (Map FeatureName (Module ()), Map FeatureName BookendedAbsSyn)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
    [(FeatureName, Module ())]
-> ((FeatureName, Module ()) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Map FeatureName (Module ()) -> [(FeatureName, Module ())]
forall k a. Map k a -> [(k, a)]
Map.toList Map FeatureName (Module ())
haskell) (((FeatureName, Module ()) -> IO ()) -> IO ())
-> ((FeatureName, Module ()) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(FeatureName
name, Module ()
modl) ->
        FeatureName -> FeatureName -> IO ()
writeIfChanged
            (FeatureName
dir FeatureName -> FeatureName -> FeatureName
</> FeatureName -> FeatureName
end FeatureName
name FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"hs")
            (Module () -> FeatureName
forall a. Pretty a => a -> FeatureName
H.prettyPrint Module ()
modl)
    [(FeatureName, BookendedAbsSyn)]
-> ((FeatureName, BookendedAbsSyn) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Map FeatureName BookendedAbsSyn -> [(FeatureName, BookendedAbsSyn)]
forall k a. Map k a -> [(k, a)]
Map.toList Map FeatureName BookendedAbsSyn
happy) (((FeatureName, BookendedAbsSyn) -> IO ()) -> IO ())
-> ((FeatureName, BookendedAbsSyn) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(FeatureName
name, BookendedAbsSyn
gram) ->
        FeatureName -> FeatureName -> IO ()
writeIfChanged
            (FeatureName
dir FeatureName -> FeatureName -> FeatureName
</> FeatureName -> FeatureName
end FeatureName
name FeatureName -> FeatureName -> FeatureName
<.> FeatureName
"y")
            (BookendedAbsSyn -> FeatureName
pprHappy BookendedAbsSyn
gram)
    FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"Done!"
    where
        end :: FeatureName -> FeatureName
end = FeatureName -> FeatureName
forall a. [a] -> [a]
reverse (FeatureName -> FeatureName)
-> (FeatureName -> FeatureName) -> FeatureName -> FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> FeatureName -> FeatureName
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'.') (FeatureName -> FeatureName)
-> (FeatureName -> FeatureName) -> FeatureName -> FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FeatureName -> FeatureName
forall a. [a] -> [a]
reverse

-- | Writes to a file only if its content differs.
--   This way, we don't need to recompile everything on every build.
writeIfChanged :: FilePath -> String -> IO ()
writeIfChanged :: FeatureName -> FeatureName -> IO ()
writeIfChanged FeatureName
path FeatureName
new = FeatureName -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length FeatureName
new Int -> IO () -> IO ()
forall a b. a -> b -> b
`seq` do
    Bool -> FeatureName -> IO ()
createDirectoryIfMissing Bool
True (FeatureName -> FeatureName
takeDirectory FeatureName
path)
    Bool
exists <- FeatureName -> IO Bool
doesFileExist FeatureName
path
    Bool
same   <- if Bool
exists then (FeatureName -> Bool) -> IO FeatureName -> IO Bool
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (FeatureName -> FeatureName -> Bool
forall a. Eq a => a -> a -> Bool
== FeatureName
new) (FeatureName -> IO FeatureName
readFile' FeatureName
path) else Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
same (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        FeatureName -> FeatureName -> IO ()
writeFile FeatureName
path FeatureName
new
        FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"Updated source file " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
path

-------------------------------------------------------------------------------
-- Listing features

-- | Colors a string representing features that can be enabled manually.
colored :: String -> String
colored :: FeatureName -> FeatureName
colored FeatureName
s = FeatureName
"\ESC[35m" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
s FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
"\ESC[0m"

-- | Prints a heading, then on each line, a feature's name and an additional detail, if applicable.
--   Names that can be enabled manually (that is, those that aren't 'hidden') are 'colored'
--   alongside a note explaining the coloring whenever the registry has any hidden features.
printFeatures :: String -> Registry -> (Feature -> String) -> IO ()
printFeatures :: FeatureName -> Registry -> (Feature -> FeatureName) -> IO ()
printFeatures FeatureName
heading Registry
reg Feature -> FeatureName
det = do
    FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
heading
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((Feature -> Bool) -> Registry -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Feature -> Bool
hidden Registry
reg) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
        FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel (FeatureName
"Note that only " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName -> FeatureName
colored FeatureName
"colored" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
" features can be enabled manually.")
    [(FeatureName, Feature)]
-> ((FeatureName, Feature) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Registry -> [(FeatureName, Feature)]
forall k a. Map k a -> [(k, a)]
Map.toList Registry
reg) (((FeatureName, Feature) -> IO ()) -> IO ())
-> ((FeatureName, Feature) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(FeatureName
name, Feature
feat) ->
        FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel ((if Feature -> Bool
hidden Feature
feat then FeatureName
name else FeatureName -> FeatureName
colored FeatureName
name) FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
": " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ Feature -> FeatureName
det Feature
feat)

-- | Lists all non-'hidden' features in the 'registry' including their descriptions. See 'descrip'.
list :: IO ()
list :: IO ()
list = FeatureName -> Registry -> (Feature -> FeatureName) -> IO ()
printFeatures FeatureName
"Listing features..." ((Feature -> Bool) -> Registry -> Registry
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (Feature -> Bool) -> Feature -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Bool
hidden) Registry
registry) Feature -> FeatureName
descrip

-- | Lists all features, including 'hidden' ones, in the 'registry' including their descriptions. See 'descrip'.
listAll :: IO ()
listAll :: IO ()
listAll = FeatureName -> Registry -> (Feature -> FeatureName) -> IO ()
printFeatures FeatureName
"Listing all features..." Registry
registry Feature -> FeatureName
descrip

-- | Lists all feature dependencies and implications.
listDeps :: IO ()
listDeps :: IO ()
listDeps = FeatureName -> Registry -> (Feature -> FeatureName) -> IO ()
printFeatures FeatureName
"Listing all dependencies and joint implications..." Registry
registry Feature -> FeatureName
detail
    where
        detail :: Feature -> FeatureName
detail Feature
feat
            | [FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FeatureName]
parts = FeatureName
"none"
            | Bool
otherwise  = FeatureName -> [FeatureName] -> FeatureName
forall a. [a] -> [[a]] -> [a]
intercalate FeatureName
", " [FeatureName]
parts
            where parts :: [FeatureName]
parts = FeatureName
-> FeatureName
-> FeatureName
-> Set (Set FeatureName)
-> [FeatureName]
describe FeatureName
"depends on "  FeatureName
" or "  FeatureName
" and " (Feature -> Set (Set FeatureName)
dependsOn Feature
feat)
                       [FeatureName] -> [FeatureName] -> [FeatureName]
forall a. [a] -> [a] -> [a]
++ FeatureName
-> FeatureName
-> FeatureName
-> Set (Set FeatureName)
-> [FeatureName]
describe FeatureName
"implied by " FeatureName
" and "  FeatureName
" or "  (Feature -> Set (Set FeatureName)
impliedBy Feature
feat)
        -- Pretty-prints a nested set of names, or nothing if it is empty.
        describe :: FeatureName
-> FeatureName
-> FeatureName
-> Set (Set FeatureName)
-> [FeatureName]
describe FeatureName
prefix FeatureName
inner FeatureName
outer Set (Set FeatureName)
xss =
            [ FeatureName
prefix FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName -> [FeatureName] -> FeatureName
forall a. [a] -> [[a]] -> [a]
intercalate FeatureName
outer
                [ if Set FeatureName -> Int
forall a. Set a -> Int
Set.size Set FeatureName
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 Bool -> Bool -> Bool
&& Set (Set FeatureName) -> Int
forall a. Set a -> Int
Set.size Set (Set FeatureName)
xss Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 then FeatureName
"(" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
s FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
")" else FeatureName
s
                | Set FeatureName
xs <- Set (Set FeatureName) -> [Set FeatureName]
forall a. Set a -> [a]
Set.toList Set (Set FeatureName)
xss, let s :: FeatureName
s = FeatureName -> [FeatureName] -> FeatureName
forall a. [a] -> [[a]] -> [a]
intercalate FeatureName
inner (Set FeatureName -> [FeatureName]
forall a. Set a -> [a]
Set.toList Set FeatureName
xs) ]
            | Bool -> Bool
not (Set (Set FeatureName) -> Bool
forall a. Set a -> Bool
Set.null Set (Set FeatureName)
xss) ]

-------------------------------------------------------------------------------
-- Listing feature combinations

-- | Generates a list of all feature combinations.
--
--   The Boolean paired with a 'FeatureName' is 'True' when the feature must be enabled manually.
combos :: LHsMakeError [[(FeatureName, Bool)]]
combos :: LHsMakeError [[(FeatureName, Bool)]]
combos = do
    (FeatureName -> Either FeatureName Any)
-> [FeatureName] -> Either FeatureName ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ FeatureName -> Either FeatureName Any
forall a b. a -> Either a b
Left (Registry -> Set FeatureName -> [FeatureName]
unknown Registry
registry ((Feature -> Set FeatureName) -> Registry -> Set FeatureName
forall m a. Monoid m => (a -> m) -> Map FeatureName a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Set (Set FeatureName) -> Set FeatureName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (Set (Set FeatureName) -> Set FeatureName)
-> (Feature -> Set (Set FeatureName)) -> Feature -> Set FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Set (Set FeatureName)
dependsOn) Registry
registry))
    ([[(FeatureName, Bool)]] -> [[(FeatureName, Bool)]])
-> LHsMakeError [[(FeatureName, Bool)]]
-> LHsMakeError [[(FeatureName, Bool)]]
forall a b.
(a -> b) -> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([(FeatureName, Bool)] -> Bool)
-> [[(FeatureName, Bool)]] -> [[(FeatureName, Bool)]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ([(FeatureName, Bool)] -> Bool) -> [(FeatureName, Bool)] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(FeatureName, Bool)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)) (LHsMakeError [[(FeatureName, Bool)]]
 -> LHsMakeError [[(FeatureName, Bool)]])
-> LHsMakeError [[(FeatureName, Bool)]]
-> LHsMakeError [[(FeatureName, Bool)]]
forall a b. (a -> b) -> a -> b
$
        [Set FeatureName]
-> (Set FeatureName -> Either FeatureName [(FeatureName, Bool)])
-> LHsMakeError [[(FeatureName, Bool)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM ((Set FeatureName -> Bool) -> [Set FeatureName] -> [Set FeatureName]
forall a. (a -> Bool) -> [a] -> [a]
filter ([FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([FeatureName] -> Bool)
-> (Set FeatureName -> [FeatureName]) -> Set FeatureName -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Registry -> Set FeatureName -> [FeatureName]
check Registry
registry)
                     (Set (Set FeatureName) -> [Set FeatureName]
forall a. Set a -> [a]
Set.toList
                         ((Set FeatureName -> Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map (Registry -> Set FeatureName -> Set FeatureName
select Registry
registry)
                                  (Set FeatureName -> Set (Set FeatureName)
forall a. Set a -> Set (Set a)
Set.powerSet (Registry -> Set FeatureName
forall k a. Map k a -> Set k
Map.keysSet ((Feature -> Bool) -> Registry -> Registry
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (Feature -> Bool) -> Feature -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Feature -> Bool
hidden) Registry
registry))))))
             (\Set FeatureName
names -> ([FeatureName] -> [(FeatureName, Bool)])
-> LHsMakeError [FeatureName]
-> Either FeatureName [(FeatureName, Bool)]
forall a b.
(a -> b) -> Either FeatureName a -> Either FeatureName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((FeatureName -> (FeatureName, Bool))
-> [FeatureName] -> [(FeatureName, Bool)]
forall a b. (a -> b) -> [a] -> [b]
map (\FeatureName
name -> (FeatureName
name, Set FeatureName -> FeatureName -> Bool
manual Set FeatureName
names FeatureName
name)))
                             (Registry -> Set FeatureName -> LHsMakeError [FeatureName]
order Registry
registry Set FeatureName
names))
    where
        -- A feature for which manual returns True must be selected manually.
        -- False means that the feature can be automatically enabled as a dependency.
        manual :: Set FeatureName -> FeatureName -> Bool
manual Set FeatureName
names FeatureName
name = Bool -> Bool
not (FeatureName
name FeatureName -> Set FeatureName -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Registry -> Set FeatureName -> Set FeatureName
select Registry
registry (FeatureName -> Set FeatureName -> Set FeatureName
forall a. Ord a => a -> Set a -> Set a
Set.delete FeatureName
name Set FeatureName
names))

-- | Generates and prints a list of all possible (non-redundant) feature combinations, 
--   with dependencies and implications automatically resolved.
--   Dependencies that must be manually enabled are printed in color.
generate :: IO ()
generate :: IO ()
generate = do
    FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel FeatureName
"Listing all (non-redundant) feature combinations..."
    FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel (FeatureName
"Note that only " FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName -> FeatureName
colored FeatureName
"colored" FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
" features need to be enabled manually. ")
            FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
"The rest can be automatically resolved."
    (FeatureName -> IO ())
-> ([[(FeatureName, Bool)]] -> IO ())
-> LHsMakeError [[(FeatureName, Bool)]]
-> IO ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FeatureName -> IO ()
putStrLn (FeatureName -> IO ())
-> (FeatureName -> FeatureName) -> FeatureName -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FeatureName -> FeatureName
errLabel) [[(FeatureName, Bool)]] -> IO ()
forall {t :: * -> *}.
Foldable t =>
t [(FeatureName, Bool)] -> IO ()
report LHsMakeError [[(FeatureName, Bool)]]
combos
    where
        report :: t [(FeatureName, Bool)] -> IO ()
report t [(FeatureName, Bool)]
entries = do
            ([(FeatureName, Bool)] -> IO ())
-> t [(FeatureName, Bool)] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FeatureName -> IO ()
putStrLn (FeatureName -> IO ())
-> ([(FeatureName, Bool)] -> FeatureName)
-> [(FeatureName, Bool)]
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FeatureName -> FeatureName
infoLabel (FeatureName -> FeatureName)
-> ([(FeatureName, Bool)] -> FeatureName)
-> [(FeatureName, Bool)]
-> FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FeatureName] -> FeatureName
unwords ([FeatureName] -> FeatureName)
-> ([(FeatureName, Bool)] -> [FeatureName])
-> [(FeatureName, Bool)]
-> FeatureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((FeatureName, Bool) -> FeatureName)
-> [(FeatureName, Bool)] -> [FeatureName]
forall a b. (a -> b) -> [a] -> [b]
map (FeatureName, Bool) -> FeatureName
paint) t [(FeatureName, Bool)]
entries
            FeatureName -> IO ()
putStrLn (FeatureName -> IO ()) -> FeatureName -> IO ()
forall a b. (a -> b) -> a -> b
$ FeatureName -> FeatureName
infoLabel (Int -> FeatureName
forall a. Show a => a -> FeatureName
show (t [(FeatureName, Bool)] -> Int
forall a. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t [(FeatureName, Bool)]
entries)) FeatureName -> FeatureName -> FeatureName
forall a. [a] -> [a] -> [a]
++ FeatureName
" feature combinations found!"
        paint :: (FeatureName, Bool) -> FeatureName
paint (FeatureName
name, Bool
manual) = if Bool
manual then FeatureName -> FeatureName
colored FeatureName
name else FeatureName
name

-- | Produces the feature combinations colored by 'generate'.
--
--   One can interpret 'generateCombos' as a filtering of 'combos' lifted into the 'IO' monad.
generateCombos :: IO [[FeatureName]]
generateCombos :: IO [[FeatureName]]
generateCombos =
    (FeatureName -> IO [[FeatureName]])
-> ([[(FeatureName, Bool)]] -> IO [[FeatureName]])
-> LHsMakeError [[(FeatureName, Bool)]]
-> IO [[FeatureName]]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FeatureName -> IO [[FeatureName]]
forall a. FeatureName -> IO a
die (FeatureName -> IO [[FeatureName]])
-> (FeatureName -> FeatureName)
-> FeatureName
-> IO [[FeatureName]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FeatureName -> FeatureName
errLabel)
           ([[FeatureName]] -> IO [[FeatureName]]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([[FeatureName]] -> IO [[FeatureName]])
-> ([[(FeatureName, Bool)]] -> [[FeatureName]])
-> [[(FeatureName, Bool)]]
-> IO [[FeatureName]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([FeatureName] -> Bool) -> [[FeatureName]] -> [[FeatureName]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([FeatureName] -> Bool) -> [FeatureName] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FeatureName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) ([[FeatureName]] -> [[FeatureName]])
-> ([[(FeatureName, Bool)]] -> [[FeatureName]])
-> [[(FeatureName, Bool)]]
-> [[FeatureName]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(FeatureName, Bool)] -> [FeatureName])
-> [[(FeatureName, Bool)]] -> [[FeatureName]]
forall a b. (a -> b) -> [a] -> [b]
map (((FeatureName, Bool) -> FeatureName)
-> [(FeatureName, Bool)] -> [FeatureName]
forall a b. (a -> b) -> [a] -> [b]
map (FeatureName, Bool) -> FeatureName
forall a b. (a, b) -> a
fst ([(FeatureName, Bool)] -> [FeatureName])
-> ([(FeatureName, Bool)] -> [(FeatureName, Bool)])
-> [(FeatureName, Bool)]
-> [FeatureName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((FeatureName, Bool) -> Bool)
-> [(FeatureName, Bool)] -> [(FeatureName, Bool)]
forall a. (a -> Bool) -> [a] -> [a]
filter (FeatureName, Bool) -> Bool
forall a b. (a, b) -> b
snd))
           LHsMakeError [[(FeatureName, Bool)]]
combos

-------------------------------------------------------------------------------
-- Dependencies

-- | A dependency is expressed as a Boolean formula of feature names.
data Dependency
    = None                -- ^ A dependency that can always be satisfied.
    | Has FeatureName     -- ^ A dependency on a single feature.
    | All [Dependency]    -- ^ All specified dependencies must be satisfied.
    | Any [Dependency]    -- ^ At least one specified dependency must be satisfied.
    deriving (Dependency -> Dependency -> Bool
(Dependency -> Dependency -> Bool)
-> (Dependency -> Dependency -> Bool) -> Eq Dependency
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Dependency -> Dependency -> Bool
== :: Dependency -> Dependency -> Bool
$c/= :: Dependency -> Dependency -> Bool
/= :: Dependency -> Dependency -> Bool
Eq, Int -> Dependency -> FeatureName -> FeatureName
[Dependency] -> FeatureName -> FeatureName
Dependency -> FeatureName
(Int -> Dependency -> FeatureName -> FeatureName)
-> (Dependency -> FeatureName)
-> ([Dependency] -> FeatureName -> FeatureName)
-> Show Dependency
forall a.
(Int -> a -> FeatureName -> FeatureName)
-> (a -> FeatureName)
-> ([a] -> FeatureName -> FeatureName)
-> Show a
$cshowsPrec :: Int -> Dependency -> FeatureName -> FeatureName
showsPrec :: Int -> Dependency -> FeatureName -> FeatureName
$cshow :: Dependency -> FeatureName
show :: Dependency -> FeatureName
$cshowList :: [Dependency] -> FeatureName -> FeatureName
showList :: [Dependency] -> FeatureName -> FeatureName
Show)

-------------------------------------------------------------------------------
-- Composing and normalizing dependencies

-- | A convenient synonym for describing a /conjunction/ of dependencies, 
--   that is, @A '/\' B@ means @A@ /and/ @B@ must be enabled.
(/\) :: Dependency -> Dependency -> Dependency
Dependency
a /\ :: Dependency -> Dependency -> Dependency
/\ Dependency
b = [Dependency] -> Dependency
All [Dependency
a, Dependency
b]

infixr 3 /\

-- | A convenient synonym for describing a /disjunction/ of dependencies, 
--   that is, @A '\/' B@ means @A@ /or/ @B@ must be enabled.
(\/) :: Dependency -> Dependency -> Dependency
Dependency
a \/ :: Dependency -> Dependency -> Dependency
\/ Dependency
b = [Dependency] -> Dependency
Any [Dependency
a, Dependency
b]

infixr 2 \/

-- | Takes the cartesian product of two nested sets and unions each pair.
combine :: Ord a => Set (Set a) -> Set (Set a) -> Set (Set a)
combine :: forall a. Ord a => Set (Set a) -> Set (Set a) -> Set (Set a)
combine Set (Set a)
xs Set (Set a)
ys = ((Set a, Set a) -> Set a) -> Set (Set a, Set a) -> Set (Set a)
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map ((Set a -> Set a -> Set a) -> (Set a, Set a) -> Set a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
Set.union) (Set (Set a) -> Set (Set a) -> Set (Set a, Set a)
forall a b. Set a -> Set b -> Set (a, b)
Set.cartesianProduct Set (Set a)
xs Set (Set a)
ys)

-- | Computes the conjunctive normal form (CNF) of a 'Dependency'.
--   See 'LH.Data.Feature.dependsOn'.
--
--   CNF denotes a set of requirements, each a set of features that must /all/ be enabled.
cnf :: Dependency -> Set (Set FeatureName)
cnf :: Dependency -> Set (Set FeatureName)
cnf Dependency
None       = Set (Set FeatureName)
forall a. Set a
Set.empty
cnf (Has FeatureName
name) = Set FeatureName -> Set (Set FeatureName)
forall a. a -> Set a
Set.singleton (FeatureName -> Set FeatureName
forall a. a -> Set a
Set.singleton FeatureName
name)
cnf (Any [Dependency]
deps) = (Dependency -> Set (Set FeatureName) -> Set (Set FeatureName))
-> Set (Set FeatureName) -> [Dependency] -> Set (Set FeatureName)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Set (Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall a. Ord a => Set (Set a) -> Set (Set a) -> Set (Set a)
combine (Set (Set FeatureName)
 -> Set (Set FeatureName) -> Set (Set FeatureName))
-> (Dependency -> Set (Set FeatureName))
-> Dependency
-> Set (Set FeatureName)
-> Set (Set FeatureName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dependency -> Set (Set FeatureName)
cnf) (Set FeatureName -> Set (Set FeatureName)
forall a. a -> Set a
Set.singleton Set FeatureName
forall a. Set a
Set.empty) [Dependency]
deps
cnf (All [Dependency]
deps) = (Dependency -> Set (Set FeatureName))
-> [Dependency] -> Set (Set FeatureName)
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Dependency -> Set (Set FeatureName)
cnf [Dependency]
deps

-- | Computes the disjunctive normal form (DNF) of a 'Dependency'.
--   See 'LH.Data.Feature.impliedBy'.
--
--   DNF denotes a set of alternatives, each a set of features that of which /one/ must be enabled.
dnf :: Dependency -> Set (Set FeatureName)
dnf :: Dependency -> Set (Set FeatureName)
dnf Dependency
None       = Set (Set FeatureName)
forall a. Set a
Set.empty
dnf (Has FeatureName
name) = Set FeatureName -> Set (Set FeatureName)
forall a. a -> Set a
Set.singleton (FeatureName -> Set FeatureName
forall a. a -> Set a
Set.singleton FeatureName
name)
dnf (Any [Dependency]
deps) = (Dependency -> Set (Set FeatureName))
-> [Dependency] -> Set (Set FeatureName)
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Dependency -> Set (Set FeatureName)
dnf [Dependency]
deps
dnf (All [Dependency]
deps) = (Dependency -> Set (Set FeatureName) -> Set (Set FeatureName))
-> Set (Set FeatureName) -> [Dependency] -> Set (Set FeatureName)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Set (Set FeatureName)
-> Set (Set FeatureName) -> Set (Set FeatureName)
forall a. Ord a => Set (Set a) -> Set (Set a) -> Set (Set a)
combine (Set (Set FeatureName)
 -> Set (Set FeatureName) -> Set (Set FeatureName))
-> (Dependency -> Set (Set FeatureName))
-> Dependency
-> Set (Set FeatureName)
-> Set (Set FeatureName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dependency -> Set (Set FeatureName)
dnf) (Set FeatureName -> Set (Set FeatureName)
forall a. a -> Set a
Set.singleton Set FeatureName
forall a. Set a
Set.empty) [Dependency]
deps