chexxor [8:29 PM]
rich [10:24 AM]
Is there an abstraction of adding an element to an Array or List, so where
Semigroup.append :: a -> a -> a, you’d have Class.fnname :: a -> f a -> f a
Where f is a Semigroup or a Monoid or somethingnatefaubion [10:25 AM]
Potentially Alternative
where you havepure :: a -> f a
andplus :: f a -> f a -> f a
^ This is an interesting topic. Is there a typeclass which says “a f
which can eat my a
and from which I maybe get that a
back out again”? This sounds like a data structure to me.
Perhaps
class Container f a where
insert :: a -> f a -> f a
query :: Query f a -> f a -> Maybe a
type Query f a = (a -> Boolean)
-- idk about "Query", but you need some way of choosing the `a` to retrieve from the `f`, right?
-- Could define a similar type for these operations occurring in a monad/context
class Container m f a where
insert :: a -> f a -> m (f a)
query :: Query f a -> f a -> m (Maybe a)
monoidmusician [8:30 PM]
FoldableWithIndex
can provide one method for selecting an element
chexxor [8:32 PM]
Ah, foldable is an interesting idea
monoidmusician [8:32 PM]
but insert
is sort of the blindspot of most existing typeclasses
pure
with <|>
usually works fine though
chexxor [8:35 PM]
Wait, how would <|>
apply to this problem?
monoidmusician [8:37 PM]
insert a fa = pure a <|> f a
(or the commuted version)
chexxor [8:39 PM]
ah, right
Dang, that’s interesting. I didn’t consider Foldable and Alt to be so analagous to traditional data structure operations
monoidmusician [8:43 PM]
every program is traverse
chexxor [8:43 PM]
Wait, the Plus
class docs say "It is similar to Monoid, except that it applies to types of kind * -> *"
But Monoid’s mempty
isn’t annihilitive like that
A law of Plus
: Annihilation: f <$> empty == empty
monoidmusician [8:43 PM]
right, you have to consider empty
together with <|>
to get the Monoid
analogy
there is no analogy for that annihilation law in monoid
chexxor [8:44 PM]
Ah, the monoid part is the left identity and right identity laws
monoidmusician [8:47 PM]
I’ve always wondered if f <$> empty == empty
is redundant … I suspect it probably just needs id <$> fa == fa
and naturality
chexxor [8:48 PM]
What is naturality?
ah, natural transformation?
monoidmusician [8:53 PM]
erm I think I mean parametricity, sorry
chexxor [8:54 PM]
that sounds better
monoidmusician [9:02 PM]
we know that id <$> empty = empty
, by the law; obviously f <$> empty = (f <<< id) <$> empty
, but f
cannot apply to any elements of empty
(because a
is arbitrary in empty :: f a
; e.g. absurd <$> empty
), so it cannot change the result, obviously, thus we have that f <$> empty = (f <<< id) <$> empty = id <$> empty = empty