Re: What will be the next MAJOR programming language for commercial use?
- From: Jon Harrop <usenet@xxxxxxxxxxxxxx>
- Date: Sun, 31 Jul 2005 01:25:16 +0100
Walter wrote:
>> >> Safety
>
> Yes.
Does D support pointer arithmetic? If so, what happens when an invalid
pointer is dereferenced?
>> >> Polymorphism
>
> Yes.
D supports OO and parametric polymorphism (via templates). However, in ML,
type inference allows many functions to be polymorphic without the
programmer having to explicitly do anything. For example, the following
function accepts a function "g" and a value "x" and returns "g(g(x))":
$ ocaml
Objective Caml version 3.08.3
# let f g x = g(g(x));;
val f : ('a -> 'a) -> 'a -> 'a = <fun>
#
The function is then valid for any type 'a. For example, this computes the
increment of the increment of 3:
# f (fun n -> n+1) 3;;
- : int = 5
>> >> Closures
>
> Yes. (dynamic closures)
I'm not sure what a "dynamic" closure is. Your web page states "The stack
variables, however, are not valid once the function declaring them has
exited, in the same manner that pointers to stack variables are not valid
upon exit from a function". This is a very low-level notion of a closure
that I've never come across. In ML, you just return a function and the
compiler takes care of working out what environment values need to be kept.
For example, the following (convoluted!) addition function is valid in OCaml
but, presumably, the equivalent would be invalid in D because the argument
"x" to "f" might be stored on the stack?
# let f x =
let g y = x + y in
g;;
val f : int -> int -> int = <fun>
In D, can you partially apply a function? For example, the following OCaml
defines an "add" function and then an "incr" function in terms of "add" and
finally increments 3 to get 4:
# let add i j = i+j;;
val add : int -> int -> int = <fun>
# let incr = add 1;;
val incr : int -> int = <fun>
# incr 3;;
- : int = 4
>> >> Higher-order functions
>
> Don't know what that means.
A higher-order function is a function that accepts a function as an
argument. The "map" function is the pedagogical example. It accepts a
function "f" and a list "[a; b; c; ...]" and returns the list "[f(a); f(b);
f(c); ...]". I think D will do this ok but you probably have to write
function-pointer-style type declarations?
>> >> Type inference
>
> Not sure what you mean.
With type inference, types are inferred by the compiler and do not have to
be written explicitly by the programmer. For example, the following OCaml
function increments the given integer but the type "int" is not written
explicitly in the function:
# let succ n = 1 + n;;
val succ : int -> int = <fun>
This is the main reason for OCaml's brevity.
> > >> Variants
>
> Can be done using a user defined class.
That's a no. :-)
Variants can be considered as such a common OO pattern that it is worth
having syntactic sugar to support them. For an example of OO vs a variant
type, look at the C++ and OCaml implementations of the scene tree in my ray
tracer:
http://www.ffconsultancy.com/free/ray_tracer/comparison.html
>> >> Polymorphic variants
>
> Can be done using a user defined class.
In addition to the benefits of ordinary variant types, polymorphic variants
can be even more useful in the presence of type inference. For example,
consider the following interpreter:
# type expr =
Int of int
| Var of string
| Let of string * expr * expr
| Add of expr * expr;;
type expr =
Int of int
| Var of string
| Let of string * expr * expr
| Add of expr * expr
# let rec eval state = function
Int i -> i
| Var s -> List.assoc s state
| Let (s, body, rest) -> eval ((s, eval state body) :: state) rest
| Add (e1, e2) -> eval state e1 + eval state e2;;
val eval : (string * int) list -> expr -> int = <fun>
This can be used to interpret the program "let x=2 in x+3":
# eval [] (Let("x", Int 2, Add(Var "x", Int 3)));;
- : int = 5
The program can be written without defining the type "expr" by using
polymorphic variants. OCaml then infers the variant type:
# let rec eval state = function
`Int i -> i
| `Var s -> List.assoc s state
| `Let (s, body, rest) -> eval ((s, eval state body) :: state) rest
| `Add (e1, e2) -> eval state e1 + eval state e2;;
val eval :
('a * int) list ->
([< `Add of 'b * 'b | `Int of int | `Let of 'a * 'b * 'b | `Var of 'a ]
as 'b) ->
int = <fun>
I can't see how this can be done using ordinary classes in D.
>> >> Pattern matching
>
> ? Supports function overloading, template partial specialization, and
> regular expression pattern matching.
Yes, I meant ML pattern matching rather than regexps. Sorry, I forgot how
many of these terms have different meanings...
ML pattern matching is an advancement upon the switch statement. For
example, this convoluted C code:
int fib(int n) {
switch (n) {
case 1 : return 1;
case 2 : return 1;
default : return fib(n-1) + fib(n-2);
}
}
can be written in OCaml as:
# let rec fib n = match n with 1 -> 1 | 2 -> 1 | n -> fib(n-1) + fib(n-2);;
val fib : int -> int = <fun>
However, ML pattern matches can be applied to many other types. For example,
given the following definition of a (polymorphic) tree as a variant type:
# type 'a tree = Empty | Node of 'a tree * 'a * 'a tree;;
type 'a tree = Empty | Node of 'a tree * 'a * 'a tree
This function counts the number of non-leaf nodes:
# let rec count = function
Empty -> 0
| Node(l, v, r) -> count l + 1 + count r;;
val count : 'a tree -> int = <fun>
# count (Node(Node(Empty, 1, Empty), 2, Empty));;
- : int = 2
As lists are built-in, they can be decomposed in a pattern just as they are
constructed by an expression. Specifically, the list "[1; 2; 3]" can also
be written "1 :: 2 :: 3 :: []" and the pattern "h :: t" gives the head "h"
of the list (the first element) and the tail "t" (the list containing the
remaining elements). For example, the following function counts the number
of elements in a list:
# let rec length = function [] -> 0 | _ :: t -> 1 + length t;;
val length : 'a list -> int = <fun>
ML pattern matching is a huge topic with lots of interesting aspects,
including the catching of redundant patterns and the optimisation of
complicated pattern matches, e.g. using perfect hashes.
>> >> Modules
>
> Yes.
>
>> >> Functors
>
> Yes.
There may be another discrepancy here. In ML, functors are module-level
functions that map modules onto modules. For example, the OCaml stdlib
contains a module "String" which implements strings. This can be applied to
the functor "Set" to obtain a module implementing a set of strings:
# module StringSet = Set.Make(String);;
module StringSet :
sig
type elt = String.t
type t = Set.Make(String).t
val empty : t
val is_empty : t -> bool
val mem : elt -> t -> bool
val add : elt -> t -> t
val singleton : elt -> t
val remove : elt -> t -> t
val union : t -> t -> t
val inter : t -> t -> t
val diff : t -> t -> t
val compare : t -> t -> int
val equal : t -> t -> bool
val subset : t -> t -> bool
val iter : (elt -> unit) -> t -> unit
val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
val for_all : (elt -> bool) -> t -> bool
val exists : (elt -> bool) -> t -> bool
val filter : (elt -> bool) -> t -> t
val partition : (elt -> bool) -> t -> t * t
val cardinal : t -> int
val elements : t -> elt list
val min_elt : t -> elt
val max_elt : t -> elt
val choose : t -> elt
val split : elt -> t -> t * bool * t
end
>> >> Native lists
>
> Can be done simply with a user defined class.
Again, the result will require much more code. Consider this function which
dices a list into a list of 3-element lists:
# let rec dice3 = function
[] -> []
| [_] | [_; _] as l -> [l]
| h1 :: h2 :: h3 :: t -> [h1; h2; h3] :: dice3 t;;
val dice3 : 'a list -> 'a list list = <fun>
# dice3 [1; 2; 3; 4; 5; 6; 7; 8];;
- : int list list = [[1; 2; 3]; [4; 5; 6]; [7; 8]]
>> >> List comprehensions
>
> I don't know what that is.
Syntactic sugar for building lists. For example, a list of even numbers can
be built in Python like this:
>>> print [i for i in range(20) if i%2 == 0]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>> >> Type classes
>
> Not sure what you mean, but all types in D are represented with a
> "TypeInfo" class instance giving information about it.
Type classes are a very generic and powerful type system construct made
famous by the programming language Haskell. For example, with type classes
you can represent a function with the type:
val add : 'a -> 'a -> 'a
for all 'a in {int, float}.
>> It would be nice to have a more comprehensive list of features supported
> by
>> various languages. I can't find a comparison between D and any ML, for
>> example.
>
> You can try www.digitalmars.com/d/comparison.html
That was the page I was reading. It is great for someone coming from a C++
background (which is most readers, after all) but it lists very few
features found in other state-of-the-art programming languages. I think
that would be very interesting, although it wouldn't show D in such a
positive light.
>> > Among it's other safety features, D has Contract Programming, which is
>> > a major leap forward in creating safe, robust programs.
>>
>> What can cause a D program to segfault, for example?
>
> A seg fault is just a thrown exception, the same thing that happens when a
> contract violation occurs.
I don't know anything about contract programming, although I have heard of
it. Must contracts be verified at run-time?
>> > GDC, the Gnu D Compiler, should be on Debian.
>>
>> I can't find it on Debian (stable, testing or unstable). Can you give us
>> a link to the .deb, please?
>
> I think DMD will work out of the box on Debian. Some people have compiled
> GDC for Debian, but I don't know if they've put up a link for the
> binaries.
I'll have a go and see if I can port my ray tracer.
Great work on D, BTW. :-)
--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com
.
- Follow-Ups:
- References:
- What will be the next MAJOR programming language for commercial use?
- From: lvirden
- Re: What will be the next MAJOR programming language for commercial use?
- From: Walter
- Re: What will be the next MAJOR programming language for commercial use?
- From: Jon Harrop
- Re: What will be the next MAJOR programming language for commercial use?
- From: Walter
- Re: What will be the next MAJOR programming language for commercial use?
- From: Jon Harrop
- Re: What will be the next MAJOR programming language for commercial use?
- From: Walter
- What will be the next MAJOR programming language for commercial use?
- Prev by Date: Re: What will be the next MAJOR programming language for commercial use?
- Next by Date: Re: What will be the next MAJOR programming language for commercial use?
- Previous by thread: Re: What will be the next MAJOR programming language for commercial use?
- Next by thread: Re: What will be the next MAJOR programming language for commercial use?
- Index(es):
Relevant Pages
|