<p dir="ltr">I can write this:</p>
<p dir="ltr">data Team player = ...</p>
<p dir="ltr">Today, I can legally write</p>
<p dir="ltr">newtype IP player = IP (Team player)<br>
instance Foldable IP where<br>
foldMap f (IP t) = only fold over players currently in play</p>
<p dir="ltr">This would let me "mask" part of the team for folding purposes without actually extracting the part I want to consider. Your law forbids this. Under your regime, I am forced to make IP sufficiently heavy to hide the benched players from *everyone*.</p>
<p dir="ltr">I understand that not every law has to be in the nature of a rewrite rule, but it's also true that not every law has to be so strong as to limit instances to a small handful of possible implementations. I think what I'm asking of you is reasonable: a situation where the law you propose would help prove a clearly useful program property. The law is getting more and more complicated to take GADTs and such into account. Such a complex law, in my opinion, needs a truly compelling purpose.</p>
<div class="gmail_quote">On Feb 27, 2015 2:17 PM, "Gershom B" <<a href="mailto:gershomb@gmail.com">gershomb@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On February 27, 2015 at 1:39:10 AM, David Feuer (<a href="mailto:david.feuer@gmail.com">david.feuer@gmail.com</a>) wrote:<br>
> I am still struggling to understand why you want this to be a law for<br>
> Foldable. It seems an interesting property of some Foldable instances,<br>
> but, unlike Edward Kmett's proposed monoid morphism law, it's not<br>
> clear to me how you can use this low to prove useful properties of<br>
> programs. Could you explain?<br>
<br>
I think there are a number of purposes for laws. Some can be thought of as “suggested rewrite rules” — and the monoid morphism law is one such, as are many related free approaches.<br>
<br>
Note that the monoid morphism law that Edward provides is _not_ a “proposed” law — it is an “almost free theorem” — given a monoid morphism, it follows for free for any Foldable. There is no possible foldable instance that can violate this law, assuming you have an actual monoid morphism.<br>
<br>
So Edward may have proposed adding it to the documentation (which makes sense to me) — but it provides absolutely no guidance or constraints as to what an “allowable” instance of Foldable is or is not.<br>
<br>
But there are other reasons for laws than just to provide rewrite rules, even though it is often desirable to express laws in such terms. Consider the first Functor law for example — fmap id === id. Now clearly we can use it to go eliminate a bunch of “fmap id” calls in our program, should we have had them. But when would that ever be the case? Instead, the law is important because it _restricts_ the range of allowable instances — and so if you know you have a data type, and you know it has a functor instance, you then know what that functor instance must do, without looking at the source code.<br>
<br>
In “An Investigation of the Laws of Traversals” from where we get our current Traversable laws, for example, Jaskelioff and Rypacek explain their motivation as establishing coherence laws that “capture the intuition of traversals” and rule out “bad” traversals (i.e. ones that violate our intuition). That’s the sort of goal I’m after — something that rules out “obviously bad” Foldable instances and lets us know something about the instances provided on structures without necessarily needing to read the code of those instances — and beyond what we can learn “for free” from the type.<br>
<br>
Let me give an example. If you had a structure that represented a sequence of ‘a’, and you discovered that its Foldable instance only folded over the second, fifth, and seventeenth elements and no others — you would, I expect, find this to be a surprising result. You might even say “well, that’s a stupid Foldable instance”. However, outside of your gut feeling that this didn’t make sense, there would be no even semi-formal way to say it was “wrong”. Under a law such as I am trying to drive towards, we would be able to rule out such instances, just as our traversable laws now let us rule out instances that e.g. drop elements from the resultant structure. Hence, if you saw such a structure, and saw it had a Foldable instance, you would now know something useful about the behavior of that instance, without having to examine the implementation of Foldable — one of those useful things being, for example, that it could not possibly, lawfully, only fold over the fifth and seventeenth elements contained but no others.<br>
<br>
(Now how to specify such a law that permits the _maximum_ amount of useful information while also permitting the _maximum_ number of instances that “match our intuition” is what my last post was driving at — I think I am closer, but certainly there remains work to be done).<br>
<br>
In fact, we should note that when a Foldable is also Traversable, we do have a law specifying how the two typeclasses cohere, and because Traversable _does_ have strong laws, this _does_ suffice to well characterize Foldable in such a case. One nice property of the sort of law I have been driving at is that it will _agree_ with the current characterization in that same circumstance — when Foldable and Traversable instances both exist — and it will allow us to extend _those same intuitions_ in a formal way to circumstances when, for whatever reason, a Traversable instance does _not_ exist.<br>
<br>
I hope this helps explain what I’m up to?<br>
<br>
Cheers,<br>
Gershom<br>
<br>
<br>
><br>
> On Wed, Feb 25, 2015 at 5:40 PM, Gershom B wrote:<br>
> > I think I can now float an amended law that is a further step in the right<br>
> > direction. First I will give the law, and then I will attempt to both<br>
> > motivate why the amended treatment is needed, and why the weakening of the<br>
> > initial proposal is a legitimate approach. Finally, I will discuss some<br>
> > remaining issues that it would be nice to examine, including some sketches<br>
> > of related but alternate approaches.<br>
> ><br>
> > So, the new law:<br>
> ><br>
> > ===<br>
> > for a lawful Foldable f, and<br>
> ><br>
> > given a fresh newtype GenericSet = GenericSet Integer deriving (Eq, Ord),<br>
> > and mkGenericSet = GenericSet, where GenericSet is otherwise fully abstract<br>
> ><br>
> > then<br>
> ><br>
> > forall (g :: forall a. f a -> Maybe a), (x :: f GenericSet). maybe True<br>
> > (`Foldable.elem` x) (g x) === True<br>
> > ==<br>
> ><br>
> > You may recall that the prior formulation asked that this condition hold for<br>
> > all a., (x :: f a), and so required an external notion of equality. Here, we<br>
> > ask that it only hold at a _specified type_ -- i.e. "GenericSet".<br>
> ><br>
> > So, in what sense was the older formulation "too strong"? Here is a minimal<br>
> > case.<br>
> ><br>
> > data TBox a where<br>
> > TAny :: a -> TBox a<br>
> > TString :: String -> TBox String<br>
> ><br>
> > TBox is just a box with some optional type information, and it should be<br>
> > able to be equipped with the obvious Foldable instance. However, we can now<br>
> > write a function such as<br>
> ><br>
> > discrim :: forall a. TBox a -> Maybe a<br>
> > discrim (TAny x) = Just x<br>
> > discrim (TString _) = "zoinks"<br>
> ><br>
> > This is to say that if we _know_ that TBox contains a string, we can now<br>
> > take _any_ string out of it. Clearly, the existence of such functions means<br>
> > that the obvious Foldable instance cannot satisfy the stronger law.<br>
> ><br>
> > By contrast, the straightforward Foldable instance for TBox still satisfies<br>
> > the weaker law, as we only test the action on "GenericSet" -- and since we<br>
> > have defined GenericSet as a fresh newtype, we know that it cannot have a<br>
> > tag to be matched on within a GADT we have already defined. Hence, our law,<br>
> > though it gives a universal property, does not give this property itself<br>
> > "universally" _at every type a_ but rather at a "generic type". This is by<br>
> > analogy with the technique of using "generic points" in algebraic geometry<br>
> > [1] to give properties that are not "everywhere true" but instead are true<br>
> > "almost everywhere", with a precise meaning given to "almost" -- i.e. that<br>
> > the space for which they do not hold _vanishes_.<br>
> ><br>
> > This is to say, a Foldable satisfying the proposed law will not have the<br>
> > property hold "at all types," but rather at "almost all types" for an<br>
> > analogously precise meaning of "almost". I believe that a statement of why<br>
> > GenericSet is indeed "generic" in some topological sense is possible,<br>
> > although I have not fully fleshed this out.<br>
> ><br>
> > To my knowledge, the introduction of this sort of approach in an FP context<br>
> > is new, but I would welcome any references showing prior art.<br>
> ><br>
> > Aside from dealing with GADTs in some fashion, the new approach has a few<br>
> > other useful features. First, by formulating things directly in terms of<br>
> > "Foldable.elem" we omit the need for some sort of notion of equality in the<br>
> > metalogic -- instead we can use the "Eq" typeclass directly. Furthermore,<br>
> > this happens to sidestep the "toList" problem in the initial approach -- it<br>
> > directly allows search of potentially infinite structures.<br>
> ><br>
> > There are still a few things that could potentially be better.<br>
> ><br>
> > 1) The "fully internal" approach means that we are now subject to the "bias"<br>
> > of "||" -- that is to say that _|_ || True -> _|_, but True || _|_ -> True.<br>
> > So we do not fully capture the searchability of infinite structures. This<br>
> > may indicate that a retreat to a metalogical description of "elem" with<br>
> > unbiased "or" is in order.<br>
> ><br>
> > 2) While we can generically characterize a Foldable instance on some f<br>
> > "almost everywhere" this is at the cost of giving it _no_ characterization<br>
> > at the points where our generic characterization fails. It would be nice to<br>
> > establish some sort of relation between the generic characterization and the<br>
> > action at specified points. However, I am not quite sure how to present<br>
> > this. An alternate approach could be to specify a Foldable law for any `f`<br>
> > that first takes `f` (which may be a GADT) to a related type `f1` (which<br>
> > must be an ordinary ADT) that squashes or omits dictionaries and equality<br>
> > constraints, and likewise takes the Foldable instance to a related instance<br>
> > on f1, and then provides a condition on f1. So rather that retreating from<br>
> > universal to generic properties, we instead take hold of the machinery of<br>
> > logical relations directly to establish a law. I would be interested in<br>
> > being pointed to related work along these lines as well.<br>
> ><br>
> > 3) An additional drawback of the "Generic Point" approach as given is that<br>
> > we chose to derive only two particular typeclasses -- Eq and Ord. An<br>
> > alternate approach would be to quantify over all a, but then give the<br>
> > property in terms of say "newtype Generify a = Generify a deriving (...)"<br>
> > which derives all classes on "a" that do not feature "a" in a positive<br>
> > position. Doing this would also mean a retreat from a fully internal notion<br>
> > of equality, of course...<br>
> ><br>
> > Anyway, this is clearly still work in progress, but I would appreciate any<br>
> > feedback on the direction this is going, or references that may seem useful.<br>
> ><br>
> > Cheers,<br>
> > Gershom<br>
> ><br>
> > [1] <a href="https://en.wikipedia.org/wiki/Generic_point" target="_blank">https://en.wikipedia.org/wiki/Generic_point</a><br>
> ><br>
> > On Thu, Feb 12, 2015 at 6:33 PM, Edward Kmett wrote:<br>
> >><br>
> >> With Foldable we do have a very nice law around foldMap.<br>
> >><br>
> >> A monoid homomorphism g is a combinator such that<br>
> >><br>
> >> g mempty = mempty<br>
> >> g (mappend a b) = mappend (g a) (mappend (g b)<br>
> >><br>
> >> For any monoid homomorphism g,<br>
> >><br>
> >> foldMap (g . f) = g . foldMap f<br>
> >><br>
> >> We can use that to construct proofs of an analogue to "the banana split<br>
> >> theorem" for foldr, but rephrased in terms of foldMap:<br>
> >><br>
> >> foldMap f &&& foldMap g = foldMap (f &&& g)<br>
> >><br>
> >> Getting there uses the fact that fst and snd are both monoid<br>
> >> homomorphisms.<br>
> >><br>
> >> There are also laws relating the behavior of all of the other combinators<br>
> >> in Foldable to foldMap.<br>
> >><br>
> >> Ultimately the reasons for the other members of the class are a sop to<br>
> >> efficiency concerns: asymptotic factors in terms of time or stack usage<br>
> >> matter.<br>
> >><br>
> >> -Edward<br>
> >><br>
> >><br>
> >> On Thu, Feb 12, 2015 at 5:22 PM, Kim-Ee Yeoh wrote:<br>
> >>><br>
> >>> (removing Libraries, since not everyone on HC is on that list)<br>
> >>><br>
> >>> I do not know all of the context of Foldable, but one I do know that's<br>
> >>> not been mentioned is the implicit rule-of-thumb that every type class<br>
> >>> should have a law.<br>
> >>><br>
> >>> So the undercurrent is that if Foldable doesn't have a law, should it<br>
> >>> even be a type class? This has led to efforts to uncover laws for Foldable.<br>
> >>><br>
> >>> Worth discussing in a separate thread is the criterion itself of "if it<br>
> >>> doesn't have a law, it's not a type class". Useful sometimes, but that's not<br>
> >>> the sine qua non of type classes.<br>
> >>><br>
> >>><br>
> >>><br>
> >>> -- Kim-Ee<br>
> >>><br>
> >>> On Fri, Feb 13, 2015 at 2:47 AM, Gershom B wrote:<br>
> >>>><br>
> >>>> For a long time, many people, including me, have said that "Foldable has<br>
> >>>> no laws" (or Foldable only has free laws) -- this is true, as it stands,<br>
> >>>> with the exception that Foldable has a non-free law in interaction with<br>
> >>>> Traversable (namely that it act as a proper specialization of Traversable<br>
> >>>> methods). However, I believe that there is a good law we can give for<br>
> >>>> Foldable.<br>
> >>>><br>
> >>>> I earlier explored this in a paper presented at IFL 2014 but<br>
> >>>> (rightfully) rejected from the IFL post-proceedings.<br>
> >>>> (<a href="http://gbaz.github.io/slides/buildable2014.pdf" target="_blank">http://gbaz.github.io/slides/buildable2014.pdf</a>). That paper got part of<br>
> the<br>
> >>>> way there, but I believe now have a better approach on the question of a<br>
> >>>> Foldable law -- as sketched below.<br>
> >>>><br>
> >>>> I think I now (unlike in the paper) can state a succinct law for<br>
> >>>> Foldable that has desired properties: 1) It is not "free" -- it can be<br>
> >>>> violated, and thus stating it adds semantic content. 2) We typically expect<br>
> >>>> it to be true. 3) There are no places where I can see an argument for<br>
> >>>> violating it.<br>
> >>>><br>
> >>>> If it pans out, I intend to pursue this and write it up more formally,<br>
> >>>> but given the current FTP discussion I thought it was worth documenting this<br>
> >>>> earlier rather than later. For simplicity, I will state this property in<br>
> >>>> terms of `toList` although that does not properly capture the infinite<br>
> >>>> cases. Apologies for what may be nonstandard notation.<br>
> >>>><br>
> >>>> Here is the law I think we should discuss requiring:<br>
> >>>><br>
> >>>> * * *<br>
> >>>> Given Foldable f, then<br>
> >>>> forall (g :: forall a. f a -> Maybe a), (x :: f a). case g x of Just a<br>
> >>>> --> a `elem` toList x<br>
> >>>> * * *<br>
> >>>><br>
> >>>> Since we do not require `a` to be of type `Eq`, note that the `elem`<br>
> >>>> function given here is not internal to Haskell, but in the metalogic.<br>
> >>>><br>
> >>>> Furthermore, note that the use of parametricity here lets us make an<br>
> >>>> "end run" around the usual problem of giving laws to Foldable -- rather than<br>
> >>>> providing an interaction with another class, we provide a claim about _all_<br>
> >>>> functions of a particular type.<br>
> >>>><br>
> >>>> Also note that the functions `g` we intend to quantify over are<br>
> >>>> functions that _can be written_ -- so we can respect the property of data<br>
> >>>> structures to abstract over information. Consider<br>
> >>>><br>
> >>>> data Funny a = Funny {hidden :: a, public :: [a]}<br>
> >>>><br>
> >>>> instance Foldable Funny where<br>
> >>>> foldMap f x = foldMap f (public x)<br>
> >>>><br>
> >>>> Now, if it is truly impossible to ever "see" hidden (i.e. it is not<br>
> >>>> exported, or only exported through a semantics-breaking "Internal" module),<br>
> >>>> then the Foldable instance is legitimate. Otherwise, the Foldable instance<br>
> >>>> is illegitimate by the law given above.<br>
> >>>><br>
> >>>> I would suggest the law given is "morally" the right thing for Foldable<br>
> >>>> -- a Foldable instance for `f` should suggest that it gives us "all the as<br>
> >>>> in any `f a`", and so it is, in some particular restricted sense, initial<br>
> >>>> among functions that extract as.<br>
> >>>><br>
> >>>> I do not suggest we add this law right away. However, I would like to<br>
> >>>> suggest considering it, and I believe it (or a cleaned-up variant) would<br>
> >>>> help us to see Foldable as a more legitimately lawful class that not only<br>
> >>>> provides conveniences but can be used to aid reasoning.<br>
> >>>><br>
> >>>> Relating this to adjointness, as I do in the IFL preprint, remains<br>
> >>>> future work.<br>
> >>>><br>
> >>>> Cheers,<br>
> >>>> Gershom<br>
> >>>><br>
> >>>> _______________________________________________<br>
> >>>> Haskell-Cafe mailing list<br>
> >>>> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
> >>>> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><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://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><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" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><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" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
><br>
<br>
</blockquote></div>