module Language.Drasil.UnitLang (
USymb(US), UDefn(..), UnitSymbol(BaseSI, DerivedSI, Defined)
, fromUDefn, compUSymb, getUSymb, getDefn
) where
import Language.Drasil.Symbol (Symbol, compsy)
import Data.Foldable (foldl')
newtype USymb = US [(Symbol, Integer)]
deriving (USymb -> USymb -> Bool
(USymb -> USymb -> Bool) -> (USymb -> USymb -> Bool) -> Eq USymb
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: USymb -> USymb -> Bool
== :: USymb -> USymb -> Bool
$c/= :: USymb -> USymb -> Bool
/= :: USymb -> USymb -> Bool
Eq)
data UDefn = USynonym USymb
| UScale Double USymb
| UShift Double USymb
fromUDefn :: UDefn -> USymb
fromUDefn :: UDefn -> USymb
fromUDefn (USynonym USymb
x) = USymb
x
fromUDefn (UScale Double
_ USymb
s) = USymb
s
fromUDefn (UShift Double
_ USymb
s) = USymb
s
compUSymb :: USymb -> USymb -> Ordering
compUSymb :: USymb -> USymb -> Ordering
compUSymb (US [(Symbol, Integer)]
l) (US [(Symbol, Integer)]
m) = (Ordering -> Ordering -> Ordering)
-> Ordering -> [Ordering] -> Ordering
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
mappend Ordering
EQ ([Ordering] -> Ordering) -> [Ordering] -> Ordering
forall a b. (a -> b) -> a -> b
$ ((Symbol, Integer) -> (Symbol, Integer) -> Ordering)
-> [(Symbol, Integer)] -> [(Symbol, Integer)] -> [Ordering]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Symbol, Integer) -> (Symbol, Integer) -> Ordering
forall {a}. Ord a => (Symbol, a) -> (Symbol, a) -> Ordering
comp [(Symbol, Integer)]
l [(Symbol, Integer)]
m
where
comp :: (Symbol, a) -> (Symbol, a) -> Ordering
comp (Symbol
s1, a
i1) (Symbol
s2, a
i2) = Symbol -> Symbol -> Ordering
compsy Symbol
s1 Symbol
s2 Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend` a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
i1 a
i2
data UnitSymbol =
BaseSI USymb
| DerivedSI USymb USymb UDefn
| Defined USymb UDefn
getUSymb :: UnitSymbol -> USymb
getUSymb :: UnitSymbol -> USymb
getUSymb (BaseSI USymb
u) = USymb
u
getUSymb (DerivedSI USymb
u USymb
_ UDefn
_) = USymb
u
getUSymb (Defined USymb
u UDefn
_) = USymb
u
getDefn :: UnitSymbol -> Maybe UDefn
getDefn :: UnitSymbol -> Maybe UDefn
getDefn (BaseSI USymb
_) = Maybe UDefn
forall a. Maybe a
Nothing
getDefn (DerivedSI USymb
_ USymb
_ UDefn
d) = UDefn -> Maybe UDefn
forall a. a -> Maybe a
Just UDefn
d
getDefn (Defined USymb
_ UDefn
d) = UDefn -> Maybe UDefn
forall a. a -> Maybe a
Just UDefn
d