<div dir="ltr"><div>I think you're off to a good start with this insert signature:</div><div><br></div><div><span style="color:rgb(0,0,0);font-size:13px">insert :: a -> C a -> Maybe (C a)</span><br></div><div><span style="color:rgb(0,0,0);font-size:13px">"Insert element `a` into structure `C a` and return a new structure if the insertion was successful."</span><br></div><div><br></div>It looks like you're following the lead of some common haskell data structures (eg, <a href="http://hackage.haskell.org/package/containers-0.5.6.3/docs/Data-Set.html#g:5">containers:Data.Set.insert</a>).<div><br></div><div>What does this lack?<br><div><div><br></div><div>---<br><div><br></div><div>If you want the container to be implicit in the arguments (albeit, explicit in the return value), then your second formulation works:</div><div><br></div><div><span style="color:rgb(0,0,0);font-size:13px">data C a = C {</span><br style="color:rgb(0,0,0);font-size:13px"><span style="color:rgb(0,0,0);font-size:13px"> insert :: a -> Maybe (C a),</span><br style="color:rgb(0,0,0);font-size:13px"><span style="color:rgb(0,0,0);font-size:13px"> remove :: Maybe (a, C a)</span><br style="color:rgb(0,0,0);font-size:13px"><span style="color:rgb(0,0,0);font-size:13px"> }</span><br></div><div><span style="color:rgb(0,0,0);font-size:13px"><br></span></div><div><span style="color:rgb(0,0,0);font-size:13px">To implement this you could make a constructor that has an internal data structure, and then constructs a `C` by closing over the internal structure. In this way, your `C` is really just an API and you'll have an internal implementation of insert & remove that take the internal structure as an explicit argument. That's a bunch of extra work for a minor convenience, so I'd recommend starting with the version that takes an explicit argument (first in this email).</span></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Dec 13, 2015 at 1:36 PM, Roman Cheplyaka <span dir="ltr"><<a href="mailto:roma@ro-che.info" target="_blank">roma@ro-che.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You may want to specify:<br>
<br>
1. whether you want the symmetry to be present in the API, the internal<br>
representation, or both<br>
2. what exactly your C type is lacking. It looks like a valid model of<br>
what you described, even if somewhat object-oriented one.<br>
<br>
You may also be interested in combinatorial species. That theory<br>
specifically considers functorial shapes containing a specific number of<br>
holes and/or elements. I think Brent Yorgey has some articles and/or<br>
code relating species to Haskell.<br>
<div><div class="h5"><br>
On 12/13/2015 10:15 PM, martin wrote:<br>
> Hello all,<br>
><br>
> here is a problem where I did not manage to find a suitable abstraction. The main idea goes like this:<br>
><br>
> a List (and many other containers) can be seen as something containing "stuff". There is a function (:) that<br>
> unconditionally adds an element to the container and returns a new container<br>
><br>
> Now suppose the container has the possiblility to refuse having another element added to it, e.g. because it has only<br>
> limited "space". In that case the corresponding function would have a signature of insert :: a -> C a -> Maybe (C a). If<br>
> an item can successfully be added, then the returned container will be less space avaiable.<br>
><br>
> I'd like stuff and space to be symmetrical (maybe there lies the first flaw, because I can enumerate the elements, but I<br>
> cannot enumerate the space). A symmetry like electrones and holes.<br>
><br>
> I started like this<br>
><br>
> data C a = C {<br>
> insert :: a -> Maybe (C a),<br>
> remove :: Maybe (a, C a)<br>
> }<br>
><br>
> but I could not implement anything sensible on top of this.<br>
><br>
> I'd be happy to hear any comments on this, including loud thinking and random ramblings.<br>
><br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
</div></div>> .<br>
><br>
<br>
<br>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>