getTableLength (Table name tys fields)
    | length tys == 2 = if all isField fields
                            then Table1d (length fields)
                            else unsupported " 1d table contains rows"
    | length tys == 3 = let r = map getElemRow fields
                         in if all (== (head r)) r
                               then Table2d (length fields) (head r)
                               else unsupported " row not of same size"
    | otherwise       = unsupported ""
         where unsupported r = error ("unsupported table " ++ name ++ " length " ++ show (length tys) ++ r)
               isAllFields = all isField

               getElemRow (TableRow f)
                    | all isField f = length f
                    | otherwise     = unsupported (" row contains rows")

               isField (TableField _) = True
               isField _              = False
getTableLength (Table name tys fields)
    | length tys == 2 = if all isField fields
                            then Table1d (length fields)
                            else unsupported " 1d table contains rows"
    | length tys == 3 = let r = map getElemRow fields
                         in if all (== (head r)) r
                               then Table2d (length fields) (head r)
                               else unsupported " row not of same size"
    | otherwise       = unsupported ""
         where unsupported r = error ("unsupported table " ++ name ++ " length " ++ show (length tys) ++ r)
               isAllFields = all isField

               getElemRow (TableRow f)
                    | all isField f = length f
                    | otherwise     = unsupported (" row contains rows")

               isField (TableField _) = True
               isField _              = False
getTableLength (Table name tys fields)
    | length tys == 2 = if all isField fields
                            then Table1d (length fields)
                            else unsupported " 1d table contains rows"
    | length tys == 3 = let r = map getElemRow fields
                         in if all (== (head r)) r
                               then Table2d (length fields) (head r)
                               else unsupported " row not of same size"
    | otherwise       = unsupported ""
         where unsupported r = error ("unsupported table " ++ name ++ " length " ++ show (length tys) ++ r)
               isAllFields = all isField

               getElemRow (TableRow f)
                    | all isField f = length f
                    | otherwise     = unsupported (" row contains rows")

               isField (TableField _) = True
               isField _              = False