:-module(_,[mmtx/3],[]).
'$applied_packages'([prelude,nonpure,condcomp,assertions,regtypes,nativeprops,rtchecks_shallow,predefres(res_steps),resdefs,resdefs/resources_decl,basicmodes,argnames,expander]).
:-add_clause_trans(expander_tr:expand_clause/4,9910).
:-prop nlilist/1+regtype.
:-prop nlist/1+regtype.
:-entry mmtx(_A,_B,_C):(nlilist(_A),nlilist(_B),var(_C)).
:-check calls mmtx(_A,_B,_C):(nlilist(_A),nlilist(_B),var(_C)).
:-true success mmtx(_A,_B,_C):(nlilist(_A),nlilist(_B),var(_C))=>nlilist(_C).
:-true pred mmtx(_A,_B,_C):(nlilist(_A),nlilist(_B),term(_C))=>(nlilist(_A),nlilist(_B),nlilist(_C)).
:-true pred mmtx(_A,_B,_C):(mshare([[_C]]),var(_C),ground([_A,_B]))=>ground([_A,_B,_C]).
:-checked calls mmtx_i(_A,_B,_C):(nlilist(_A),nlilist(_B),var(_C)).
:-checked success mmtx_i(_A,_B,_C):(nlilist(_A),nlilist(_B),var(_C))=>nlilist(_C).
:-true pred mmtx_i(_A,_B,_C):(nlilist(_A),nlilist(_B),term(_C))=>(nlilist(_A),nlilist(_B),nlilist(_C)).
:-true pred mmtx_i(_A,_B,_C):(mshare([[_C]]),var(_C),ground([_A,_B]))=>ground([_A,_B,_C]).
:-checked calls multiply(_A,_1,_B):(nlilist(_A),nlist(_1)).
:-checked success multiply(_A,_1,_B):(nlilist(_A),nlist(_1))=>nlist(_B).
:-true pred multiply(_A,_1,_B):(nlilist(_A),nlist(_1),term(_B))=>(nlilist(_A),nlist(_1),nlist(_B)).
:-true pred multiply(_A,_1,_B):(mshare([[_B]]),var(_B),ground([_A,_1]))=>ground([_A,_1,_B]).
:-checked calls vmul(_A,_B,Result):(nlist(_A),nlist(_B)).
:-checked success vmul(_A,_B,Result):(nlist(_A),nlist(_B))=>num(Result).
:-true pred vmul(_A,_B,Result):(nlist(_A),nlist(_B),term(Result))=>(nlist(_A),nlist(_B),num(Result)).
:-true pred vmul(_A,_B,Result):(mshare([[Result]]),var(Result),ground([_A,_B]))=>ground([_A,_B,Result]).
:-prop nlilist(_A)+regtype.
:-prop nlist(_A)+regtype.

mmtx(_1,_2,_3) :-
        'mmtx$pre'(_1,_2,_3),
        'mmtx$cls'(_1,_2,_3).
'mmtx$pre'(_1,_2,_3) :-
        reify_check(nlilist(_1),[_1],_5),
        reify_check(nlilist(_2),[_2],_6),
        reify_check(var(_3),[_3],_4),
        _7 is _4/\(_5/\_6),
        _8 is _7,
        _9 is _7,
        _10 is _8\/_9,
        warn_if_false(_10,'CP').
'mmtx$cls'(A,B,C) :-
        mmtx_i(A,B,C).
mmtx_i([],_1,[]).
mmtx_i([V0|Rest],V1,[Result|Others]) :-
        mmtx_i(Rest,V1,Others),
        multiply(V1,V0,Result).
multiply([],_1,[]).
multiply([V0|Rest],V1,[Result|Others]) :-
        multiply(Rest,V1,Others),
        vmul(V0,V1,Result).
vmul([],[],0).
vmul([H1|T1],[H2|T2],Result) :-
        vmul(T1,T2,Newresult),
        Product is H1*H2,
        Result is Product+Newresult.
nlilist([]).
nlilist([X|Xs]) :-
        nlist(X),
        nlilist(Xs).
nlist([]).
nlist([X|Xs]) :-
        num(X),
        nlist(Xs).