Definition flatten := foldr cat (Nil T).Definition shape := map (@size T).Fixpoint reshape sh s :=
  if sh is n :: sh' then take n s :: reshape sh' (drop n s) else [::].Definition flatten_index sh r c := sumn (take r sh) + c.Definition reshape_offset sh i := i - sumn (take (reshape_index sh i) sh).

Lemma nth_flatten x0 ss i (r := reshape_index (shape ss) i) : nth x0 (flatten ss) i = nth x0 (nth [::] ss r) (reshape_offset (shape ss) i).