# The variables are explained in the file gap/data.g
# name := number;

IF_maple := 90;


##############################################################################
# Administrative data


IF_SUBPACKAGE_VERSION[IF_maple] := "dev";

IF_CAS_DESCRIPTION[IF_maple] :=
[ "Maple",
"Maple is a general-purpose commercial computer algebra system",
"http://www.maplesoft.com/" ];


##############################################################################
# Low level data


IF_EXEC_NAME[IF_maple] := "maple";
#IF_EXEC_FULL_PATH[IF_maple] := "";
IF_EXEC_OPTIONS[IF_maple] := [ "-t" ];
# With Maple version >= 10 use the following:
# IF_EXEC_OPTIONS[IF_maple] := [ "-t", "-u" ];


IF_QUIT_COMMAND[IF_maple] := ";\nquit;";
IF_DISPLAY_VERSION[IF_maple] := "interface(version);";


# use the following with Maple version >= 6
IF_FIRST_COMMAND[IF_maple] := "interface( echo=0, screenwidth=infinity, errorcursor=false );";
# use the following with Maple version = 5.5
# IF_FIRST_COMMAND[IF_maple] := "interface( echo=0, screenwidth=9999, errorcursor=false );";
# use the following with Maple version < 5.5
# IF_FIRST_COMMAND[IF_maple] := "interface( echo=0, screenwidth=9999 );";


IF_FIRST_PROMPT[IF_maple] := "#-->";
IF_PROMPTS[IF_maple] := [ "\r#-->" ];
IF_FUNC_REMOVE_PROMPT[IF_maple] := function( OUT )
    local p;
    p := PositionSublist( OUT, "Warning" );
    if p <> fail  then
        Print( "#I  Warning in Maple: ", OUT{[ p .. PositionSublist( OUT, "\n", p ) ]} );
    fi;
    p := PositionSublist( OUT, "\n\033[0;1m" );
    if p <> fail  then
        Error( "in Maple: ", OUT{[ p .. PositionSublist( OUT, "\r#-->", p ) ]} );
    fi;
    # return ReplacedString( OUT, IF_PROMPTS[IF_maple][1], "" );
    # below, use ";" instead of "\n", with Maple version < 5.5
    return OUT{[ PositionSublist( OUT, "\n" ) + 1 .. Length( OUT ) - Length( IF_PROMPTS[IF_maple][1] ) -1 ]};
end;
IF_IS_THERE_PROMPT[IF_maple] := true;


##############################################################################
# High level data


IF_ASSIGN[IF_maple] := ":=";
IF_END_COMMAND[IF_maple] := ";";
IF_END_COMMAND_NO_OUTPUT[IF_maple] := ":";
IF_FUNC_TYPE[IF_maple] := "whattype";
IF_TYPE_IS_QUOTED[IF_maple] := false;

IF_FUNC_DISPLAY[IF_maple] := "";

IF_STRING_ARE_QUOTED[IF_maple] := true;
IF_FUNC_STRING[IF_maple] := "proc(obj) convert(obj, string) end proc";
IF_PROHIBITED_CHARS[IF_maple] := Union( [ 0 .. 31 ], [ 127, 128, 254, 255 ] ); 


##############################################################################
# data types data


# ?whattype



# IF_MAPLE__X := Indeterminate( Rationals, "_X" : new );
IF_MAPLE__X := Indeterminate( Rationals );



IF_DATA_TYPES[IF_maple] := rec(

# whattype basic types


\`\*\` := [
"Arithmetic operators +, -, *, /, ^",
,
,
function( obj, name ) return IF_Maple_op( IF_maple, obj, name, \* ); end
],


\`\+\` := [
"Arithmetic operators +, -, *, /, ^",
,
,
function( obj, name ) return IF_Maple_op( IF_maple, obj, name, \+ ); end
],


\`\.\` := [
"operator for non-commutative or dot product multiplication",
,
,
function( obj, name ) return IF_Maple_op( IF_maple, obj, name, \* ); end
],


\`\.\.\` := [
"Expressions of Type range",
obj -> IsRange( obj ) and IsRangeRep( obj ) and obj[2] - obj[1] = 1,
obj -> Concatenation( String( obj[1] ), " .. ", String( obj[Length( obj )] ) ),
function( obj, name ) 
    return IF_Maple_op( IF_maple, obj, name, function( x, y ) return [ x .. y ]; end ); 
end
],


\`\:\:\` := [],

\`\<\` := [],

\`\<\=\` := [],

\`\<\>\` := [],

\`\=\` := [],


\`\^\` := [
"Arithmetic operators +, -, *, /, ^",
,
,
function( obj, name ) return IF_Maple_op( IF_maple, obj, name, \^ ); end
],


\`and\` := [],

array := [],


float := [
"Floating-point Numbers and Their Constructors",
,
,
IdFunc # Floating-point Numbers are not supported in Gap
],


fraction := [
"Fractions and the Fraction Constructor",
,
,
function( obj, name ) return IF_Maple_op( IF_maple, obj, name, \/ ); end
],


\function := [],

hfarray := [],

indexed := [],


integer := [
"Integers and the Integer Constructor",
IsInt,
String,
Int
],


\`not\` := [],

\`or\` := [],

procedure := [],

series := [],


string := [
"Strings",
IsString and IsStringRep,
IF_ParseGapString,
IF_EvalString
# use the following instead of IF_EvalString, with Maple version < 5.5
# function ( obj, name )
#     return IF_ConvertToGap( IF_maple, obj, "symbol" , name );
# end
],


symbol := [
"Names",
,
,
function( obj, name )
    local  type;

    # polynom
    type := IF_LowLevelInterface( IF_maple, "", "type", [ name, "polynom" ] );
    if type = "true" and obj = "_X"  then
        return IF_MAPLE__X;
    elif type = "true" and obj{[ 1 .. 2 ]} = "x_" and ForAll( obj{[ 3 .. Length( obj ) ]}, IsDigitChar )  then
        return Indeterminate( Rationals, Int( obj{[ 3 .. Length( obj ) ]} ) );
    fi;

    # boolean
    type := IF_LowLevelInterface( IF_maple, "", "type", [ name, "boolean" ] );
    if type = "true"  then
        if obj = "true"  then
            return true;
        elif obj = "false"  then
            return false;
        elif obj = "FAIL"  then
            return fail;
        fi;
    fi;

    return obj;

end
],


table := [],

uneval := [],

Array := [],


Matrix := [
"matrices",
obj -> IsMatrix( obj ) and ForAll( obj, x ->
    ForAll( x, y -> IF_Type(IF_maple, y) <> fail ) ),
function( obj )
    local  str, dim, i, j;
    dim := DimensionsMat( obj );
    str := "Matrix( ";
    Append( str, String( dim[1] ) );
    Append( str, ", " );
    Append( str, String( dim[2] ) );
    Append( str, ", [" );
    for i  in [ 1 .. dim[1] ]  do
        Append( str, "\n[" );
        for j  in [ 1 .. dim[2] ]  do
            Append( str, IF_ConvertFromGap( IF_maple, obj[i][j] ) );
            if not j = dim[2]  then
                Append( str, "," );
            fi;
            if j mod 50 = 0  then
                Append( str, "\n" );
            fi;
        od;
        if not i = dim[1]   then
            Append( str, "]," );
        else
            Append( str, "]" );
        fi;
    od;
    Append( str, "] )" );
    return str;
end,
function ( obj, name )
    local  dim, i, ii, mat, subname;
    dim := IF_LowLevelInterface( IF_maple, "", "op", [ "1", name ] );
    dim := List( SplitString( dim, ",", " " ), Int );
    mat := [  ];
    for i  in [ 1 .. dim[1] ]  do
        mat[i] := [  ];
        for ii  in [ 1 .. dim[2] ]  do
            subname := Concatenation( name, "_elm" );
            IF_LowLevelInterface( IF_maple, subname, "", 
               Concatenation( name, "[", String( i ), ",", String( ii ), "]" ) );
    
            mat[i][ii] := IF_GetCasOutput( IF_maple, subname );
        od;
    od;
    return mat;
end
],


Vector\[column\] := [],


Vector\[row\] := [
"Vector - construct a Vector",
obj -> IsRowVector( obj ) and ForAll( obj, y -> IF_Type(IF_maple, y) <> fail ),
function( obj )
    local  str, dim, i, j;
    dim := Length( obj );
    str := "Vector[row]( ";
    Append( str, String( dim ) );
    Append( str, ", [" );
    for i  in [ 1 .. dim ]  do
        Append( str, IF_ConvertFromGap( IF_maple, obj[i] ) );
        if not i = dim  then
            Append( str, "," );
        fi;
        if i mod 50 = 0  then
            Append( str, "\n" );
        fi;
    od;
    Append( str, "] )" );
    return str;
end,
function ( obj, name )
    local  dim, i, ii, vec, subname;
    dim := IF_LowLevelInterface( IF_maple, "", "op", [ "1", name ] );
    dim := Int( dim );
    vec := [  ];
    for i  in [ 1 .. dim ]  do
            subname := Concatenation( name, "_elm" );
            IF_LowLevelInterface( IF_maple, subname, "",
               Concatenation( name, "[", String( i ), "]" ) );
    
            vec[i] := IF_GetCasOutput( IF_maple, subname );
    od;
    return vec;
end
],


list := [
"Sets and Lists",
obj -> IsDenseList( obj ) and ForAll( obj, y -> IF_Type(IF_maple, y) <> fail ),
obj -> IF_ParseGapList( IF_maple, obj, "[", "]" ),
function( obj, name ) return IF_Maple_op( IF_maple, obj, name, Add ); end
],


set := [
"Sets and Lists",
obj -> IsDenseList( obj ) and ForAll( obj, y -> IF_Type(IF_maple, y) <> fail ),
obj -> IF_ParseGapList( IF_maple, obj, "{", "}" ),
function( obj, name ) return IF_Maple_op( IF_maple, obj, name, Add ); end
],


exprseq := [
"Expression Sequences",
obj -> IsDenseList( obj ) and ForAll( obj, y -> IF_Type(IF_maple, y) <> fail ),
obj -> IF_ParseGapList( IF_maple, obj, "op(", ")" ),
function( obj, name )
    IF_LowLevelInterface( IF_maple, name, "", Concatenation( "[ ", name, " ]" ) );
    return IF_Maple_op( IF_maple, Concatenation( "[", obj, "]" ), name, Add  );
end
],



# Other types


boolean := [
"Boolean expressions",
obj -> IsBool( obj ) and ( obj = true or obj = false ),
String,
# see symbol
],


rational := [
"rational",
IsRat,
String,
],


complex := [
"Complex Numbers and the Complex Constructor",
IsGaussRat,
function ( obj )
    local  real, imaginary;
    real := x -> (x + ComplexConjugate( x )) / 2;
    imaginary := x -> (x - ComplexConjugate( x )) * -1 / 2 * E( 4 );
# E := proc(n) description "returns  the primitive <n>-th root of unity"; exp(2*I*Pi/n) end proc
    return Concatenation( "( ", IF_ConvertFromGap( IF_maple, real( obj ) ), 
        " + I*(", IF_ConvertFromGap( IF_maple, imaginary( obj ) ), ") )" );
end,
function ( obj, name )
    local  type;
    type := IF_LowLevelInterface( IF_maple, "", "type", [ name, "imaginary" ] );
    if type = "true"  then
        return E( 4 ) * IF_Maple_op( IF_maple, obj, name, fail );
    else
        return IF_Maple_op( IF_maple, obj, name, function ( x, y ) return x + E( 4 ) * y; end );
    fi;
end
],


complex\(extended_numeric\) := [
"some type as 'complex', used from Maple 9.5 on",
,
,
function ( obj, name )
    return IF_ConvertToGap( IF_maple, obj, "complex" , name );
end
],


extended_numeric := [
"infinity, -infinity, or undefined",
IsInfinity,
String,
IF_EvalString
],


polynom := [ 
"Polynomials",
IsRationalFunction and IsPolynomial,
function ( pol )
    local  str, mons, k, mon, m, len;
    if IsZero( pol )  then
        return "0";
    fi;
    str := "(";
    mons := ExtRepPolynomialRatFun( pol );
    k := 1;
    len := 0;
    while k <= Length( mons )  do
        if Length( str ) - len >= 500  then
            Append( str, "\n" );
            len := Length( str );
        fi;
        if k > 1  then
            Add( str, '+' );
        fi;
        Add( str, '(' );
        Append( str, IF_ConvertFromGap( IF_maple, mons[k + 1] ) );
        Add( str, ')' );
        mon := mons[k];
        m := 1;
        while m <= Length( mon )  do
            Append( str, "*x_" );
            Append( str, String( mon[m] ) );
            Append( str, "^" );
            Append( str, String( mon[m + 1] ) );
            m := m + 2;
        od;
        k := k + 2;
    od;
    Add( str, ')' );
    return str;
end,
# see symbol
],


ratpoly := [
"rational polynomials (rational functions)",
obj -> IsRationalFunction( obj ) and ForAll( [ NumeratorOfRationalFunction( obj ),
    DenominatorOfRationalFunction( obj ) ], y -> IF_Type(IF_maple, y) <> fail ),
obj -> Concatenation( IF_ConvertFromGap( IF_maple, NumeratorOfRationalFunction( obj ) ), "/",
    IF_ConvertFromGap( IF_maple, DenominatorOfRationalFunction( obj ) ) ),

],


SDMPolynom := [
"Sparse Distributed Multivariate Polynomial",
IsRationalFunction and IsPolynomial,
obj -> Concatenation( "SDMPolynom( ", IF_ConvertFromGap( IF_maple, obj ), ", ", 
        IF_ConvertFromGap( IF_maple, IndeterminatesOfPolynomialRing(DefaultRing(obj)) ), ")" ),
function ( obj, name )
    IF_LowLevelInterface( IF_maple, name, "convert", [ name, "polynom" ] );
    return IF_GetCasOutput( IF_maple, name );
end
]





);

