MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Types in Mathematica

On Tuesday 06 December 2005 13:04, Steven T. Hatton wrote:
> Jon Harrop wrote:
> > Steven T. Hatton wrote:
> >> I believe most people here will agree that Mathematica is not a
> >> strongly typed programming language.
> >
> > Actually, one can productively say that Mathematica is a strongly typed
> > language because it has only one type (expr).
> Is that assertion really meaningful or useful?

Yes. It is a direct result of Mathematica being a rewrite language.

> >> What would a type system in Mathematica be?
> >
> > Anything you want.
> What would a _useful_ type system in Mathematica be?

I think there are many possible answers. However, I am not sure of their
utility. Perhaps it would be nice if you could write "conventional" code in
Mathematica and have it run as fast and be as statically checked as it
would be in other languages.

> >> What might be lost?
> >
> > Simplicity, brevity, clarity.
> It might be argued that these could be increased in some circumstance by
> having a type system.


> Likewise, performance might take a hit from extensive typechecking.

If your type checker is very slow (e.g. if it were written in Mathematica)
and your program was doing large amounts of type-checked run-time code
generation and the resulting programs were trivial then yes. However, that
is unlikely.

> When I think about dynamic type checking, I think about determining the
> actual type of an object referred to by a variable referring to a basetype
> of multiple derived types.  I can imagine circumstances where such a
> feature might be useful in Mathematica.  The example of testing to
> determine if some object represents a "real number" was intended to
> suggest this.


> An integer is a real number, as is a rational number. 

In mathematics, yes.

> That is an 
> "is a" relation in OOP terminology, and thus implies an inheritance
> hierarchy.

Maybe. I don't think that OOP is relevant to this discussion.

> > Because Mathematica is a rewrite system, it is comparatively easy to
> > write interpreters and compilers in it (compared to C, for example).
> I can certainly imagine writing a C++ parser in Mathematica, but that
> makes 
> assumptions about the input.  In particular, that it is written in C++
> which already has a type system defined.

You could define a new language that is a subset of Mathematica's existing
language that is designed to be type checked. You could run programs in
this language either without type checking in the current Mathematica, or
run them through a form of compiler to have them type checked. I think this
is clearly preferable to incorporating a completely different language into

> > As a type
> > system and, more importantly, a type checker is part of a
> > compiler/interpreter, these can be written in Mathematica.
> Yes, but see my previous comment.  You would need a means of expressing
> types in Mathematica in order to check them.

Not necessarily. A type system could be implemented inside the Mathematica
kernel and the user need not know. This could be used to improve
performance "under the hood".

> > You could write a type checking function to type check Mathematica
> > programs. This could mimic an ML-like type system, for example.
> I have wanted to study ML for a long time, but I have to plead virtual
> ignorance here.

There are many such languages (e.g. SML, OCaml, Haskell) that are much
better representations of modern type systems than C++, Java etc.

> > To improve performance, the use of a more refined type system could be
> > entirely automated and invisible to the user.
> One aspect of comparing Mathematica to compiled languages in the area of
> type checking is that compiled languages typically do their type checking
> at compile time.

Yes. In the context of type checking in Mathematica, static vs dynamic type
checking really refers to whether or not the checking can be hoisted from
the algorithm that is being evaluated.

> Yes, dynamic type checking can happen at runtime, but 
> that already assumes certain compile time checking has been performed.

What compile-time checking is required by dynamic type checking?

> Furthermore, dynamic type checking usually incurs a performance hit.


> If 
> there were typechecking in Mathematica, that could mean a test is
> perfromed for every parameter each time a function is evaluated.

Optimising those tests away is premature.

> Ironically, in C++, it appears that functional programming constructs tend
> to be slower than procedural ones.  In Mathematica, the opposite appears
> to be the case.  The same may be true of type checking.

Do you mean that static type checking might slow Mathematica down?

Dr Jon D Harrop, Flying Frog Consultancy Ltd.

  • Prev by Date: Re: Re: Types in Mathematica
  • Next by Date: Re: Types in Mathematica thread
  • Previous by thread: Re: Re: Re: Types in Mathematica
  • Next by thread: Re: Types in Mathematica