News for hackers. Stuff that matters.
Re:Making Wrong Code Look Wrong by pavpanchekha on Monday August 22, 2011 @12:18PM (#2912943)
Does it really have to be a monad? Isn't a functor what we're really looking for?
Re:Making Wrong Code Look Wrong by jrockway on Monday August 22, 2011 @02:39PM (#2913523)
It depends; if all you want to do is lift normal functions to the domain of unsafe operation (where an unsafe input implies unsafe output), then sure:

    newtype Unsafe a = Unsafe a
    
    instance Functor Unsafe where
        fmap f (Unsafe k) = Unsafe . f $ k

    addTwo :: Int -> Int
    addTwo = (+2)

    unsafeAddTwo :: Unsafe Int -> Unsafe Int
    unsafeAddTwo = fmap addTwo
But really, I'm not sure this is the right approach. Even values generated inside my program need to be quoted for inclusion on an HTML page. What you want to avoid is double-quoting, so what you need is simpler:

    data Content = Quoted String | Unquoted String

    output :: [Content] -> Content
    output = concatMap f
       where f (Unquoted x) = quote x
             f (Quoted   x) = x
Now the type system ensures that (output . output) == output, which is what you really want to ensure. Tainted data, I think, is a separate concern. And the solution, in that case, doesn't involve a functor, it involves making sure your library tags everything as Unsafe and that your data validation functions remove that annotation:

    type Params a = Map String (Unsafe a) -- keys may also be unsafe, YMMV

    readHtmlForm :: Request -> Params String
    validateField :: Validatable a => Unsafe a -> a

    main = output . Unquoted . validateField . get "foo" . readHtmlForm <$> fakeHttpRequest
Re:Making Wrong Code Look Wrong by Bootvis on Monday August 22, 2011 @02:18PM (#2913437)
That comment illustrates the problem with Haskell. Yes it's nice and logical but for some reason also very hard.
Re:Making Wrong Code Look Wrong by pavpanchekha on Tuesday August 23, 2011 @09:32AM (#2916460)
This comment illustrates the problem with comments about problems with Haskell. It doesn't quite understand what it's complaining about.

Functor vs. Monad in this case is a way of talking about how exactly this construct should work --- it's not a meaningless distinction. As another response to my comment mentioned, both a functor or a monad are applicable. The question is whether two instances of this data type (I'm using non-Haskell-y terms for clarity) have influence on each other (in rough terms). So it's not that I was complaining that the parent was wrong. I was complaining that the semantics he was imposing on his quoted strings were too restrictive. Another poster instead mentioned ways in which mine were too loose. So my post was part of a constructive debate on what exactly we want quoted strings to do. That Haskell provides a vocabulary for communicating precisely and tersely is not it's fault, it is one of its strengths.

Re:Making Wrong Code Look Wrong by jrockway on Monday August 22, 2011 @02:22PM (#2913452)
Any monad is also a functor, so they're both right!
Re:Making Wrong Code Look Wrong by eru on Monday August 22, 2011 @04:36PM (#2914014)
Actually, he probably wants an applicative functor, which sits between functors and monads.

Or perhaps an arrow or a co-monad are better? ;o)

Re:Making Wrong Code Look Wrong by eru on Monday August 22, 2011 @04:37PM (#2914017)
> [...] but for some reason also very hard.

Yes. But that's a feature, not a bug.

Re:Making Wrong Code Look Wrong by gmartres on Tuesday August 23, 2011 @07:53AM (#2916205)
It's not hard, it's different.
Re:Making Wrong Code Look Wrong by yummyfajitas on Monday August 22, 2011 @09:17PM (#2914899)
The way I envisioned using it, yes. The use of a Monad over a Functor is merely because I'm still very much a Haskell newbie.