<div dir="auto"><b>tl;dr</b>: use modules and functors, not typeclasses.</div><div dir="auto"><br></div><div dir="auto">Unlike Haskell, Ur/Web allows local typeclass instances. This means (for concreteness, focusing on binary trees—similar for other examples) we can't rely on being passed the same ord typeclass dictionary every time we call a tree function. Instead, the comparison function has to somehow be built into the tree object. One way to do this would be to include it in the tree representation, but the Ur/Web compiler will probably be unable to inline this extra closure, so it won't work on the server side. Another way, which is probably the best option, is to use a functor to generate the tree functions from an ord instance.</div><div dir="auto"><br></div><div dir="auto">signature ORD = sig</div><div dir="auto"> type t</div><div dir="auto"> val t_ord : ord t</div><div dir="auto">end</div><div dir="auto"><br></div><div dir="auto">functor Tree(M : ORD) = struct</div><div dir="auto"> ...</div><div dir="auto">end</div><div dir="auto"><br></div><div dir="auto">Because ord is a typeclass, Ur/Web will fill in the signature by itself if it can find an ord instance at the functor instantiation site.</div><div dir="auto"><br></div><div dir="auto">structure MyTree = Tree(struct type t = myType end)</div><div dir="auto"><br></div><div dir="auto">But now we know that all MyTree functions will use the same ord instance.</div><div dir="auto"><br></div><div><br><div class="gmail_quote"><div>On Mon, Jul 24, 2017 at 15:47 Aistis Raulinaitis <<a href="mailto:sheganinans@gmail.com">sheganinans@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>I agree, I think arrays are much overused in programming for the most part. My thoughts on are that these libs should facilitate easier interop with the existing JS libs rather than as a bedrock for a data structure on the frontend. On the other hand, I like the idea of a finger tree implementation. My only question is, since we have the choice, would it be more
appropriate to implement it using modules or type classes? My Haskell
background makes me lean in one direction, but I think it would be
interesting to have both. That being said, I should have the js array library out in the next day or two. Mind you both of these libs in their forEach functions call execF twice. So you are invoking the Ur/Web runtime twice for each element. This shouldn't be too bad depending on the calculation, but it's something to keep in mind.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 23, 2017 at 9:50 PM, Artyom Shalkhakov <span><<a href="mailto:artyom.shalkhakov@gmail.com" target="_blank">artyom.shalkhakov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>2017-07-23 0:15 GMT+06:00 Benjamin Barenblat <<a href="mailto:benjamin@barenblat.name" target="_blank">benjamin@barenblat.name</a>>:<br>
> On Sat, Jul 22, 2017 at 11:59 AM, Artyom Shalkhakov<br>
> <<a href="mailto:artyom.shalkhakov@gmail.com" target="_blank">artyom.shalkhakov@gmail.com</a>> wrote:<br>
>> Is it possible to extend Ur/Web with arrays and maps?<br>
><br>
> If you really want an array, I think you’re stuck with the FFI. However,<br>
> if you just want a bag and amortized runtimes are good enough, you<br>
> should implement a finger tree. You lose spatial locality, but finger<br>
> trees are much more suited to pure languages like Ur. There is also a<br>
> high-quality BSD-licensed implementation in Haskell* that you can base<br>
> your work on.<br>
><br>
<br>
</span>I'm currently doing some exercises here:<br>
<br>
<a href="https://github.com/ashalkhakov/urweb-projects/blob/master/sam/app.ur" rel="noreferrer" target="_blank">https://github.com/ashalkhakov/urweb-projects/blob/master/sam/app.ur</a><br>
<br>
and the next thing I'm going to tackle is a TodoMVC clone (I know that<br>
a demo is available on the Ur/Web website, but I'd like to implement<br>
it according to State-Action-Model structuring pattern). This requires<br>
implementing a client-side "model" for storing TODO items. Typical JS<br>
applications don't bother and use the built-in arrays. So my idea was<br>
to go the same route, and then test performance on a big dataset. I'm<br>
a bit concerned about performance.<br>
<br>
I was also thinking that if Ur/Web is missing arrays/maps, then it's a<br>
good project to tackle.<br>
<span><br>
> On the map front, the traditional functional map construction is a<br>
> balanced binary tree. This one’s a bit unfortunate, because you lose the<br>
> amortized O(1) promise that you get from hash tables, and you wind up<br>
> with amortized O(log n) instead. However, they’re simple to implement<br>
> and only require an `ord` instance for the keys. If you implement finger<br>
> trees or arrays, you can use them to build hash tables and hash<br>
> sets. However, you then have to create a `hashable` type class and all<br>
> the infrastructure associated with it, so it’s a bit more work. Both<br>
> binary trees and hash data structures are useful to have, so go for<br>
> whatever sounds most fun to program.<br>
><br>
<br>
</span>Thanks! I'll see what I can do about it. It should be a fun exercise<br>
to implement a finger tree or an RB tree or some such.<br>
<span class="m_-2127914357318090134im m_-2127914357318090134HOEnZb"><br>
><br>
> * <a href="https://hackage.haskell.org/package/containers/docs/Data-Sequence.html" rel="noreferrer" target="_blank">https://hackage.haskell.org/package/containers/docs/Data-Sequence.html</a><br>
><br>
> _______________________________________________<br>
> Ur mailing list<br>
> <a href="mailto:Ur@impredicative.com" target="_blank">Ur@impredicative.com</a><br>
> <a href="http://www.impredicative.com/cgi-bin/mailman/listinfo/ur" rel="noreferrer" target="_blank">http://www.impredicative.com/cgi-bin/mailman/listinfo/ur</a><br>
<br>
<br>
<br>
</span><span class="m_-2127914357318090134HOEnZb"><font color="#888888">--<br>
Cheers,<br>
Artyom Shalkhakov<br>
</font></span><div class="m_-2127914357318090134HOEnZb"><div class="m_-2127914357318090134h5"><br>
_______________________________________________<br>
Ur mailing list<br>
<a href="mailto:Ur@impredicative.com" target="_blank">Ur@impredicative.com</a><br>
<a href="http://www.impredicative.com/cgi-bin/mailman/listinfo/ur" rel="noreferrer" target="_blank">http://www.impredicative.com/cgi-bin/mailman/listinfo/ur</a><br>
</div></div></blockquote></div><br></div>
_______________________________________________<br>
Ur mailing list<br>
<a href="mailto:Ur@impredicative.com" target="_blank">Ur@impredicative.com</a><br>
<a href="http://www.impredicative.com/cgi-bin/mailman/listinfo/ur" rel="noreferrer" target="_blank">http://www.impredicative.com/cgi-bin/mailman/listinfo/ur</a><br>
</blockquote></div></div>