linear-base icon indicating copy to clipboard operation
linear-base copied to clipboard

Function to apply to a `Moveable` argument?

Open andreasabel opened this issue 3 years ago • 3 comments

I spent considerable time searching the base-linear library for a function that would allow me to use a unrestricted function as a linear one provided the domain is Moveable. Is there something like that?

If not, these could be sensible additions to the library:

applyMove :: Movable a => (a -> b) %1 -> a %1 -> b
applyMove f x = f `applyUr` move x

applyUr :: (a -> b) %1 -> Ur a %1 -> b
applyUr f (Ur a) = f a

Example use:

import Control.Functor.Linear
import Data.HashMap.Mutual.Linear

lookupSt :: String %1 -> State (HashMap String v) (Ur (Maybe v))
lookupSt k = state (lookup `applyMove` k)
  -- lookup :: String -> HashMap String v %1 -> ...

Bikeshedding of the names welcome! Maybe there are suitable operators for these function reminiscent of $.

andreasabel avatar Apr 10 '22 19:04 andreasabel

applyUr f is unur . Data.fmap f. But I don't believe that we wrote an equivalent to applyMove (though I guess it's unur . Data.map f . move; but it's starting to be too unwieldy to use without naming it).

It has nice symmetry with the unsafe toLinear functions that we already have. Maybe is should be a variant on $? The already exists$! to mean strict apply. Should we take a symbol to indicate the use of move?

aspiwack avatar Apr 11 '22 07:04 aspiwack

Maybe $^ is still free (https://hoogle.haskell.org/?hoogle=%24%5E) and indicates some (upwards) movement ^ in connection with application $.

andreasabel avatar Apr 11 '22 09:04 andreasabel

Ok. Let's go for ($^).

aspiwack avatar Apr 28 '22 09:04 aspiwack