generic way to construct and deconstruct a newtype

Hello, I try to write some code use to parse an ini file for one of my softwares, using config-ini[1]. In this config file I have scientific values with units, meter, angstrom, degrees, etc... So I created a bunch of newtype in order to deal with this part of the config file newtype Meter = Meter (Length Double) deriving (Eq, Show) newType Angstrom = Angstrom (Length Double) [...] Then I created a typeclasse in order to have a generic way to extract the fileds values using the BiDir api [2] class HasFieldValue a where fieldvalue :: FieldValue a instance HasFieldValue Meter where fieldvalue = FieldValue { fvParse = mapRight (Meter . (*~ meter)) . fvParse auto , fvEmit = \(Meter m) -> pack . show . (/~ meter) $ m } instance HasFieldValue Angstrom where fieldvalue = FieldValue { fvParse = mapRight (Meter . (*~ angstrom)) . fvParse auto , fvEmit = \(Angstrom m) -> pack . show . (/~ angstrom) $ m } I would like to factorize via a method which should take at least an unit, in order to avoid repetition. I started with this sort of funtion, but I do not know how to write something generic for <???> numberUnit :: (Num a, Fractional a, Read a, Show a, Typeable a) => Unit m d a -> FieldValue (Quantity d a) numberUnit u = FieldValue { fvParse = mapRight (<???> . (*~ u)) . fvParse number' , fvEmit = \(<???> v) -> pack . show . (/~ u) $ v } thanks for your help [1] https://hackage.haskell.org/package/config-ini-0.2.4.0 [2] https://hackage.haskell.org/package/config-ini-0.2.4.0/docs/Data-Ini-Config-...

On Tue, 1 Mar 2022, PICCA Frederic-Emmanuel wrote:
Hello, I try to write some code use to parse an ini file for one of my softwares, using config-ini[1].
In this config file I have scientific values with units, meter, angstrom, degrees, etc... So I created a bunch of newtype in order to deal with this part of the config file
newtype Meter = Meter (Length Double) deriving (Eq, Show)
newType Angstrom = Angstrom (Length Double)
[...]
I would define newtype Quantity dimension a = Quantity a or use one of the libraries for physical dimensions. Then I would convert all values to SI standard units, i.e. all lengths to meter. If you must preserve the units from the config file, I would instead define newtype Quantity dimension unit a = Quantity a

or use one of the libraries for physical dimensions. Then I would convert all values to SI standard units, i.e. all lengths to meter. If you must preserve the units from the config file, I would instead define
I am already using the dimensional library, I need to check if I can use the low lovel Quantity definition in order to define the right instance.

If I look at the definition of an unit in dimensional I have this angstrom :: (Fractional a) => Unit 'NonMetric DLength a angstrom = mkUnitQ (ucum "Ao" "Å" "Ångström") 0.1 $ nano meter metre, meter :: Num a => Unit 'Metric DLength a metre = mkUnitZ I.nMeter 1 siUnit -- International English. meter = metre -- American English. So it seems thaht I can not differenciate angstrom and metter at the type level.
participants (2)
-
Henning Thielemann
-
PICCA Frederic-Emmanuel