Re: [Haskell-cafe] Specify array or list size?

2005-05-08 Thread Glynn Clements

Thomas Davie wrote:

  I'm not familiar with your C++ example (not being familiar with C++),
  but I think that it's a bit of a stretch of the imagination to say
  that C introduces a variable of type array of 50 ints, the fact
  that this is now an array of 50 integers is never checked at any
  point in the compilation or run, and I'm not sure it can be even if
  KR had wanted to.
 
 
  The size is taken into account when such array type is an element of
  another array, and by sizeof.
 
  int (*p)[50]; /* p may legally point only to arrays of 50 ints each */
  ++p; /* p is assumed to point into an array, and is moved by one
  element, i.e. by 50 ints */
 I'm not sure what you're trying to prove by saying that... There is  
 still no type information that says that the contents of p are an  
 array of 50 elements...

Put it this way, then:

1   void foo(void)
2   {
3   int a[2][50];
4   int b[2][60];
5   int (*p)[50];
6   
7   p = a;
8   p = b;
}

$ gcc -c -Wall foo.c
foo.c: In function `foo':
foo.c:8: warning: assignment from incompatible pointer type

In line 7, an expression of type int (*)[50] is assigned to a
variable of type int (*)[50], which is OK. In line 8, an expression
of type int (*)[60] is assigned to a variable of type int (*)[50],
and the compiler complains.

 I can still attempt to access element 51 and get a runtime memory
 error.

That's because C doesn't do bounds checking on array accesses. It has
nothing to do with types.

 The type of p is still int**,

No it isn't. int** and int (*)[50] are different types and have
different run-time behaviour.

 not pointer to array of 50 ints

Yes it is. The semantics of C pointer arithmetic mean that the size of
the target is an essential part of the pointer type.

In C, arrays and pointers are *not* the same thing. They are often
confused because C does several automatic conversions:

1. When used as an expression (rather than an lvalue), arrays are
automatically converted to pointers. Arrays only ever occur as
lvalues, never as expressions.

2. In a declaration, the x[...] syntax indicates that x is an array,
but in an expression, x must be a pointer (which includes an array
which has been converted to a pointer due to rule 1 above).

3. When declaring function arguments, you can use T x[] or T x[N]
as an alternative syntax for T *x; x is still a pointer, regardless
of the syntax used.

-- 
Glynn Clements [EMAIL PROTECTED]
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Hamilton Richards
At 9:24 AM -0400 2005/5/7, Daniel Carrera wrote:
Hello,
Right now I'm using type declarations like:
f :: Int - [Int]
So f returns a list of Ints.
Is there a way to tell Haskell that a list or array must have 
exactly (say) 256 elements? I'd love to have Haskell make sure that 
the array I build is the correct size.
Well, for starters, lists and arrays are two entirely different 
topics. I've noticed that Haskell newbies sometimes confuse them 
--possibly the use of [] in list types and enumerations triggers an 
unconscious association with [] used in conventional languages for 
array indexing.

It's easy to define a function that constructs a list of a given 
size, but that size is not part of the list's type. As far as I know, 
the last programming language that included arrays' sizes in their 
types was Standard Pascal, and it turned out to be an unmitigated 
disaster. Because array parameters were typed with their sizes, a 
procedure for searching arrays of size 100 could not be used for 
arrays of any other size. Every useful (non-Standard) dialect of 
Pascal provided a way around that restriction, as did Pascal's 
successor, Modula-2, and as far as I know the mistake has not been 
repeated.

As for Haskell arrays, I've been programming in Haskell since the 
early 1990's, and I've never really needed them. Most Haskell 
programmers find lists much more useful, and you'd probably be better 
off concentrating on lists until you encounter a problem in which 
arrays are really needed. When you do, you'll discover that in 
Haskell as in most other languages, an array's size is not part of 
its type.

It's nice to see you taking up Haskell with such enthusiasm. If you 
agree with many of us that Haskell's one of today's best programming 
programming languages, I hope you'll help spread the word.

With best wishes,
--Ham
--
--
Hamilton Richards, PhD   Department of Computer Sciences
Senior Lecturer  The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138Austin, Texas 78712-0233
[EMAIL PROTECTED][EMAIL PROTECTED]
http://www.cs.utexas.edu/users/ham/richards
--
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Daniel Carrera
Hamilton Richards wrote:
Well, for starters, lists and arrays are two entirely different topics. 
I've noticed that Haskell newbies sometimes confuse them --possibly the 
use of [] in list types and enumerations triggers an unconscious 
association with [] used in conventional languages for array indexing.
I think it's because there's no real reason for someone to think that 
the words list and array might not be synonims. I certainly don't 
seen a linguistic distinction. Either term refers to an ordered 
collection of items.

Suppose that you learn a new computer language, and it happens to assign 
special meanings to the words collection and group. You don't know 
this. So you start talking about groups as you do in every day English 
and people tell you that you're mixing up concepts.

I guess that a more likely example in programming would be a language 
that differentiates between functions, procedures and subroutines.

As for Haskell arrays, I've been programming in Haskell since the early 
1990's, and I've never really needed them. Most Haskell programmers find 
lists much more useful, and you'd probably be better off concentrating 
on lists until you encounter a problem in which arrays are really 
needed.
Arrays have O(1) lookup, and Lists have O(n) lookup. For some purposes, 
arrays might be better. I'm thinking of reimplementing RC4 with arrays 
to see if it makes a differece.

When you do, you'll discover that in Haskell as in most other 
languages, an array's size is not part of its type.
Well... that's not quite what I was looking for. I just wanted to raise 
an error if the array ever has length other than 256. And I don't know 
how to do that on a language that doesn't have side-effects.

It's nice to see you taking up Haskell with such enthusiasm. If you 
agree with many of us that Haskell's one of today's best programming 
programming languages, I hope you'll help spread the word.
:-)
A lot of concepts from Haskell match the way I think about problems. You 
see, my background is in math, not programming. For example, currying 
and higher order functions are essentially new names for things I've 
been doing for years. So after the concept was explained to me, it 
didn't take me that long to get it.

My current impression is that Haskell is great for anyone with good 
mathematics education. But I don't know how it'd fare with other people.

I have a lady friend who wants to learn how to program. She's a 
technical person, but has no math background to speak of. I can't decide 
whether to start with a clear-syntax imperative language (Ruby) or a 
functional language (Haskell). I confess I've been leaning towards Ruby.

Cheers,
Daniel.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Sebastian Sylvan
On 5/7/05, Daniel Carrera [EMAIL PROTECTED] wrote:
 Hamilton Richards wrote:
 
  Well, for starters, lists and arrays are two entirely different topics.
  I've noticed that Haskell newbies sometimes confuse them --possibly the
  use of [] in list types and enumerations triggers an unconscious
  association with [] used in conventional languages for array indexing.
 
 I think it's because there's no real reason for someone to think that
 the words list and array might not be synonims. I certainly don't
 seen a linguistic distinction. Either term refers to an ordered
 collection of items.
 

Hmm. Not sure I agree. For me an array is a fixed set of elements
which may or not contain stuff (not necessarily in a CS context but
just linguistically). A list is more of a linear structure.
An array is a sheet of paper with lines on it, a list is the todo-list
written on that paper.

Anyway. There is a difference, and I think the names reflect that pretty well.

/S

-- 
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Antti-Juhani Kaijanaho
On 20050507T093613-0700, Fergus Henderson wrote:
 On 07-May-2005, Hamilton Richards [EMAIL PROTECTED] wrote:
  As far as I know, the last programming language that included
  arrays' sizes in their types was Standard Pascal,
 
 There have been many such languages since Standard Pascal.  For
 example C, C++, C#, Java, Ada, VHDL, and NU-Prolog.

C, C++ and Java do not belong to that list.  I can't speak about the
others, not being very familiar with them.

In C and C++, the declaration int n[50]; introduces an array variable
with size 50 having the type array of int.  The size is *not* part of
the type.  In Java, the array size is not given in the declaration at
all (instead, it is given in the new expression), and is not part of the
type.
-- 
Antti-Juhani Kaijanaho  http://antti-juhani.kaijanaho.info/

Blogi - http://kaijanaho.info/antti-juhani/blog/
 Toys - http://www.cc.jyu.fi/yhd/toys/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Marcin 'Qrczak' Kowalczyk
Antti-Juhani Kaijanaho [EMAIL PROTECTED] writes:

  As far as I know, the last programming language that included
  arrays' sizes in their types was Standard Pascal,
 
 There have been many such languages since Standard Pascal.  For
 example C, C++, C#, Java, Ada, VHDL, and NU-Prolog.

 C, C++ and Java do not belong to that list.  I can't speak about the
 others, not being very familiar with them.

Java and C# don't belong to that list, but C and C++ do.
I don't know about others.

 In C and C++, the declaration int n[50]; introduces an array variable
 with size 50 having the type array of int.  The size is *not* part of
 the type.

No, it introduces a variable of type array of 50 ints, which can be
converted to pointer to int.

It matters when you make a pointer of such arrays, an array of such
arrays, or sizeof such array. In C++ the size can be matched by
template parameter, and you can have separate overloadings for
separate array sizes.

-- 
   __( Marcin Kowalczyk
   \__/   [EMAIL PROTECTED]
^^ http://qrnik.knm.org.pl/~qrczak/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Marcin 'Qrczak' Kowalczyk
Sebastian Sylvan [EMAIL PROTECTED] writes:

 A list is, for me, more of a logical entity (as opposed to
 structural). It's a sequence of stuff not a particular way to
 store it (singly-linked, doubly-linked, arraylists etc.).

I call it sequence.
A list is usually a concrete type in a given language.

Exceptions: Python calls its arrays lists, and Perl calls its
sequences lists.

-- 
   __( Marcin Kowalczyk
   \__/   [EMAIL PROTECTED]
^^ http://qrnik.knm.org.pl/~qrczak/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Thomas Davie
No, it introduces a variable of type array of 50 ints, which can be
converted to pointer to int.
It matters when you make a pointer of such arrays, an array of such
arrays, or sizeof such array. In C++ the size can be matched by
template parameter, and you can have separate overloadings for
separate array sizes.
I'm not familiar with your C++ example (not being familiar with C++),  
but I think that it's a bit of a stretch of the imagination to say  
that C introduces a variable of type array of 50 ints, the fact  
that this is now an array of 50 integers is never checked at any  
point in the compilation or run, and I'm not sure it can be even if  
KR had wanted to.  If I'm thinking straight then *any* array  
definition merely gets re-written to a memory allocation of the  
relevant amount of ram, and beyond this point it is forever of type  
pointer to array content type.

As an example:
int bobsArray[5];
bobsArray[6] = 23;
is not badly typed  it is merely a badly broken program.
Bob
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Antti-Juhani Kaijanaho
On 20050507T203246+0200, Marcin 'Qrczak' Kowalczyk wrote:
  In C and C++, the declaration int n[50]; introduces an array variable
  with size 50 having the type array of int.  The size is *not* part of
  the type.
 
 No, it introduces a variable of type array of 50 ints, which can be
 converted to pointer to int.

ISO 9899:1999 (C99) section 6.7.5.2:3 says that its type is array of
int, not array of 50 ints:

  If, in the declaration ``T D1'', D1 has one of the forms:
 D[ type-qualifier-listopt assignment-expressionopt ]
 D[ static type-qualifier-listopt assignment-expression ]
 D[ type-qualifier-list static assignment-expression ]
 D[ type-qualifier-listopt * ]
  and the type specified for ident in the declaration ``T D'' is ``
  derived-declarator-type-list T '', then the type specified for ident
  is ``derived-declarator-type-list array of T ''.121) (See 6.7.5.3 for
  the meaning of the optional type qualifiers and the keyword static.)

  121) When several ``array of'' specifications are adjacent, a
  multidimensional array is declared.

 It matters when you make a pointer of such arrays, an array of such
 arrays, or sizeof such array. In C++ the size can be matched by
 template parameter, and you can have separate overloadings for
 separate array sizes.

For C, in all those cases, the array size is a property of the variable,
not of the type. 
-- 
Antti-Juhani Kaijanaho  http://antti-juhani.kaijanaho.info/

Blogi - http://kaijanaho.info/antti-juhani/blog/
 Toys - http://www.cc.jyu.fi/yhd/toys/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Jacob Nelson
GCC knows how big an array is:
jake$ cat  arrsizetest.c
#include stdio.h
int main()
{
int a[50];
printf(sizeof a == %d\n,sizeof(a));
return 0;
}
jake$ gcc arrsizetest.c
jake$ ./a.out
sizeof a == 200
jacob
On Sat, 7 May 2005, Thomas Davie wrote:
No, it introduces a variable of type array of 50 ints, which can be
converted to pointer to int.
It matters when you make a pointer of such arrays, an array of such
arrays, or sizeof such array. In C++ the size can be matched by
template parameter, and you can have separate overloadings for
separate array sizes.
I'm not familiar with your C++ example (not being familiar with C++), but I 
think that it's a bit of a stretch of the imagination to say that C 
introduces a variable of type array of 50 ints, the fact that this is now 
an array of 50 integers is never checked at any point in the compilation or 
run, and I'm not sure it can be even if KR had wanted to.  If I'm thinking 
straight then *any* array definition merely gets re-written to a memory 
allocation of the relevant amount of ram, and beyond this point it is forever 
of type pointer to array content type.

As an example:
int bobsArray[5];
bobsArray[6] = 23;
is not badly typed ? it is merely a badly broken program.
Bob
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Marcin 'Qrczak' Kowalczyk
Thomas Davie [EMAIL PROTECTED] writes:

 I'm not familiar with your C++ example (not being familiar with C++),
 but I think that it's a bit of a stretch of the imagination to say
 that C introduces a variable of type array of 50 ints, the fact
 that this is now an array of 50 integers is never checked at any
 point in the compilation or run, and I'm not sure it can be even if
 KR had wanted to.

The size is taken into account when such array type is an element of
another array, and by sizeof.

int (*p)[50]; /* p may legally point only to arrays of 50 ints each */
++p; /* p is assumed to point into an array, and is moved by one
element, i.e. by 50 ints */

 As an example:

 int bobsArray[5];
 bobsArray[6] = 23;

 is not badly typed - it is merely a badly broken program.

Because the array size is not taken into account by indexing. But it's
a part of the type. These issues are independent, for example in C#
both are the opposite.

-- 
   __( Marcin Kowalczyk
   \__/   [EMAIL PROTECTED]
^^ http://qrnik.knm.org.pl/~qrczak/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Antti-Juhani Kaijanaho
On 20050507T120430-0400, Daniel Carrera wrote:
 I think it's because there's no real reason for someone to think that 
 the words list and array might not be synonims. I certainly don't 
 seen a linguistic distinction. Either term refers to an ordered 
 collection of items.

I don't even know what array means outside of programming (but then
again, English is not my native language).  However...

It is dangerous for anyone to infer the meaning of a technical term
based on the common meaning of the word.  The common meaning usually
helps in *remembering* the technical meaning, but that comes *after*
finding out what the technical meaning is.  This applies in any
technology or science, not just Haskell programming or programming in
general.

 Suppose that you learn a new computer language, and it happens to assign 
 special meanings to the words collection and group.

Those terms have a lot of meanings in technical jargon.

 You don't know this. So you start talking about groups as you do in
 every day English and people tell you that you're mixing up concepts.

Your mistake is the start talking about groups as you do in every day
English part.

 I guess that a more likely example in programming would be a language 
 that differentiates between functions, procedures and subroutines.

Well, most languages use function contrary to the everyday meaning of
the word.  Even functional in functional programming does not mean
what you'd think it means.  (Anybody else heard people shout But C *is*
a functional language!? - And I'm not talking about geeks who use the
FP style in C:)

-- 
Antti-Juhani Kaijanaho  http://antti-juhani.kaijanaho.info/

Blogi - http://kaijanaho.info/antti-juhani/blog/
 Toys - http://www.cc.jyu.fi/yhd/toys/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Daniel Carrera
Antti-Juhani Kaijanaho wrote:
Your mistake is the start talking about groups as you do in every day
English part.
The point I'm trying to make is that you can't necessarily predict that 
a programming language will abscribe special meaning to standard known 
words like group or list. I can very well talk about a list without 
knowing that the members of this list (see?) might have a special 
meaning for that word that I'm not aware of. You can't possibly forsee 
that sort of thing. It is not a mistake to use the word group to mean 
what it means in English, I can't possibly know that an Algebraist has a 
particular definition of group that differs from every-day use.

Cheers,
Daniel.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread feucht
On  7 May, Daniel Carrera wrote:
 Hello,
 
 Right now I'm using type declarations like:
 
 f :: Int - [Int]
 
 So f returns a list of Ints.
 
 Is there a way to tell Haskell that a list or array must have exactly 
 (say) 256 elements? I'd love to have Haskell make sure that the array I 
 build is the correct size.
 
 Cheers,
 Daniel.

Hi,
you may use a tuple?

greetings,
Philip

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Thomas Davie
On May 7, 2005, at 8:07 PM, Marcin 'Qrczak' Kowalczyk wrote:
Thomas Davie [EMAIL PROTECTED] writes:

I'm not familiar with your C++ example (not being familiar with C++),
but I think that it's a bit of a stretch of the imagination to say
that C introduces a variable of type array of 50 ints, the fact
that this is now an array of 50 integers is never checked at any
point in the compilation or run, and I'm not sure it can be even if
KR had wanted to.
The size is taken into account when such array type is an element of
another array, and by sizeof.
int (*p)[50]; /* p may legally point only to arrays of 50 ints each */
++p; /* p is assumed to point into an array, and is moved by one
element, i.e. by 50 ints */
I'm not sure what you're trying to prove by saying that... There is  
still no type information that says that the contents of p are an  
array of 50 elements... I can still attempt to access element 51 and  
get a runtime memory error.  The type of p is still int**, not  
pointer to array of 50 ints

As an example:
int bobsArray[5];
bobsArray[6] = 23;
is not badly typed - it is merely a badly broken program.
Because the array size is not taken into account by indexing. But it's
a part of the type. These issues are independent, for example in C#
both are the opposite.
I don't think it is part of the type... Does the compiler ever know  
any more about the type of bobsArray other than it's a pointer to an  
integer?  I think that the above code can be directly translated to:

int *bobsArray;
bobsArray = (int *)malloc(5 * sizeof(int));
bobsArray[6] = 23
Which stores exactly the same about on type information.
Bob
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Jacob Nelson

On Sat, 7 May 2005, Abraham Egnor wrote:
So does ghc:
...
That doesn't mean the size is part of the *type*.
Sure. I'm just pointing out that
   int a[50];
is not *quite* the same as
   int *a = (int *)malloc(50 * sizeof(int));
jacob
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread David Roundy
On Sat, May 07, 2005 at 08:20:15PM +0100, Thomas Davie wrote:
 On May 7, 2005, at 8:07 PM, Marcin 'Qrczak' Kowalczyk wrote:
 The size is taken into account when such array type is an element of
 another array, and by sizeof.
 
 int (*p)[50]; /* p may legally point only to arrays of 50 ints each */
 ++p; /* p is assumed to point into an array, and is moved by one
 element, i.e. by 50 ints */

 I'm not sure what you're trying to prove by saying that... There is  
 still no type information that says that the contents of p are an  
 array of 50 elements... I can still attempt to access element 51 and  
 get a runtime memory error.  The type of p is still int**, not  
 pointer to array of 50 ints

No, int (*p)[50] is a multidimensional array, one of the most useless
concepts in C, and is equivalent to int p[50][] (or is it p[][50]... I
always get my matrix subscripts messed up).

In a multi-dimensional array, all the dimensions but the first (or last?)
are fixed in size.  Unfortunately, these are fixed at compile time, so
there's no way to write a function that can act upon multidimensional
arrays of arbitrary size.  So we get the joy of writing terms like m[i+n*j]
to deal with matrices...
-- 
David Roundy
http://www.darcs.net
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Marcin 'Qrczak' Kowalczyk
David Roundy [EMAIL PROTECTED] writes:

 No, int (*p)[50] is a multidimensional array, one of the most useless
 concepts in C, and is equivalent to int p[50][] (or is it p[][50]...
 I always get my matrix subscripts messed up).

No, it's not equivalent to either. Array type are not the same as
pointer type.

(Except that in function parameters an array type really means a pointer.)

int p[50][] is an error because array element type must be a complete type,
and an array without a dimension is an incomplete type.

int p[][50] declares p as an array with unknown size, of arrays of size 50.
Such declaration makes sense:
1. as a declaration of an array defined elsewhere (usually with extern);
   it must have a size specified in some place;
2. with an initializer in braces, whose size determines the array size;
3. as function parameter, where it means a pointer.

int (*p)[50] declares p as a pointer to an array of size 50. Such declaration
can be used anywhere without restrictions - this is a complete type.

-- 
   __( Marcin Kowalczyk
   \__/   [EMAIL PROTECTED]
^^ http://qrnik.knm.org.pl/~qrczak/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Antti-Juhani Kaijanaho
On 20050507T212832+0200, Marcin 'Qrczak' Kowalczyk wrote:
  ISO 9899:1999 (C99) section 6.7.5.2:3 says that its type is array of
  int, not array of 50 ints:
 
 Ok, so in C terminology type is different from most statically typed
 languages in this respect. The dimension is used together with the
 type to determine static properties, and 6.7.5.2:4 says:
 
[#4]  For  two array types to be compatible, both shall have

Actually, that's 6.7.5.2:6.  It is the only place where array size is
truly used as part of the type.  In all other contexts, it is easily
interpretable as a property of the variable, and since the size of the
array is not otherwise used as a type attribute, it is fair to say that
it is not really a type attribute.  (Not in the Pascal sense, in any
case.)

 In both languages lvalueness is also not considered a part of the type
 but an alternative language presentation could use a wording where it is.

There are always alternative ways to present a language.

-- 
Antti-Juhani Kaijanaho  http://antti-juhani.kaijanaho.info/

Blogi - http://kaijanaho.info/antti-juhani/blog/
 Toys - http://www.cc.jyu.fi/yhd/toys/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Hamilton Richards
At 9:36 AM -0700 2005/5/7, Fergus Henderson wrote:
On 07-May-2005, Hamilton Richards [EMAIL PROTECTED] wrote:
 As far as I know,
 the last programming language that included arrays' sizes in their
 types was Standard Pascal,
There have been many such languages since Standard Pascal.
For example C, C++, C#, Java, Ada, VHDL, and NU-Prolog.
Well, perhaps we're using the terminology in somewhat different ways. 
When I say that Standard Pascal includes arrays' sizes in their type, 
I mean that in Pascal these two arrays:

X : array [1..50] of integer;
Y : array [1..100] of integer;
belong to different types. If I define two procedures
procedure P( a : array [1..50] of integer )
procedure Q( a : array [1..100] of integer )
then the calls
P( X )
Q( Y )
are valid, but
P( Y )
Q( X )
are invalid.
That's not the case in C, C++, Java, or Ada. In C and C++, for 
example, given two arrays

int X[50];
int Y[100];
and a function declared as
void P( int a[] )
then these calls
P( X )
P( Y )
are both valid, because the sizes of X and Y are not part of their type.

 and it turned out to be an unmitigated
 disaster. Because array parameters were typed with their sizes, a
 procedure for searching arrays of size 100 could not be used for
 arrays of any other size. Every useful (non-Standard) dialect of
 Pascal provided a way around that restriction, as did Pascal's
 successor, Modula-2, and as far as I know the mistake has not been
 repeated.
The disaster was the lack of polymorphism in Pascal's type system,
not making array sizes part of their types.
Polymorphism would certainly have improved Pascal's type system, but 
that's a far bigger leap than simply separating arrays' sizes from 
their types (as was done in Modula-2).

The languages above all have some means of writing a procedure
that works for different sized arrays, whether it be using
pointers (in C), templates (in C++), unconstrained array
parameters (in Ada and VHDL), or inheritence (in C# and Java).
As my C/C++ example above shows, in those languages at least, no such 
special means is necessary. In C and C++, there's not even any way 
for a function to discover the size of an array argument. This is the 
opposite extreme from Pascal, where the size is determined by the 
parameter type. Between these extremes is Modula-2, which allows 
array arguments of any size to be bound to an open array parameter, 
but provides a primitive function HIGH which, applied to an array 
parameter, returns the size of the argument to which it is bound.

Another example of a programming language for which array lengths are
part of their type is Cryptol www.cryptol.net.  Cryptol was
designed by Galois Connections, the company that I work for,
starting about five years ago (i.e. before I joined Galois).
It is notable in this context because it was designed by expert functional
programmers who were very familiar with Haskell, and who had indeed
participated in the design and implementation of Haskell.
They chose to include array sizes in the type system, despite the
resulting increase in the complexity of the type system, because
array sizes are often important for the domain of cryptography --
as the original poster noticed!
Thanks for the pointer-- I had not encountered Cryptol. Looks quite 
interesting, and I can readily see how making the size of a sequence 
part of its type can be useful.

But it's one thing to make this a feature of a language specific to a 
domain in which it is useful, or to provide ways to define 
size-specific array types in a language such as C++, and quite 
something else to make size-typed arrays the only array types in what 
was intended to be a general-purpose programming language. That, I 
still maintain, was a mistake.

Cheers,
--Ham
--
--
Hamilton Richards, PhD   Department of Computer Sciences
Senior Lecturer  The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138Austin, Texas 78712-0233
[EMAIL PROTECTED][EMAIL PROTECTED]
http://www.cs.utexas.edu/users/ham/richards
--
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Marcin 'Qrczak' Kowalczyk
Hamilton Richards [EMAIL PROTECTED] writes:

 That's not the case in C, C++, Java, or Ada. In C and C++, for
 example, given two arrays

   int X[50];
   int Y[100];

 and a function declared as

   void P( int a[] )

 then these calls

   P( X )
   P( Y )

 are both valid, because the sizes of X and Y are not part of their type.

But here you don't pass the array but a pointer to its first element.
You can even call P(x) where x is an int, not an array at all.

Consider this:

int X[50];
int Y[100];
void P(int (a)[50]) {}
int main() {
   P(X); // valid
   P(Y); // invalid
}

 In C and C++, there's not even any way for a function to discover
 the size of an array argument.

templateint N
int size(int (a)[N]) {
return N;
}

-- 
   __( Marcin Kowalczyk
   \__/   [EMAIL PROTECTED]
^^ http://qrnik.knm.org.pl/~qrczak/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Specify array or list size?

2005-05-07 Thread Hamilton Richards
At 12:04 PM -0700 2005/5/7, Jacob Nelson wrote:
GCC knows how big an array is:
jake$ cat  arrsizetest.c
#include stdio.h
int main()
{
int a[50];
printf(sizeof a == %d\n,sizeof(a));
return 0;
}
jake$ gcc arrsizetest.c
jake$ ./a.out
sizeof a == 200
jacob
gcc knows the size of an array variable only in the scope of its definition:
#include stdio.h
void f( int x[] )
{
printf(f:sizeof a == %d\n,sizeof(x));
}
int main()
{
int a[50];
printf(main: sizeof a == %d\n,sizeof(a));
f( a );
return 0;
}
ham% gcc arrsize.c
ham% a.out
main: sizeof a == 200
f:sizeof a == 4
Cheers,
--Ham
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe