Table

Table is a list of dicts, where each dict represents a row, with keys corresponding to column names and values to the respective entries in the table
Imports
open Array;
Table of Contents

Types

Table

(type)
type Table

Row

(type)
type Row

Functions

from_csv

Reads a CSV file and returns a Table.
def from_csv (path : String) : Table

to_csv

Writes a Table to a CSV file.
def to_csv (table : Table) (path : String) : Unit

columns

Returns the list of column names in the table.
def columns (table : Table) : (Array String)

select

Selects a subset of columns from the table.
def select (table : Table) (1 columns : (Array ({s : String | hasColumn table s}))) : Table

filter

Filters rows in the table using a predicate function.
def filter (table : Table) (predicate : (row : Row) → Bool) : Table

map_column

Applies a function to a column in all rows.
def map_column (table : Table) (column : String | intColumn table column) (f : (a : Int) → Int) : Table

summary

Applies a summary function to a column in the table.
def summary (table : Table) (column : String | intColumn table column) (f : (1 a : (Array Int)) → b) : b

group_by

Groups the table by a column, returning a list of tables (one per group). - For each unique value in the specified column, creates a group (table) of all rows with that value.
def group_by (table : Table) (column : String | intColumn table column || stringColumn table column) : (Array Table)

pivot

Pivots a table: turns unique values from one column into new columns. - For each unique value in the `index` column of the input table, create a row in the output table. - For each unique value in the `columns` column, create a new column in the output table. - For each original row, place its value from the `values` column at the intersection defined by its `index` and `columns` values.
def pivot (table : Table) (index : String | intColumn table index || stringColumn table index) (columns : String | stringColumn table columns) (values : String | hasColumn table values) : Table

melt

Melts a table: turns columns into rows (unpivot). - For each row in the input table, and for each column in value_vars, creates a new row. - The new row contains all id_vars, the name of the melted column, and its value.
def melt (table : Table) (1 id_vars : (Array ({s : String | hasColumn table s}))) (1 value_vars : (Array ({s : String | hasColumn table s}))) (var_name : String) (value_name : String) : Table

nrow

Number of rows in the table.
def nrow (borrow table : Table) : {n : Int | n ≥ 0 && n = rowCount table}

ncol

Number of columns in the table.
def ncol (borrow table : Table) : Int

cell

The integer cell at row `r` (0-based) of column `col` -- DACE's spatial cell reference.
def cell (borrow table : Table) (r : Int | 0 ≤ r && r < rowCount table) (col : String | intColumn table col) : Int

mutate

Add a new column `newcol` whose value in each row is `f` applied to that row. This is the workhorse of data completion: filling a column with a formula over the other columns (e.g. `mutate t "total" (fun row => ...)`).
def mutate (table : Table) (newcol : String) (f : (row : Row) → Int) : {r : Table | rowCount r = rowCount table && intColumn r newcol}

sum_col

Sum of the integer column `column`.
def sum_col (table : Table) (column : String | intColumn table column) : Int

mean_col

Integer mean (floor) of the column `column`.
def mean_col (table : Table) (column : String | intColumn table column) : Int

max_col

Maximum of the integer column `column`.
def max_col (table : Table | rowCount table > 0) (column : String | intColumn table column) : Int

min_col

Minimum of the integer column `column`.
def min_col (table : Table | rowCount table > 0) (column : String | intColumn table column) : Int

count

Number of rows (COUNT).
def count (borrow table : Table) : {n : Int | n ≥ 0 && n = rowCount table}

arrange

Sort the rows in ascending order of `column` (arrange).
def arrange (table : Table) (column : String | intColumn table column) : Table

head

The first `n` rows (head / take).
def head (table : Table) (n : Int) : Table

cumsum

Add a column `newcol` holding the running sum of `column` down the rows -- the cumulative-total completion pattern (e.g. a balance column).
def cumsum (table : Table) (column : String | intColumn table column) (newcol : String) : {r : Table | rowCount r = rowCount table && intColumn r newcol}

join

Inner-join `left` and `right` on equal values of key column `on`.
def join (left : Table) (right : Table) (on : String | hasColumn left on && hasColumn right on) : Table

spread

`spread` is `pivot`: long -> wide.
def spread (table : Table) (index : String | intColumn table index || stringColumn table index) (columns : String | stringColumn table columns) (values : String | hasColumn table values) : Table

gather

`gather` is `melt`: wide -> long.
def gather (table : Table) (1 id_vars : (Array ({s : String | hasColumn table s}))) (1 value_vars : (Array ({s : String | hasColumn table s}))) (var_name : String) (value_name : String) : Table

dispose

Release a table that retains transferred array storage.
def dispose (1 table : Table) : Unit

has_column

def has_column (borrow table : Table) (name : String) : {b : Bool | b = hasColumn table name}

is_int_column

def is_int_column (borrow table : Table) (name : String) : {b : Bool | b = intColumn table name && (b = false || hasColumn table name)}

is_string_column

def is_string_column (borrow table : Table) (name : String) : {b : Bool | b = stringColumn table name && (b = false || hasColumn table name)}

Uninterpreted

Functions declared as def f ... = uninterpreted: only their signature is known to the verifier; they have no body.

rowCount

uninterpreted
def rowCount : (table : Table) → Int

hasColumn

uninterpreted
def hasColumn : (table : Table) → (name : String) → Bool

intColumn

uninterpreted
def intColumn : (table : Table) → (name : String) → Bool

stringColumn

uninterpreted
def stringColumn : (table : Table) → (name : String) → Bool