{-# Language TemplateHaskell, RankNTypes #-}
-- | Defines types and functions for Theoretical Models.
module Theory.Drasil.Theory (
  -- * Class
  Theory(..),
  -- * Type
  TheoryModel,
  -- * Constructors
  tm, tmNoRefs) where

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

import Language.Drasil
import Language.Drasil.Development (showUID)
import Data.Drasil.TheoryConcepts (thModel)

import Theory.Drasil.ModelKinds

-- | Theories are the basis for building models with context,
-- spaces, quantities, operations, invariants, etc.
class Theory t where
  valid_context :: Lens' t [TheoryModel]
  spaces        :: Lens' t [SpaceDefn]
  quantities    :: Lens' t [QuantityDict]
  operations    :: Lens' t [ConceptChunk] -- FIXME: Should not be Concept
  defined_quant :: Lens' t [ModelQDef]
  invariants    :: Lens' t [ModelExpr]
  defined_fun   :: Lens' t [ModelQDef]

data SpaceDefn -- FIXME: This should be defined.

-- | A TheoryModel is a collection of:
--
--      * tUid - a UID,
--      * con - a ConceptChunk,
--      * vctx - definition context ('TheoryModel's),
--      * spc - type definitions ('SpaceDefn's),
--      * quan - quantities ('QuantityDict's),
--      * ops - operations ('ConceptChunk's),
--      * defq - definitions ('QDefinition's),
--      * invs - invariants ('ModelExpr's),
--      * dfun - defined functions ('QDefinition's),
--      * ref - accompanying references ('DecRef's),
--      * lb - a label ('SpaceDefn'),
--      * ra - reference address ('SpaceDefn'),
--      * notes - additional notes ('Sentence's).
-- 
-- Right now, neither the definition context (vctx) nor the
-- spaces (spc) are ever defined.
data TheoryModel = TM 
  { TheoryModel -> ModelKind ModelExpr
_mk    :: ModelKind ModelExpr
  , TheoryModel -> [TheoryModel]
_vctx  :: [TheoryModel]
  , TheoryModel -> [SpaceDefn]
_spc   :: [SpaceDefn]
  , TheoryModel -> [QuantityDict]
_quan  :: [QuantityDict]
  , TheoryModel -> [ConceptChunk]
_ops   :: [ConceptChunk]
  , TheoryModel -> [ModelQDef]
_defq  :: [ModelQDef]
  , TheoryModel -> [ModelExpr]
_invs  :: [ModelExpr]
  , TheoryModel -> [ModelQDef]
_dfun  :: [ModelQDef]
  , TheoryModel -> [DecRef]
_rf    :: [DecRef]
  ,  TheoryModel -> ShortName
lb    :: ShortName
  ,  TheoryModel -> String
ra    :: String
  , TheoryModel -> [Sentence]
_notes :: [Sentence]
  }
makeLenses ''TheoryModel

-- | Finds the 'UID' of a 'TheoryModel'.
instance HasUID             TheoryModel where uid :: Lens' TheoryModel UID
uid = Lens' TheoryModel (ModelKind ModelExpr)
mk forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid
-- | Finds the term ('NP') of the 'TheoryModel'.
instance NamedIdea          TheoryModel where term :: Lens' TheoryModel NP
term = Lens' TheoryModel (ModelKind ModelExpr)
mk forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. NamedIdea c => Lens' c NP
term
-- | Finds the idea of the 'ConceptChunk' contained in the 'TheoryModel'.
instance Idea               TheoryModel where getA :: TheoryModel -> 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' TheoryModel (ModelKind ModelExpr)
mk
-- | Finds the definition of the 'ConceptChunk' contained in a 'TheoryModel'.
instance Definition         TheoryModel where defn :: Lens' TheoryModel Sentence
defn = Lens' TheoryModel (ModelKind ModelExpr)
mk forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. Definition c => Lens' c Sentence
defn
{-- | Finds 'Reference's contained in the 'TheoryModel'.
instance HasReference       TheoryModel where getReferences l = map ref $ rf l-}
-- | Finds 'DecRef's contained in the 'TheoryModel'.
instance HasDecRef          TheoryModel where getDecRefs :: Lens' TheoryModel [DecRef]
getDecRefs = Lens' TheoryModel [DecRef]
rf
-- | Finds the domain of the 'ConceptChunk' contained in a 'TheoryModel'.
instance ConceptDomain      TheoryModel where cdom :: TheoryModel -> [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' TheoryModel (ModelKind ModelExpr)
mk
-- | Finds any additional notes for the 'TheoryModel'.
instance HasAdditionalNotes TheoryModel where getNotes :: Lens' TheoryModel [Sentence]
getNotes = Lens' TheoryModel [Sentence]
notes

-- TODO: I think we should be gathering these from the ModelKinds of the TheoryModel.
--       If we need "more than 1 ModelKind" in the TheoryModel, we may need to create 
--       a "stacked model" that allows for composing them.

-- | Finds the aspects of the 'Theory' behind the 'TheoryModel'.
instance Theory             TheoryModel where
  valid_context :: Lens' TheoryModel [TheoryModel]
valid_context = Lens' TheoryModel [TheoryModel]
vctx
  spaces :: Lens' TheoryModel [SpaceDefn]
spaces        = Lens' TheoryModel [SpaceDefn]
spc
  quantities :: Lens' TheoryModel [QuantityDict]
quantities    = Lens' TheoryModel [QuantityDict]
quan
  operations :: Lens' TheoryModel [ConceptChunk]
operations    = Lens' TheoryModel [ConceptChunk]
ops
  defined_quant :: Lens' TheoryModel [ModelQDef]
defined_quant = Lens' TheoryModel [ModelQDef]
defq
  invariants :: Lens' TheoryModel [ModelExpr]
invariants    = Lens' TheoryModel [ModelExpr]
invs
  defined_fun :: Lens' TheoryModel [ModelQDef]
defined_fun   = Lens' TheoryModel [ModelQDef]
dfun
-- | Finds the 'ShortName' of the 'TheoryModel'.
instance HasShortName       TheoryModel where shortname :: TheoryModel -> ShortName
shortname = TheoryModel -> ShortName
lb
-- | Finds the reference address of the 'TheoryModel'.
instance HasRefAddress      TheoryModel where getRefAdd :: TheoryModel -> LblType
getRefAdd TheoryModel
l = IRefProg -> String -> LblType
RP (String -> IRefProg
prepend forall a b. (a -> b) -> a -> b
$ forall c. CommonIdea c => c -> String
abrv TheoryModel
l) (TheoryModel -> String
ra TheoryModel
l)
-- | Finds the idea of a 'TheoryModel' (abbreviation).
instance CommonIdea         TheoryModel where abrv :: TheoryModel -> String
abrv TheoryModel
_ = forall c. CommonIdea c => c -> String
abrv CI
thModel
-- | Finds the reference address of a 'TheoryModel'.
instance Referable TheoryModel where
  refAdd :: TheoryModel -> String
refAdd      = TheoryModel -> String
ra
  renderRef :: TheoryModel -> LblType
renderRef TheoryModel
l = IRefProg -> String -> LblType
RP (String -> IRefProg
prepend forall a b. (a -> b) -> a -> b
$ forall c. CommonIdea c => c -> String
abrv TheoryModel
l) (forall s. Referable s => s -> String
refAdd TheoryModel
l)

-- TODO: Theory Models should generally be using their own UID, instead of
--       having their UIDs derived by the model kind.


-- This "smart" constructor is really quite awful, it takes way too many arguments.
-- This should likely be re-arranged somehow. Especially since since of the arguments
-- have the same type!
-- | Constructor for theory models. Must have a source. Uses the shortname of the reference address.
tm :: (Quantity q, MayHaveUnit q, Concept c) => ModelKind ModelExpr ->
    [q] -> [c] -> [ModelQDef] ->
    [ModelExpr] -> [ModelQDef] -> [DecRef] ->
    String -> [Sentence] -> TheoryModel
tm :: forall q c.
(Quantity q, MayHaveUnit q, Concept c) =>
ModelKind ModelExpr
-> [q]
-> [c]
-> [ModelQDef]
-> [ModelExpr]
-> [ModelQDef]
-> [DecRef]
-> String
-> [Sentence]
-> TheoryModel
tm ModelKind ModelExpr
mkind [q]
_ [c]
_ [ModelQDef]
_  [ModelExpr]
_   [ModelQDef]
_   [] String
_   = forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Source field of " forall a. [a] -> [a] -> [a]
++ forall a. HasUID a => a -> String
showUID ModelKind ModelExpr
mkind forall a. [a] -> [a] -> [a]
++ String
" is empty"
tm ModelKind ModelExpr
mkind [q]
q [c]
c [ModelQDef]
dq [ModelExpr]
inv [ModelQDef]
dfn [DecRef]
r  String
lbe = 
  ModelKind ModelExpr
-> [TheoryModel]
-> [SpaceDefn]
-> [QuantityDict]
-> [ConceptChunk]
-> [ModelQDef]
-> [ModelExpr]
-> [ModelQDef]
-> [DecRef]
-> ShortName
-> String
-> [Sentence]
-> TheoryModel
TM ModelKind ModelExpr
mkind [] [] (forall a b. (a -> b) -> [a] -> [b]
map forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw [q]
q) (forall a b. (a -> b) -> [a] -> [b]
map forall c. Concept c => c -> ConceptChunk
cw [c]
c) [ModelQDef]
dq [ModelExpr]
inv [ModelQDef]
dfn [DecRef]
r (Sentence -> ShortName
shortname' forall a b. (a -> b) -> a -> b
$ String -> Sentence
S String
lbe)
      (forall c. CommonIdea c => c -> String -> String
prependAbrv CI
thModel String
lbe)

-- | Constructor for theory models. Uses the shortname of the reference address.
tmNoRefs :: (Quantity q, MayHaveUnit q, Concept c) => ModelKind ModelExpr ->
    [q] -> [c] -> [ModelQDef] -> [ModelExpr] -> [ModelQDef] -> 
    String -> [Sentence] -> TheoryModel
tmNoRefs :: forall q c.
(Quantity q, MayHaveUnit q, Concept c) =>
ModelKind ModelExpr
-> [q]
-> [c]
-> [ModelQDef]
-> [ModelExpr]
-> [ModelQDef]
-> String
-> [Sentence]
-> TheoryModel
tmNoRefs ModelKind ModelExpr
mkind [q]
q [c]
c [ModelQDef]
dq [ModelExpr]
inv [ModelQDef]
dfn String
lbe = 
  ModelKind ModelExpr
-> [TheoryModel]
-> [SpaceDefn]
-> [QuantityDict]
-> [ConceptChunk]
-> [ModelQDef]
-> [ModelExpr]
-> [ModelQDef]
-> [DecRef]
-> ShortName
-> String
-> [Sentence]
-> TheoryModel
TM ModelKind ModelExpr
mkind [] [] (forall a b. (a -> b) -> [a] -> [b]
map forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw [q]
q) (forall a b. (a -> b) -> [a] -> [b]
map forall c. Concept c => c -> ConceptChunk
cw [c]
c) [ModelQDef]
dq [ModelExpr]
inv [ModelQDef]
dfn [] (Sentence -> ShortName
shortname' forall a b. (a -> b) -> a -> b
$ String -> Sentence
S String
lbe)
      (forall c. CommonIdea c => c -> String -> String
prependAbrv CI
thModel String
lbe)