 <PRIVATE
 
 : cut-point ( seq -- n )
-    [ last ] keep [ [ > ] keep swap ] find-last drop nip ; inline
+    [ last-unsafe ] keep [ [ > ] keep swap ] find-last drop nip ; inline
 
 : greater-from-last ( n seq -- i )
-    [ nip ] [ nth ] 2bi [ > ] curry find-last drop ; inline
+    [ nip ] [ nth-unsafe ] 2bi [ > ] curry find-last drop ; inline
 
 : reverse-tail! ( n seq -- seq )
-    [ swap 1 + tail-slice reverse! drop ] keep ; inline
+    [ swap 1 + (tail) <slice-unsafe> reverse! drop ] keep ; inline
 
 : (next-permutation) ( seq -- seq )
     dup cut-point [
         swap [ greater-from-last ] 2keep
-        [ exchange ] [ reverse-tail! nip ] 3bi
+        [ exchange-unsafe ] [ reverse-tail! nip ] 3bi
     ] [ reverse! ] if* ;
 
 HINTS: (next-permutation) array ;


<PRIVATE

: permutations-quot ( seq quot -- seq quot' )
    [ [ permutation-iota ] [ length <iota> >array ] [ ] tri ] dip
    '[ drop _ [ _ nths-unsafe @ ] keep next-permutation drop ] ; inline

PRIVATE>

<PRIVATE

: should-swap? ( start curr seq -- ? )
    [ nipd nth-unsafe ] [ <slice-unsafe> member? not ] 3bi ; inline

:: unique-permutations ( ... seq i n quot: ( ... elt -- ... ) -- ... )
    i n >= [
