POV-Ray : Newsgroups : povray.off-topic : Why is Haskell interesting? Server Time
26 Nov 2026 09:41:08 EST (-0500)
  Why is Haskell interesting? (Message 38 to 87 of 87)  
<<< Previous 37 Messages Goto Initial 50 Messages
From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 04:51:34
Message: <4b8a3ca6$1@news.povray.org>
nemesis wrote:

>>   So why choose ! as "the nth element"?
> 
> v!1 is briefer than v[1] :)

So is v@1. But that's not what was chosen.

Personally, I'd kinda like to be able to use v[1], but hey.

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 05:04:29
Message: <4b8a3fad$1@news.povray.org>
>>    case size c of
>>      3 -> case (size (c ! 0), size (c ! 1), size (c ! 2)) of
>>             (1, 2, 3) -> (c ! 0 ! 0) + (c ! 1 ! 0) + (c ! 2 ! 0)
>>             _         -> 0
>>      _ -> 0
> 
>   I don't know why, but I got an irresistible urge to write some faux haskell
> after seeing that.
> 
>     case closed in d by
>       x -> case (open, not!, closed, yes!, 2) because
>            (1, 2, 3) -> (one, two, three) + x
>             hence    -> yes
>       hence -> no
> 
>   To me, it makes exactly as much sense. :P

It makes sense to me, but it's awfully ugly. At present I can't think of 
a way to do better...

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 05:05:27
Message: <4b8a3fe7$1@news.povray.org>
Captain Jack wrote:

> I know it's interesting to *me* because I'd never heard of it before I 
> started reading this newsgroup. Always like learning something new... :-)

Heh, yeah. People claim it's the most talked-about language on the 
Internet, but I'd never heard of it either until I had a random 
encounter one day. And I've never looked back... ;-)

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 08:40:29
Message: <4b8a724d@news.povray.org>
Neeum Zawan <m.n### [at] ieeeorg> wrote:
>         Now you know how lots of people feel when they have to learn UNIX
> (command line). At some point, you just tell them, "Hey, that's the way
> it is. Weird, obscure, but works, and doesn't really get in your way
> once you know it".

  The difference is that unix is 43 years old and there were no established
conventions back then.

  If you develop a new programming language today, there should be a good
reason why you go against general conventions. Making it different just for
the sake of being different is not smart.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 11:46:29
Message: <4b8a9de5$1@news.povray.org>
Warp wrote:
> Neeum Zawan <m.n### [at] ieeeorg> wrote:
>>         Now you know how lots of people feel when they have to learn UNIX
>> (command line). At some point, you just tell them, "Hey, that's the way
>> it is. Weird, obscure, but works, and doesn't really get in your way
>> once you know it".
> 
>   The difference is that unix is 43 years old and there were no established
> conventions back then.
> 
>   If you develop a new programming language today, there should be a good
> reason why you go against general conventions. Making it different just for
> the sake of being different is not smart.

Perhaps you're forgetting that Haskell is also 20 years old?

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 11:57:19
Message: <4b8aa06e@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> Perhaps you're forgetting that Haskell is also 20 years old?

  Hence a complete newcomer as programming languages are concerned.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 12:10:04
Message: <4b8aa36c$1@news.povray.org>
>> Perhaps you're forgetting that Haskell is also 20 years old?
> 
>   Hence a complete newcomer as programming languages are concerned.

Not nearly as new as the likes of Java, Python, Ruby, etc. But sure, not 
as ancient as Lisp or C...

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 12:21:53
Message: <4b8aa630@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> Not nearly as new as the likes of Java

  Well if you consider 5 years older as "not *nearly* as new"... :P

> Python

  Or 1 year older in this case...

> Ruby

  Ruby is 5 years newer as well.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 14:54:25
Message: <4b8ac9f1$1@news.povray.org>
Orchid XP v8 wrote:
> Heh. If you mean "why isn't it some random symbol?" then the answer is 
> "you can only use symbols for *binary* functions, not *unary* functions 
> such as NOT".

It could be worse. It could be APL, where functions are in greek.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 16:15:11
Message: <4b8adcdf@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> Heh. If you mean "why isn't it some random symbol?" then the answer is 
> "you can only use symbols for *binary* functions, not *unary* functions 
> such as NOT".

  How do you negate an integral or floating point value, then?

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 16:49:27
Message: <4b8ae4e7$1@news.povray.org>
>> Heh. If you mean "why isn't it some random symbol?" then the answer is 
>> "you can only use symbols for *binary* functions, not *unary* functions 
>> such as NOT".
> 
>   How do you negate an integral or floating point value, then?

Haskell possesses exactly *one* unary operator: unary minus. (Because, 
let's face it, not having this would just be annoying.) This is 
hard-wired into the language spec, however. You cannot define new unary 
operators.

The existence of the unary minus operator causes the following glitch:

   (5+) is a function.
   (5-) is a function.
   (5*) is a function.
   (5/) is a function.
   (+5) is a function.
   (-5) is a number.
   (*5) is a function.
   (/5) is a function.

Spot the odd one out. (There's a library function named "subtract" so 
that you can write "subtract 5" instead of "(-5)" to get a function.)

And it *is* unary minus, not part of the numeric literal syntax; for 
example "-x" works just as well as "-5" does.

(Saying "-x" is equivilent to saying "negate x". In particular, since 
unary minus is a syntax quirk, you can't define it directly. You define 
"negate" directly, and the compiler does the rest. Also, just for 
giggles, the default implementation of binary minus is "x - y = x + 
(negate y)"...)

So there - aren't you sorry you asked?

You may also have noticed that "sin -5" doesn't work. That's because 
it's interpretted as *binary* minus, i.e. "sin - 5", which obviously 
doesn't type-check. And *that* is why you have to say "sin (-5)". 
Annoying, isn't it?

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: nemesis
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 17:15:01
Message: <web.4b8aea70b2efd740fa5d1ec00@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> Neeum Zawan wrote:
>
> >  Now you know how lots of people feel when they have to learn UNIX
>
> ls, cp, mv, rn and friends. Becuase, let's face it, adding two octets to
> cp to make copy would be a crime...

there were no visual text editors back then.  You entered chars and there was a
lag before they were printed.  The less chars, the less the chance of errors.
Kinda like the choice of bits too.


Post a reply to this message

From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 17:30:35
Message: <4b8aee8b$1@news.povray.org>
>> ls, cp, mv, rn and friends. Becuase, let's face it, adding two octets to
>> cp to make copy would be a crime...
> 
> there were no visual text editors back then.  You entered chars and there was a
> lag before they were printed.  The less chars, the less the chance of errors.
> Kinda like the choice of bits too.

When I was briefly in the local council offices, I went into their 
central computer room.

Now, this was 1997 or so, but I am not kidding, EVERYTHING WAS BROWN! Do 
you have any idea how long ago they stopped making computers in brown 
and cream?!?

They had a machine with a big green screen - you know, the kind where 
you can actually *see* the scanlines, and text takes a split second or 
two to actually fade off the screen. (And you can see what the most 
common message is, because it's permanently burned into the phosphor.)

Anyway, when you type on the clunky keyboard, there's a sort of 2-second 
delay, and then the character appears on the screen. Apparently the 
mainframe you're operating is actually 60 miles down the road, connected 
over some kind of telephone link. And the terminal is a *real* dumb 
terminal. o_O

I have no idea what kind of system it runs - it wasn't Unix, or at least 
didn't appear to be. But if you type in something like "lx 37f7h.b" or 
similar, the huge line printer next to it suddenly goes crazy and starts 
printing people's benefits cheques.

It wouldn't surprise me if that's how the council's computer systems 
*still* work...

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 17:33:27
Message: <4b8aef37@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
>    (+5) is a function.

  But I thought you said you couldn't have symbols as unary functions.

-- 
                                                          - Warp


Post a reply to this message

From: nemesis
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 17:35:00
Message: <web.4b8aeeacb2efd740fa5d1ec00@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> >> Perhaps you're forgetting that Haskell is also 20 years old?
> >
> >   Hence a complete newcomer as programming languages are concerned.
>
> Not nearly as new as the likes of Java, Python, Ruby, etc. But sure, not
> as ancient as Lisp or C...

I believe Haskell is older than those.  Haskell was to be a kind of open
reimplementation of Miranda, which was developed in the early 80's.  It adopted
most of Miranda's syntax, and if some of that sounds weird, it is because C++
was still being developed back then as well...


Post a reply to this message

From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 17:36:41
Message: <4b8aeff9$1@news.povray.org>
Warp wrote:
> Orchid XP v8 <voi### [at] devnull> wrote:
>>    (+5) is a function.
> 
>   But I thought you said you couldn't have symbols as unary functions.

It's a curried function.

If you take any N-argument function and call it with 1 argument, you get 
a new (N-1)-argument function as the result. Since (+) is a 2-argument 
function, giving it one argument (here 5) yields a new 1-argument function.

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Orchid XP v8
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 17:37:55
Message: <4b8af043$1@news.povray.org>
nemesis wrote:
> Orchid XP v8 <voi### [at] devnull> wrote:
>>>> Perhaps you're forgetting that Haskell is also 20 years old?
>>>   Hence a complete newcomer as programming languages are concerned.
>> Not nearly as new as the likes of Java, Python, Ruby, etc. But sure, not
>> as ancient as Lisp or C...
> 
> I believe Haskell is older than those.  Haskell was to be a kind of open
> reimplementation of Miranda, which was developed in the early 80's.  It adopted
> most of Miranda's syntax, and if some of that sounds weird, it is because C++
> was still being developed back then as well...

Apparently Miranda was released 1985, making it "only" 5 years older 
than Haskell. So even Miranda is "only" 25 years old.

To me, 25 years sounds like a hell of a long time in computing terms. 
But what do I know?

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: nemesis
Subject: Re: Why is Haskell interesting?
Date: 28 Feb 2010 18:10:00
Message: <web.4b8af76ab2efd740fa5d1ec00@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> nemesis wrote:
> Apparently Miranda was released 1985, making it "only" 5 years older
> than Haskell. So even Miranda is "only" 25 years old.

still, older than all of those, born in the 90's from C and Unix ideas mostly.

> To me, 25 years sounds like a hell of a long time in computing terms.
> But what do I know?

It is in terms of processing power, but not in how people tap that processing
power.  Things evolve a lot slower in the software side, thanks to the same old
ideas that drag around...


Post a reply to this message

From: Invisible
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 04:26:08
Message: <4b8b8830$1@news.povray.org>
Darren New wrote:
> Orchid XP v8 wrote:
>> just mildly curious to know whether you can implement your own 
>> tokenising rules or not.
> 
> You're actually reading the input from the file. How could you *not* 
> implement your own tokenizing rules?

If it hands the input to you one token at a time, then it's already been 
split into tokens. If, on the other hand, it lets you take one character 
at a time, then you can do whatever you want.


Post a reply to this message

From: Invisible
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 04:46:20
Message: <4b8b8cec$1@news.povray.org>
>>> Hell, even Fortran is OO for the last 10 years.
>> People use Fortran? I thought that was only for scientific applications. 
> 
> And scientists aren't people?

[There's gotta be a joke in there somewhere...]

Scientific applications are a vanishingly small market segment.

>> Look at job adverts, or websites which talk about programming.
> 
> That's not "mainstream", that's "popular". :-)

There's a difference?

>> If you specifically want a binary tree, the simplest and most logical 
>> OO way would be to make leaves and branches different subclasses.
> 
> Not especially. What about a branch with only one child node?

Why would a branch have only one child? I've not seen that in too many 
algorithm descriptions. Usually a node has 0 or 2 children, not 1.

> And yes, even if leaves and branches are separate classes, it doesn't 
> mean that they both aren't of type "treenode", or the leaf could be a 
> subclass of branch or vice versa.

Sure, depending on what kind of tree you want, leaf and branch aren't 
necessarily sibling classes.

>> I meant I've never seen it done like that in an OO language.
> 
> You've never seen a N-ary tree in an OO language that used the same 
> class for branches and leaves?  Wow.

I haven't seen an N-ary tree in an OO language. ;-) Only strictly 
*binary* trees.

>> My point is that something like a parse tree usually doesn't need to 
>> be dynamically extensible. If you make an extension to the language, 
>> you typically need to rewrite all the code for processing the parse 
>> tree anyway, so the fact that it's monolithic isn't too much of an issue.
> 
> I disagree. The whole point of giving you the list of passes was showing 
> you how modular each pass can be, as well as showing you how modular 
> each parse node type needs to be.

Well, I guess no matter what I say everybody will conclude that I'm just 
a moron who doesn't know what he's talking about anyway, so...

>> And you can take a C++ program and use pointer arithmetic to access 
>> private member variables. Does that mean C++ doesn't provide 
>> encapsulation?
> 
> Yes.

Right. So any programming language that runs on a digital computer 
inherantly lacks encapsulation then?

That's a stupid definition. Encapsulation is about whether the language 
tries to limit your access to the internal implementation of an object, 
not about whether it's physically possible to circumvent the restriction 
by some suitably elaborate route.

> What is this?
> 
>    (lambda (x) (x + 1))
> 
> It's a lambda expression.
> 
>    y = (lambda (x) (x + 1))
> 
> What is y?  It's a closure.

I still don't get it. (But then, I don't even know what language that is...)

>> OK. So in what way does this mean that "functions are not first-class"?
> 
> I didn't say it did. I said that everything in Javascript is an object. 

OK. So even a function is an object. Since objects are first-class, that 
means objects are first class.

> OK, you said
>  > The difference is that Haskell functions work on data, while 
> Smalltalk only has objects. In other words, you can't invoke a function 
> without knowing what object that "function" is a method of. (This is 
> also the case for Javascript, btw.)

You wanna go back and check that?

YOU wrote this paragraph, not me. ;-)

>> I can imagine there are product written in Java that somebody has to 
>> support, or that there are Java compilers that somebody has to 
>> support. But the Java language itself?
> 
> Well, no, there's nobody who has to support the spec, as such. I meant 
> there are major systems where someone is paid to make sure the Java it's 
> based on keeps working.

And this isn't the case for Haskell?

As I said, the likes of Galois and Well Typed make their money primarily 
designing systems where correctness is vital. What kind of systems do 
you suppose those are?

(It's also conspicuous that both Galois and Well Typed employ people who 
are also GHC developers... so maybe that's your answer!)


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 12:10:44
Message: <4b8bf514$1@news.povray.org>
Invisible wrote:
> Scientific applications are a vanishingly small market segment.

I'm not so sure about that.

>> That's not "mainstream", that's "popular". :-)
> There's a difference?

Yes.

>>> If you specifically want a binary tree, the simplest and most logical 
>>> OO way would be to make leaves and branches different subclasses.
>>
>> Not especially. What about a branch with only one child node?
> 
> Why would a branch have only one child? 

Populate the tree one element at the time. At least half the time, you'll 
have a node with only one child.

Perhaps the reason you don't see it a lot in Haskell is because the 
definition of a node with only one leaf would be ugly. :-)

> I haven't seen an N-ary tree in an OO language. ;-) Only strictly 
> *binary* trees.

Wow. You need to read more code.  You've never seen a hashtable full of 
hashtables, or a parse tree?

> Right. So any programming language that runs on a digital computer 
> inherantly lacks encapsulation then?

Nope. If the language defines what happens when you try to violate 
encapsulation, then you have encapsulation.

> That's a stupid definition. Encapsulation is about whether the language 
> tries to limit your access to the internal implementation of an object, 
> not about whether it's physically possible to circumvent the restriction 
> by some suitably elaborate route.

Right. But in unsafe languages, it's not a matter of "suitably elaborate."

Let's ask this: what good is encapsulation? Why do people think 
encapsulation is a good thing?

Answer: because it limits where you have to look to understand the behavior 
of code. If your language has encapsulation, you can look at the class of 
that object to determine what accesses and modifies its internal state. If 
the language has encapsulation but also an escape to an unsafe language 
(like JNI or P/Invoke or so), then you have to look at your class and all 
unsafe code that might change the contents of your class.

Let's ask this: Why doesn't C++ have encapsulation?

Answer: Where in your code might lie a bug that is causing your class to 
violate its invariants? If you have two variables in your instance, one of 
which must always be two times the other, where do you have to look if that 
is not the case? How much of your code base might cause that change?

> 
>> What is this?
>>
>>    (lambda (x) (x + 1))
>>
>> It's a lambda expression.
>>
>>    y = (lambda (x) (x + 1))
>>
>> What is y?  It's a closure.
> 
> I still don't get it. (But then, I don't even know what language that 
> is...)

Do you understand the difference between classes and instances?



>>> OK. So in what way does this mean that "functions are not first-class"?
>>
>> I didn't say it did. I said that everything in Javascript is an object. 
> 
> OK. So even a function is an object. Since objects are first-class, that 
> means objects are first class.

Yes, and yes.  And functions have the class "Function".

> YOU wrote this paragraph, not me. ;-)

OK. My bad.  Altho that would certainly explain why I was confused. ;-)

> And this isn't the case for Haskell?

I don't know. Maybe it is, but you don't make it sound that way.

> As I said, the likes of Galois and Well Typed make their money primarily 
> designing systems where correctness is vital. What kind of systems do 
> you suppose those are?

I couldn't guess. Galios has an empty web site and Well Typed are consultants.

I'd honestly be quite surprised if either one builds systems in Haskell 
where correctness is actually vital. As in, an error in the program means 
people die.

> (It's also conspicuous that both Galois and Well Typed employ people who 
> are also GHC developers... so maybe that's your answer!)

Yep.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 14:31:15
Message: <4b8c1603@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Let's ask this: Why doesn't C++ have encapsulation?

  Clearly we have a different definition of "encapsulation".

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 14:52:29
Message: <4b8c1afd@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Let's ask this: Why doesn't C++ have encapsulation?
> 
>   Clearly we have a different definition of "encapsulation".

I explained why encapsulation is important and why unsafe languages don't 
have it.  What's your definition of encapsulation, and why does it make a 
difference to the programming productivity?

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 15:02:44
Message: <4b8c1d64@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> > Darren New <dne### [at] sanrrcom> wrote:
> >> Let's ask this: Why doesn't C++ have encapsulation?
> > 
> >   Clearly we have a different definition of "encapsulation".

> I explained why encapsulation is important and why unsafe languages don't 
> have it.  What's your definition of encapsulation, and why does it make a 
> difference to the programming productivity?

  It seems that in your quest to belittle "unsafe languages" you have taken
some obscure definition of a term somewhere, treat it as if it was the
unversally accepted definition, and point out how your beloved "unsafe
languages" don't conform to that obscure definition.

  Encapsulation in object-oriented languages is not *my* definition:
http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29

  Nowhere do I see anything related to what you wrote.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 16:04:09
Message: <4b8c2bc9$1@news.povray.org>
Warp wrote:
>   It seems that in your quest to belittle "unsafe languages" you have taken
> some obscure definition of a term somewhere, 

Not at all. I'm simply explaining why safety is important to encapsulation.

Do you think Python encapsulates data?  If so, then C++ does too.

If you think Python insufficiently protects its data, then neither does any 
unsafe-by-default language.

>   Nowhere do I see anything related to what you wrote.

The whole "information hiding" bit, for one.

"""
Under this definition, encapsulation means that the internal representation 
of an object is generally hidden from view outside of the object's definition.
"""

C++ fails this. Internal representation is in the header file. But that's 
not what I'm talking about.

"""
Typically, only the object's own methods can directly inspect or manipulate 
its fields.
"""

All unsafe languages fail this.  (Lots of safe languages do too, with 
appropriate declarations of their unsafeness.)

"""
Hiding the internals of the object protects its integrity by preventing 
users from setting the internal data of the component into an invalid or 
inconsistent state.
"""

Unsafe languages fail at this. Either that, or you have never ever 
encountered a wild-pointer bug. The fact that you don't frequently violate 
encapsulation doesn't mean that encapsulation is enforced, any more than the 
fact that people don't usually screw around with the internals of Python 
instances doesn't mean Python has good encapsulation in that sense.

In theory, yes, if you don't violate the language mechanisms, then C++ 
provides encapsulation. In practice, it doesn't, because when some value in 
your object is randomly changed by a bug in some code running in other 
thread, you're fairly well screwed in terms of figuring out the 
encapsulation.  That is, pretty much, the difference between safe languages 
and unsafe languages: Safe languages *enforce* encapsulation. (And again, 
most safe languages allow you to circumvent that by dropping into an unsafe 
subset, but some don't.)

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 16:42:15
Message: <4b8c34b7@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> """
> Under this definition, encapsulation means that the internal representation 
> of an object is generally hidden from view outside of the object's definition.
> """

> C++ fails this. Internal representation is in the header file. But that's 
> not what I'm talking about.

  "Data hiding" doesn't mean "hidden from the programmer", but "hidden from
the outside scope". In other words, code out of the scope of the class has
no direct access to the internals of the class (in other words, you get a
compiler error if you try to access).

  You could as well argue that if you had an open source library in a language
with true data hiding (by your definition of the term), it stops being data
hiding because the programmer could just look at the source code of the
library and see the internal structure of the objects. Just because the
programmer can see the internal structure of the object doesn't stop it
from being data hiding.

  The purpose of encapsulation and data hiding is not to obscure information
from the programmer, but to offer an abstraction tool for the program design.
(In that way "data hiding" is perhaps slightly a misnomer, and something like
"scope access restrictions" would be better.)

  In other words, if you want to keep a good abstraction level in your
program, if something is marked as "private" then that something doesn't
interest you unless you are actually implementing the class which owns
that private member. Just because the class says explicitly "hey, I have
this private member" doesn't stop it from being encapsulation and data
hiding.

> """
> Typically, only the object's own methods can directly inspect or manipulate 
> its fields.
> """

> All unsafe languages fail this.  (Lots of safe languages do too, with 
> appropriate declarations of their unsafeness.)

  Now you are deliberately confusing a general non-restriction of memory
access with the concept of data hiding.

  Again, data hiding is a tool offered by the language which you can use
to increase abstraction in your program. Just because the language doesn't
guarantee that modification of random memory locations using wild pointers
will always be caught doesn't make the tool nonexistent. The tool is still
there for you to use, and the compiler will enforce it as long as you write
your code as intended.

> """
> Hiding the internals of the object protects its integrity by preventing 
> users from setting the internal data of the component into an invalid or 
> inconsistent state.
> """

> Unsafe languages fail at this. Either that, or you have never ever 
> encountered a wild-pointer bug.

  A wild pointer bug is at a completely different conceptual level than
data hiding. Encapsulation exists as a tool for *programming*, not as a
tool for the runtime. If you use encapsulation as intended, and your
program is correct, then the compiler will enforce the encapsulation.

> In theory, yes, if you don't violate the language mechanisms, then C++ 
> provides encapsulation. In practice, it doesn't, because when some value in 
> your object is randomly changed by a bug in some code running in other 
> thread, you're fairly well screwed in terms of figuring out the 
> encapsulation.

  I think that your problem is that you somehow write like having bugs
in your program is some kind of valid programming technique. "Encapsulation
should be enforced even if there are bugs in your program" sounds quite
silly. It kind of implies that "your program should still run ok even if
there are bugs in it".

  Assume that you have a bug in your program in that you forgot to write
"private" in conjunction with a member declaration, making it public.
There's a bug in your program, which made the member accessible from the
outside. By your definition this programming language doesn't enforce
encapsulation because if there are bugs in your program it may allow
private members to be accessible from the outside.

  That's just silly. You cannot define the concept of encapsulation in
terms of buggy programming.

>  That is, pretty much, the difference between safe languages 
> and unsafe languages: Safe languages *enforce* encapsulation.

  No, they don't. If you make a programming error and inadvertedly make
a member public, the compiler will do squat about that.

  You are deliberately confusing a programming technique such as
encapsulation with a runtime technique such as memory protection.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 17:22:18
Message: <4b8c3e1a@news.povray.org>
Warp wrote:
>   "Data hiding" doesn't mean "hidden from the programmer", but "hidden from
> the outside scope".

Fair enough. If that's how you want to look at it.

The way I see it is that encapsulation is a mechanism to reduce the area you 
have to look to understand or use or debug a particular part of a program. 
It makes things modular. The less modular changes are, the less encapsulated 
the changes are.

If you can't see or access the private variables at all, that's extremely 
encapsulated. If you can see and access them only through reflection or 
explicitly-declared unsafe operations, that's less encapsulated. If you need 
to see them because changes in private parts mean you have to deal with it 
in your own code (say, by recompiling or changing the size of structure 
declarations), that's also less encapsulated. When a bug in one part of the 
program can cause arbitrary changes elsewhere, that's really minor 
encapsulation.

I find "the compiler tells you if you explicitly write code that tries to 
access stuff you can see but is flagged private" as the weakest form of 
encapsulation out there, in terms of "information hiding". Only something 
like Python that doesn't even try is weaker than that.

> In other words, code out of the scope of the class has
> no direct access to the internals of the class 

And you understand why I'm disputing that this is the case, right?

*Correct* code has no direct access to the internals of the class *by name.*

Incorrect code has direct access to the internals of the class, and correct 
code has direct access as long as it doesn't do so by name. (For example,
   write(outfile, &myInstance, sizeof myInstance);

>   You could as well argue that if you had an open source library in a language
> with true data hiding (by your definition of the term), it stops being data
> hiding because the programmer could just look at the source code of the
> library and see the internal structure of the objects. Just because the
> programmer can see the internal structure of the object doesn't stop it
> from being data hiding.

No. I'm not talking about just the programmer.

>   The purpose of encapsulation and data hiding is not to obscure information
> from the programmer, but to offer an abstraction tool for the program design.

That's one of many purposes. I think the primary purpose is not to say "this 
is public and that is private", which could be done as easily by a naming 
convention or comments, but to also allow for debugging, understanding, 
production pipelines, etc.

This abstraction is the only encapsulation that C++ provides, so naturally 
that's what you think the purpose of encapsulation is. But there's a whole 
bunch of reasons for encapsulation.  Plus, these additional forms of 
encapsulation have all kinds of other benefits, which is why (for example) 
you don't see applets written in C++.

> (In that way "data hiding" is perhaps slightly a misnomer, and something like
> "scope access restrictions" would be better.)

Now who is changing definitions? :-)

>   In other words, if you want to keep a good abstraction level in your
> program, if something is marked as "private" then that something doesn't
> interest you unless you are actually implementing the class which owns
> that private member. 

And in Python, if a variable name starts with _ then that variable doesn't 
interest you. Does that mean Python has encapsulation like C++ does?

In languages where the private variables aren't exposed at all, this 
question doesn't even come up. So C++ (and Ada, and the other languages that 
increase efficiency by exposing the private declarations to client code) 
isn't even very good at this. You wouldn't even *need* a "private" 
declaration in the header file but for that. It wouldn't be a case of "the 
compiler tells me that's private" as much as "the compiler tells me that's 
not a member of the class."

>> All unsafe languages fail this.  (Lots of safe languages do too, with 
>> appropriate declarations of their unsafeness.)
> 
>   Now you are deliberately confusing a general non-restriction of memory
> access with the concept of data hiding.

No. You're confusing data hiding with encapsulation. You've changed the 
subject from "encapsulation" to "data hiding", because data hiding is the 
*only* kind of encapsulation C++ supports.

If you asked me if C++ supports data hiding, I would have said "Yes, poorly, 
because you can see what's there and have to deal with it, but the compiler 
enforces the private keyword."

I told you why encapsulation is important, listing several reasons. Data 
hiding is just one of them.

>   Again, data hiding is a tool offered by the language which you can use
> to increase abstraction in your program. Just because the language doesn't
> guarantee that modification of random memory locations using wild pointers
> will always be caught doesn't make the tool nonexistent. The tool is still
> there for you to use, and the compiler will enforce it as long as you write
> your code as intended.

That's correct. But that's not all that encapsulation encompasses. "Data 
hiding" is not the only thing that encapsulation provides.

> 
>> """
>> Hiding the internals of the object protects its integrity by preventing 
>> users from setting the internal data of the component into an invalid or 
>> inconsistent state.
>> """
> 
>> Unsafe languages fail at this. Either that, or you have never ever 
>> encountered a wild-pointer bug.
> 
>   A wild pointer bug is at a completely different conceptual level than
> data hiding. 

Yep. Not the point tho. It's at a different conceptual level, which is 
exactly *why* it is so hard to debug. You can't look at the assignments to 
the variable and figure out which one might have set the denominator of your 
Fraction class to zero.

Look, I'm just going thru the definition *you* pointed me at and showing 
that unsafe languages don't meet the definition.

You don't need to hide the internal variables to prevent someone from 
setting them to an invalid state. For example, Eiffel has clauses in the 
source code that describe the valid states of internal variables, so you 
could in theory check at each assignment that nobody outside the class is 
breaking the internal consistency.

The only way in which hiding data prevents the consistency from being 
corrupted is if it's actually inaccessible when it is hidden. Unsafe 
languages fail at that. You can clobber and make inconsistent data that is 
hidden at the "conceptual level" of the language.

> Encapsulation exists as a tool for *programming*, not as a
> tool for the runtime. 

I disagree. Considering what the compiler does without considering what the 
runtime does says little about your language. You're the one that says C++ 
is superior to Java because of the runtime.

Runtime encapsulation is what made Java popular in the first place. Indeed, 
runtime encapsulation was pretty much the entire point of Java's development.

> If you use encapsulation as intended, and your
> program is correct, then the compiler will enforce the encapsulation.

I already granted that "if your program is correct" then C++ has a minimal 
level of encapsulation. That's not really saying much, tho. "If your program 
is correct" in Python, you don't screw with publicly-accessible members that 
aren't documented.  That doesn't mean Python has as good encapsulation as 
C++ does.

>   I think that your problem is that you somehow write like having bugs
> in your program is some kind of valid programming technique. 

No. I write like someone who codes against libraries built by people who 
aren't as good a programmer as I am. We're long past the point in computers 
where people run "their program" in an environment where every bug is 
guaranteed to be their own.

Heck, even *with* the runtime encapsulation that Javascript enjoys, people 
*still* run separate tabs in separate processes.

> "Encapsulation
> should be enforced even if there are bugs in your program" sounds quite
> silly. 

Tell that to people running Java applets in their web browser. I mean, isn't 
that basically the difference between Java applets and ActiveX?

 > It kind of implies that "your program should still run ok even if
> there are bugs in it".

You know something? There's an awful lot of software that falls into this 
category. Know why? Because programmers aren't perfect.

>   Assume that you have a bug in your program in that you forgot to write
> "private" in conjunction with a member declaration, making it public.
> There's a bug in your program, which made the member accessible from the
> outside. By your definition this programming language doesn't enforce
> encapsulation because if there are bugs in your program it may allow
> private members to be accessible from the outside.

Not at all. The difference is that when I go to my class where the problem 
manifests as incorrect data in instance variables, and I look through the 
code that references those ought-to-be-private variables and see that none 
of them set it wrong, and then I look at the declaration and see it's 
public, I can change that to private and see who is accessing it that 
shouldn't.

On the other hand, when you have a wild pointer bug overwrite my instance 
variable, the place I have to look in the code to find that is *everywhere.* 
Including code I don't have source for.

You're either trolling me, or you're a really crappy and/or inexperienced 
programmer if you don't understand the difference between the scale of 
looking for a bug in those two places. The whole *point* of encapsulation is 
not to serve as documentation as to what's public and what's private, but to 
limit the amount of code you have to look at to understand the behavior of a 
given variable.

>   That's just silly. You cannot define the concept of encapsulation in
> terms of buggy programming.

Yes! You can! That's exactly the point. You're encapsulating who can change 
what values, so it's easy to find.

That's like saying "You cannot define the concept of structured programming 
based on not executing gotos to arbitrary locations, because if you're 
careful with your gotos, it's the same code as structured programming." 
Sure, that's exactly what it is, because you're *guaranteeing* that the 
structure is there. You don't have to check every goto to make sure it's 
only a goto from the end of "then" to the end of "else" or some such.

>>  That is, pretty much, the difference between safe languages 
>> and unsafe languages: Safe languages *enforce* encapsulation.
> 
>   No, they don't. If you make a programming error and inadvertedly make
> a member public, the compiler will do squat about that.

True, but now you're asking the compiler to detect errors in *correct* 
programs, which is orders of magnitude harder than asking the compiler to 
detect errors in incorrect programs. There are very few compilers that will 
detect that the program doesn't match the spec, altho LOTOS and Eiffel both try.

>   You are deliberately confusing a programming technique such as
> encapsulation with a runtime technique such as memory protection.

Not really. Indeed, when taken to extremes, encapsulation enforces safe 
language features. By using a "safe pointer" in C++, you've encapsulated the 
operations on the pointer and ensure you don't do something like access it 
after it's disposed. By using a "container" instead of just an array, you've 
encapsulated access to the container's elements and can enforce that you 
don't go out of bounds or throw an exception if you change it during 
iteration or some such.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 18:16:49
Message: <4b8c4adf@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> When a bug in one part of the 
> program can cause arbitrary changes elsewhere, that's really minor 
> encapsulation.

  You are still confusing a runtime memory protection mechanism with the
programming technique of data encapsulation.

  "A bug in the program breaks encapsulation promises" doesn't imply there's
no encapsulation. It only implies that there's a bug in your program which
makes it misbehave.

  You might argue that encapsulation without runtime memory protection of
the modules is worthless (or at least less useful), but that's a different
thing from claiming that there's *no* encapsulation. Encapsulation is a
programming technique, not a runtime mechanism. The memory protection
encompasses a lot more than just protecting module members from being
trashed.

> > In other words, code out of the scope of the class has
> > no direct access to the internals of the class 

> And you understand why I'm disputing that this is the case, right?

> *Correct* code has no direct access to the internals of the class *by name.*

> Incorrect code has direct access to the internals of the class, and correct 
> code has direct access as long as it doesn't do so by name. (For example,
>    write(outfile, &myInstance, sizeof myInstance);

  You are still confusing runtime memory protection with the programming
technique of encapsulation.

> > (In that way "data hiding" is perhaps slightly a misnomer, and something like
> > "scope access restrictions" would be better.)

> Now who is changing definitions? :-)

  Suggesting a more descriptive term is not changing the definition.
Data hiding has always been about access rights.

> >   In other words, if you want to keep a good abstraction level in your
> > program, if something is marked as "private" then that something doesn't
> > interest you unless you are actually implementing the class which owns
> > that private member. 

> And in Python, if a variable name starts with _ then that variable doesn't 
> interest you. Does that mean Python has encapsulation like C++ does?

  Does the compiler give you an error if you try to access the private
variable from the outside?

> In languages where the private variables aren't exposed at all, this 
> question doesn't even come up.

  Where the private members are specified is inconsequential.

> >   Now you are deliberately confusing a general non-restriction of memory
> > access with the concept of data hiding.

> No. You're confusing data hiding with encapsulation. You've changed the 
> subject from "encapsulation" to "data hiding", because data hiding is the 
> *only* kind of encapsulation C++ supports.

  They are used as synonyms, and I'm not the only one to do so.

> If you asked me if C++ supports data hiding, I would have said "Yes, poorly, 
> because you can see what's there and have to deal with it, but the compiler 
> enforces the private keyword."

  As I said, you could as well argue that if you look into the source code
of a library and see the internal structure of the object, it breaks data
hiding and thus the programming language has "poor support" for it.

  Just because the programmer can see what the object is composed of doesn't
mean there's no data hiding. Data hiding is about access rights.

> I told you why encapsulation is important, listing several reasons. Data 
> hiding is just one of them.

  Your definition of encapsulation is mixed with a runtime mechanism of
memory protection. I don't see that requirement anywhere.

> You're the one that says C++ is superior to Java because of the runtime.

  I don't even understand what you mean by that, or where I have said such
a thing. My complaints about Java are mainly based on its language features
(such as the inability to abstract a basic type away). These are basically
all compile-time features.

> Runtime encapsulation is what made Java popular in the first place. Indeed, 
> runtime encapsulation was pretty much the entire point of Java's development.

  No, what made Java popular was garbage collection (and lacking some
features which some people saw as "bad", such as multiple inheritance).
Also the idea of "write once, run everywhere".

> > "Encapsulation
> > should be enforced even if there are bugs in your program" sounds quite
> > silly. 

> Tell that to people running Java applets in their web browser. I mean, isn't 
> that basically the difference between Java applets and ActiveX?

  Now you are confusing it with sandboxing, another memory protection
mechanism.

>  > It kind of implies that "your program should still run ok even if
> > there are bugs in it".

> You know something? There's an awful lot of software that falls into this 
> category. Know why? Because programmers aren't perfect.

  And this is related to the definition of encapsulation how?

> >   Assume that you have a bug in your program in that you forgot to write
> > "private" in conjunction with a member declaration, making it public.
> > There's a bug in your program, which made the member accessible from the
> > outside. By your definition this programming language doesn't enforce
> > encapsulation because if there are bugs in your program it may allow
> > private members to be accessible from the outside.

> Not at all. The difference is that when I go to my class where the problem 
> manifests as incorrect data in instance variables, and I look through the 
> code that references those ought-to-be-private variables and see that none 
> of them set it wrong, and then I look at the declaration and see it's 
> public, I can change that to private and see who is accessing it that 
> shouldn't.

> On the other hand, when you have a wild pointer bug overwrite my instance 
> variable, the place I have to look in the code to find that is *everywhere.* 
> Including code I don't have source for.

  Some bugs are easier to find than others. This has what to do with the
definition of "encapsulation"?

  If there's a bug in the operating system which randomly trashes the
memory of a program, does that mean the programming language which was
used to create that program has no encapsulation?

  What if the runtime which executes the program has a bug which trashes
its memory. Does that mean the programming language has no encapsulation?

  If there's a faulty RAM chip in your computer which causes part of the
program's memory to be trashed, does that mean there's no encapsulation in
the programming language?

  You are confusing runtime memory protection with the programming technique
of encapsulation. They are different things.

> You're either trolling me, or you're a really crappy and/or inexperienced 
> programmer if you don't understand the difference between the scale of 
> looking for a bug in those two places.

  Some bugs are easier to find than others. This has what to do with the
definition of "encapsulation"?

> The whole *point* of encapsulation is 
> not to serve as documentation as to what's public and what's private, but to 
> limit the amount of code you have to look at to understand the behavior of a 
> given variable.

  And you arbitrarily set the limit of what still constitutes as acceptable
"encapsulation" and what doesn't somewhere between the program's own runtime
and the OS.

> >   That's just silly. You cannot define the concept of encapsulation in
> > terms of buggy programming.

> Yes! You can! That's exactly the point. You're encapsulating who can change 
> what values, so it's easy to find.

  Well, in that case no programming language supports encapsulation, then.
If there's a bug in the runtime which trashes your program's memory, those
private members can be changed.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 19:20:38
Message: <4b8c59d6$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> When a bug in one part of the 
>> program can cause arbitrary changes elsewhere, that's really minor 
>> encapsulation.
> 
>   You are still confusing a runtime memory protection mechanism with the
> programming technique of data encapsulation.

We will have to agree to disagree.

>   "A bug in the program breaks encapsulation promises" doesn't imply there's
> no encapsulation. 

It implies you have very little encapsulation.

Seriously, what is the value of encapsulation if a bug can disrupt it? I've 
never found encapsulation in unsafe languages any more useful than a naming 
convention is in a safe language.

Note that's a serious question. I'm not mocking or trying to prove a point. 
I'm simply trying to figure out why you think "private:" is noticeably 
better than a naming convention or documentation.

I'm also seriously asking why you think encapsulation is at all valuable. 
What benefit does it give you in practice, that would be difficult to obtain 
just by having decent documentation?

>   You might argue that encapsulation without runtime memory protection of
> the modules is worthless (or at least less useful), but that's a different
> thing from claiming that there's *no* encapsulation.

There's very *little* encapsulation. There's attempted encapsulation. :-)

> Encapsulation is a programming technique, not a runtime mechanism. 

Right. But a programming technique that is designed to make debugging and 
understanding the interactions of parts of a program easier yet that doesn't 
  actually do so is a poor implementation, IMO.

What do you think is the benefit of encapsulation to the programming process?

>   You are still confusing runtime memory protection with the programming
> technique of encapsulation.

I'm just going by the definition you pointed me to. If you want a different 
definition, let's see it. Maybe I'll agree with it. :-)

>>> (In that way "data hiding" is perhaps slightly a misnomer, and something like
>>> "scope access restrictions" would be better.)
> 
>> Now who is changing definitions? :-)
> 
>   Suggesting a more descriptive term is not changing the definition.
> Data hiding has always been about access rights.

Yes, but we're not talking about data hiding. We're talking about 
encapsulation. I'll grant that C++ has a weak form of data hiding, yes.

>> And in Python, if a variable name starts with _ then that variable doesn't 
>> interest you. Does that mean Python has encapsulation like C++ does?
> 
>   Does the compiler give you an error if you try to access the private
> variable from the outside?

Probably not. Do you think that would make a difference?  That when you're 
typing in the name of the member variable, you don't notice it starts with 
an underscore?

>> In languages where the private variables aren't exposed at all, this 
>> question doesn't even come up.
> 
>   Where the private members are specified is inconsequential.

Well, I'll have to disagree again there. The fact that you can see them and 
access them

>> No. You're confusing data hiding with encapsulation. You've changed the 
>> subject from "encapsulation" to "data hiding", because data hiding is the 
>> *only* kind of encapsulation C++ supports.
> 
>   They are used as synonyms, and I'm not the only one to do so.

Well, I'm making a distinction. You can have one without the other. People 
make lots of mistakes by not knowing as wide a range of possibilities as 
they should, thinking that (for example) static typing means you have to 
specify the types of variables, or that object-oriented means you have 
instances of classes, or etc.

>> If you asked me if C++ supports data hiding, I would have said "Yes, poorly, 
>> because you can see what's there and have to deal with it, but the compiler 
>> enforces the private keyword."
> 
>   As I said, you could as well argue that if you look into the source code
> of a library and see the internal structure of the object, it breaks data
> hiding and thus the programming language has "poor support" for it.

Not really, because there are legal and valid ways of getting to those 
private variables in C++, guaranteed to work by the language. If the private 
variables really were private, you'd have no reason to specify how they're 
laid out in memory, since the only code that could get to them is the class 
itself.

>   Just because the programmer can see what the object is composed of doesn't
> mean there's no data hiding. Data hiding is about access rights.

OK. I disagree, but OK.

>> I told you why encapsulation is important, listing several reasons. Data 
>> hiding is just one of them.
> 
>   Your definition of encapsulation is mixed with a runtime mechanism of
> memory protection. I don't see that requirement anywhere.

There doesn't have to be any memory protection involved. Java doesn't have 
memory protection, but applets are encapsulated. Erlang doesn't have memory 
protection, but it has encapsulation.

>> You're the one that says C++ is superior to Java because of the runtime.
> 
>   I don't even understand what you mean by that, or where I have said such
> a thing. My complaints about Java are mainly based on its language features
> (such as the inability to abstract a basic type away).

You complain that everything in Java has to be allocated on the heap, for 
example. Which is purely a runtime mechanism that has little to do with the 
language. (As shown by the newer compilers that don't allocate all objects 
on the heap, yet compile standard Java, for example.)

>> Runtime encapsulation is what made Java popular in the first place. Indeed, 
>> runtime encapsulation was pretty much the entire point of Java's development.
> 
>   No, what made Java popular was garbage collection (and lacking some
> features which some people saw as "bad", such as multiple inheritance).
> Also the idea of "write once, run everywhere".

And nobody would have heard of it if it weren't for applets. Trust me on 
this one - I was there.

Anyway, Java stuff snipped, since it's irrelevant to the conversation.

>>  > It kind of implies that "your program should still run ok even if
>>> there are bugs in it".
> 
>> You know something? There's an awful lot of software that falls into this 
>> category. Know why? Because programmers aren't perfect.
> 
>   And this is related to the definition of encapsulation how?

Because if your definition only applies to bug-free software, claiming that 
your program supports it isn't a very useful property.

>   Some bugs are easier to find than others. This has what to do with the
> definition of "encapsulation"?

Because encapsulation is there to help you find bugs!  It's there to help 
you understand the program and to ensure that each piece works how it's 
supposed to in isolation. To ensure that testing one piece and seeing that 
it works means it works when combined with other pieces.

I.e., that a class is correct as implemented.

>   If there's a bug in the operating system which randomly trashes the
> memory of a program, does that mean the programming language which was
> used to create that program has no encapsulation?

No, because that's outside the scope of the programming language's definition.

C++ on the other hand guarantees that you can access stuff by indexing 
outside the defined boundaries of objects. Hence the assurance that things 
are laid out in the order you declare them, the ability to use write() to 
store data from classes into files, etc. ("Write()" still lets you write out 
a class, doesn't it?)

>   What if the runtime which executes the program has a bug which trashes
> its memory. Does that mean the programming language has no encapsulation?

No, but it means the encapsulation has failed. It also means that that 
particular interpreter does not correctly implement the encapsulation that 
the language defined.  You could say that if the JVM randomly scribbles over 
private values, then that JVM did not correctly implement Java.

But C++ defines mechanisms for violating encapsulation, both accidentally 
and on purpose.

>   If there's a faulty RAM chip in your computer which causes part of the
> program's memory to be trashed, does that mean there's no encapsulation in
> the programming language?

No, it means your computer did not correctly implement the programming language.

>   You are confusing runtime memory protection with the programming technique
> of encapsulation. They are different things.

I explained how they're not.

>> You're either trolling me, or you're a really crappy and/or inexperienced 
>> programmer if you don't understand the difference between the scale of 
>> looking for a bug in those two places.
> 
>   Some bugs are easier to find than others. This has what to do with the
> definition of "encapsulation"?

Because encapsulation is there to make finding bugs easier.

>> The whole *point* of encapsulation is 
>> not to serve as documentation as to what's public and what's private, but to 
>> limit the amount of code you have to look at to understand the behavior of a 
>> given variable.
> 
>   And you arbitrarily set the limit of what still constitutes as acceptable
> "encapsulation" and what doesn't somewhere between the program's own runtime
> and the OS.

Because the language's runtime is part of the definition of the language. 
The OS usually isn't. Sometimes it is, in which case yes, if the OS is 
specific to the language you're using, then a failure in the OS is a 
violation of encapsulation.

>> Yes! You can! That's exactly the point. You're encapsulating who can change 
>> what values, so it's easy to find.
> 
>   Well, in that case no programming language supports encapsulation, then.
> If there's a bug in the runtime which trashes your program's memory, those
> private members can be changed.

It's not a binary choice. Of course if your hardware fails and changes 
random values in your program, then there's a problem you need to track 
down. But if you can prove that no method of your class should be changing 
one of Java's private variables set in the constructor but it changes 
anyway, then you know the code that's broken isn't Java code. You know it's 
the OS, or JVM, or hardware, or something.

In contrast, C++ has well-defined mechanisms for bypassing the encapsulation 
that are used as a normal part of programming in that language.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 19:58:26
Message: <4b8c62b2@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Seriously, what is the value of encapsulation if a bug can disrupt it? I've 
> never found encapsulation in unsafe languages any more useful than a naming 
> convention is in a safe language.

  I have found it extremely useful. For example, my FunctionParser library
(which has got some popularity) is, basically, one class with a relatively
brief public interface.

  During its history the library has gone through two major rewrites (the
second one almost completely from scratch, reusing only some portions of
the old version), yet without breaking the public interface and hence the
programs using it. This means that programs which use the library can safely
upgrade to a newer version of the library and still work just fine.

  As you might imagine, the almost complete rewrites have also entailed an
almost complete redesign of the private parts of the class, which have
usually ended up being completely unlike the older versions. The public
interface has stayed almost completely intact (except for some additions
which don't break backwards compatibility), which means that existing
programs don't break.

  *That* is the beauty of encapsulation.

  Of course I'm sure you have some obscure name from some obscure source
for that as well, just for the sake of disagreement.

> I'm also seriously asking why you think encapsulation is at all valuable. 
> What benefit does it give you in practice, that would be difficult to obtain 
> just by having decent documentation?

  Do you really think that the compiler telling you "you must not access
this member, I refuse to compile it" is not a better way to enforce
encapsulation than just some documentation that nobody is going to read
anyways? Given that it's quite easy for compilers to do that, then why not?

  If you must have "visible" members in the class declaration for technical
and/or efficiency reasons, it's better if the compiler offers a tool for
telling the user "even though you can see this you really shouldn't be
referencing it directly" rather than relying solely on a comment line.

  It also makes it clearer in inheritance what the derived class can and
shouldn't access: The base class' private part is private, but it might
offer a 'protected' part for derived classes to use. The compiler then
explicitly checks that this abstraction level is obeyed. Easier and better
than just having some comments saying "this can be be used in derived
classes but should not be accessed from the outside".

  So yes, it's much better if the compiler tells the user he is accessing
what he shouldn't.

> >   You are still confusing runtime memory protection with the programming
> > technique of encapsulation.

> I'm just going by the definition you pointed me to. If you want a different 
> definition, let's see it. Maybe I'll agree with it. :-)

  But where do you draw the line between what is and isn't encapsulation?

  Clearly, a C struct offers no encapsulation. Also, clearly, a faulty RAM
chip trashing your program's memory doesn't mean there's no encapsulation
in the programming language. However, where is the line between those two
extremes, where encapsulation becomes non-encapsulation?

  If you call a library offered by the programming language, and that
library is buggy and trashes your program's memory, does that break
encapsulation? What if the library calls a system library (such as clib)
and *that* has a bug which trashes your program's memory? Does that break
encapsulation? What if instead of a language's own library it's a third-party
library? What if the kernel has a bug which trashes your program's memory?
Where exactly lies the line between a bug which trashes the program's memory
not breaking encapsulation, and doing so? And why?

> In contrast, C++ has well-defined mechanisms for bypassing the encapsulation 
> that are used as a normal part of programming in that language.

  Actually it doesn't. You can read the binary representation of an object
in memory, but the standard gives no guarantees as to which byte means what.
The memory layout of objects is completely implementation-defined and thus
while you can read the individual bytes of the memory location where the
object is, you cannot safely make any assumptions about their meaning. And
trying to *modify* the memory occupied by the object is outright undefined
behavior.

  The standard allows doing that even if it's undefined behavior simply
because imposing restrictions on that would mean that compilers would
have to generate significantly less efficient code even for programs
which don't even attempt doing that. Many people don't mind.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 1 Mar 2010 20:44:56
Message: <4b8c6d98$1@news.povray.org>
Warp wrote:
>   *That* is the beauty of encapsulation.

OK. So you agree that Python also has encapsulation?

Certainly that's one form of encapsulation, and indeed, it's a form also 
supported by DLLs, client-server architectures, operating systems, 
sandboxes, and many of the other things you said weren't encapsulation.

It's also very useful when designing the original program. But it's more the 
modularity part than the data-hiding part. In other words, if you go back to 
your original wikipedia link, it's the "language construct that facilitates 
the bundling of data with methods operating on that data", not the 
"mechanism for restricting access".

You can do that same sort of thing in any language, if you just make clear 
what's the public API and what isn't. C does this with FILE* structures, for 
example, as well as all the other standard libraries, without any real 
features for encapsulation beyond incomplete types. You can also do it in 
Smalltalk, which very clearly doesn't have anything beyond documentation to 
enforce its modularity.

>   Do you really think that the compiler telling you "you must not access
> this member, I refuse to compile it" is not a better way to enforce
> encapsulation than just some documentation that nobody is going to read
> anyways? 

Not if you're not going to enforce it the other ways as well.

>   If you must have "visible" members in the class declaration for technical
> and/or efficiency reasons, it's better if the compiler offers a tool for
> telling the user "even though you can see this you really shouldn't be
> referencing it directly" rather than relying solely on a comment line.

Sure. What about a naming convention that "comes with the language"? I.e., 
not just a naming convention you made up, but one in common for every single 
program written in the language from day one?

>   So yes, it's much better if the compiler tells the user he is accessing
> what he shouldn't.

I agree it's better. But since you seem to like binary definitions, and you 
don't like me saying "C++ has a little bit of encapsulation but not much", 
I'm curious if you think that (say) Python has encapsulation, given that 
everyone knows that names with _ are private to the class they're in?

>   But where do you draw the line between what is and isn't encapsulation?

I don't make it a binary choice. I say "C has no encapsulation, Python has 
very little (more modularity than encapsulation), C++ has a little more than 
Python (because the compiler refuses to compile stuff that violates the 
language rules), Java and C# have more (because you have to use reflection 
or unsafe code), Erlang has a whole bunch (because only bugs in unsafe code 
can violate it), and Sing# and Hermes have the most (because they have no 
unsafe subsets at all)."

For me, the amount of benefit that encapsulation gives relative to 
documentation is nowhere near the amount of benefit that encapsulation gives 
relative to guarantees about relationships between code and data enforced by 
the language.

>   Clearly, a C struct offers no encapsulation. Also, clearly, a faulty RAM
> chip trashing your program's memory doesn't mean there's no encapsulation
> in the programming language. However, where is the line between those two
> extremes, where encapsulation becomes non-encapsulation?

Myself, I'd say when you can accidentally violate encapsulation, you have 
poor encapsulation.

>   If you call a library offered by the programming language, and that
> library is buggy and trashes your program's memory, does that break
> encapsulation? What if the library calls a system library (such as clib)
> and *that* has a bug which trashes your program's memory? Does that break
> encapsulation? What if instead of a language's own library it's a third-party
> library? What if the kernel has a bug which trashes your program's memory?
> Where exactly lies the line between a bug which trashes the program's memory
> not breaking encapsulation, and doing so? And why?

Those are all breaks of encapsulation, certainly. But those are in the 
implementations, not the languages. A buggy RAM chip that randomly changes 
values on the stack has nothing to do with whether C++ the language offers 
valuable encapsulation.

>> In contrast, C++ has well-defined mechanisms for bypassing the encapsulation 
>> that are used as a normal part of programming in that language.
> 
>   Actually it doesn't. You can read the binary representation of an object
> in memory, but the standard gives no guarantees as to which byte means what.

I don't believe that's true. It certainly wasn't in C.

> The memory layout of objects is completely implementation-defined

Nonsense. Arrays are contiguous. The elements of a structure are all between 
&x and &x + sizeof(x).  There's lots of guarantees. And I'm pretty sure that 
if you have "struct x {int a; int b} y;" that the standard guarantees
&b > &a. Positive integers are represented in twos-complement coding. 
Unsigned integers occupy the the number of bits indicated by multiplying 
sizeof(unsigned) by bits_per_char or whatever the appropriate #define is.

Now, if you're going to argue those guarantees don't count, Ok. I'd say some 
do and some don't - I wouldn't argue that point very hard. But if they're 
there, it's because enough people writing code in the language rely on them 
being there that it was worth standardizing.

>   The standard allows doing that even if it's undefined behavior simply
> because imposing restrictions on that would mean that compilers would
> have to generate significantly less efficient code even for programs
> which don't even attempt doing that. Many people don't mind.

Can I get you to come work for my company? :-)

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Invisible
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 04:25:45
Message: <4b8cd999$1@news.povray.org>
>> Scientific applications are a vanishingly small market segment.
> 
> I'm not so sure about that.

How many people use computers to do scientific calculations?

How many people use computers to run their office suite?

Yeah, exactly.

>>> That's not "mainstream", that's "popular". :-)
>> There's a difference?
> 
> Yes.

Care to elaborate on what it is?

>> Why would a branch have only one child? 
> 
> Populate the tree one element at the time. At least half the time, 
> you'll have a node with only one child.

How do you figure that?

Tree with 1 element: L
Tree with 2 elements: L + L
Tree with 3 elements: L + (L + L)
Tree with 4 elements: (L + L) + (L + L)

Every node has either zero or two children.

> Perhaps the reason you don't see it a lot in Haskell is because the 
> definition of a node with only one leaf would be ugly. :-)

No, it would be as trivial as the usual binary tree.

>> I haven't seen an N-ary tree in an OO language. ;-) Only strictly 
>> *binary* trees.
> 
> Wow. You need to read more code.  You've never seen a hashtable full of 
> hashtables, or a parse tree?

I've never read the implementation of a hash table, for that matter.

Now a *parse tree* probably has a much more complicated structure than a 
simple binary tree. I can well imagine it containing nodes with all 
sorts of numbers of children. (And there might well be some complicated 
inheritance relationships between the node types.)

> Let's ask this: what good is encapsulation? Why do people think 
> encapsulation is a good thing?
> 
> Answer: because it limits where you have to look to understand the 
> behavior of code. If your language has encapsulation, you can look at 
> the class of that object to determine what accesses and modifies its 
> internal state. If the language has encapsulation but also an escape to 
> an unsafe language (like JNI or P/Invoke or so), then you have to look 
> at your class and all unsafe code that might change the contents of your 
> class.
> 
> Let's ask this: Why doesn't C++ have encapsulation?
> 
> Answer: Where in your code might lie a bug that is causing your class to 
> violate its invariants? If you have two variables in your instance, one 
> of which must always be two times the other, where do you have to look 
> if that is not the case? How much of your code base might cause that 
> change?

In just about any programming language imaginable, it would be possible 
to look at the raw assembly code generated to figure out where some data 
is physically stored in memory, and then link in some raw assembly to 
directly access that data. So by this argument, because it's technically 
possible to circumvent the language restrictions in just about any 
programming language, no programming language exists which supports 
encapsulation.

Which is, of course, nonesense. Real programs don't do this kind of 
thing. If your program doesn't attempt to circumvent encapsulation, then 
you get the benefits of encapsulation.

>>> What is this?
>>>
>>>    (lambda (x) (x + 1))
>>>
>>> It's a lambda expression.
>>>
>>>    y = (lambda (x) (x + 1))
>>>
>>> What is y?  It's a closure.
>>
>> I still don't get it. (But then, I don't even know what language that 
>> is...)
> 
> Do you understand the difference between classes and instances?

Yes. (But I'm not sure how this is related to functions...)

>> YOU wrote this paragraph, not me. ;-)
> 
> OK. My bad.  Altho that would certainly explain why I was confused. ;-)

Heh. Talking to yourself is the first sign of madness. ;-)

>> And this isn't the case for Haskell?
> 
> I don't know. Maybe it is, but you don't make it sound that way.

I can't even remember what this statement refers to now...

>> As I said, the likes of Galois and Well Typed make their money 
>> primarily designing systems where correctness is vital. What kind of 
>> systems do you suppose those are?
> 
> I couldn't guess. Galios has an empty web site and Well Typed are 
> consultants.
> 
> I'd honestly be quite surprised if either one builds systems in Haskell 
> where correctness is actually vital. As in, an error in the program 
> means people die.

No, I think it's more "failure would cost us craploads of money, very 
fast". I believe that's the kind of area they work in. (They also seem 
to do quite a bit of government work.)

>> (It's also conspicuous that both Galois and Well Typed employ people 
>> who are also GHC developers... so maybe that's your answer!)
> 
> Yep.

Of course, if you're the language implementers, you can implement 
whatever you want. ;-) (Within reason, of course...)

I wonder if any of the other Haskell compilers will be production-ready 
any time soon?


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 11:33:16
Message: <4b8d3dcc$1@news.povray.org>
Invisible wrote:
>>> Scientific applications are a vanishingly small market segment.
>>
>> I'm not so sure about that.
> 
> How many people use computers to do scientific calculations?
> How many people use computers to run their office suite?
> Yeah, exactly.

Sure. I wouldn't say "vanishingly small", given the number of offices that 
actually do science for a living.

> Care to elaborate on what it is?

Not especially. Let's just mention "COBOL".

> Every node has either zero or two children.

Fair dinkum. I was thinking of a different algorithm.

> I've never read the implementation of a hash table, for that matter.

You should. I expect things like skip lists and skip graphs and DHTs are the 
sorts of things you'd get into.

> In just about any programming language imaginable, it would be possible 
> to look at the raw assembly code generated to figure out where some data 
> is physically stored in memory, and then link in some raw assembly to 
> directly access that data. 

So, because you can link assembly code to your Haskell, that means Haskell 
isn't functional? Because assembly code isn't functional?

> Which is, of course, nonesense. Real programs don't do this kind of 
> thing. If your program doesn't attempt to circumvent encapsulation, then 
> you get the benefits of encapsulation.

Uh, no. That's my point. You might get some of the benefits of encapsulation 
(like correct code being easier to understand), but if neither the compiler 
nor the runtime enforce encapsulation and you have no way of detecting 
errors that violate encapsulation, then your language is doing a poor job of 
encapsulation, at least for many of the benefits of encapsulation.

>>>> What is this?
>>>>
>>>>    (lambda (x) (x + 1))
>>>>
>>>> It's a lambda expression.
>>>>
>>>>    y = (lambda (x) (x + 1))
>>>>
>>>> What is y?  It's a closure.
>>>
>>> I still don't get it. (But then, I don't even know what language that 
>>> is...)
>>
>> Do you understand the difference between classes and instances?
> 
> Yes. (But I'm not sure how this is related to functions...)

"Lambda" is the class. "Closure" is the instance.

Lambda is an expression. A closure is the value of the expression.

What is this:
    \ x . x + y

It's a lambda expression. It's not a value, because you don't know what "y" 
is. It means different things depending on where it is in the program and 
when you evaluate it. Every time you evaluate it, you'll get something 
different back, depending on "y", yes?

That thing you get back, that encapsulates the value of "y"? That's a closure.

> Heh. Talking to yourself is the first sign of madness. ;-)

Oh, you can't help that. We're all mad here.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 12:14:26
Message: <4b8d4771@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> >   *That* is the beauty of encapsulation.

> OK. So you agree that Python also has encapsulation?

  Does the compiler enforce it?
> >   Do you really think that the compiler telling you "you must not access
> > this member, I refuse to compile it" is not a better way to enforce
> > encapsulation than just some documentation that nobody is going to read
> > anyways? 

> Not if you're not going to enforce it the other ways as well.

  Right. So if it's possible to modify the data by having some wild pointer
bug in the program, then it's not worth having compiler checks at all and
just have everything public.

  As you said, we'll just have to agree to disagree.

> >   If you must have "visible" members in the class declaration for technical
> > and/or efficiency reasons, it's better if the compiler offers a tool for
> > telling the user "even though you can see this you really shouldn't be
> > referencing it directly" rather than relying solely on a comment line.

> Sure. What about a naming convention that "comes with the language"? I.e., 
> not just a naming convention you made up, but one in common for every single 
> program written in the language from day one?

  By that reasoning you could argue that C is an object-oriented language.
After all, you can get OO'ish behavior if you follow certain coding
conventions.

> >   So yes, it's much better if the compiler tells the user he is accessing
> > what he shouldn't.

> I agree it's better. But since you seem to like binary definitions, and you 
> don't like me saying "C++ has a little bit of encapsulation but not much", 
> I'm curious if you think that (say) Python has encapsulation, given that 
> everyone knows that names with _ are private to the class they're in?

  How is it encapsulation if everything is public?

> >   But where do you draw the line between what is and isn't encapsulation?

> I don't make it a binary choice. I say "C has no encapsulation, Python has 
> very little (more modularity than encapsulation), C++ has a little more than 
> Python (because the compiler refuses to compile stuff that violates the 
> language rules)

  That actually contradicts what you said originally. Originally you said
that C++ has *no* encapsulation. Clearly you were drawing a clear line
somewhere.

> >> In contrast, C++ has well-defined mechanisms for bypassing the encapsulation 
> >> that are used as a normal part of programming in that language.
> > 
> >   Actually it doesn't. You can read the binary representation of an object
> > in memory, but the standard gives no guarantees as to which byte means what.

> I don't believe that's true. It certainly wasn't in C.

  What wasn't in C?

  As for the C++ standard, it does certainly not give any guarantees about
the bit representation of objects in memory. It's implementation-defined.
You can read the bits, but there's no guarantee about their meaning (nor
that the meaning won't change from class to class).

> > The memory layout of objects is completely implementation-defined

> Nonsense. Arrays are contiguous.

  Arrays are not objects. Arrays are a collection of objects.

> The elements of a structure are all between 
> &x and &x + sizeof(x).

  No, they aren't. The standard doesn't specify how much padding there may
be between struct elements. The compiler can add as much padding as it wants
(or none at all).

  Also, the standard doesn't guarantee that the first member of a struct
starts from the same memory address as the struct instance itself. (In
practice this can actually differ if the struct has virtual functions.
In that case the pointer-to-struct-instance will not point to the first
member of the struct. There will be some implementation-dependent vtable
pointer there instead.)

  If a member is private, you have no safe, portable way of accessing it
from the outside.

>  There's lots of guarantees. And I'm pretty sure that 
> if you have "struct x {int a; int b} y;" that the standard guarantees
> &b > &a.

  Probably, but if a and b are private, you have no portable way of getting
an address to them. You don't know the offset.

> Positive integers are represented in twos-complement coding. 

  I'm not completely sure the standard guarantees that.

> Unsigned integers occupy the the number of bits indicated by multiplying 
> sizeof(unsigned) by bits_per_char or whatever the appropriate #define is.

  Well, duh, because sizeof(type) is *defined* as telling the amount of
bytes that the type requires.

  That still doesn't help you accessing private data of an object portably.

> Now, if you're going to argue those guarantees don't count, Ok.

  Don't count to what? The issue was whether it's *well-defined* to break
encapsulation in C++. Your own words.

  It's not well-defined. You can try, but the results are not guaranteed.

> >   The standard allows doing that even if it's undefined behavior simply
> > because imposing restrictions on that would mean that compilers would
> > have to generate significantly less efficient code even for programs
> > which don't even attempt doing that. Many people don't mind.

> Can I get you to come work for my company? :-)

  You mean C++ is causing problems there?

  I suppose I should consider myself lucky in that I get to work in
projects where I don't have to go fixing existing code made by incompetent
C++ programmers...

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 12:55:34
Message: <4b8d5116$1@news.povray.org>
Warp wrote:
>   Right. So if it's possible to modify the data by having some wild pointer
> bug in the program, then it's not worth having compiler checks at all and
> just have everything public.

Well, sure, it's a little bit better. Not a whole lot better. As I said, 
it's a sliding scale.

For me, not sufficiently important that it's worth worrying about. The fact 
that I can use a pointer to step through the data of a class with private 
members is no better than the fact that I can iterate over the members of a 
Python instance by groping thru the hash table that implements it.

>> Sure. What about a naming convention that "comes with the language"? I.e., 
>> not just a naming convention you made up, but one in common for every single 
>> program written in the language from day one?
> 
>   By that reasoning you could argue that C is an object-oriented language.
> After all, you can get OO'ish behavior if you follow certain coding
> conventions.

Well, that's what I'm asking. I find it odd that "encapsulation" covers 
exactly everything that C++ does, no more no less.

>>>   So yes, it's much better if the compiler tells the user he is accessing
>>> what he shouldn't.
> 
>> I agree it's better. But since you seem to like binary definitions, and you 
>> don't like me saying "C++ has a little bit of encapsulation but not much", 
>> I'm curious if you think that (say) Python has encapsulation, given that 
>> everyone knows that names with _ are private to the class they're in?
> 
>   How is it encapsulation if everything is public?

"""
A language construct that facilitates the bundling of data with the methods 
operating on that data.
"""

Again, it's the definition you pointed to.

>   That actually contradicts what you said originally. Originally you said
> that C++ has *no* encapsulation. Clearly you were drawing a clear line
> somewhere.

It has so little encapsulation (in the "restricting access" part of the 
definition) that it might as well not have encapsulation. If you're talking 
about the data hiding part, I'd say that unsafe languages don't have that. 
Many have the other "bundling data and code" kind of encapsulation.

In other words, I find that marking something as "private" doesn't give 
significantly more protection to private data than marking something with an 
underline does in Python, in practice. In practice, both are trivial to get 
around, accidentally or on purpose.

>>>> In contrast, C++ has well-defined mechanisms for bypassing the encapsulation 
>>>> that are used as a normal part of programming in that language.
>>>   Actually it doesn't. You can read the binary representation of an object
>>> in memory, but the standard gives no guarantees as to which byte means what.
> 
>> I don't believe that's true. It certainly wasn't in C.
> 
>   What wasn't in C?

That the standard said there's no guarantees as to which bytes mean what or 
how things are laid out in memory.

>   As for the C++ standard, it does certainly not give any guarantees about
> the bit representation of objects in memory. It's implementation-defined.

Funky. I was pretty sure at least unsigned had a guarantee that they were 
represented in two's complement binary (e.g., that the bit pattern 0x11 was 
the integer value 3).

>>> The memory layout of objects is completely implementation-defined
>> Nonsense. Arrays are contiguous.
>   Arrays are not objects. Arrays are a collection of objects.

Yeah, I figured that would be your answer.

>> The elements of a structure are all between 
>> &x and &x + sizeof(x).
> 
>   No, they aren't. The standard doesn't specify how much padding there may
> be between struct elements. The compiler can add as much padding as it wants
> (or none at all).

That doesn't invalidate the formula. I didn't say there was nothing else there.

>   Also, the standard doesn't guarantee that the first member of a struct
> starts from the same memory address as the struct instance itself. (In
> practice this can actually differ if the struct has virtual functions.
> In that case the pointer-to-struct-instance will not point to the first
> member of the struct. There will be some implementation-dependent vtable
> pointer there instead.)

I didn't say that it did.

Wasn't one idea of C++ was that structs without vtables would layout the 
same way as C structures?

>   If a member is private, you have no safe, portable way of accessing it
> from the outside.

Right. But I'm not talking about the safe parts. :-)

In any case, yes, there is, because the class can return a pointer to the 
private variable. You might say "that means it's not private any more", but 
it's still accessing the program's private variables. (Uh, that *is* legal 
in C++, right? Returning an int* that points to a private instance variable? 
If not, then color me suitably impressed. :-)

>>  There's lots of guarantees. And I'm pretty sure that 
>> if you have "struct x {int a; int b} y;" that the standard guarantees
>> &b > &a.
> 
>   Probably, but if a and b are private, you have no portable way of getting
> an address to them. You don't know the offset.

You do inside the class. :-)  And besides, I'm just disputing your assertion 
that the language makes no guarantees about the layout of data.

I've worked with languages that make *no* guarantees about the layout. Where 
the lowest bit of *every* integer was 1, regardless of its value. Where 
pointers have reference counts and type flags in the high bits. Stuff like that.

Trust me, C++ makes some guarantees. :-)

>> Positive integers are represented in twos-complement coding. 
>   I'm not completely sure the standard guarantees that.
> 
>> Unsigned integers occupy the the number of bits indicated by multiplying 
>> sizeof(unsigned) by bits_per_char or whatever the appropriate #define is.
> 
>   Well, duh, because sizeof(type) is *defined* as telling the amount of
> bytes that the type requires.

Yes. And then you multiple by bits_per_char to find out the range, which 
means unsigneds don't have padding in the middle, for example.

>   That still doesn't help you accessing private data of an object portably.

That's not what I'm discussing at this point. Besides, you can *access* them 
portably. You can't *interpret* them portably, perhaps, if the structure is 
complicated enough.

>> Now, if you're going to argue those guarantees don't count, Ok.
> 
>   Don't count to what? The issue was whether it's *well-defined* to break
> encapsulation in C++. Your own words.

Don't count the guarantees I listed. You said "C++ makes no guarantess on 
layout" and I listed a bunch it makes.

And breaking encapsulation is portably accessing private members from 
outside the class. Which I can certainly do using memcpy for example.

>   It's not well-defined. You can try, but the results are not guaranteed.

So write(out, &xyz, sizeof(xyz)) where xyz is a class or struct with private 
members might actually crash?

memcpy(myarray[0], myarray[1], sizeof(myarray[0])) isn't well-defined? 
There's no meaning for that statement when myarray[0] is a struct with 
private members?

Regardless of whether you know which private variable is laid out where, 
you're still "accessing private variables" in ways that more secure 
languages disallow.

>   You mean C++ is causing problems there?

Oh, you wouldn't believe. Not C++ per se, but unsafe languages in general, 
poorly organized and badly documented.

>   I suppose I should consider myself lucky in that I get to work in
> projects where I don't have to go fixing existing code made by incompetent
> C++ programmers...

Yep. Well, honestly, the whole development chain is screwed. Not only is the 
code buggy, but the authors are unwilling to fix it, describe it, document 
it, or really take any responsibility at all for its sorry shape.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 14:36:31
Message: <4b8d68be@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> >   How is it encapsulation if everything is public?

> """
> A language construct that facilitates the bundling of data with the methods 
> operating on that data.
> """

> Again, it's the definition you pointed to.

  Only if you ignore the data hiding part, which is essential for
abstraction.

> >   That actually contradicts what you said originally. Originally you said
> > that C++ has *no* encapsulation. Clearly you were drawing a clear line
> > somewhere.

> It has so little encapsulation (in the "restricting access" part of the 
> definition) that it might as well not have encapsulation.

  You are exaggerating. On purpose.

> In other words, I find that marking something as "private" doesn't give 
> significantly more protection to private data than marking something with an 
> underline does in Python, in practice. In practice, both are trivial to get 
> around, accidentally or on purpose.

  You are exaggerating. On purpose.

  You can't bypass the access rights in C++ if you want to write a program
which is correct and portable. If you try to bypass it, your program will
not be portable, will be against the standard, and thus incorrect. C++ allows
you to write incorrect programs which will compile and run (on one given
system) for efficiency reasons, but that doesn't make the program correct.

  That's a *completely* different thing than having everything public and
only using a naming convention to denote "private" members. If the member
is public, then there will be a 100% correct way of accessing it from the
outside which is not implementation-defined.

  There's a huge difference. You make it sound like there was no difference
at all. Just for the sake of argument.

> >> I don't believe that's true. It certainly wasn't in C.
> > 
> >   What wasn't in C?

> That the standard said there's no guarantees as to which bytes mean what or 
> how things are laid out in memory.

  I don't think the C standard guarantees how much padding there will be
between members of a struct either. The bit representation of a struct
instance may change from system to system, and even from compiler to
compiler, and hence you must not make assumptions if you want your program
to be portable.

  (Moreover, C doesn't guarantee endianess either.)

> >   As for the C++ standard, it does certainly not give any guarantees about
> > the bit representation of objects in memory. It's implementation-defined.

> Funky. I was pretty sure at least unsigned had a guarantee that they were 
> represented in two's complement binary (e.g., that the bit pattern 0x11 was 
> the integer value 3).

  An unsigned is not an object, mind you. (We are talking, after all, about
accessing the private members of an object. An unsigned integer has no such
thing.)

  Also the standard doesn't guarantee word endianess, so the bit pattern
may actually differ from one system to the next even when we are talking
about unsigned integers.

> >> The elements of a structure are all between 
> >> &x and &x + sizeof(x).
> > 
> >   No, they aren't. The standard doesn't specify how much padding there may
> > be between struct elements. The compiler can add as much padding as it wants
> > (or none at all).

> That doesn't invalidate the formula. I didn't say there was nothing else there.

  So you said that "a given type, all by itself, will require sizeof(type)
bytes of memory". Well, duh, that's how sizeof() is defined, after all.

  However, the subject in question here is objects and accessing their
private members. You have no portable way of knowing the offset of members
inside objects because their layout is not guaranteed. The first member
might not start from the same address as the object's pointer, and the
amount of padding between members is implementation-defined. Just because
you know that a member requires n bytes of memory doesn't help you much
in resolving its location inside the object.

  (That doesn't mean you can't have a pointer pointing to a member of an
object, in a portable way. However, that requires either for the member
to be public or for the object to give you the pointer.)

> >   Also, the standard doesn't guarantee that the first member of a struct
> > starts from the same memory address as the struct instance itself. (In
> > practice this can actually differ if the struct has virtual functions.
> > In that case the pointer-to-struct-instance will not point to the first
> > member of the struct. There will be some implementation-dependent vtable
> > pointer there instead.)

> I didn't say that it did.

  But we are talking about accessing private members here.

> Wasn't one idea of C++ was that structs without vtables would layout the 
> same way as C structures?

  But the C standard doesn't give guarantees about padding between members
either, AFAIK.

> In any case, yes, there is, because the class can return a pointer to the 
> private variable. You might say "that means it's not private any more", but 
> it's still accessing the program's private variables.

  Of course if the object exposes the private member in its public interface,
then the outside can have access to it. Returning a reference or pointer to
a member exposes it to the outside.

  (There's still the minor difference that it's slightly more abstract than
having the member in the public part of the class in that you could change
the method to return a reference/pointer to something else.)

> (Uh, that *is* legal 
> in C++, right? Returning an int* that points to a private instance variable? 
> If not, then color me suitably impressed. :-)

  It is possible to return a pointer to a member variable. (It's generally
regarded as bad design, though. Returning a const reference is a borderline
case, though. Sometimes used because returning big members by value can be
quite inefficient.)

> >>  There's lots of guarantees. And I'm pretty sure that 
> >> if you have "struct x {int a; int b} y;" that the standard guarantees
> >> &b > &a.
> > 
> >   Probably, but if a and b are private, you have no portable way of getting
> > an address to them. You don't know the offset.

> You do inside the class. :-)

  "From the outside" implied that the struct doesn't want to expose them.

>  And besides, I'm just disputing your assertion 
> that the language makes no guarantees about the layout of data.

  Well, it doesn't. Not any that would help you accessing them from the
outside if they are unexposed.

  (Btw, I didn't mean "doesn't give any guarantees whatsoever". I meant
"doesn't give any guarantees that would help you resolving the address of
a private member from the outside in a portable way".)

> >> Positive integers are represented in twos-complement coding. 
> >   I'm not completely sure the standard guarantees that.
> > 
> >> Unsigned integers occupy the the number of bits indicated by multiplying 
> >> sizeof(unsigned) by bits_per_char or whatever the appropriate #define is.
> > 
> >   Well, duh, because sizeof(type) is *defined* as telling the amount of
> > bytes that the type requires.

> Yes. And then you multiple by bits_per_char to find out the range, which 
> means unsigneds don't have padding in the middle, for example.

  But if you had, for example, an array of two unsigneds, I'm not sure the
standard guarantees they will be at consecutive memory locations, with no
padding between them. (I could be wrong on this one, though.)

  Not that any compiler in the universe would put padding between them,
mind you, but it's probable that the standard doesn't *force* compilers
to not to use padding (eg. because an exotic hardware requires it).

> >   That still doesn't help you accessing private data of an object portably.

> That's not what I'm discussing at this point. Besides, you can *access* them 
> portably. You can't *interpret* them portably, perhaps, if the structure is 
> complicated enough.

  Well, if "access" means "reading their bit pattern in memory", then yes,
you can "access" them portably.

> >> Now, if you're going to argue those guarantees don't count, Ok.
> > 
> >   Don't count to what? The issue was whether it's *well-defined* to break
> > encapsulation in C++. Your own words.

> Don't count the guarantees I listed. You said "C++ makes no guarantess on 
> layout" and I listed a bunch it makes.

  The context in which I said it implied "no guarantees which would help you
resolve the address of a member variable from the outside".

> And breaking encapsulation is portably accessing private members from 
> outside the class. Which I can certainly do using memcpy for example.

  What you are using with memcpy is copying the bit pattern of the object
as it is stored in memory. It's arguable whether that's "accessing the
private members" of an object.

  To me, accessing means that you actually read (or even write) a private
member variable and write code which depends on it. You can't do that with
memcpy because it doesn't tell you the value of *that* member variable in
question. It only gives you the byte data of the entire object. You can't
start interpreting that byte data in a portable way.

> >   It's not well-defined. You can try, but the results are not guaranteed.

> So write(out, &xyz, sizeof(xyz)) where xyz is a class or struct with private 
> members might actually crash?

  Exactly which member variable did you just access? "All of them" isn't an
answer.

  What you did was take the bit pattern of the object in memory and write
it somewhere.

> memcpy(myarray[0], myarray[1], sizeof(myarray[0])) isn't well-defined? 

  Only in a very limited set of cases, actually. If myarray consists of
class instances, the result is undefined behavior (because you are
bypassing copy constructors).

  With basic types it's well-defined, and maybe for PODs with trivial
constructors and destructors.

> There's no meaning for that statement when myarray[0] is a struct with 
> private members?

  It depends on what kind of constructor/destructor/copy constructor the
struct has.

> Regardless of whether you know which private variable is laid out where, 
> you're still "accessing private variables" in ways that more secure 
> languages disallow.

  Well, C++ doesn't stop you from reading the bytes of an object in memory.
Nobody is denying that.

  Whether that constitutes "accessing a private member" is arguable.

> >   You mean C++ is causing problems there?

> Oh, you wouldn't believe. Not C++ per se, but unsafe languages in general, 
> poorly organized and badly documented.

  Safe languages never cause problems, then?

  (If I'm to believe thedailywtf.com, I don't think that's the case either.)

> >   I suppose I should consider myself lucky in that I get to work in
> > projects where I don't have to go fixing existing code made by incompetent
> > C++ programmers...

> Yep. Well, honestly, the whole development chain is screwed. Not only is the 
> code buggy, but the authors are unwilling to fix it, describe it, document 
> it, or really take any responsibility at all for its sorry shape.

  I'm not sure using a "safe" language would fix those problems either.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 17:23:18
Message: <4b8d8fd6$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>>>   How is it encapsulation if everything is public?
> 
>> """
>> A language construct that facilitates the bundling of data with the methods 
>> operating on that data.
>> """
> 
>> Again, it's the definition you pointed to.
> 
>   Only if you ignore the data hiding part, which is essential for
> abstraction.

Really, Python isn't noticeably less private than C++. C++ shows you 
everything. The compiler says "don't reference that by name", instead of the 
IDE doing it.

>> It has so little encapsulation (in the "restricting access" part of the 
>> definition) that it might as well not have encapsulation.
> 
>   You are exaggerating. On purpose.

I disagree. I'm simply expressing an opinion that differs from yours. If I 
know something is private, I don't try to access it.

>> In other words, I find that marking something as "private" doesn't give 
>> significantly more protection to private data than marking something with an 
>> underline does in Python, in practice. In practice, both are trivial to get 
>> around, accidentally or on purpose.
> 
>   You are exaggerating. On purpose.

Again, I disagree. I disagree that C++ refusing to compile when you access 
by name something marked as private is significantly more encapsulated than 
Python's naming convention that you shouldn't access fields marked with an 
underscore by name.

>   There's a huge difference. You make it sound like there was no difference
> at all. Just for the sake of argument.

In practice, I disagree. I recognize the difference. I'm just of the opinion 
that the difference is so small as to be irrelevant. You think that having 
the compiler complain is important. I don't.

Do you think Java doesn't have private members because there's a way of 
accessing them via reflection?

>   I don't think the C standard guarantees how much padding there will be
> between members of a struct either. The bit representation of a struct
> instance may change from system to system, and even from compiler to
> compiler, and hence you must not make assumptions if you want your program
> to be portable.

Right. But that's far from "makes no guarantees" you see. You often make a 
broad sweeping statement, and when I call you on it, you backpedal and then 
say I'm exaggerating "on purpose".

>>>> The elements of a structure are all between 
>>>> &x and &x + sizeof(x).
>>>   No, they aren't. The standard doesn't specify how much padding there may
>>> be between struct elements. The compiler can add as much padding as it wants
>>> (or none at all).
> 
>> That doesn't invalidate the formula. I didn't say there was nothing else there.
> 
>   So you said that "a given type, all by itself, will require sizeof(type)
> bytes of memory". Well, duh, that's how sizeof() is defined, after all.

You said "there are no guarantees". I said yes, there is at least the 
guarantee that the elements of a class even with private members are laid 
out in the space between the start and the end.

What you mean to say is "there aren't enough guarantees on layout to ensure 
that you can do something reasonable in a portable way by breaking 
encapsulation."  That's far different from "there are no guarantees."

You exaggerate, on purpose. ;-)

>   (That doesn't mean you can't have a pointer pointing to a member of an
> object, in a portable way. However, that requires either for the member
> to be public or for the object to give you the pointer.)

Yep. Fully agreed.

>> I didn't say that it did.
>   But we are talking about accessing private members here.

I can still do that. I can't do something *useful* with them, but *in 
practice*, I can zero out your object by mistake. You may discount that as 
irrelevant, but I don't. I don't think that any amount of definition 
wrangling is going to resolve *that* disagreement.

Unsafe languages don't keep code from accessing your private variables. They 
keep you from reliably portably accessing private variables in a correctly 
written system where every piece is bug free.  You're taking the latter to 
mean the former. I'm not.

>>  And besides, I'm just disputing your assertion 
>> that the language makes no guarantees about the layout of data.
> 
>   Well, it doesn't. Not any that would help you accessing them from the
> outside if they are unexposed.

See what you did there?

>   (Btw, I didn't mean "doesn't give any guarantees whatsoever". I meant
> "doesn't give any guarantees that would help you resolving the address of
> a private member from the outside in a portable way".)

OK. I'll agree with that. :-)

>   But if you had, for example, an array of two unsigneds, I'm not sure the
> standard guarantees they will be at consecutive memory locations, with no
> padding between them. (I could be wrong on this one, though.)

I'm not sure.

>   Not that any compiler in the universe would put padding between them,
> mind you, but it's probable that the standard doesn't *force* compilers
> to not to use padding (eg. because an exotic hardware requires it).

Certainly there are machines where floats, for example, need to be on 
particular boundaries and which might not be packed to those boundaries. 
Stupid design, but conceivable. :-)  Of course, on such machines, you 
usually can't get a C compiler.

>   Well, if "access" means "reading their bit pattern in memory", then yes,
> you can "access" them portably.

And, as I said, you can overwrite them, which *I* feel violates 
data-security encapsulation.

>   To me, accessing means that you actually read (or even write) a private
> member variable and write code which depends on it. 

OK. And to me, "accessing" means reading it or writing it without going thru 
the interface defined by the class.

>> memcpy(myarray[0], myarray[1], sizeof(myarray[0])) isn't well-defined? 
> 
>   Only in a very limited set of cases, actually. If myarray consists of
> class instances, the result is undefined behavior (because you are
> bypassing copy constructors).

Fair dinkum.

>   With basic types it's well-defined, and maybe for PODs with trivial
> constructors and destructors.

And for structs or classes with no vtable, as those are defined to have the 
same layout as in C, and it works in C, yes?

>   Well, C++ doesn't stop you from reading the bytes of an object in memory.
> Nobody is denying that.
> 
>   Whether that constitutes "accessing a private member" is arguable.

Right. I think it is, because if you can change the values of my private 
variables without going thru my member functions, I have no way of enforcing 
my code's correctness or my invariants.

>>>   You mean C++ is causing problems there?
> 
>> Oh, you wouldn't believe. Not C++ per se, but unsafe languages in general, 
>> poorly organized and badly documented.
> 
>   Safe languages never cause problems, then?

Safe languages merely make it easier to prove who has the bug. It also tends 
to catch the bug when (say) you open the file, instead of 10 minutes later 
when you try to parse what you read from it.

>> Yep. Well, honestly, the whole development chain is screwed. Not only is the 
>> code buggy, but the authors are unwilling to fix it, describe it, document 
>> it, or really take any responsibility at all for its sorry shape.
> 
>   I'm not sure using a "safe" language would fix those problems either.

Not completely, no. But at least you could assign blame. Right now, if I 
write code that invokes their libbrary and it segfaults, yet it doesn't 
segfault in their trivial test application that only ever invokes one 
operation in the library before exiting, then it must be my fault.

In a safe language, you could say "Look, it throws an exception in your code 
. Find where that exception is, and see what causes it."


-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 17:29:34
Message: <4b8d914e$1@news.povray.org>
Darren New wrote:
> Really, Python isn't noticeably less private than C++. C++ shows you 
> everything. The compiler says "don't reference that by name", instead of 
> the IDE doing it.

Or, to phrase it a different way, I've never had a problem caused by someone 
intentionally accessing a private member in a Python-like environment that 
would have been solved by making the code not compile when that happens.

I have never known someone to accidentally reference x._y and think they 
were supposed to be accessing _y as a normal part of the functioning of the 
program.

Have you ever known someone to accidentally name a C function __XYZ__ and 
not be aware they might be stepping on the compiler's namespace? Is it 
really a problem that the compiler doesn't prevent you from using names like 
that?

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 18:15:54
Message: <4b8d9c2a$1@news.povray.org>
Warp wrote:
>   (That doesn't mean you can't have a pointer pointing to a member of an
> object, in a portable way. However, that requires either for the member
> to be public or for the object to give you the pointer.)

Actually, just thinking about it a bit more, if the class returns a pointer 
to a member variable, and you can look in the header and see that pointer is 
in an array, then you have a well-defined way of accessing the other private 
data in the same array.

Like, if the class returns a FILE* from open(), and you look in the class 
declaration and there's something along the lines of
    FILE open_files[MAX_OPEN_FILES];
then chances are you can wander up and down that array as you like. :-)

I wouldn't say that particularly breaks encapsulation tho. Just a thought.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 18:32:56
Message: <4b8da028@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Again, I disagree. I disagree that C++ refusing to compile when you access 
> by name something marked as private is significantly more encapsulated than 
> Python's naming convention that you shouldn't access fields marked with an 
> underscore by name.

  Then show me correct C++ which reads a certain private member variable of
a class, eg. by reading its value to a variable of the same type, without
the class deliberately exposing it. (Note that the class may have eg. virtual
functions.)

> >   There's a huge difference. You make it sound like there was no difference
> > at all. Just for the sake of argument.

> In practice, I disagree.

  In practice? Well, then show me in practice. Some actual code, please.

> >   I don't think the C standard guarantees how much padding there will be
> > between members of a struct either. The bit representation of a struct
> > instance may change from system to system, and even from compiler to
> > compiler, and hence you must not make assumptions if you want your program
> > to be portable.

> Right. But that's far from "makes no guarantees" you see. You often make a 
> broad sweeping statement, and when I call you on it, you backpedal and then 
> say I'm exaggerating "on purpose".

  You are calling me a liar now? Like, when I said "the standard offers no
guarantees about the layout of a class" I was really meaning "the standard
offers no guarantees whatsoever about the memory layout of any type" rather
than "the standard offers you no guarantees about the layout which would
allow you to access the private members of a class", but later I claimed
otherwise? I was always talking about accessing private members and why
you can't access them in a portable way. Calling me a liar is offensive.

  I might well claim that you "backpedaled" when you first claimed that
C++ has *no* encapsulation, and later changed it to "little" encapsulation.

> >   So you said that "a given type, all by itself, will require sizeof(type)
> > bytes of memory". Well, duh, that's how sizeof() is defined, after all.

> You said "there are no guarantees".

  I was talking about the memory layout of an object. There *are* no
guarantees about it.

> I said yes, there is at least the 
> guarantee that the elements of a class even with private members are laid 
> out in the space between the start and the end.

  You seriously don't understand the difference between "size" and "layout"?

> >   Well, if "access" means "reading their bit pattern in memory", then yes,
> > you can "access" them portably.

> And, as I said, you can overwrite them, which *I* feel violates 
> data-security encapsulation.

  Then, as said in earlier posts, there are no languages with encapsulation
because you can't physically protect data from being corrupted. There can
always be a glitch somewhere that corrupts it.

  (By your principle of "if it can't be protected from everything it's not
worth protecting at all", all these "safe" languages are useless. They can't
protect themselves from everything, hence why try it at all? Better, to just
go back to C, then. No useless protection there.)

> >   With basic types it's well-defined, and maybe for PODs with trivial
> > constructors and destructors.

> And for structs or classes with no vtable, as those are defined to have the 
> same layout as in C, and it works in C, yes?

  No, if they or any of the members have a non-trivial copy constructor.

> Not completely, no. But at least you could assign blame. Right now, if I 
> write code that invokes their libbrary and it segfaults, yet it doesn't 
> segfault in their trivial test application that only ever invokes one 
> operation in the library before exiting, then it must be my fault.

  You don't use a debugger to resolve where the crash happens?

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 18:37:32
Message: <4b8da13c@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Darren New wrote:
> > Really, Python isn't noticeably less private than C++. C++ shows you 
> > everything. The compiler says "don't reference that by name", instead of 
> > the IDE doing it.

> Or, to phrase it a different way, I've never had a problem caused by someone 
> intentionally accessing a private member in a Python-like environment that 
> would have been solved by making the code not compile when that happens.

  And I have never had a problem with a wild pointer modifying the private
members of a class either.

  Is your anecdotal evidence better than mine?

> Have you ever known someone to accidentally name a C function __XYZ__ and 
> not be aware they might be stepping on the compiler's namespace?

  Actually it's quite common in C and C++ out there to have names starting
with underscore even though the standard says it shouldn't be done.

> Is it 
> really a problem that the compiler doesn't prevent you from using names like 
> that?

  Well, it is quite a common objection in C++ groups when people post code.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 18:39:35
Message: <4b8da1b7@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> >   (That doesn't mean you can't have a pointer pointing to a member of an
> > object, in a portable way. However, that requires either for the member
> > to be public or for the object to give you the pointer.)

> Actually, just thinking about it a bit more, if the class returns a pointer 
> to a member variable, and you can look in the header and see that pointer is 
> in an array, then you have a well-defined way of accessing the other private 
> data in the same array.

  Only if you also know which array element it's pointing to. (If you know,
then the class is, effectively, exposing the entire array.)

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 19:05:41
Message: <4b8da7d5@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Again, I disagree. I disagree that C++ refusing to compile when you access 
>> by name something marked as private is significantly more encapsulated than 
>> Python's naming convention that you shouldn't access fields marked with an 
>> underscore by name.
> 
>   Then show me correct C++ which reads a certain private member variable of
> a class, eg. by reading its value to a variable of the same type, without
> the class deliberately exposing it. (Note that the class may have eg. virtual
> functions.)

Do you understand what I mean by "significant"?

Look, you understand what I'm saying, I'm sure. I understand where you're 
coming from. Now you're just trying to tell me my opinion of what's 
important is incorrect, simply because you don't find it to be important.

*I* just don't see any practical advantage of having the compiler enforce 
that. You gave an example from your function library. I contend that the 
same thing could have been done with Python's level of encapsulation. It 
would be no more of an error for me (say) to change how variables that start 
with "_" work than it would be for you to change the size of the class you 
wrote.

*Correct* Python code doesn't rely on "_" members remaining the same from 
one release of code to the next. Just because the compiler doesn't enforce 
it doesn't mean it's "correct".

>>>   There's a huge difference. You make it sound like there was no difference
>>> at all. Just for the sake of argument.
> 
>> In practice, I disagree.
> 
>   In practice? Well, then show me in practice. Some actual code, please.

For what, writing to private variables, or reading from them?  In practice, 
bugs clobber data they shouldn't in unsafe languages.

>> Right. But that's far from "makes no guarantees" you see. You often make a 
>> broad sweeping statement, and when I call you on it, you backpedal and then 
>> say I'm exaggerating "on purpose".
> 
>   You are calling me a liar now? 

No. Merely noting a trend. You don't always say precisely and clearly what 
you mean, and when I give counter-examples, you clarify and then act 
indignant that I addressed what you said instead of what you meant.

>   I might well claim that you "backpedaled" when you first claimed that
> C++ has *no* encapsulation, and later changed it to "little" encapsulation.

I did indeed. It has a small amount of encapsulation, but not enough (IMO) 
to be significant. I granted you that one.

>>>   So you said that "a given type, all by itself, will require sizeof(type)
>>> bytes of memory". Well, duh, that's how sizeof() is defined, after all.
> 
>> You said "there are no guarantees".
> 
>   I was talking about the memory layout of an object. There *are* no
> guarantees about it.

Yes, there are. There's even one I pointed out to which you said "Well, duh, 
that's the definition."  Remember?

You weren't talking about "guarantees about the memory layout of an object." 
You were apparently talking about "guarantees about the memory layout of an 
object that would let you safely and portably access private member 
variables of an arbitrary class without invoking undefined behavior."

And *I* am saying those qualifiers reduce the utility of the form of 
encapsulation that C++ provides.

>> I said yes, there is at least the 
>> guarantee that the elements of a class even with private members are laid 
>> out in the space between the start and the end.
> 
>   You seriously don't understand the difference between "size" and "layout"?

I do. Do you seriously tell me that knowing there isn't data from another 
object between the lowest-addressed private member and the highest-addresses 
private member isn't a guarantee about the layout of the data of a class?

If I take the sizeof a child class, it's always going to be at least as 
large as the sizeof a parent class. If I have a class with two members of 
class X, it's going to be at least twice as large as class X, even if that 
means there are two vtable entries where one would be enough. If I have 
class X that inherits multiply from class A and class B, I know that I can 
take a pointer to class B and not worry that there are bits of A mixed up 
inside.

What you're seeing as "so obvious it doesn't need to be mentioned" is 
exactly what I'm saying are the guarantees. :-)  These sorts of things 
certainly aren't guaranteed in any number of other languages. (For example, 
IIRC, member variables in C# that are shared amongst multiple lambda 
expressions in the same class wind up being allocated in an area outside 
what would be the "sizeof" area of an instance of the class they're declared 
in, because they have to outlast the actual instance variable.  Again, IIRC.)

>   Then, as said in earlier posts, there are no languages with encapsulation
> because you can't physically protect data from being corrupted. There can
> always be a glitch somewhere that corrupts it.

I'll grant you that there are no language implementations on real computers 
that guarantee 100% encapsulation. However, that doesn't mean that any 
amount of encapsulation is equivalent to any other amount.

What's more isolated? A thread, or a process, or multiple independent 
processes all running in the same address space? Well, certainly, if all 
your code is 100% correct, you don't need separate processes at all, right? 
All you have to do to keep from having to reboot your 8086 is just write 
correct code, and no problem.

You may argue that encapsulation hasn't anything to do with correctness. I 
disagree. That's OK. We can agree not to agree on that point.

>   (By your principle of "if it can't be protected from everything it's not
> worth protecting at all",

That's not my principle. My principle is "if you write your combination on 
the outside of the lock, it's not a very secure lock."  Sure, it has all the 
attributes of a good lock, and it might be impossible to pick, but I'll take 
the one that doesn't come with the combination painted on it.

>   You don't use a debugger to resolve where the crash happens?

That's the thing, see. When someone comes along and stomps all over the 
stack, you can't really prove what's happening very easily, even with a 
debugger.

I suppose if we went out and bought a few hundred thousand dollars worth of 
hardware debugging hardware, JTAGs and stuff like that, we could find who is 
writing outside the stack. Unfortunately, clobbering stuff you shouldn't 
clobber doesn't really cause a detectable error until much later sometimes.

More importantly, there's no way to ensure that *your* code isn't clobbering 
*their* code, so there's always an excuse not to look at it if it works for 
their test code.  I.e., if there's a bug in my code that clobbers their 
private/static variables before they even get called, the same symptoms 
might manifest.

Unfortunately, running a device driver under a debugger works poorly without 
hardware support. Plus, we barely fit the system under test in the address 
space, let alone the debugger as well.

Apparently the entire industry is this screwed.  Which would certainly 
explain why in the time the phone system has evolved from cell phones that 
take two hands to carry into iPhones, the set top box industry still can't 
get digital video or even web pages onto the television.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 19:06:36
Message: <4b8da80c$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Darren New wrote:
>>> Really, Python isn't noticeably less private than C++. C++ shows you 
>>> everything. The compiler says "don't reference that by name", instead of 
>>> the IDE doing it.
> 
>> Or, to phrase it a different way, I've never had a problem caused by someone 
>> intentionally accessing a private member in a Python-like environment that 
>> would have been solved by making the code not compile when that happens.
> 
>   And I have never had a problem with a wild pointer modifying the private
> members of a class either.
> 
>   Is your anecdotal evidence better than mine?

No. That's why it's a matter of opinion.

>> Have you ever known someone to accidentally name a C function __XYZ__ and 
>> not be aware they might be stepping on the compiler's namespace?
> 
>   Actually it's quite common in C and C++ out there to have names starting
> with underscore even though the standard says it shouldn't be done.
> 
>> Is it 
>> really a problem that the compiler doesn't prevent you from using names like 
>> that?
> 
>   Well, it is quite a common objection in C++ groups when people post code.

Really? I'm quite surprised.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 19:06:59
Message: <4b8da823$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Warp wrote:
>>>   (That doesn't mean you can't have a pointer pointing to a member of an
>>> object, in a portable way. However, that requires either for the member
>>> to be public or for the object to give you the pointer.)
> 
>> Actually, just thinking about it a bit more, if the class returns a pointer 
>> to a member variable, and you can look in the header and see that pointer is 
>> in an array, then you have a well-defined way of accessing the other private 
>> data in the same array.
> 
>   Only if you also know which array element it's pointing to. (If you know,
> then the class is, effectively, exposing the entire array.)

That's true.

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Warp
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 19:50:04
Message: <4b8db23c@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> *I* just don't see any practical advantage of having the compiler enforce 
> that. You gave an example from your function library. I contend that the 
> same thing could have been done with Python's level of encapsulation. It 
> would be no more of an error for me (say) to change how variables that start 
> with "_" work than it would be for you to change the size of the class you 
> wrote.

  Oh, but access rights go beyond simply restricting access from the outside.

  For example, in C++ a base class can have a private member with a certain
name, and a class derived from that base class can have a private member
with the same name. There's no clash. Each class unambiguously accesses its
own member without having to explicitly specify which one with a class prefix.
The private part of the base class really is private: It doesn't even
interfere with a derived class's scope, which might have members with same
names as the base class without problems.

  Also, what naming convention is used in Python for a protected member
variable or function? (In other words, a variable or function which is
accessible by a derived class but not from the outside.)

  Private inheritance is not an extremely common technique in C++, but it
sometimes has its uses (one could think of private inheritance as a kind
of composition where the object itself can act as an object of either the
base or derived class). I suppose there's no such a thing as private
inheritance in Python.

> *Correct* Python code doesn't rely on "_" members remaining the same from 
> one release of code to the next. Just because the compiler doesn't enforce 
> it doesn't mean it's "correct".

  Does the Python standard explicitly state this?

> No. Merely noting a trend. You don't always say precisely and clearly what 
> you mean, and when I give counter-examples, you clarify and then act 
> indignant that I addressed what you said instead of what you meant.

  There's this thing called reading comprehension. Understanding what the
other wrote, in its context, rather than nitpicking on words.

> >>>   So you said that "a given type, all by itself, will require sizeof(type)
> >>> bytes of memory". Well, duh, that's how sizeof() is defined, after all.
> > 
> >> You said "there are no guarantees".
> > 
> >   I was talking about the memory layout of an object. There *are* no
> > guarantees about it.

> Yes, there are. There's even one I pointed out to which you said "Well, duh, 
> that's the definition."  Remember?

  The size of an object says nothing about its layout.

  You could as well claim that sizeof(double) tells you something about
how the different parts of a base-2 floating point value are laid out
inside the variable. Of course it doesn't. It just tells how much memory
the type takes, not its internal layout.

  Making a guarantee about the layout of a double would be, for example,
"the most significant bit is always the sign bit".

> I do. Do you seriously tell me that knowing there isn't data from another 
> object between the lowest-addressed private member and the highest-addresses 
> private member isn't a guarantee about the layout of the data of a class?

  Actually there's no such guarantee. If you use virtual inheritance in a
diamond inheritance situation, the common base will be shared among the
classes which were multiple-inherited from. This means that the layout
of the objects inside the most derived class will be split, rather than
being at contiguous memory locations.

> If I take the sizeof a child class, it's always going to be at least as 
> large as the sizeof a parent class. If I have a class with two members of 
> class X, it's going to be at least twice as large as class X, even if that 
> means there are two vtable entries where one would be enough.

  Sizes say nothing about the layout any more then sizeof(double) says
anything about the internal layout of a floating point number.

> If I have 
> class X that inherits multiply from class A and class B, I know that I can 
> take a pointer to class B and not worry that there are bits of A mixed up 
> inside.

  But you have no way of knowing the internal layout of B, especially if
A and B have a common base class from which they inherit virtually.

> >   You don't use a debugger to resolve where the crash happens?

> That's the thing, see. When someone comes along and stomps all over the 
> stack, you can't really prove what's happening very easily, even with a 
> debugger.

  Depends on the debugger. For example programs like valgrind and AQtime
are quite good at catching out-of-bounds accesses, memory leaks and such.
(Of course they can't catch every possible bug, but quite many.)

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Why is Haskell interesting?
Date: 2 Mar 2010 20:45:04
Message: <4b8dbf20$1@news.povray.org>
Warp wrote:
>   For example, in C++ a base class can have a private member with a certain
> name, and a class derived from that base class can have a private member
> with the same name. There's no clash. 

That's cool. I imagine that helps with maintaining large code bases that 
other people are inheriting from.

>   Also, what naming convention is used in Python for a protected member
> variable or function? 

I'm not sure, honestly.

>   Private inheritance is not an extremely common technique in C++, but it
> sometimes has its uses (one could think of private inheritance as a kind
> of composition where the object itself can act as an object of either the
> base or derived class). I suppose there's no such a thing as private
> inheritance in Python.

Given there's no "private", no, I don't think you'd have "private 
inheritance." :-)

>   There's this thing called reading comprehension. Understanding what the
> other wrote, in its context, rather than nitpicking on words.

Yeah, let's both try to improve that. :-)

>> Yes, there are. There's even one I pointed out to which you said "Well, duh, 
>> that's the definition."  Remember?
> 
>   The size of an object says nothing about its layout.

It does. And I've given you multiple examples where it does. The very fact 
that "sizeof" is defined tells you something about how you have to implement 
things.

>   Actually there's no such guarantee. If you use virtual inheritance in a
> diamond inheritance situation, the common base will be shared among the
> classes which were multiple-inherited from. This means that the layout
> of the objects inside the most derived class will be split, rather than
> being at contiguous memory locations.

Really? OK.

>   Depends on the debugger. For example programs like valgrind and AQtime
> are quite good at catching out-of-bounds accesses, memory leaks and such.

Yeah. Now, get them to run while the graphics chips are expecting you to 
respond promptly to interrupts, and without taking up more than another 3K 
of memory. :-)

> (Of course they can't catch every possible bug, but quite many.)

True.  Of course, I'd argue that the existence of such tools show that such 
bugs are not so uncommon that they're worth disregarding. :-)

-- 
Darren New, San Diego CA, USA (PST)
   The question in today's corporate environment is not
   so much "what color is your parachute?" as it is
   "what color is your nose?"


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Why is Haskell interesting?
Date: 28 Mar 2010 20:31:32
Message: <4baff4e4$1@news.povray.org>
Invisible wrote:
> C has cpp, which (as far as I know) is very primitive and not
> Turing-complete.

It's not turing complete, but if you run it multiple times, it may be:

http://www.ioccc.org/2001/herrmann1.hint
http://www.ioccc.org/2001/herrmann1.c


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Why is Haskell interesting?
Date: 30 Mar 2010 21:24:56
Message: <4bb2a468@news.povray.org>
Darren New wrote:
>>> Bzzzt. You just don't know javascript very well. What object do you
>>> get when  foo or bar references "this"?
>> 
>> I have no idea. (And no obvious way of finding out...)
> 
> Hint: It's called "window".
> 
> function foo() {...}
> 
> is the same as
> 
> window["foo"] = Function(){...}

More precisely, a 'foo' property is added to the global object, which 
happens to be "window" when the JS interpreter is hosted in a web browser.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Why is Haskell interesting?
Date: 30 Mar 2010 21:30:02
Message: <4bb2a59a@news.povray.org>
Darren New wrote:
>>> window["foo"] = Function(){...}
>> OK. So in what way does this mean that "functions are not first-class"?
> 
> I didn't say it did. I said that everything in Javascript is an object.

Correct, including functions themselves.

> There are no functions unassociated with a corresponding instance.

Functions are not associated with an instance when they are created, the 
"association" is at call time.

var f = function (x) { return x; };

What instance is f associated to?

If you call f(4), 'this' inside the function body will be the global object 
because of the way you *called* it. However:

var o = {};
o.f = f;
o.f(4);

This time, 'this' inside the function will be set to 'o'.

You can also do this:

var o = {};
f.call(o, 4);


Post a reply to this message

<<< Previous 37 Messages Goto Initial 50 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.