 
 : set-indices ( object sequence matrix -- )
     '[ _ set-index ] with each ; inline
-    
 
 : matrix-map ( matrix quot -- )
     '[ _ map ] map ; inline

: lower-matrix-indices ( matrix -- matrix' )
    dimension-range [ head-slice >array ] 2map concat ;


: make-lower-matrix ( object m n -- matrix )
    zero-matrix [ lower-matrix-indices ] [ set-indices ] [ ] tri ;



GENERIC: square-rows ( object -- matrix )
M: integer square-rows iota square-rows ;
M: sequence square-rows
    >array [ length ] keep [ clone ] curry { } replicate-as ;

GENERIC: square-cols ( object -- matrix )
M: integer square-cols iota square-cols ;
M: sequence square-cols
    [ length ] keep [ <array> ] with { } map-as ;

: make-matrix-with-indices ( m n quot -- matrix )
    [ [ iota ] bi@ ] dip '[ @ ] cartesian-map ; inline

: null-matrix? ( matrix -- ? ) empty? ; inline

: well-formed-matrix? ( matrix -- ? )
    [ t ] [
        [ ] [ first length ] bi
        '[ length _ = ] all?
    ] if-empty ;

: dim ( matrix -- pair/f )
    [ 2 0 <array> ]
