r/purescript • u/Crystelium • Apr 01 '19
Issues With Equating Types
I'm having an issue with a piece of code which converts types.
Currently, I'm running a remote procedure call which returns a result in the format "value type" where value is the computed value and type is the type of that value. For example: "12 i32" or "0.5 f32" where i32 is an Int and f32 is a Float (i.e. Number in PureScript.) I want to use this to then convert the value to the specified type.
Admittedly, I'm pretty new to FP - but I'd do this as a GADT in Haskell. I tried to do something similar in PureScript using purescript-leibniz and got pretty confused:
data WasmType a =
I32 Int (a ~ Int) |
F32 Number (a ~ Number) |
ErrorType
convertType :: forall a. Maybe String -> Maybe String -> WasmType a
convertType (Just "i32") (Just v) = I32 (fromMaybe 0 $ I.fromString v) identity
convertType (Just "f32") (Just v) = F32 (fromMaybe 0 $ N.fromString v) identity
convertType _ _ = ErrorType
parseResult :: forall a. String -> WasmType a
parseResult s = do
let splits = split (Pattern " ") s
rType <- head splits
rValue <- last splits
convertType rType rValue
It complains that it can't match type Int with type a0. What exactly am I doing wrong here?
2
u/natefaubion Apr 01 '19
You have to use
coerceorcoerceSymmwith the equality. PureScript does not propagate type equalities, so you have to manually apply it.2
u/gb__ Apr 01 '19
What you're trying to do there isn't going to work - if you try a similar thing with GADTs in Haskell you'll find the same problem:
Results in:
Assuming you could have `WasmType a` as a result from this function, that doesn't really help you out as you've now lost the information about which type it's carrying - you may as well not be using Leibniz / a GADT at all.
I think what you'd want here really is a bit of a different way of dealing with things, and defining an "f-algebra" for requests that return a particular type, then you can interpret that, and have the knowledge of the expected type on hand:
Then you can say things like: