@@ -1,8 +1,10 @@
1{-# language AllowAmbiguousTypes #-}
1{-# language AllowAmbiguousTypes #-}
2{-# language DataKinds #-}
2{-# language DataKinds #-}
3+{-# language FlexibleContexts #-}
3{-# language FlexibleInstances #-}
4{-# language FlexibleInstances #-}
4{-# language LambdaCase #-}
5{-# language LambdaCase #-}
5{-# language MultiParamTypeClasses #-}
6{-# language MultiParamTypeClasses #-}
7+{-# language PolyKinds #-}
6{-# language RankNTypes #-}
8{-# language RankNTypes #-}
7{-# language ScopedTypeVariables #-}
9{-# language ScopedTypeVariables #-}
8{-# language StandaloneKindSignatures #-}
10{-# language StandaloneKindSignatures #-}
@@ -12,8 +14,8 @@
12{-# language UndecidableInstances #-}
14{-# language UndecidableInstances #-}
14module Rel8.Generic.Table.ADT
16module Rel8.Generic.Table.ADT
15− ( GADTTable (fromGADTColumns, toGADTColumns)
17+ ( GTableADT, GColumnsADT, gfromColumnsADT, gtoColumnsADT, gtableADT
16− , GADTColumns
18+ , GToExprsADT, gfromResultADT, gtoResultADT
@@ -30,156 +32,295 @@ import Prelude hiding ( null )
32import Rel8.FCF ( Eval, Exp )
34import Rel8.FCF ( Eval, Exp )
33−import Rel8.Generic.Table ( GTable, GColumns, fromGColumns, toGColumns )
35+import Rel8.Generic.Table.Record
36+ ( GTable, GColumns, gtable
37+ , GToExprs, gfromResult, gtoResult
34import Rel8.Schema.Context ( Col( Result ) )
39import Rel8.Schema.Context ( Col( Result ) )
35−import Rel8.Schema.Context.Label ( labeler, unlabeler )
40+import Rel8.Schema.Context.Label ( HLabelable, hlabeler, labeler, unlabeler )
41+import Rel8.Schema.Context.Result ( null, nullifier, unnullifier )
36−import Rel8.Schema.HTable ( HTable )
42+import Rel8.Schema.HTable ( HTable, hmap )
37import Rel8.Schema.HTable.Label ( HLabel, hlabel, hunlabel )
43import Rel8.Schema.HTable.Label ( HLabel, hlabel, hunlabel )
38import Rel8.Schema.HTable.Nullify ( HNullify, hnulls, hnullify, hunnullify )
44import Rel8.Schema.HTable.Nullify ( HNullify, hnulls, hnullify, hunnullify )
39import Rel8.Schema.HTable.Product ( HProduct( HProduct ) )
45import Rel8.Schema.HTable.Product ( HProduct( HProduct ) )
40import Rel8.Schema.HTable.Type ( HType( HType ) )
46import Rel8.Schema.HTable.Type ( HType( HType ) )
41import qualified Rel8.Schema.Kind as K
47import qualified Rel8.Schema.Kind as K
48+import Rel8.Schema.Null ( Nullify )
49+import Rel8.Schema.Spec ( Spec( Spec ), SSpec )
42import Rel8.Schema.Result ( Result )
50import Rel8.Schema.Result ( Result )
43−import Rel8.Table ( null, nullifier, unnullifier )
44import Rel8.Type.Tag ( Tag( Tag ) )
51import Rel8.Type.Tag ( Tag( Tag ) )
47import Data.Text ( pack )
54import Data.Text ( pack )
50−type GADTColumns
57+type GColumnsADT
51 :: (Type -> Exp K.HTable)
58 :: (Type -> Exp K.HTable)
52 -> (Type -> Type) -> K.HTable
59 -> (Type -> Type) -> K.HTable
53−type family GADTColumns _Columns rep where
60+type family GColumnsADT _Columns rep where
54− GADTColumns _Columns (M1 D _ rep) =
61+ GColumnsADT _Columns (M1 D _ rep) =
55− GADTColumns' _Columns (HLabel "tag" (HType Tag)) rep
62+ GColumnsADT' _Columns (HLabel "tag" (HType Tag)) rep
58−type GADTColumns'
65+type GColumnsADT'
59 :: (Type -> Exp K.HTable)
66 :: (Type -> Exp K.HTable)
60 -> K.HTable -> (Type -> Type) -> K.HTable
67 -> K.HTable -> (Type -> Type) -> K.HTable
61−type family GADTColumns' _Columns htable rep where
68+type family GColumnsADT' _Columns htable rep where
62− GADTColumns' _Columns htable (a :+: b) =
69+ GColumnsADT' _Columns htable (a :+: b) =
63− GADTColumns' _Columns (GADTColumns' _Columns htable a) b
70+ GColumnsADT' _Columns (GColumnsADT' _Columns htable a) b
64− GADTColumns' _Columns htable (M1 C ('MetaCons _ _ _) U1) = htable
71+ GColumnsADT' _Columns htable (M1 C ('MetaCons _ _ _) U1) = htable
65− GADTColumns' _Columns htable (M1 C ('MetaCons label _ _) rep) =
72+ GColumnsADT' _Columns htable (M1 C ('MetaCons label _ _) rep) =
66 HProduct htable (HLabel label (HNullify (GColumns _Columns rep)))
73 HProduct htable (HLabel label (HNullify (GColumns _Columns rep)))
69−type GADTTable
76+type GTableADT
70 :: (Type -> Exp Constraint)
77 :: (Type -> Exp Constraint)
71 -> (Type -> Exp K.HTable)
78 -> (Type -> Exp K.HTable)
72− -> (Type -> Type) -> Constraint
79+ -> K.HContext -> (Type -> Type) -> Constraint
73−class GADTTable _Table _Columns rep where
80+class GTableADT _Table _Columns context rep where
74− fromGADTColumns :: ()
81+ gfromColumnsADT :: ()
82+ => (forall spec. context spec -> Col Result spec)
83+ -> (forall spec. Col Result spec -> context spec)
75− => (forall a. Eval (_Table a) => Eval (_Columns a) (Col Result) -> a)
84+ -> (forall a. Eval (_Table a) => Eval (_Columns a) context -> a)
76− -> GADTColumns _Columns rep (Col Result)
85+ -> GColumnsADT _Columns rep context
79− toGADTColumns :: ()
88+ gtoColumnsADT :: ()
89+ => (forall spec. context spec -> Col Result spec)
90+ -> (forall spec. Col Result spec -> context spec)
80− => (forall a. Eval (_Table a) => a -> Eval (_Columns a) (Col Result))
91+ -> (forall a. Eval (_Table a) => a -> Eval (_Columns a) context)
82− -> GADTColumns _Columns rep (Col Result)
93+ -> GColumnsADT _Columns rep context
96+ => (forall a proxy. Eval (_Table a) => proxy a -> Eval (_Columns a) context)
97+ -> (forall a labels necessity. ()
98+ => SSpec ('Spec labels necessity a)
99+ -> context ('Spec labels necessity a)
100+ -> context ('Spec labels necessity (Nullify a)))
101+ -> GColumnsADT _Columns rep context
86 ( htable ~ HLabel "tag" (HType Tag)
105 ( htable ~ HLabel "tag" (HType Tag)
106+ , HTable (GColumnsADT _Columns (M1 D meta rep))
107+ , Eval (_Table (htable context))
108+ , Eval (_Columns (htable context)) ~ htable
87− , GADTTable' _Table _Columns htable rep
109+ , GTableADT' _Table _Columns htable context rep
110+ , GToExprsADT' (Const _Table) _Columns htable rep rep
89− => GADTTable _Table _Columns (M1 D meta rep)
112+ => GTableADT _Table _Columns context (M1 D meta rep)
91− fromGADTColumns fromColumns columns =
114+ gfromColumnsADT fromContext toContext fromColumns =
115+ gfromResultADT @(Const _Table) @_Columns @(M1 D meta rep) @(M1 D meta rep)
116+ (const (fromColumns . hmap toContext)) .
119+ gtoColumnsADT fromContext toContext toColumns =
121+ gtoResultADT @(Const _Table) @_Columns @(M1 D meta rep) @(M1 D meta rep)
122+ (const (hmap fromContext . toColumns))
124+ gtableADT table hnullifier =
125+ gtableADT' @_Table @_Columns @htable @context @rep table hnullifier htable
127+ htable = table (Proxy @(htable context))
131+ :: (Type -> Exp Constraint)
132+ -> (Type -> Exp K.HTable)
133+ -> K.HTable -> K.HContext -> (Type -> Type) -> Constraint
134+class GTableADT' _Table _Columns htable context rep where
136+ => (forall a proxy. Eval (_Table a) => proxy a -> Eval (_Columns a) context)
137+ -> (forall a labels necessity. ()
138+ => SSpec ('Spec labels necessity a)
139+ -> context ('Spec labels necessity a)
140+ -> context ('Spec labels necessity (Nullify a)))
142+ -> GColumnsADT' _Columns htable rep context
146+ ( htable' ~ GColumnsADT' _Columns htable a
147+ , GTableADT' _Table _Columns htable context a
148+ , GTableADT' _Table _Columns htable' context b
150+ => GTableADT' _Table _Columns htable context (a :+: b)
152+ gtableADT' table hnullifier =
153+ gtableADT' @_Table @_Columns @_ @_ @b table hnullifier .
154+ gtableADT' @_Table @_Columns @_ @_ @a table hnullifier
157+instance meta ~ 'MetaCons label _fixity _isRecord =>
158+ GTableADT' _Table _Columns htable context (M1 C meta U1)
163+instance {-# OVERLAPPABLE #-}
164+ ( HTable (GColumns _Columns rep)
165+ , GTable _Table _Columns context rep
166+ , HLabelable context
167+ , meta ~ 'MetaCons label _fixity _isRecord
169+ , GColumnsADT' _Columns htable (M1 C ('MetaCons label _fixity _isRecord) rep) ~
170+ HProduct htable (HLabel label (HNullify (GColumns _Columns rep)))
172+ => GTableADT' _Table _Columns htable context (M1 C meta rep)
174+ gtableADT' table hnullifier htable =
177+ hnullify hnullifier $
178+ gtable @_Table @_Columns @_ @rep table
182+ :: (Type -> Type -> Exp Constraint)
183+ -> (Type -> Exp K.HTable)
184+ -> (Type -> Type) -> (Type -> Type) -> Constraint
185+class GToExprsADT _ToExprs _Columns exprs rep where
186+ gfromResultADT :: ()
187+ => (forall expr a proxy.
188+ ( Eval (_ToExprs expr a)
189+ , HTable (Eval (_Columns expr))
192+ -> Eval (_Columns expr) (Col Result)
194+ -> GColumnsADT _Columns exprs (Col Result)
198+ => (forall expr a proxy.
199+ ( Eval (_ToExprs expr a)
200+ , HTable (Eval (_Columns expr))
204+ -> Eval (_Columns expr) (Col Result))
206+ -> GColumnsADT _Columns exprs (Col Result)
210+ ( htable ~ HLabel "tag" (HType Tag)
211+ , GToExprsADT' _ToExprs _Columns htable exprs rep
213+ => GToExprsADT _ToExprs _Columns (M1 D meta exprs) (M1 D meta rep)
215+ gfromResultADT fromResult columns =
92− case fromGADTColumns' @_Table @_Columns fromColumns tag columns of
216+ case gfromResultADT' @_ToExprs @_Columns @_ @exprs fromResult tag columns of
93 Just rep -> M1 rep
217 Just rep -> M1 rep
94 _ -> error "ADT.fromColumns: mismatch between tag and data"
218 _ -> error "ADT.fromColumns: mismatch between tag and data"
96 tag = (\(HType (Result a)) -> a) . hunlabel @_ @"tag" unlabeler
220 tag = (\(HType (Result a)) -> a) . hunlabel @_ @"tag" unlabeler
98− toGADTColumns toColumns (M1 rep) =
222+ gtoResultADT toResult (M1 rep) =
99− toGADTColumns' @_Table @_Columns toColumns tag (Just rep)
223+ gtoResultADT' @_ToExprs @_Columns @_ @exprs toResult tag (Just rep)
101 tag = hlabel @_ @"tag" labeler . HType . Result
225 tag = hlabel @_ @"tag" labeler . HType . Result
104−type GADTTable'
228+type GToExprsADT'
105− :: (Type -> Exp Constraint)
229+ :: (Type -> Type -> Exp Constraint)
106 -> (Type -> Exp K.HTable)
230 -> (Type -> Exp K.HTable)
107− -> K.HTable -> (Type -> Type) -> Constraint
231+ -> K.HTable -> (Type -> Type) -> (Type -> Type) -> Constraint
108−class GADTTable' _Table _Columns htable rep where
232+class GToExprsADT' _ToExprs _Columns htable exprs rep where
109− fromGADTColumns' :: ()
233+ gfromResultADT' :: ()
234+ => (forall expr a proxy.
235+ ( Eval (_ToExprs expr a)
236+ , HTable (Eval (_Columns expr))
110− => (forall a. Eval (_Table a) => Eval (_Columns a) (Col Result) -> a)
239+ -> Eval (_Columns expr) (Col Result)
111 -> (htable (Col Result) -> Tag)
241 -> (htable (Col Result) -> Tag)
112− -> GADTColumns' _Columns htable rep (Col Result)
242+ -> GColumnsADT' _Columns htable exprs (Col Result)
113 -> Maybe (rep x)
243 -> Maybe (rep x)
115− toGADTColumns' :: ()
245+ gtoResultADT' :: ()
246+ => (forall expr a proxy.
247+ ( Eval (_ToExprs expr a)
248+ , HTable (Eval (_Columns expr))
116− => (forall a. Eval (_Table a) => a -> Eval (_Columns a) (Col Result))
252+ -> Eval (_Columns expr) (Col Result))
117 -> (Tag -> htable (Col Result))
253 -> (Tag -> htable (Col Result))
118 -> Maybe (rep x)
254 -> Maybe (rep x)
119− -> GADTColumns' _Columns htable rep (Col Result)
255+ -> GColumnsADT' _Columns htable exprs (Col Result)
121− extract :: GADTColumns' _Columns htable rep context -> htable context
257+ extract :: GColumnsADT' _Columns htable exprs context -> htable context
125− ( htable' ~ GADTColumns' _Columns htable a
261+ ( htable' ~ GColumnsADT' _Columns htable a
126− , GADTTable' _Table _Columns htable a
262+ , GToExprsADT' _ToExprs _Columns htable a rep1
127− , GADTTable' _Table _Columns htable' b
263+ , GToExprsADT' _ToExprs _Columns htable' b rep2
129− => GADTTable' _Table _Columns htable (a :+: b)
265+ => GToExprsADT' _ToExprs _Columns htable (a :+: b) (rep1 :+: rep2)
131− fromGADTColumns' fromColumns f columns =
267+ gfromResultADT' fromResult f columns =
132 case ma of
268 case ma of
133 Just a -> Just (L1 a)
269 Just a -> Just (L1 a)
134 Nothing -> R1 <$>
270 Nothing -> R1 <$>
135− fromGADTColumns' @_Table @_Columns @_ @b
271+ gfromResultADT' @_ToExprs @_Columns @_ @b @rep2
136− fromColumns
272+ fromResult
137− (f . extract @_Table @_Columns @_ @a)
273+ (f . extract @_ToExprs @_Columns @_ @a @rep1)
141− fromGADTColumns' @_Table @_Columns @_ @a
277+ gfromResultADT' @_ToExprs @_Columns @_ @a @rep1
142− fromColumns
278+ fromResult
144− (extract @_Table @_Columns @_ @b columns)
280+ (extract @_ToExprs @_Columns @_ @b @rep2 columns)
146− toGADTColumns' toColumns tag = \case
282+ gtoResultADT' toResult tag = \case
147 Just (L1 a) ->
283 Just (L1 a) ->
148− toGADTColumns' @_Table @_Columns @_ @b
284+ gtoResultADT' @_ToExprs @_Columns @_ @b @rep2
149− toColumns
285+ toResult
150− (\_ -> toGADTColumns' @_Table @_Columns @_ @a
286+ (\_ -> gtoResultADT' @_ToExprs @_Columns @_ @a @rep1
151− toColumns
287+ toResult
153 (Just a))
289 (Just a))
155 Just (R1 b) ->
291 Just (R1 b) ->
156− toGADTColumns' @_Table @_Columns @_ @b
292+ gtoResultADT' @_ToExprs @_Columns @_ @b @rep2
157− toColumns
293+ toResult
158 (\tag' ->
294 (\tag' ->
159− toGADTColumns' @_Table @_Columns @_ @a
295+ gtoResultADT' @_ToExprs @_Columns @_ @a @rep1
160− toColumns
296+ toResult
161 (\_ -> tag tag')
297 (\_ -> tag tag')
164 Nothing ->
300 Nothing ->
165− toGADTColumns' @_Table @_Columns @_ @b
301+ gtoResultADT' @_ToExprs @_Columns @_ @b @rep2
166− toColumns
302+ toResult
167− (\_ -> toGADTColumns' @_Table @_Columns @_ @a toColumns tag Nothing)
303+ (\_ -> gtoResultADT' @_ToExprs @_Columns @_ @a @rep1 toResult tag Nothing)
170− extract = extract @_Table @_Columns @_ @a . extract @_Table @_Columns @_ @b
307+ extract @_ToExprs @_Columns @_ @a @rep1 .
308+ extract @_ToExprs @_Columns @_ @b @rep2
312+ ( meta ~ 'MetaCons label _fixity _isRecord
173−instance KnownSymbol label =>
313+ , KnownSymbol label
174− GADTTable' _Table _Columns htable (M1 C ('MetaCons label _fixity _isRecord) U1)
314+ )
315+ => GToExprsADT' _ToExprs _Columns htable (M1 C meta U1) (M1 C meta U1)
176− fromGADTColumns' _ tag columns
317+ gfromResultADT' _ tag columns
177 | tag columns == tag' = Just (M1 U1)
318 | tag columns == tag' = Just (M1 U1)
178 | otherwise = Nothing
319 | otherwise = Nothing
180 tag' = Tag $ pack $ symbolVal (Proxy @label)
321 tag' = Tag $ pack $ symbolVal (Proxy @label)
182− toGADTColumns' _ tag _ = tag tag'
323+ gtoResultADT' _ tag _ = tag tag'
184 tag' = Tag $ pack $ symbolVal (Proxy @label)
325 tag' = Tag $ pack $ symbolVal (Proxy @label)
@@ -187,29 +328,34 @@ instance KnownSymbol label =>
189instance {-# OVERLAPPABLE #-}
330instance {-# OVERLAPPABLE #-}
190− ( HTable (GColumns _Columns rep)
331+ ( HTable (GColumns _Columns exprs)
191− , GTable _Table _Columns (Col Result) rep
332+ , GToExprs _ToExprs _Columns exprs rep
333+ , meta ~ 'MetaCons label _fixity _isRecord
192 , KnownSymbol label
334 , KnownSymbol label
193− , GADTColumns' _Columns htable (M1 C ('MetaCons label _fixity _isRecord) rep) ~
335+ , GColumnsADT' _Columns htable (M1 C meta exprs) ~
194− HProduct htable (HLabel label (HNullify (GColumns _Columns rep)))
336+ HProduct htable (HLabel label (HNullify (GColumns _Columns exprs)))
196− => GADTTable' _Table _Columns htable (M1 C ('MetaCons label _fixity _isRecord) rep)
338+ => GToExprsADT' _ToExprs _Columns htable (M1 C meta exprs) (M1 C meta rep)
198− fromGADTColumns' fromColumns tag (HProduct a b)
340+ gfromResultADT' fromResult tag (HProduct a b)
199 | tag a == tag' =
341 | tag a == tag' =
200− M1 . fromGColumns @_Table @_Columns fromColumns <$>
342+ M1 . gfromResult @_ToExprs @_Columns @exprs fromResult <$>
201 hunnullify unnullifier (hunlabel unlabeler b)
343 hunnullify unnullifier (hunlabel unlabeler b)
202 | otherwise = Nothing
344 | otherwise = Nothing
204 tag' = Tag $ pack $ symbolVal (Proxy @label)
346 tag' = Tag $ pack $ symbolVal (Proxy @label)
206− toGADTColumns' toColumns tag = \case
348+ gtoResultADT' toResult tag = \case
207 Nothing -> HProduct (tag tag') (hlabel labeler (hnulls null))
349 Nothing -> HProduct (tag tag') (hlabel labeler (hnulls null))
208 Just (M1 rep) -> HProduct (tag tag') $
350 Just (M1 rep) -> HProduct (tag tag') $
209 hlabel labeler $
351 hlabel labeler $
210 hnullify nullifier $
352 hnullify nullifier $
211− toGColumns @_Table @_Columns toColumns rep
353+ gtoResult @_ToExprs @_Columns @exprs toResult rep
213 tag' = Tag $ pack $ symbolVal (Proxy @label)
355 tag' = Tag $ pack $ symbolVal (Proxy @label)
215 extract (HProduct a _) = a
357 extract (HProduct a _) = a
360+data Const :: (a -> Exp Constraint) -> a -> a -> Exp Constraint
361+type instance Eval (Const f x a) = (Eval (f a), x ~ a)