Skip to content

Commit

Permalink
Add HasUnits class and implement instance to UnitDefn and DefinedQuan…
Browse files Browse the repository at this point in the history
…tityDict.
  • Loading branch information
Yuzhi Zhao committed Jul 18, 2018
1 parent e61581d commit 413550c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion code/drasil-lang/Language/Drasil.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module Language.Drasil (
-- Unit
, UnitDefn(..), unitWrapper
, unitCon, fund, comp_unitdefn, makeDerU, unitWrapper'
, (^:), (/:), (*:), (*$), (/$), (^$), new_unit, getsymb,getCu,getunit
, (^:), (/:), (*:), (*$), (/$), (^$), new_unit, getsymb, getCu
-- UID
, UID
-- Classes
Expand Down
9 changes: 6 additions & 3 deletions code/drasil-lang/Language/Drasil/Chunk/DefinedQuantity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import Language.Drasil.Chunk.Derivation (Derivation)
import Language.Drasil.Classes (HasUID(uid), NamedIdea(term), Idea(getA),
Definition(defn), ConceptDomain(cdom), Concept, HasSymbol(symbol),
HasSpace(typ), IsUnit, HasDerivation(derivations),
IsUnit(udefn))
IsUnit(udefn), HasUnits(getUnits))
import Language.Drasil.Chunk.Concept (ConceptChunk, cw)
import qualified Language.Drasil.Chunk.Quantity as Q
import Language.Drasil.UID
import Language.Drasil.Symbol (Symbol, Stage)
import Language.Drasil.Space (Space)
import Language.Drasil.Development.Unit (UnitDefn, unitWrapper, getunit)
import Language.Drasil.Development.Unit (UnitDefn, unitWrapper)
import Language.Drasil.Chunk.Derivation (Derivation)

import Control.Lens ((^.), makeLenses, view)
Expand All @@ -43,6 +43,9 @@ instance Q.HasSpace DefinedQuantityDict where typ = spa
instance HasSymbol DefinedQuantityDict where symbol = view symb
instance Q.Quantity DefinedQuantityDict where getUnit = view unit'
instance HasDerivation DefinedQuantityDict where derivations = deri
instance HasUnits DefinedQuantityDict where getUnits c = case c ^. unit' of
Nothing -> []
Just a -> getUnits a

-- For when the symbol is constant through stages
dqd :: ConceptChunk -> Symbol -> Space -> Maybe UnitDefn -> DefinedQuantityDict
Expand Down Expand Up @@ -71,4 +74,4 @@ uwMUnitDefn (Just a) = [a]
uwMUnitDefn Nothing = []

uwMUnitDefnL :: [DefinedQuantityDict] -> [UID]
uwMUnitDefnL l = concat (map getunit $ concat (map uwMUnitDefn $ uwDQDL l))
uwMUnitDefnL l = concatMap getUnits l
4 changes: 4 additions & 0 deletions code/drasil-lang/Language/Drasil/Classes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Language.Drasil.Classes (
, HasDerivation(derivations)
, HasAdditionalNotes(getNotes)
, HasRefAddress(getRefAdd)
, HasUnits(getUnits)
) where

import Language.Drasil.Chunk.Constrained.Core (Constraint)
Expand Down Expand Up @@ -132,3 +133,6 @@ class UnitEq u where
-- TODO : there is a design bug here not at all apparent from its definition; have to come back to it (Pull Request #532)
class ExprRelat c where
relat :: Lens' c Expr

class HasUnits c where
getUnits :: c -> [UID]
4 changes: 2 additions & 2 deletions code/drasil-lang/Language/Drasil/Development.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Language.Drasil.Development (
, scale, shift, fshift, fscale
, derUC, derUC', derUC'', unitWrapper
, fund, comp_unitdefn, derCUC, derCUC', derCUC'', getsymb
, makeDerU, getunit, unitWrapper',getCu
, makeDerU, unitWrapper',getCu
-- UnitLang
, USymb(US), UDefn(..)
, from_udefn, comp_usymb
Expand All @@ -19,6 +19,6 @@ import Language.Drasil.Development.Unit (UnitDefn(..)
, scale, shift, fshift, fscale
, derUC, derUC', derUC'', unitWrapper
, fund, comp_unitdefn, derCUC, derCUC', derCUC'', getsymb
, makeDerU, getunit, unitWrapper',getCu)
, makeDerU, unitWrapper',getCu)
import Language.Drasil.Development.UnitLang (UDefn(..), USymb(US), comp_usymb,
from_udefn)
12 changes: 5 additions & 7 deletions code/drasil-lang/Language/Drasil/Development/Unit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ module Language.Drasil.Development.Unit (
, scale, shift, fshift, fscale
, derUC, derUC', derUC'', unitWrapper
, fund, comp_unitdefn, derCUC, derCUC', derCUC'', getsymb
, makeDerU, getunit, unitWrapper',getCu
, makeDerU, unitWrapper',getCu
) where

import Control.Lens (Simple, Lens', Lens, (^.), makeLenses, view)
import Control.Arrow (second)

import Language.Drasil.Classes (HasUID(uid), NamedIdea(term), Idea(getA),
Definition(defn), ConceptDomain(cdom), HasUnitSymbol(usymb), IsUnit(udefn),
UnitEq(uniteq))
UnitEq(uniteq), HasUnits(getUnits))
import Language.Drasil.Chunk.Concept (ConceptChunk, dcc, cc')
import Language.Drasil.Symbol (Symbol(Atomic))
import Language.Drasil.Development.UnitLang (USymb(US),
Expand All @@ -40,9 +40,7 @@ instance ConceptDomain UnitDefn where
cdom = vc . cdom
instance HasUnitSymbol UnitDefn where usymb f (UD a b c e d) = fmap (\x -> UD a x c e d) (f b)
instance IsUnit UnitDefn where udefn = ud

getunit :: UnitDefn -> [UID]
getunit a = view cu a
instance HasUnits UnitDefn where getUnits c = c ^. cu

data UnitEquation = UE {_contributingUnit :: [UID], _us :: USymb}
makeLenses ''UnitEquation
Expand Down Expand Up @@ -90,15 +88,15 @@ unitWrapper :: (IsUnit u) => u -> UnitDefn
unitWrapper u = UD (cc' u (u ^. defn)) (u ^. usymb) Nothing (u ^. udefn) []

unitWrapper' :: UnitDefn -> UnitDefn
unitWrapper' u = UD (cc' u (u ^. defn)) (u ^. usymb) Nothing (u ^. udefn) (getunit u)
unitWrapper' u = UD (cc' u (u ^. defn)) (u ^. usymb) Nothing (u ^. udefn) (getUnits u)

helperUnit :: UnitDefn -> [UID]
helperUnit a = case a ^. udefn of
Just x -> case x of
FUSynonym _ -> [a ^. uid]
FUScale _ _ -> [a ^. uid]
FUShift _ _ -> [a ^. uid]
_ -> getunit a
_ -> getUnits a
Nothing -> [a ^. uid]

--- These conveniences go here, because we need the class
Expand Down

0 comments on commit 413550c

Please sign in to comment.