"Everything is an expression" is a popular citation from many Mathematica guidebooks. So, what is type in Mathematica? How does it relate to common types from Haskell, for example?
I did some simulation of dependent types:
DependentType::illegal= "Value is illegal for dependent types with constraints";
FixedSizedVector[n_?(Positive[#]&)] :=
Module[{type = Symbol["Vector" <> ToString[n]]},
type[dat_] := Message[DependentType::illegal] /; VectorQ[dat] && Length[dat] != n;
type
];
types =
Map[
FixedSizedVector[#]&,
Range[1, 25]
];
I have received an array types
of symbols that are constrained with pattern checking rules. Is it a family of types?
Another point is Head
replacing. For example, for list Range[1, 5]
I can just type Plus @@ Range[1, 5]
and get Integer[15]
. From the point of view of type, what is Apply
?
Answer
The nearest Mathematica has to "types" are Heads of expressions that are Atoms. For example:
Through[{AtomQ, Head}[2]]
{True, Integer}
Through[{AtomQ, Head}[2 + I]]
{True, Complex}
Through[{AtomQ, Head}["cat"]]
{True, String}
and so on...
There are also somewhat different "types" in the context of Compile.
Comments
Post a Comment