axiom-mail
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [fricas-devel] Re: [Axiom-mail] Programming with BTREEs.


From: Ralf Hemmecke
Subject: Re: [fricas-devel] Re: [Axiom-mail] Programming with BTREEs.
Date: Tue, 07 Apr 2009 16:45:56 +0200
User-agent: Thunderbird 2.0.0.19 (X11/20090105)

Yet another instance of why I hate "Any".

... The interpreter seems to like List(Any) more than my specific types. See bolow... :-(

Let's do a little programming ... this is the more complicated form.

Simon, my example is not completely finished since the MyBinaryTree you find below, doesn't have much functionality (except equality), but it is preferable to BinaryTree, because if you construct a tree in MyBinaryTree(I, L) then you have no chance to construct a tree that has inner nodes that are not of type I and leaves of any other type than L. So the code below is an example of how to translate your tree structure into SPAD code.

The input then is

B := MyBinaryTree(List Integer, Symbol)
t: B := [e,[5,1],[[a,[1,1],address@hidden,[1,1],[c,[1,2],address@hidden

The @B is still needed since otherwise the Axiom/FriCAS interpreter tries to construct again something of type List(Any)... Extremely bad
behaviour of the interpreter in my eyes...

The @B is even needed if you add

l: List Symbol := [a,b,c,d,e]
(a,b,c,d,e):=(l 1, l 2, l 3, l 4, l 5)

before the assignment to t.

You have to live with that for the moment, at least you have the chance that adding @B gives a hint to the interpreter of what you actually intended with your input.

Try for example

t: B := [a,b,c]

You get

(5) -> t:B := [a,b,c]

   Cannot convert right-hand side of assignment
   [a,b,c]

      to an object of the type MyBinaryTree(List(Integer),Symbol) of
      the left-hand side.

Seeing such error message in Axiom is quite common, but wait. Think of it for a moment. You just asked to system to create a tree for you (namely an element t of type B) which would have the inner node b. However, b is of type Symbol and inner nodes should not be of type Symbol, but rather of type List(Integer). So the system is completely right throwing that error message at you.

Imagine you are inside a big program and you've put there an assignment like (5). The SPAD compiler will detect that you are doing things where the types do not match and will tell you at compile time (not at runtime).

So here is your code... put it into aaa.spad and in Axiom say

)co aaa.spad

followed by the assignments at the beginning of this mail.

Ralf

---rhxBEGIN aaa.spad
-- Note that I and L must always be different types.
-- MyBinaryTree(Integer, Integer) is forbidden.

COUT==>CoercibleTo(OutputForm)

)abbrev domain MBTREE MyBinaryTree
MyBinaryTree(I: COUT, L: COUT): COUT with
  construct: L -> %
  construct: (L, I, L) -> %
  construct: (%, I, L) -> %
  construct: (L, I, %) -> %
  construct: (%, I, %) -> %
 == add
  N := Union(I, L)
  Rep := BinaryTree N
  construct(l: L): % == binaryTree(l::N)$Rep
  V ==> construct
  construct(l: L, i: I, r: L): % == binaryTree(V l, i, V r)
  construct(l: %, i: I, r: L): % == binaryTree(  l, i, V r)
  construct(l: L, i: I, r: %): % == binaryTree(V l, i,   r)
  construct(l: %, i: I, r: %): % == binaryTree(  l, i,   r)
  coerce(x: %): OutputForm == coerce(x)$Rep
  ((x: %) = (y: %)): Boolean == (x::Rep) =$Rep (y::Rep)
---rhxEND aaa.spad




reply via email to

[Prev in Thread] Current Thread [Next in Thread]