5 ms·
Monadic lifting in C++
- reubenmorais 13y agoFor those of you who like me didn't know about this, operators <<= and >>= exist, can be overloaded, and do "bitwise shift assignment". |a <<= b| is the same as |a = a << b|
- jxf 13y agoDoes |x| mean something specific here in C++ that I'm not aware of, or are you just using it to bracket the expressions?
- adamnemecek 13y agoThe latter.
- banachtarski 13y agoWell, yes that's true, but in the context of "monadic lifting" >>= refers to the monadic bind operator.
- adamnemecek 13y agoCorrect but in order for an operator to be overloadable in C++, it must have a meaning in standard C++ as well. The comment was pointing out that it's surprising that >>= is an operator in standard C++.
- banachtarski 13y agoOh my mistake, I actually use >>= and <<= frequently.
- eru 13y agoNice to see that C++ can express this pattern. (It is extremely ugly there, though.)
- zem 13y agouseful would be a transpiler that converted some limited subset of haskell to c++ code using this pattern.
- skybrian 13y agoIf you're new to the whole concept, Elm has a good example of "lift" used in a somewhat practical application: http://elm-lang.org/learn/What-is-FRP.elm http://elm-lang.org/learn/What-is-FRP.elm
- zrb 13y agoIs Maybe chaining the same as lifting? I worked on a toy example for this before. http://stackoverflow.com/questions/7690864/haskell-style-maybe-type-chaining-in-c11 http://stackoverflow.com/questions/7690864/haskell-style-may... http://stackoverflow.com/questions/9692630/implementing-haskells-maybe-monad-in-c11 http://stackoverflow.com/questions/9692630/implementing-hask...
- coolsunglasses 13y agoSpeaking as a Haskell user: Maybe chaining is just sequencing functions against results with bind. Lifting is a general concept that is applicable to functors, applicatives, monads, etc. --> signifies the result of the computation fmap (+1) [0, 1, 2] --> [1, 2, 3] map (+1) [0, 1, 2] --> [1, 2, 3] fmap (+1) (Just 2) --> Just 3 In the above examples, fmap (from the Functor typeclass, polymorphic) and map (monomorphic function for mapping functions over lists) are lifting the functions they are passed. There's a little bit of regrettable redundancy in Haskell's typeclasses, fmap, liftA, and liftM (functor, applicative, monad) all the same thing. But liftM, while relevant to monads, isn't >>=, which is bind. bind being what's relevant to Maybe chaining (just using >>= against Maybe values).
- chas 13y agoLifting like this is one of the most useful ideas I have taken from Haskell, but I think it's hard to see its utility through the C++. At it's core, lifting allows functions that don't require a computational context to be composed with values and functions that have a computational context. For instance, `unwords . reverse . words` is a function from Strings to Strings and is used like unwords . reverse . words $ "The quick brown fox" => "fox brown quick The" or (unwords . reverse . words) "The quick brown fox" => "fox brown quick The". This is just a pure deterministic function mapping values to values, it has no special context. To compose it with a value with a context like strings may not be present (Maybe String), it can be lifted with fmap (which really would be more informatively named liftF to keep with the Haskell lift function naming scheme). fmap (unwords . reverse . words) (Just "The quick brown fox") => Just "fox brown quick The" fmap (unwords . reverse . words) Nothing => Nothing It now has failure awareness for free, with no effort by the original function author. Haskell has an infix synonym for fmap, <$>, that makes this even more natural: unwords . reverse . words <$> Just "The quick brown fox" => Just "fox brown quick The" This looks very much like the first example: unwords . reverse . words $ "The quick brown fox" => "fox brown quick The" This is really powerful because it means any function with no special context can now be used with any context that implements Functor (or Applicative for multi-argument functions). The view that fmap executes functions with no context in a special context also explains why fmap for lists is map. A function returning a list can be looked at as a function non-deterministically returning multiple values. This can be seen when using monadic bind (flatMap/concatMap) with functions returning lists: [1..5] >>= (\x -> [x-1, x, x+1]) => [0,1,2,1,2,3,2,3,4,3,4,5,4,5,6] Looking at lists like this, a function with only one return value is deterministic, so if it is given a non-deterministic value, the result should be the result of applying the deterministic function to each possible non-deterministic value with no change in the number of possible non-deterministic values, that is to say that it should be mapped over the list! show $ 1 => "1" show <$> [1..5] => ["1","2","3","4","5"] fmap show [1..5] =>["1","2","3","4","5"] The key idea here is that lifting like this provides a way to easily and extensibly compose functions with different expectations about the context in which they will be executing. This idea could be fruitfully applied in many languages outside of Haskell and I'm excited to see how it interacts with other parts of C++.
- 13y ago