{-# Language TemplateHaskell #-}
-- | Add constraints and a reasonable value to chunks that are quantities.
module Language.Drasil.Chunk.Constrained (
  -- * Constrained Chunks
  -- ** From an Idea
  ConstrainedChunk(..), cuc, cvc, cnstrw,
  -- ** From a Concept
  ConstrConcept(..),
  cnstrw', constrained', constrainedNRV', cuc', cuc'') where

import Control.Lens ((^.), makeLenses, view)

import Language.Drasil.Chunk.Concept (cw, dcc)
import Language.Drasil.Chunk.DefinedQuantity (DefinedQuantityDict, dqd, dqd', dqdWr)
import Language.Drasil.Chunk.Quantity (QuantityDict, qw, vc)
import Language.Drasil.Chunk.Unital (uc')
import Language.Drasil.Chunk.Unitary (unitary)
import Language.Drasil.Symbol (HasSymbol(..), Symbol)
import Language.Drasil.Classes (NamedIdea(term), Idea(getA), Express(express),
  Definition(defn), ConceptDomain(cdom), Concept, Quantity,
  IsUnit, Constrained(constraints), HasReasVal(reasVal))
import Language.Drasil.Constraint (ConstraintE)
import Language.Drasil.Chunk.UnitDefn (unitWrapper, MayHaveUnit(getUnit))
import Language.Drasil.Expr.Lang (Expr(..))
import Language.Drasil.Expr.Class (sy)
import Language.Drasil.NounPhrase.Core (NP)
import Language.Drasil.Sentence (Sentence(S))
import Language.Drasil.Space (Space, HasSpace(..))
import Language.Drasil.Stages (Stage)
import Language.Drasil.UID (HasUID(..))

-- | ConstrainedChunks are symbolic quantities ('QuantityDict')
-- with 'Constraint's and maybe a typical value ('Maybe' 'Expr').
--
-- Ex. Measuring the length of a pendulum would have some reasonable value (between 1 cm and 2 m)
-- and the constraint that the length cannot be a negative value.
data ConstrainedChunk = ConstrainedChunk { ConstrainedChunk -> QuantityDict
_qd     :: QuantityDict
                                         , ConstrainedChunk -> [ConstraintE]
_constr :: [ConstraintE]
                                         , ConstrainedChunk -> Maybe Expr
_reasV  :: Maybe Expr
                                         }
makeLenses ''ConstrainedChunk

-- | Finds 'UID' of the 'QuantityDict' used to make the 'ConstrainedChunk'.
instance HasUID        ConstrainedChunk where uid :: Lens' ConstrainedChunk UID
uid = Lens' ConstrainedChunk QuantityDict
qd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid
-- | Finds term ('NP') of the 'QuantityDict' used to make the 'ConstrainedChunk'.
instance NamedIdea     ConstrainedChunk where term :: Lens' ConstrainedChunk NP
term = Lens' ConstrainedChunk QuantityDict
qd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. NamedIdea c => Lens' c NP
term
-- | Finds the idea contained in the 'QuantityDict' used to make the 'ConstrainedChunk'.
instance Idea          ConstrainedChunk where getA :: ConstrainedChunk -> Maybe String
getA = forall c. Idea c => c -> Maybe String
getA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ConstrainedChunk QuantityDict
qd
-- | Finds the 'Space' of the 'QuantityDict' used to make the 'ConstrainedChunk'.
instance HasSpace      ConstrainedChunk where typ :: Getter ConstrainedChunk Space
typ = Lens' ConstrainedChunk QuantityDict
qd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasSpace c => Getter c Space
typ
-- | Finds the 'Symbol' of the 'QuantityDict' used to make the 'ConstrainedChunk'.
instance HasSymbol     ConstrainedChunk where symbol :: ConstrainedChunk -> Stage -> Symbol
symbol ConstrainedChunk
c = forall c. HasSymbol c => c -> Stage -> Symbol
symbol (ConstrainedChunk
cforall s a. s -> Getting a s a -> a
^.Lens' ConstrainedChunk QuantityDict
qd)
-- | 'ConstrainedChunk's have a 'Quantity'. 
instance Quantity      ConstrainedChunk where
-- | Finds the 'Constraint's of a 'ConstrainedChunk'.
instance Constrained   ConstrainedChunk where constraints :: Lens' ConstrainedChunk [ConstraintE]
constraints = Lens' ConstrainedChunk [ConstraintE]
constr
-- | Finds a reasonable value for the 'ConstrainedChunk'.
instance HasReasVal    ConstrainedChunk where reasVal :: Lens' ConstrainedChunk (Maybe Expr)
reasVal     = Lens' ConstrainedChunk (Maybe Expr)
reasV
-- | Equal if 'UID's are equal.
instance Eq            ConstrainedChunk where ConstrainedChunk
c1 == :: ConstrainedChunk -> ConstrainedChunk -> Bool
== ConstrainedChunk
c2 = (ConstrainedChunk
c1 forall s a. s -> Getting a s a -> a
^. Lens' ConstrainedChunk QuantityDict
qd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid) forall a. Eq a => a -> a -> Bool
== (ConstrainedChunk
c2 forall s a. s -> Getting a s a -> a
^. Lens' ConstrainedChunk QuantityDict
qd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid)
-- | Finds units contained in the 'QuantityDict' used to make the 'ConstrainedChunk'.
instance MayHaveUnit   ConstrainedChunk where getUnit :: ConstrainedChunk -> Maybe UnitDefn
getUnit = forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ConstrainedChunk QuantityDict
qd

-- | Creates a constrained unitary chunk from a 'UID', term ('NP'), 'Symbol', unit, 'Space', 'Constraint's, and an 'Expr'.
cuc :: (IsUnit u) => String -> NP -> Symbol -> u
  -> Space -> [ConstraintE] -> Expr -> ConstrainedChunk
cuc :: forall u.
IsUnit u =>
String
-> NP
-> Symbol
-> u
-> Space
-> [ConstraintE]
-> Expr
-> ConstrainedChunk
cuc String
i NP
t Symbol
s u
u Space
space [ConstraintE]
cs Expr
rv = QuantityDict -> [ConstraintE] -> Maybe Expr -> ConstrainedChunk
ConstrainedChunk (forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw (forall u.
IsUnit u =>
String -> NP -> Symbol -> u -> Space -> UnitaryChunk
unitary String
i NP
t Symbol
s u
u Space
space)) [ConstraintE]
cs (forall a. a -> Maybe a
Just Expr
rv)

-- | Creates a constrained unitary chunk from a 'UID', term ('NP'), 'Symbol', 'Space', 'Constraint's, and a 'Maybe' 'Expr' (Similar to 'cuc' but no units).
cvc :: String -> NP -> Symbol -> Space -> [ConstraintE] -> Maybe Expr -> ConstrainedChunk
cvc :: String
-> NP
-> Symbol
-> Space
-> [ConstraintE]
-> Maybe Expr
-> ConstrainedChunk
cvc String
i NP
des Symbol
sym Space
space = QuantityDict -> [ConstraintE] -> Maybe Expr -> ConstrainedChunk
ConstrainedChunk (forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw (String -> NP -> Symbol -> Space -> QuantityDict
vc String
i NP
des Symbol
sym Space
space))

-- | Creates a new ConstrainedChunk from either a 'ConstrainedChunk', 'ConstrConcept', 'UncertainChunk', or an 'UncertQ'.
cnstrw :: (Quantity c, Constrained c, HasReasVal c, MayHaveUnit c) => c -> ConstrainedChunk
cnstrw :: forall c.
(Quantity c, Constrained c, HasReasVal c, MayHaveUnit c) =>
c -> ConstrainedChunk
cnstrw c
c = QuantityDict -> [ConstraintE] -> Maybe Expr -> ConstrainedChunk
ConstrainedChunk (forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw c
c) (c
c forall s a. s -> Getting a s a -> a
^. forall c. Constrained c => Lens' c [ConstraintE]
constraints) (c
c forall s a. s -> Getting a s a -> a
^. forall c. HasReasVal c => Lens' c (Maybe Expr)
reasVal)

-- | ConstrConcepts are conceptual symbolic quantities ('DefinedQuantityDict')
-- with 'Constraint's and maybe a reasonable value (no units!).
-- Similar to 'ConstrainedChunk' but includes a definition and domain. 
--
-- Ex. Measuring the length of a pendulum arm could be a concept that has some reasonable value
-- (between 1 cm and 2 m) and the constraint that the length cannot be a negative value.
data ConstrConcept = ConstrConcept { ConstrConcept -> DefinedQuantityDict
_defq    :: DefinedQuantityDict
                                   , ConstrConcept -> [ConstraintE]
_constr' :: [ConstraintE]
                                   , ConstrConcept -> Maybe Expr
_reasV'  :: Maybe Expr
                                   }
makeLenses ''ConstrConcept

-- | Finds 'UID' of the 'DefinedQuantityDict' used to make the 'ConstrConcept'.
instance HasUID        ConstrConcept where uid :: Lens' ConstrConcept UID
uid = Lens' ConstrConcept DefinedQuantityDict
defq forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid
-- | Finds term ('NP') of the 'DefinedQuantityDict' used to make the 'ConstrConcept'.
instance NamedIdea     ConstrConcept where term :: Lens' ConstrConcept NP
term = Lens' ConstrConcept DefinedQuantityDict
defq forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. NamedIdea c => Lens' c NP
term
-- | Finds the idea contained in the 'DefinedQuantityDict' used to make the 'ConstrConcept'.
instance Idea          ConstrConcept where getA :: ConstrConcept -> Maybe String
getA = forall c. Idea c => c -> Maybe String
getA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ConstrConcept DefinedQuantityDict
defq
-- | Finds the 'Space' of the 'DefinedQuantityDict' used to make the 'ConstrConcept'.
instance HasSpace      ConstrConcept where typ :: Getter ConstrConcept Space
typ = Lens' ConstrConcept DefinedQuantityDict
defq forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasSpace c => Getter c Space
typ
-- | Finds the 'Symbol' of the 'DefinedQuantityDict' used to make the 'ConstrConcept'.
instance HasSymbol     ConstrConcept where symbol :: ConstrConcept -> Stage -> Symbol
symbol ConstrConcept
c = forall c. HasSymbol c => c -> Stage -> Symbol
symbol (ConstrConcept
cforall s a. s -> Getting a s a -> a
^.Lens' ConstrConcept DefinedQuantityDict
defq)
-- | 'ConstrConcept's have a 'Quantity'. 
instance Quantity      ConstrConcept where
-- | Finds definition of the 'DefinedQuantityDict' used to make the 'ConstrConcept'.
instance Definition    ConstrConcept where defn :: Lens' ConstrConcept Sentence
defn = Lens' ConstrConcept DefinedQuantityDict
defq forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. Definition c => Lens' c Sentence
defn
-- | Finds the domain contained in the 'DefinedQuantityDict' used to make the 'ConstrConcept'.
instance ConceptDomain ConstrConcept where cdom :: ConstrConcept -> [UID]
cdom = forall c. ConceptDomain c => c -> [UID]
cdom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ConstrConcept DefinedQuantityDict
defq
-- | Finds the 'Constraint's of a 'ConstrConcept'.
instance Constrained   ConstrConcept where constraints :: Lens' ConstrConcept [ConstraintE]
constraints  = Lens' ConstrConcept [ConstraintE]
constr'
-- | Finds a reasonable value for the 'ConstrConcept'.
instance HasReasVal    ConstrConcept where reasVal :: Lens' ConstrConcept (Maybe Expr)
reasVal      = Lens' ConstrConcept (Maybe Expr)
reasV'
-- | Equal if 'UID's are equal.
instance Eq            ConstrConcept where ConstrConcept
c1 == :: ConstrConcept -> ConstrConcept -> Bool
== ConstrConcept
c2 = (ConstrConcept
c1 forall s a. s -> Getting a s a -> a
^.Lens' ConstrConcept DefinedQuantityDict
defqforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall c. HasUID c => Lens' c UID
uid) forall a. Eq a => a -> a -> Bool
== (ConstrConcept
c2 forall s a. s -> Getting a s a -> a
^.Lens' ConstrConcept DefinedQuantityDict
defqforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall c. HasUID c => Lens' c UID
uid)
-- | Finds the units of the 'DefinedQuantityDict' used to make the 'ConstrConcept'.
instance MayHaveUnit   ConstrConcept where getUnit :: ConstrConcept -> Maybe UnitDefn
getUnit = forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ConstrConcept DefinedQuantityDict
defq
-- | Convert the symbol of the 'ConstrConcept' to a 'ModelExpr'.
instance Express       ConstrConcept where express :: ConstrConcept -> ModelExpr
express = forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy

-- | Creates a 'ConstrConcept' with a quantitative concept, a list of 'Constraint's and an 'Expr'.
constrained' :: (Concept c, MayHaveUnit c, Quantity c) =>
  c -> [ConstraintE] -> Expr -> ConstrConcept
constrained' :: forall c.
(Concept c, MayHaveUnit c, Quantity c) =>
c -> [ConstraintE] -> Expr -> ConstrConcept
constrained' c
q [ConstraintE]
cs Expr
rv = DefinedQuantityDict -> [ConstraintE] -> Maybe Expr -> ConstrConcept
ConstrConcept (forall c.
(Quantity c, Concept c, MayHaveUnit c) =>
c -> DefinedQuantityDict
dqdWr c
q) [ConstraintE]
cs (forall a. a -> Maybe a
Just Expr
rv)

-- | Similar to 'constrained'', but defaults 'Maybe' 'Expr' to 'Nothing'.
constrainedNRV' :: (Concept c, MayHaveUnit c, Quantity c) =>
  c -> [ConstraintE] -> ConstrConcept
constrainedNRV' :: forall c.
(Concept c, MayHaveUnit c, Quantity c) =>
c -> [ConstraintE] -> ConstrConcept
constrainedNRV' c
q [ConstraintE]
cs = DefinedQuantityDict -> [ConstraintE] -> Maybe Expr -> ConstrConcept
ConstrConcept (forall c.
(Quantity c, Concept c, MayHaveUnit c) =>
c -> DefinedQuantityDict
dqdWr c
q) [ConstraintE]
cs forall a. Maybe a
Nothing

-- | Creates a constrained unitary chunk from a 'UID', term ('NP'), description ('String'), 'Symbol', unit, 'Space', 'Constraint's, and an 'Expr'.
cuc' :: (IsUnit u) => String -> NP -> String -> Symbol -> u
            -> Space -> [ConstraintE] -> Expr -> ConstrConcept
cuc' :: forall u.
IsUnit u =>
String
-> NP
-> String
-> Symbol
-> u
-> Space
-> [ConstraintE]
-> Expr
-> ConstrConcept
cuc' String
nam NP
trm String
desc Symbol
sym u
un Space
space [ConstraintE]
cs Expr
rv =
  DefinedQuantityDict -> [ConstraintE] -> Maybe Expr -> ConstrConcept
ConstrConcept (forall u.
IsUnit u =>
ConceptChunk -> Symbol -> Space -> u -> DefinedQuantityDict
dqd (forall c. Concept c => c -> ConceptChunk
cw (forall u.
IsUnit u =>
String -> NP -> Sentence -> Symbol -> Space -> u -> UnitalChunk
uc' String
nam NP
trm (String -> Sentence
S String
desc) Symbol
sym Space
space u
un)) Symbol
sym Space
space UnitDefn
uu) [ConstraintE]
cs (forall a. a -> Maybe a
Just Expr
rv)
  where uu :: UnitDefn
uu = forall u. IsUnit u => u -> UnitDefn
unitWrapper u
un

-- | Similar to 'cuc'', but 'Symbol' is dependent on 'Stage'.
cuc'' :: (IsUnit u) => String -> NP -> String -> (Stage -> Symbol) -> u
            -> Space -> [ConstraintE] -> Expr -> ConstrConcept
cuc'' :: forall u.
IsUnit u =>
String
-> NP
-> String
-> (Stage -> Symbol)
-> u
-> Space
-> [ConstraintE]
-> Expr
-> ConstrConcept
cuc'' String
nam NP
trm String
desc Stage -> Symbol
sym u
un Space
space [ConstraintE]
cs Expr
rv =
  DefinedQuantityDict -> [ConstraintE] -> Maybe Expr -> ConstrConcept
ConstrConcept (ConceptChunk
-> (Stage -> Symbol)
-> Space
-> Maybe UnitDefn
-> DefinedQuantityDict
dqd' (String -> NP -> String -> ConceptChunk
dcc String
nam NP
trm String
desc) Stage -> Symbol
sym Space
space (forall a. a -> Maybe a
Just UnitDefn
uu)) [ConstraintE]
cs (forall a. a -> Maybe a
Just Expr
rv)
  where uu :: UnitDefn
uu = forall u. IsUnit u => u -> UnitDefn
unitWrapper u
un

-- | Similar to 'cnstrw', but types must also have a 'Concept'.
cnstrw' :: (Quantity c, Concept c, Constrained c, HasReasVal c, MayHaveUnit c) => c -> ConstrConcept
cnstrw' :: forall c.
(Quantity c, Concept c, Constrained c, HasReasVal c,
 MayHaveUnit c) =>
c -> ConstrConcept
cnstrw' c
c = DefinedQuantityDict -> [ConstraintE] -> Maybe Expr -> ConstrConcept
ConstrConcept (forall c.
(Quantity c, Concept c, MayHaveUnit c) =>
c -> DefinedQuantityDict
dqdWr c
c) (c
c forall s a. s -> Getting a s a -> a
^. forall c. Constrained c => Lens' c [ConstraintE]
constraints) (c
c forall s a. s -> Getting a s a -> a
^. forall c. HasReasVal c => Lens' c (Maybe Expr)
reasVal)