Hi everyone.
Currently I’m playing with type families and talking to chatGPT and came here to ask help and suggestions because GPT suggests mad examples.
Suppose I wanna take some data from stdin with help of optparse-applicative and store it in the following type
data Raw
data Validated
data Test a = Test
{ field1 :: Maybe Text
, field2 :: Maybe Text
}
I’ve deliberately chosen the following way - optparse parses and stores the data in the Test Raw
type - I’m not interested to put any logic on the parsing stage, I just want to make my life easier by validating Maybe fields instead of bare types - and then I wanna validate that type and return Test Validated
- I’m practicing Use a data structure that makes illegal states unrepresentable
I also have this type class
class Validate a where
validate :: a Raw -> Either Error (a Validated)
In the end, I wanna have type Test looks like this
data Test a = Test
{ field1 :: Text
, field2 :: Text
}
I know that I can do that with type families without involving other types
type family Field a b c where
Field Raw b _ = b
Field Validated _ c = c
data Test a = Test
{ field1 :: Field a (Maybe Text) Text
, field2 :: Field a (Maybe Text) Text
}
However, I have several questions:
- how to use coerce with type families in that case? I just want to convert
Test Raw
toTest Validated
without involving creating a new term - I always had troubles with understanding of Parse, don’t validate approach, so the question: am I implemented it correctly? based on examples and description that I provided
Your help will be appreciated, and sorry for English - it’s not my native
Thanks!