Question: Application of how to test types in a module?

 About 6 years ago I aked  a question on testing types in a package. How to Test Input Typed to a Procedure to Determine what to do? - MaplePrimes.  @Carl Love supplied the very good code below. I never used the answer back then as I really didn't understand what to do with it. I have a somewhat of a better idea now and I really need to be able to use this in general..

 Basically the code checks that all the inputs are of the same type and order (which is what the original question specified).

 Q1:- I now need to check  a mixed input type

         A:: a 2 list , B a 3 list   

          or 

           A:: a 2 Vector, and B a 3 Vector.

         etc

       I tried modifying the ModuleApply part of the code at the end, but that either causes errors or kernal looses connection.

Q2:-  What is a good way to handle different numbers of inputs to procedures (exported) here eg Proc1(a), Proc2(a,b), ....,                   ProcN(a1,...,aN)

Q3:-    Would the package exports of the module be the procedures inside the Mydispatch? Or does all of MyModule sit inside                the  package module?

  I have three different packages I would like to apply this to in general.

restart

 

MyModule:= module()
uses TT= TypeTools;
global _T1, _T2L, _T2V, _T3L, _T3V, _MyType;
local
     MyTypes:= {_T1, _T2L, _T2V, _T3L, _T3V},
     AllMyTypes:= MyTypes union {_MyType},

     ModuleLoad:= proc()
     local
          g, #iterator over module globals
          e
     ;
          #op([2,6], ...) of a module is its globals.
          for g in op([2,6], thismodule) do
               e:= eval(g);
               if g <> e and e in AllMyTypes then
                    error "The name %1 must be globally available.", g
               end if
          end do;
          TT:-AddType(_T1, algebraic);
          TT:-AddType(_T2V, 'Vector(2, algebraic)');
          TT:-AddType(_T2L, [algebraic $ 2]);
          TT:-AddType(_T3V, 'Vector(3, algebraic)');
          TT:-AddType(_T3L, [algebraic $ 3]);
          TT:-AddType(_MyType, MyTypes)
     end proc,

     ModuleUnload:= proc()
     local T;
          for T in AllMyTypes do TT:-RemoveType(T) end do
     end proc,

     MyDispatch:= overload([
          proc(A::_T1, B, C)
          option overload;
          local r:= "A, B, C are T1."; #unnecessary; just an example.
               #statements to process this type
          end proc,

          proc(A::_T2L, B, C)
          option overload;
          local r:= "A, B, C are T2L.";
               #
          end proc,

          proc(A::_T2V, B, C)
          option overload;
          local r:= "A, B, C are T2V.";
               #
          end proc,

          proc(A::_T3L, B, C)
          option overload;
          local r:= "A, B, C are T3L.";
               #         
          end proc,

          proc(A, B, C)
          local r:= "A, B, C are T3V.";
               #
          end proc,
#
#I added this
#
          proc(A, B)
          local r:= "A, B, are mixed.";
               #
          end proc
     ]),
#
# I have have  added  'Or'(....(A::_T2L,B::_T3L)
#
     ModuleApply:= proc(
          A::'Or'(And(
               _MyType,
               satisfies(A-> andmap(T-> A::T implies B::T and C::T, MyTypes) )
          ),

          (A::_T2L,B::_T3L)),
          B::_MyType, C::_MyType
     )
          MyDispatch(args)
     end proc
;
     ModuleLoad()    
end module:
 

#Example usage:
#

x:=[9,4];
y:=[5,7];
z:=[1,9];                                 
MyModule(x,y,z);

[9, 4]

[5, 7]

[1, 9]

"A, B, C are T2L."

x:=[9,4];
y:=[5,6,7];
z:=p;                                 
MyModule(x,y);  #Looses kernel connection

[9, 4]

[5, 6, 7]

p

Download Q_19-11-22_Module_Test_Types.mw

Please Wait...