
for a Bool-like newtype: newtype B = B Bool , is there an easy way to use this newtype B in place of Bool? e.g. let b1 = B True in if b1 then 1 else 0 ?

Imants Cekusins
for a Bool-like newtype:
newtype B = B Bool
, is there an easy way to use this newtype B in place of Bool?
e.g.
let b1 = B True in if b1 then 1 else 0
?
Imants, You can create a function that converts a B to a Bool: toBool :: B -> Bool toBool B b = b And then use the result of applying that function to your variable: let b1 = B True in if toBool b1 then 1 else 0 To do anything like this automatically would seem to me to defeat the purpose of having distinct types, which is to clearly articulate what values are appropriate in particular contexts. Mike.

True: distinct types help. In this case a function expects a couple bool args so the newtypes help ensure that correct args are passed in correct order. However when working with vals within a function, it may be handy to be able to deduce bool from the newtype, at least to be used with if. This may introduce complexity so not suggesting / requesting anything. Just thought: something might be already available. For example, num-like newtypes can be added, compared as are, without the need to extract the num. Num newtypes conveniently can be derived with "deriving". A function you suggest is good and it works. Thank you.

Hello, That kind of a function can be also declared automatically, as in: newtype B = B { toBool :: Bool } Best regards, Marcin Mrotek

As-is, no. As Michael said, that would defeat the purpose of a newtype.
However, there is a generic way: all newtypes are given instances of
Coercible, so instead of individual unwrap functions you can use coerce
from Data.Coerce.
On Aug 7, 2016 7:59 AM, "Imants Cekusins"
newtype B = B { toBool :: Bool }
yep, this works well. toBool b1 is easy enough ;)
I wondered if there was an easy (with deriving etc) way to test B as it is. If there isn't - no biggie.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Thank you Theodore.
Data.Coerce
Could you hint the name of the package, please? Does it coerce safely? If Coercible does not type check, another option could be to define a class and a function that works similar to if statement, constrained to instances of this class. class Newtype_base nt base where base_t::nt -> base if_::Newtype_base nt Bool => nt -> then -> else Standard if is clearer of course but with a few newtypes being passed around, this may save some key strokes.

... this may be correct: if_::Newtype_base nt Bool => nt -> result -> result -> result if_ if0 then0 else0 = ...

Consider replacing your bools with meaningful sum types, which avoids this
issue, prevents boolean blindness, and makes your code more
self-documenting.
For example, instead of newtype SwitchsState = SwitchState Bool, data
SwitchState = On | Off
On Sun, Aug 7, 2016 at 2:47 PM Imants Cekusins
found it:
https://wiki.haskell.org/GHC/Coercible
yep, it type checks.
Ta _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Imants Cekusins
for a Bool-like newtype:
newtype B = B Bool
, is there an easy way to use this newtype B in place of Bool?
e.g.
let b1 = B True in if b1 then 1 else 0
?
Imants, You can create a function that converts a B to a Bool: toBool :: B -> Bool toBool B b = b And then use the result of applying that function to your variable: let b1 = B True in if toBool b1 then 1 else 0 To do anything like this automatically would seem to me to defeat the purpose of having distinct types, which is to clearly articulate what values are appropriate in particular contexts. Mike.
participants (5)
-
Imants Cekusins
-
Marcin Mrotek
-
Michael Alan Dorman
-
Rein Henrichs
-
Theodore Lief Gannon