Hi Fredric,

below are two ways to solve it, first with GADTs, the other with type classes. But I think in both cases you need to define the specific handling of the types in multiple places. With GADT's you need to add yet another constructor for yet another `t`. With type classes you need to specify the type in-line at the place you branch off, not sure how elegant that is.

The singleton based approach is perhaps better than both of the below if your set of types is closed, since you only keep the string in one data constructor (in the `A "here"`), while with a GADT you have it nested in one of the AHdf5 or ACbf. On the other hand singletons need some extensions to be turned.

Hope it helps, code follows:


data A t = A String

data Unchecked
data Hdf5
data Cbf

-- * A with GADT

data A' where
  AHdf5 :: A Hdf5 -> A'
  ACbf :: A Cbf -> A'
  AUnchecked :: A Unchecked -> A'

check :: A Unchecked -> A'
check a = case a of
  A str | suffix ".h5" -> AHdf5 (A str)
        | suffix ".cdf" -> ACbf (A str)
        | otherwise -> AUnchecked (A str)
    where
      suffix suf = suf `isSuffixOf` str

-- * Type classes

type SomethnigCommon = ()

class Continue a where
  go :: A a -> SomethnigCommon
instance Continue Hdf5 where
  go (A hdf5) = undefined -- implementation for hdf5 here
instance Continue Cbf where
  go (A cbf) = undefined -- implementation for cbf here
instance Continue Unchecked where
  go (A unchecked) = undefined -- implementation for unchecked here

check' :: A Unchecked -> SomethnigCommon
check' a = case a of
  A str | suffix ".h5" -> go (A str :: A Hdf5)
        | suffix ".cdf" -> go (A str :: A Cbf)
        | otherwise -> go (A str :: A Unchecked)
    where
      suffix suf = suf `isSuffixOf` str



On Tue, Oct 2, 2018 at 6:48 PM PICCA Frederic-Emmanuel <frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
Hello


suppose that I have a bunch  of type like this

data Unchecked
data Hdf5
data Cbf

data A t > A String

The A thing come from a database as A Unchecked

now if I read the String and it ends with  .h5, I have a A Hdf5 type
and If the string end with .cbf, I have a A Cbf.

So I would like a function which allow to return a A Hdf5 or a A Cbf depending on the String content.

check :: A Unchecked -> A ???
check = ???

Is it possible to do this ?

Thanks

Frederic

PS: At the end I will have more tha one tag.
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


--
Markus Läll