[Haskell-cafe] Explicitly calling syntactic equality on datatypes
Juan Casanova
juan.casanova at ed.ac.uk
Wed Sep 18 13:34:36 UTC 2019
Quoting MarLinn <monkleyon at gmail.com> on Wed, 18 Sep 2019 14:53:32 +0200:
> What about phantom types?
>
> {-# LANGUAGE KindSignatures, DataKinds #-}
> data IsNormalized = Normalized | NotNormalized
> data Sum (n :: IsNormalized) = Value Int | Sum (Sum n) (Sum n)
>
> normalizeSum :: Sum NotNormalized -> Sum Normalized -- or even
> create a class with two inhabitants for this
> instance Eq (Sum NotNormalized) where (==) = (==) `on` normalizeSum
> instance Eq (Sum Normalized) where … -- real implementation
That looks quite good tbh. I did not know you could do that. Care to
explain, are "Normalized" and "NotNormalized" types, data
constructors, type families? A combination of them?
Of course you still have to write a fair amount of code, but it does
look a lot better than what I've been using and what I've seen
suggested so far. At least to me.
Thanks,
Juan.
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
More information about the Haskell-Cafe
mailing list