haskell - Creating (getting) a value instance from a phantom type -
I am using GADT to create original dimensions for currencies (according to the physical dimensions). Dimensions (such as USD, USD / EUR, EUR / USD) are displayed in the form of phantom type I would like to be able to print the quantity of a currency in the style of the example "10.3USD" or "0EUR" and rates such as show By using "10.3USD / EUR" I'm not sure how to describe my problem, so I would go for an example of how to solve it:
{- # LANGUAGE GADTs # -} class) = & gt; Currency is a where unphantom :: A data USD = USD USD Show data EUR = Euro show example currency currency USD where unphantom = USD example currency EUR where unphantom = EUR data zodiac where one location :: currency one => Float - & gt; Show an example amount (amount A) where show (zodiac x) = show X ++ shows (Unfantom :: A) data where rate :: (currency A, currency B) => Float - & gt; Rate AB - ...
With this code, I get an error
$ ghc example.hs [1 of 1] Compile main (Example .hs, example.o) example.hs: 14: 37: Currency (1) arising out of the use of 'unfantom' from the reference (currency A) binded by a pattern with the constructor can not be extracted. Currency A = & gt; Float - & gt; For example, the amount in the equation of `show ': for example HS: 14: 9-16 potential improvement: in the context of expression type sign (currency a1) add: A1 or data constructor' zodiac 'or example declaration The first argument of `show 'i.e., in the second argument (` +++)' of `(unfontom :: A) ', i.e.' Show (Unfantom :: A) 'in the expression: Show X + show (Unfantam :: A )
I should say that I do not understand that in this case Why is the compiler talking about a a1
type when I a
Of course, I do not want to represent the dimensions outside the Haskell type system because this adds extra boilerplate code to me and as far as I can say theoretically unnecessary (i.e. compiler should be). >
ScopedTypeVariables and your code has been compiled.
Typically, without ScopedTypeVariables
when you type
example Show (zodiac A) where shows (zodiac x) = show X + show (unphantom :: A)
the a
Unfortem :: A
is fresh and it is not designed to be combined with a
forces you to unite it. example (zodiac A) where
. < Turning on Code> ScopedTypeVariables
Comments
Post a Comment