Skip to content

Commit

Permalink
Updated relational algebra example to new syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
duvenaud committed Jan 6, 2021
1 parent 4fe389a commit de1fbd3
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions examples/relational_algebra.dx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'# The Relational Algebra
Unlike other languages which have bespoke "table" datatypes, in principle
Dex's record system should natively allow the manipulation of tables of records
Unlike other languages which have bespoke "table" or "dataframe" datatypes,
Dex's record system allows the manipulation of tables of records
to do the sorts of data-munging usually done by packages such as pandas.

'This is an attempt to express the basic operations on datasets
Expand Down Expand Up @@ -75,7 +75,7 @@ automatically, but I don't know what it is.
3. As in the other examples, the resulting records should be flattened.


def inner_join_one (_: Eq b) ?=>
def inner_join_one [Eq b]
-- Would rather have an Eq instance for just the inner
-- type of b, not its named type.
(left_iso : Iso ({&} & a1) (b & c1))
Expand All @@ -89,7 +89,7 @@ def inner_join_one (_: Eq b) ?=>
then Just (left_b, left_c, right_c)
else Nothing

def inner_join (_: Eq b) ?=>
def inner_join [Eq b]
(left_iso : Iso ({&} & a1) (b & c1))
(right_iso: Iso ({&} & a2) (b & c2))
(left_tab:n=>a1) (right_tab:m=>a2) : List (b & c1 & c2) =
Expand All @@ -106,17 +106,15 @@ by automatically generating equality instances for records,
or by somehow allowing an unpack operation to directly access
the data held by any field.

-- This general List equality instance should probably go in the prelude.
@instance
def listEq (_: Eq a)?=> : Eq (List a) =
MkEq \(AsList n1 xs) (AsList n2 ys).
-- This should probably go in the prelude.
instance [Eq a] Eq (List a)
(==) = \(AsList n1 xs) (AsList n2 ys).
if n1 == n2
then all for i. xs.i == ys.((ordinal i)@_)
then all for i. xs.i == ys.(unsafeFromOrdinal _ (ordinal i))
else False

-- This bespoke equality instance should be automatically generated.
@instance
def nameEq (_: Eq a)?=> : Eq {name: a} =
MkEq $ \r1 r2. (getAt #name r1) == (getAt #name r2)
instance [Eq a] Eq {name: a}
(==) = \r1 r2. (getAt #name r1) == (getAt #name r2)

inner_join #&name #&name table1 table2

0 comments on commit de1fbd3

Please sign in to comment.