Items tagged with type-checking

Trying to write a generat purpose procedure insteat of having 3 or 4 different named procceduers.

Need to determine the input types of say A,B,C


1) A,B,C could be for the form 3/2 , 5x-7 , x^2-sqrt(x+3)  i.e numbers or formulas

2) A,B,C could be 2 elenent lists [2,3]  , [5x-6y,8x+2] ,[2,sqrt(3)]  i.e 2D points

3) A,B,C could be 3 element lists as above    i.e 3D points

4) A,B,C could be 2D vectors

5) A,B,C could be 3D vectors

A, B, C will not be of mixed types but would be good to check A, B, C for consistency and if not retrun an error

Looking to calculate output based on

If type 1) ......

elif type 2).....



elif type 5)...

end if


end proc



The issue Type check of parameters was resolved using the depends modifier. As far as I can tell, this modifier is not allowed for expected or keyword parameters, though. Thus the issue seems to reemerge for these types of parameters. Consider the following test example:

createModule := proc(V::Vector)
   local dim := LinearAlgebra:-Dimension(V);
      export f,g,h;
      f := proc( x::depends('Vector'(dim))              ) x end proc;
      g := proc( x::expects('Vector'(dim)) := something ) x end proc;
      h := proc({x::        'Vector'(dim)  := something}) x end proc;
   end module
end proc:
createModule(Vector(4)):-f(    Vector(4));
createModule(Vector(4)):-g(    Vector(4));
createModule(Vector(4)):-h(x = Vector(4));

The function f is just a restatement of the already resolved issue, compare the above link, while the functions g and h are for the expected and keyword parameter cases, respectively. The problem remains the same: the variable dim is not evaluated for g and h. What to do? Does there exist a solution equally satisfactory as the one for f?

Consider the following two test procedures for creation of the same module:

createModule1 := proc(dim::posint)
        export det;
        det := (x::Matrix(1..dim,1..dim)) -> Determinant(x);
    end module
end proc:


createModule2 := proc(A::Matrix(square))
    local dim;
    dim := RowDimension(A);
        export det;
        det := (x::Matrix(1..dim,1..dim)) -> Determinant(x);
    end module
end proc:

as well as the following code lines calling these:

createModule1(       2 ):-det(IdentityMatrix(2));

The first line executes unproblematically, while for the second line an error results concerning the dimensionality check 1..dim,1..dim of the matrix. Why is dim available/initialized in the first version, while not in the second?

I do the following:

z := x + y;
type(z, specfunc(name, `+`));

However the result is 'false'. How to correct my type checking?

I have two problems with type declarations in procedures. They seem to be related, so I'll ask both questions at once.

1. One problem is that I make this sort of declaration.


where n could be an integer or a float, but not negative nor complex. I'm not sure what's wrong with the code below: the type-checking fails, but I don't understand why. I followed the template laid out by Preben here:

Page 1 of 1