Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  private types (Dominic Mulligan)
   2. Re:  private types (Stephen Tetley)
   3. Re:  private types (Dominic Mulligan)
   4. Re:  Encoding strings (Daniel Fischer)
   5. Re:  private types (Ashish Agarwal)


----------------------------------------------------------------------

Message: 1
Date: Thu, 08 Apr 2010 21:35:44 +0100
From: Dominic Mulligan <[email protected]>
Subject: Re: [Haskell-beginners] private types
To: Ashish Agarwal <[email protected]>
Cc: Haskell-beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

The GHC extension "view patterns" does roughly what you want.

See here: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns

Ashish Agarwal wrote:
> Is there a Haskell analogue to OCaml's private types? In OCaml, you 
> can do this (converting to pseudo-Haskell syntax):
>
> type T = private A Int
>
> Then, A cannot be used directly to construct values of type T.
> A 3
> Error: Cannot create values of the private type T
>
> You must provide another constructor, makeT, which takes an Int and 
> returns a T. This allows you to do additional checks on the Int 
> argument. However, you can still deconstruct values of type T.
>
> f (A x) = x + 2
>
> This works even when f is defined in a different module. The benefit 
> is that you can restrict the allowed values of T, but still have the 
> convenience of using existing operations on these values once constructed.
>
> The solution that comes to mind is to make T abstract and an instance 
> of some appropriate class, but there is no class that lets you pattern 
> match on arbitrary variant types.
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>   



------------------------------

Message: 2
Date: Thu, 8 Apr 2010 22:10:36 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] private types
Cc: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Dominic

They can get a bit closer but (I think) you would still need to define
a auxiliary view type that exports its constructor:


module View1 (
  T,         -- opaque
  TView(..), -- not opaque
  tview,
  mkT
  )
  where

data T = PrivateT Int   deriving (Eq,Show)

-- auxillary type
data TView = TView Int  deriving (Eq,Show)

tview :: T -> TView
tview (PrivateT i) = TView i

mkT :: Int -> T
mkT i = PrivateT i


-- client module with views (new file) :

{-# LANGUAGE ViewPatterns #-}

module UseView where

import View1

-- Use the view pattern:
add1 :: T -> T
add1 (tview -> TView i) = mkT (i+1)

-- or long hand...
add1_alt :: T -> T
add1_alt t = case tview t of
               TView i -> mkT i







On 8 April 2010 21:35, Dominic Mulligan
<[email protected]> wrote:
> The GHC extension "view patterns" does roughly what you want.
>
> See here: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns
>
> Ashish Agarwal wrote:
>>
>> Is there a Haskell analogue to OCaml's private types? In OCaml, you can do
>> this (converting to pseudo-Haskell syntax):
>>
>> type T = private A Int
>>
>> Then, A cannot be used directly to construct values of type T.
>> A 3
>> Error: Cannot create values of the private type T
>>
>> You must provide another constructor, makeT, which takes an Int and
>> returns a T. This allows you to do additional checks on the Int argument.
>> However, you can still deconstruct values of type T.
>>
>> f (A x) = x + 2
>>
>> This works even when f is defined in a different module. The benefit is
>> that you can restrict the allowed values of T, but still have the
>> convenience of using existing operations on these values once constructed.
>>
>> The solution that comes to mind is to make T abstract and an instance of
>> some appropriate class, but there is no class that lets you pattern match on
>> arbitrary variant types.
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


------------------------------

Message: 3
Date: Thu, 08 Apr 2010 22:27:53 +0100
From: Dominic Mulligan <[email protected]>
Subject: Re: [Haskell-beginners] private types
To: Stephen Tetley <[email protected]>,  Haskell-beginners
        <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi Stephen,

Yes, that is correct.  But I don't see why this matters (unless I'm 
missing something obvious?).  In this case, you cannot construct a value 
of type T using TView's constructors, and presumably all the functions 
defined in module View1 will be defined over T, not TView.  If the 
question was to find a perfect analogue to O'Caml's private types, then 
probably one doesn't exist, but GHC's view patterns are the closest 
Haskell feature that does the intended purpose of allowing pattern 
matching without breaking abstraction (that I'm aware of, of course :)).

Stephen Tetley wrote:
> Hi Dominic
>
> They can get a bit closer but (I think) you would still need to define
> a auxiliary view type that exports its constructor:
>
>
> module View1 (
>   T,         -- opaque
>   TView(..), -- not opaque
>   tview,
>   mkT
>   )
>   where
>
> data T = PrivateT Int   deriving (Eq,Show)
>
> -- auxillary type
> data TView = TView Int  deriving (Eq,Show)
>
> tview :: T -> TView
> tview (PrivateT i) = TView i
>
> mkT :: Int -> T
> mkT i = PrivateT i
>
>
> -- client module with views (new file) :
>
> {-# LANGUAGE ViewPatterns #-}
>
> module UseView where
>
> import View1
>
> -- Use the view pattern:
> add1 :: T -> T
> add1 (tview -> TView i) = mkT (i+1)
>
> -- or long hand...
> add1_alt :: T -> T
> add1_alt t = case tview t of
>                TView i -> mkT i
>
>
>
>
>
>
>
> On 8 April 2010 21:35, Dominic Mulligan
> <[email protected]> wrote:
>   
>> The GHC extension "view patterns" does roughly what you want.
>>
>> See here: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns
>>
>> Ashish Agarwal wrote:
>>     
>>> Is there a Haskell analogue to OCaml's private types? In OCaml, you can do
>>> this (converting to pseudo-Haskell syntax):
>>>
>>> type T = private A Int
>>>
>>> Then, A cannot be used directly to construct values of type T.
>>> A 3
>>> Error: Cannot create values of the private type T
>>>
>>> You must provide another constructor, makeT, which takes an Int and
>>> returns a T. This allows you to do additional checks on the Int argument.
>>> However, you can still deconstruct values of type T.
>>>
>>> f (A x) = x + 2
>>>
>>> This works even when f is defined in a different module. The benefit is
>>> that you can restrict the allowed values of T, but still have the
>>> convenience of using existing operations on these values once constructed.
>>>
>>> The solution that comes to mind is to make T abstract and an instance of
>>> some appropriate class, but there is no class that lets you pattern match on
>>> arbitrary variant types.
>>>
>>> ------------------------------------------------------------------------
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>       
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>     
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>   



------------------------------

Message: 4
Date: Thu, 8 Apr 2010 21:57:26 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Encoding strings
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag 08 April 2010 21:26:56 schrieb legajid:
> Hi,
> i've  found another solution : instead of putStr (show text), i just
> write putStr text.

Ah, yes, the Show instance escapes all characters beyond '\DEL', so (show 
text) doesn't contain any accented letters.

>
> As a remaining problem, i don't how to install cabal in order to install
> the desired module.

For Windows, there should be a cabal binary somewhere (try 
http://www.haskell.org/cabal/download.html
).
There is supposed to be one included in the Haskell Platform, so installing 
that might be a good move.

>
> Thanks,
> Didier.


------------------------------

Message: 5
Date: Thu, 8 Apr 2010 19:10:46 -0400
From: Ashish Agarwal <[email protected]>
Subject: Re: [Haskell-beginners] private types
To: Dominic Mulligan <[email protected]>
Cc: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Thanks for all the suggestions. I also see that Haskell allows the same
record field name in multiple constructors. This also helps for the type
definition I had in mind:

module XYZ
 (
   T(foo,bar),
   makeA,
   makeB

 ) where

data T
    = PrivateA {
        foo :: Int
      }
    | PrivateB {
        foo :: Int,
        bar :: Int
      }
    deriving (Eq,Show)

makeA :: Int -> T
makeA i = PrivateA {foo=i}

makeB :: Int -> Int -> T
makeB i j = PrivateB {foo=i, bar=j}


On Thu, Apr 8, 2010 at 5:27 PM, Dominic Mulligan <
[email protected]> wrote:

> Hi Stephen,
>
> Yes, that is correct.  But I don't see why this matters (unless I'm missing
> something obvious?).  In this case, you cannot construct a value of type T
> using TView's constructors, and presumably all the functions defined in
> module View1 will be defined over T, not TView.  If the question was to find
> a perfect analogue to O'Caml's private types, then probably one doesn't
> exist, but GHC's view patterns are the closest Haskell feature that does the
> intended purpose of allowing pattern matching without breaking abstraction
> (that I'm aware of, of course :)).
>
>
> Stephen Tetley wrote:
>
>> Hi Dominic
>>
>> They can get a bit closer but (I think) you would still need to define
>> a auxiliary view type that exports its constructor:
>>
>>
>> module View1 (
>>  T,         -- opaque
>>  TView(..), -- not opaque
>>  tview,
>>  mkT
>>  )
>>  where
>>
>> data T = PrivateT Int   deriving (Eq,Show)
>>
>> -- auxillary type
>> data TView = TView Int  deriving (Eq,Show)
>>
>> tview :: T -> TView
>> tview (PrivateT i) = TView i
>>
>> mkT :: Int -> T
>> mkT i = PrivateT i
>>
>>
>> -- client module with views (new file) :
>>
>> {-# LANGUAGE ViewPatterns #-}
>>
>> module UseView where
>>
>> import View1
>>
>> -- Use the view pattern:
>> add1 :: T -> T
>> add1 (tview -> TView i) = mkT (i+1)
>>
>> -- or long hand...
>> add1_alt :: T -> T
>> add1_alt t = case tview t of
>>               TView i -> mkT i
>>
>>
>>
>>
>>
>>
>>
>> On 8 April 2010 21:35, Dominic Mulligan
>> <[email protected]> wrote:
>>
>>
>>> The GHC extension "view patterns" does roughly what you want.
>>>
>>> See here: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns
>>>
>>> Ashish Agarwal wrote:
>>>
>>>
>>>> Is there a Haskell analogue to OCaml's private types? In OCaml, you can
>>>> do
>>>> this (converting to pseudo-Haskell syntax):
>>>>
>>>> type T = private A Int
>>>>
>>>> Then, A cannot be used directly to construct values of type T.
>>>> A 3
>>>> Error: Cannot create values of the private type T
>>>>
>>>> You must provide another constructor, makeT, which takes an Int and
>>>> returns a T. This allows you to do additional checks on the Int
>>>> argument.
>>>> However, you can still deconstruct values of type T.
>>>>
>>>> f (A x) = x + 2
>>>>
>>>> This works even when f is defined in a different module. The benefit is
>>>> that you can restrict the allowed values of T, but still have the
>>>> convenience of using existing operations on these values once
>>>> constructed.
>>>>
>>>> The solution that comes to mind is to make T abstract and an instance of
>>>> some appropriate class, but there is no class that lets you pattern
>>>> match on
>>>> arbitrary variant types.
>>>>
>>>> ------------------------------------------------------------------------
>>>>
>>>> _______________________________________________
>>>> Beginners mailing list
>>>> [email protected]
>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>>
>>>>
>>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100408/9c5123b2/attachment.html

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 22, Issue 12
*****************************************

Reply via email to