Christopher Howard | 21 Dec 14:36 2012

Layer on a layer of record syntax in the type synonym?

Using a simple type I gave earlier from my monadic type question...

code:
--------
data Socket3 a b c = Socket3 a b c
  deriving (Show)
--------

Is it possible somehow to layer on record syntax onto a synonym of the type?

The idea would be something like this...

code:
--------
type SpaceShip =
  Socket3 { engine :: Last Engine
          , hull :: Last Hull
          , guns :: [Guns]
          }
--------

...purely for the convenience. But this doesn't seem to work with "type"
as it assumes you are referring to already made constructors, and
evidently "newtype" only allows use of a single record. I could wrap it
in a normal "data" declaration but that would add an extra layer of
complexity I think.

--

-- 
frigidcode.com

(Continue reading)

Ivan Lazar Miljenovic | 21 Dec 14:42 2012
Picon

Re: Layer on a layer of record syntax in the type synonym?

On 22 December 2012 00:36, Christopher Howard
<christopher.howard <at> frigidcode.com> wrote:
> Using a simple type I gave earlier from my monadic type question...
>
> code:
> --------
> data Socket3 a b c = Socket3 a b c
>   deriving (Show)
> --------
>
> Is it possible somehow to layer on record syntax onto a synonym of the type?
>
> The idea would be something like this...
>
> code:
> --------
> type SpaceShip =
>   Socket3 { engine :: Last Engine
>           , hull :: Last Hull
>           , guns :: [Guns]
>           }
> --------
>
> ...purely for the convenience. But this doesn't seem to work with "type"
> as it assumes you are referring to already made constructors, and
> evidently "newtype" only allows use of a single record. I could wrap it
> in a normal "data" declaration but that would add an extra layer of
> complexity I think.

No, you can't suddenly add records in just for a type alias.  You
(Continue reading)

Daniel Trstenjak | 21 Dec 14:52 2012
Picon

Re: Layer on a layer of record syntax in the type synonym?


Hi Christopher,

On Fri, Dec 21, 2012 at 04:36:04AM -0900, Christopher Howard wrote:
> Using a simple type I gave earlier from my monadic type question...
> 
> code:
> --------
> data Socket3 a b c = Socket3 a b c
>   deriving (Show)
> --------
> 
> Is it possible somehow to layer on record syntax onto a synonym of the type?
> 
> The idea would be something like this...
> 
> code:
> --------
> type SpaceShip =
>   Socket3 { engine :: Last Engine
>           , hull :: Last Hull
>           , guns :: [Guns]
>           }
> --------
> 
> ...purely for the convenience. But this doesn't seem to work with "type"
> as it assumes you are referring to already made constructors, and
> evidently "newtype" only allows use of a single record. I could wrap it
> in a normal "data" declaration but that would add an extra layer of
> complexity I think.
(Continue reading)

Christopher Howard | 21 Dec 22:50 2012

Re: Layer on a layer of record syntax in the type synonym?

On 12/21/2012 04:52 AM, Daniel Trstenjak wrote:
> 
> Why having a Socket3 in the first place, what's the point of it?
> 

The idea was to have some generic structures (Sockets) which were
already instanced into the Monoids-within-Monoids abstraction, yet could
still be made concrete into anything more specific.

So, I have...

code:
--------
data Socket3 a b c = Socket3 a b c
  deriving (Show)

instance (Monoid a, Monoid b, Monoid c) => Monoid (Socket3 a b c) where
    mempty = Socket3 mempty mempty mempty
    Socket3 a b c `mappend` Socket3 w x y =
        Socket3 (a <> w) (b <> x) (c <> y)

nullSocket3 :: (Monoid a, Monoid b, Monoid c) => Socket3 a b c
nullSocket3 = Socket3 mempty mempty mempty
--------

...which allows me to have...

code:
--------
type ShipSys = Socket3 (Last Engine) (Last RotThruster) [LinThruster]
(Continue reading)

Antoine Latter | 24 Dec 05:31 2012
Picon

Re: Layer on a layer of record syntax in the type synonym?

You could look into the "Generic Monoid" solution proposed in your
other thread, then you wouldn't need your "Socket" types  - you would
use the "Generic Monoid" machinery to make a Monoid instance for
whatever type needed it.

This approach loses some type-safety, as you might pass on version of
a Scoket3 to a function that was meant to take a different type of
Socket3.

On Fri, Dec 21, 2012 at 4:50 PM, Christopher Howard
<christopher.howard <at> frigidcode.com> wrote:
> On 12/21/2012 04:52 AM, Daniel Trstenjak wrote:
>>
>> Why having a Socket3 in the first place, what's the point of it?
>>
>
> The idea was to have some generic structures (Sockets) which were
> already instanced into the Monoids-within-Monoids abstraction, yet could
> still be made concrete into anything more specific.
>
> So, I have...
>
> code:
> --------
> data Socket3 a b c = Socket3 a b c
>   deriving (Show)
>
> instance (Monoid a, Monoid b, Monoid c) => Monoid (Socket3 a b c) where
>     mempty = Socket3 mempty mempty mempty
>     Socket3 a b c `mappend` Socket3 w x y =
(Continue reading)

Nicolas Trangez | 21 Dec 15:01 2012

Re: Layer on a layer of record syntax in the type synonym?

On Fri, 2012-12-21 at 04:36 -0900, Christopher Howard wrote:
> Using a simple type I gave earlier from my monadic type question...
> 
> code:
> --------
> data Socket3 a b c = Socket3 a b c
>   deriving (Show)
> --------
> 
> Is it possible somehow to layer on record syntax onto a synonym of the type?
> 
> The idea would be something like this...
> 
> code:
> --------
> type SpaceShip =
>   Socket3 { engine :: Last Engine
>           , hull :: Last Hull
>           , guns :: [Guns]
>           }
> --------
> 
> ...purely for the convenience. But this doesn't seem to work with "type"
> as it assumes you are referring to already made constructors, and
> evidently "newtype" only allows use of a single record. I could wrap it
> in a normal "data" declaration but that would add an extra layer of
> complexity I think.

Although this 'Socket3' data type which all of a sudden should be
aliased as 'SpaceShip' feels/looks really strange (are you sure that's
(Continue reading)


Gmane