1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=7 #-}
4 -- | Symantic for 'Quantity'.
5 module Hcompta.LCC.Sym.Quantity where
8 import Data.Function (($), (.))
9 import Data.Functor ((<$>))
10 import Data.Maybe (Maybe(..))
12 import Data.Type.Equality ((:~:)(Refl))
13 import Prelude (Enum, Num, Real)
14 import Text.Show (Show(..))
15 import qualified Data.Text as Text
17 import Hcompta (Addable, Negable, Subable)
18 import Hcompta.LCC.Amount
19 import Hcompta.LCC.Grammar
21 import Language.Symantic.Grammar as Sym
22 import Language.Symantic
23 import Language.Symantic.Lib ()
25 -- * Class 'Sym_Quantity'
26 type instance Sym (Proxy Quantity) = Sym_Quantity
27 class Sym_Quantity term where
28 quantity :: Quantity -> term Quantity
29 default quantity :: Sym_Quantity (UnT term) => Trans term => Quantity -> term Quantity
30 quantity = trans . quantity
32 instance Sym_Quantity Eval where
34 instance Sym_Quantity View where
35 quantity a = View $ \_p _v -> Text.pack (show a)
36 instance (Sym_Quantity r1, Sym_Quantity r2) => Sym_Quantity (Dup r1 r2) where
37 quantity x = quantity x `Dup` quantity x
38 instance (Sym_Quantity term, Sym_Lambda term) => Sym_Quantity (BetaT term)
40 instance ClassInstancesFor Quantity where
41 proveConstraintFor _c (TyApp _ (TyConst _ _ q) c)
42 | Just HRefl <- proj_ConstKiTy @(K Quantity) @Quantity c
44 _ | Just Refl <- proj_Const @Addable q -> Just Dict
45 | Just Refl <- proj_Const @Negable q -> Just Dict
46 | Just Refl <- proj_Const @Subable q -> Just Dict
47 | Just Refl <- proj_Const @Enum q -> Just Dict
48 | Just Refl <- proj_Const @Eq q -> Just Dict
49 -- | Just Refl <- proj_Const @Fractional q -> Just Dict
50 | Just Refl <- proj_Const @Num q -> Just Dict
51 | Just Refl <- proj_Const @Ord q -> Just Dict
52 | Just Refl <- proj_Const @Real q -> Just Dict
53 -- | Just Refl <- proj_Const @RealFrac q -> Just Dict
54 | Just Refl <- proj_Const @Show q -> Just Dict
56 proveConstraintFor _c _q = Nothing
57 instance TypeInstancesFor Quantity
58 instance -- Gram_Term_AtomsFor
66 ) => Gram_Term_AtomsFor src ss g Quantity where
70 (\(qty, _sty) src -> BinTree0 $ Token_Term $ TermAVT $ (`setSource` src) $ teQuantity qty)
72 -- <$> some (choice $ char <$> ['0'..'9'])
73 -- <*> option [] ((:) <$> char '.' <*> some (choice $ char <$> ['0'..'9']))
75 instance (Source src, Inj_Sym ss Quantity) => ModuleFor src ss Quantity where
76 moduleFor _s = ["Quantity"] `moduleWhere`
80 tyQuantity :: Source src => Inj_Len vs => Type src vs Quantity
81 tyQuantity = tyConst @(K Quantity) @Quantity
83 teQuantity :: Source src => Inj_Sym ss Quantity => Quantity -> Term src ss ts '[] Quantity
84 teQuantity a = Term noConstraint tyQuantity $ teSym @Quantity $ quantity a