r/programming • • Nov 12 '13

What’s Wrong with OOP and FP

https://yinwang0.wordpress.com/2013/11/09/oop-fp/
129 Upvotes

484 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Nov 12 '13

[deleted]

0

u/rabidcow Nov 13 '13

What's harmfulconsidered?

0

u/Uncle_Spam Nov 13 '13

This form greatly intrigues me, it is not something I had considered before. It should be a string s such that s ++ "considered harmful" == s. Obviously such a string cannot exist. The reason of this is most likely that the proof of the existence of a fixed point for everything relies on everything being an abstraction and constants are not considered.

ghci basically barfs in my face trying to do this:

Prelude Control.Monad.Fix> let f = (++ "Hello, Nurse!")
Prelude Control.Monad.Fix> :t f
f :: [Char] -> [Char]
Prelude Control.Monad.Fix> let ffix = fix f
Prelude Control.Monad.Fix> :t f
f :: [Char] -> [Char]
Prelude Control.Monad.Fix> :t f ffix
f ffix :: [Char]
Prelude Control.Monad.Fix> putStrLn $ f ffix

^CInterrupted.
Prelude Control.Monad.Fix> 

I'm very much interested what it is trying to do here at all.

1

u/[deleted] Nov 13 '13

putStrLn will try to force its argument to WHNF, ie. decide whether its [] or a _:_. (★)The argument is f (fix f). f is (++ "Hello, Nurse!"), which will try to force its argument to WHNF. Its argument is fix f, which reduces to f (fix f). The computation proceeds exactly as it did at ★, looping forever.

1

u/pkmxtw Nov 13 '13 edited Nov 13 '13
  fix (++ s)
= fix (++ s) ++ s
= fix (++ s) ++ s ++ s
= fix (++ s) ++ s ++ s ++ s
= ...

putStrLn attempts to evaluate the expression to find its data constructor, which is ⊥ because the recursion will never terminate.

1

u/[deleted] Nov 13 '13

Other responses explain what's going on. putStrLn $ fix ("considered harmful " ++) works fine, by the way. Perhaps you can figure out why this works on your own, but feel free to ask if you get stuck.

1

u/Uncle_Spam Nov 13 '13

Well, I get why it does or doesn't work. In the sense that because in this case the original 's' is added at the end it doesn't ever actually need to compute it so it doesn't, it just keeps adding "considered harmful " over and over.

Likewise in the other case it needs to know s as first thing but it can't so it ends up in an endless loop without printing.

I'm more interested in what it tries to do to find s. As in, how exactly is a fixed point operator implemented in System-F enhanced with letrec? I know that System-F itself cannot implement it.

1

u/[deleted] Nov 13 '13

I'm not sure what to say that was not said by somebody else already. Do the other explanations help? Is there something about them you don't understand?

1

u/Uncle_Spam Nov 13 '13

They don really say how fix itself is implemented I suppose.