Skip to content

RowList operations #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/Type/Row.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ module Type.Row
, RLProxy(..)
, class RowToList
, class ListToRow
, class RowListRemove
, class RowListSet
, class RowListNub
, class RowListAppend
) where

import Type.Equality (class TypeEquals)
import Type.Data.Symbol as Symbol
import Type.Data.Boolean as Boolean

data RProxy (row :: # Type) = RProxy

-- Must not be exported
Expand Down Expand Up @@ -70,3 +78,67 @@ instance listToRowCons
:: ( ListToRow tail tailRow
, RowCons label ty tailRow row )
=> ListToRow (Cons label ty tail) row

-- | Remove all occurences of a given label from a RowList
class RowListRemove (label :: Symbol)
(input :: RowList)
(output :: RowList)
| label input -> output

instance rowListRemoveNil
:: RowListRemove label Nil Nil

instance rowListRemoveCons
:: ( RowListRemove label tail tailOutput
, Symbol.Equals label key eq
, Boolean.If eq
(RLProxy tailOutput)
(RLProxy (Cons key head tailOutput))
(RLProxy output)
)
=> RowListRemove label (Cons key head tail) output

-- | Add a label to a RowList after removing other occurences.
class RowListSet (label :: Symbol)
(typ :: Type)
(input :: RowList)
(output :: RowList)
| label typ input -> output

instance rowListSetImpl
:: ( TypeEquals (Symbol.SProxy label) (Symbol.SProxy label')
, TypeEquals typ typ'
, RowListRemove label input lacking )
=> RowListSet label typ input (Cons label' typ' lacking)

-- | Remove label duplicates, keeps earlier occurrences.
class RowListNub (input :: RowList)
(output :: RowList)
| input -> output

instance rowListNubNil
:: RowListNub Nil Nil

instance rowListNubCons
:: ( TypeEquals (Symbol.SProxy label) (Symbol.SProxy label')
, TypeEquals head head'
, TypeEquals (RLProxy nubbed) (RLProxy nubbed')
, RowListRemove label tail removed
, RowListNub removed nubbed )
=> RowListNub (Cons label head tail) (Cons label' head' nubbed')

-- Append two row lists together
class RowListAppend (lhs :: RowList)
(rhs :: RowList)
(out :: RowList)
| lhs rhs -> out

instance rowListAppendNil
:: TypeEquals (RLProxy rhs) (RLProxy out)
=> RowListAppend Nil rhs out

instance rowListAppendCons
:: ( RowListAppend tail rhs out'
, TypeEquals (RLProxy (Cons label head out')) (RLProxy out) )
=> RowListAppend (Cons label head tail) rhs out