@@ -19,7 +19,6 @@ where
21import Data.Bifunctor ( Bifunctor, bimap )
21import Data.Bifunctor ( Bifunctor, bimap )
22−import Data.Functor.Identity ( Identity )
23import Data.Kind ( Constraint, Type )
22import Data.Kind ( Constraint, Type )
24import Data.List.NonEmpty ( NonEmpty )
23import Data.List.NonEmpty ( NonEmpty )
25import Prelude
24import Prelude
@@ -45,6 +44,7 @@ import Rel8.Schema.Insert ( Insert, Col(..) )
45import qualified Rel8.Schema.Kind as K
44import qualified Rel8.Schema.Kind as K
46import Rel8.Schema.Name ( Name(..), Col(..) )
45import Rel8.Schema.Name ( Name(..), Col(..) )
47import Rel8.Schema.Null ( Sql )
46import Rel8.Schema.Null ( Sql )
47+import Rel8.Schema.Result ( Result )
48import Rel8.Schema.Spec ( Spec( Spec ) )
48import Rel8.Schema.Spec ( Spec( Spec ) )
49import Rel8.Table
49import Rel8.Table
50 ( Table, Columns, Context, fromColumns, toColumns
50 ( Table, Columns, Context, fromColumns, toColumns
@@ -65,12 +65,12 @@ import Data.These ( These )
65type Field :: K.Context -> Necessity -> Type -> Type
65type Field :: K.Context -> Necessity -> Type -> Type
66type family Field context necessity a where
66type family Field context necessity a where
67 Field (Reify context) necessity a = AField context necessity a
67 Field (Reify context) necessity a = AField context necessity a
68− Field Identity _necessity a = a
68+ Field Aggregate _necessity a = Aggregate (Expr a)
69 Field Expr _necessity a = Expr a
69 Field Expr _necessity a = Expr a
70 Field Insert 'Required a = Expr a
70 Field Insert 'Required a = Expr a
71 Field Insert 'Optional a = Maybe (Expr a)
71 Field Insert 'Optional a = Maybe (Expr a)
72− Field Aggregate _necessity a = Aggregate (Expr a)
72+ Field Name _necessity a = Name a
73− Field context _necessity a = context a
73+ Field Result _necessity a = a
76type HEither :: K.Context -> Type -> Type -> Type
76type HEither :: K.Context -> Type -> Type -> Type
@@ -78,10 +78,9 @@ type family HEither context where
78 HEither (Reify context) = AHEither context
78 HEither (Reify context) = AHEither context
79 HEither Aggregate = EitherTable
79 HEither Aggregate = EitherTable
80 HEither Expr = EitherTable
80 HEither Expr = EitherTable
81− HEither Identity = Either
82 HEither Insert = EitherTable
81 HEither Insert = EitherTable
83 HEither Name = EitherTable
82 HEither Name = EitherTable
84− HEither _ = Either
83+ HEither Result = Either
87type HList :: K.Context -> Type -> Type
86type HList :: K.Context -> Type -> Type
@@ -89,10 +88,9 @@ type family HList context where
89 HList (Reify context) = AHList context
88 HList (Reify context) = AHList context
90 HList Aggregate = ListTable
89 HList Aggregate = ListTable
91 HList Expr = ListTable
90 HList Expr = ListTable
93 HList Insert = ListTable
91 HList Insert = ListTable
94 HList Name = ListTable
92 HList Name = ListTable
95− HList _ = []
93+ HList Result = []
98type HMaybe :: K.Context -> Type -> Type
96type HMaybe :: K.Context -> Type -> Type
@@ -100,10 +98,9 @@ type family HMaybe context where
100 HMaybe (Reify context) = AHMaybe context
98 HMaybe (Reify context) = AHMaybe context
101 HMaybe Aggregate = MaybeTable
99 HMaybe Aggregate = MaybeTable
102 HMaybe Expr = MaybeTable
100 HMaybe Expr = MaybeTable
103− HMaybe Identity = Maybe
104 HMaybe Insert = MaybeTable
101 HMaybe Insert = MaybeTable
105 HMaybe Name = MaybeTable
102 HMaybe Name = MaybeTable
106− HMaybe _ = Maybe
103+ HMaybe Result = Maybe
109type HNonEmpty :: K.Context -> Type -> Type
106type HNonEmpty :: K.Context -> Type -> Type
@@ -111,10 +108,9 @@ type family HNonEmpty context where
111 HNonEmpty (Reify context) = AHNonEmpty context
108 HNonEmpty (Reify context) = AHNonEmpty context
112 HNonEmpty Aggregate = NonEmptyTable
109 HNonEmpty Aggregate = NonEmptyTable
113 HNonEmpty Expr = NonEmptyTable
110 HNonEmpty Expr = NonEmptyTable
114− HNonEmpty Identity = NonEmpty
115 HNonEmpty Insert = NonEmptyTable
111 HNonEmpty Insert = NonEmptyTable
116 HNonEmpty Name = NonEmptyTable
112 HNonEmpty Name = NonEmptyTable
117− HNonEmpty _ = NonEmpty
113+ HNonEmpty Result = NonEmpty
120type HThese :: K.Context -> Type -> Type -> Type
116type HThese :: K.Context -> Type -> Type -> Type
@@ -122,10 +118,9 @@ type family HThese context where
122 HThese (Reify context) = AHThese context
118 HThese (Reify context) = AHThese context
123 HThese Aggregate = TheseTable
119 HThese Aggregate = TheseTable
124 HThese Expr = TheseTable
120 HThese Expr = TheseTable
125− HThese Identity = These
126 HThese Insert = TheseTable
121 HThese Insert = TheseTable
127 HThese Name = TheseTable
122 HThese Name = TheseTable
128− HThese _ = These
123+ HThese Result = These
131type AField :: K.Context -> Necessity -> Type -> Type
126type AField :: K.Context -> Necessity -> Type -> Type
@@ -304,9 +299,9 @@ type SContext :: K.Context -> Type
304data SContext context where
299data SContext context where
305 SAggregate :: SContext Aggregate
300 SAggregate :: SContext Aggregate
306 SExpr :: SContext Expr
301 SExpr :: SContext Expr
307− SIdentity :: SContext Identity
308 SInsert :: SContext Insert
302 SInsert :: SContext Insert
309 SName :: SContext Name
303 SName :: SContext Name
304+ SResult :: SContext Result
310 SReify :: SContext context -> SContext (Reify context)
305 SReify :: SContext context -> SContext (Reify context)
@@ -323,8 +318,8 @@ instance Reifiable Expr where
323 contextSing = SExpr
318 contextSing = SExpr
326−instance Reifiable Identity where
321+instance Reifiable Result where
327− contextSing = SIdentity
322+ contextSing = SResult
330instance Reifiable Insert where
325instance Reifiable Insert where
@@ -359,7 +354,7 @@ sfromColumn :: ()
359sfromColumn = \case
354sfromColumn = \case
360 SAggregate -> \(Aggregation a) -> AField a
355 SAggregate -> \(Aggregation a) -> AField a
361 SExpr -> \(DB a) -> AField a
356 SExpr -> \(DB a) -> AField a
362− SIdentity -> \(Result a) -> AField a
357+ SResult -> \(Result a) -> AField a
363 SInsert -> \case
358 SInsert -> \case
364 RequiredInsert a -> AField a
359 RequiredInsert a -> AField a
365 OptionalInsert a -> AField a
360 OptionalInsert a -> AField a
@@ -375,7 +370,7 @@ stoColumn :: ()
375stoColumn = \case
370stoColumn = \case
376 SAggregate -> \_ (AField a) -> Aggregation a
371 SAggregate -> \_ (AField a) -> Aggregation a
377 SExpr -> \_ (AField a) -> DB a
372 SExpr -> \_ (AField a) -> DB a
378− SIdentity -> \_ (AField a) -> Result a
373+ SResult -> \_ (AField a) -> Result a
379 SInsert -> \case
374 SInsert -> \case
380 SRequired -> \(AField a) -> RequiredInsert a
375 SRequired -> \(AField a) -> RequiredInsert a
381 SOptional -> \(AField a) -> OptionalInsert a
376 SOptional -> \(AField a) -> OptionalInsert a
@@ -393,7 +388,7 @@ sbimapEither :: ()
393sbimapEither = \case
388sbimapEither = \case
394 SAggregate -> \f g (AHEither a) -> AHEither (bimap f g a)
389 SAggregate -> \f g (AHEither a) -> AHEither (bimap f g a)
395 SExpr -> \f g (AHEither a) -> AHEither (bimap f g a)
390 SExpr -> \f g (AHEither a) -> AHEither (bimap f g a)
396− SIdentity -> \f g (AHEither a) -> AHEither (bimap f g a)
391+ SResult -> \f g (AHEither a) -> AHEither (bimap f g a)
397 SInsert -> \f g (AHEither a) -> AHEither (bimap f g a)
392 SInsert -> \f g (AHEither a) -> AHEither (bimap f g a)
398 SName -> \f g (AHEither a) -> AHEither (bimap f g a)
393 SName -> \f g (AHEither a) -> AHEither (bimap f g a)
399 SReify context -> \f g (AHEither a) -> AHEither (sbimapEither context f g a)
394 SReify context -> \f g (AHEither a) -> AHEither (sbimapEither context f g a)
@@ -414,7 +409,7 @@ sfromColumnsEither = \case
414 bimap (fromColumns . hreify) (fromColumns . hreify) .
409 bimap (fromColumns . hreify) (fromColumns . hreify) .
415 fromColumns .
410 fromColumns .
417− SIdentity ->
412+ SResult ->
418 AHEither .
413 AHEither .
419 bimap (fromColumns . hreify) (fromColumns . hreify) .
414 bimap (fromColumns . hreify) (fromColumns . hreify) .
420 fromColumns .
415 fromColumns .
@@ -451,7 +446,7 @@ stoColumnsEither = \case
451 toColumns .
446 toColumns .
452 bimap (hunreify . toColumns) (hunreify . toColumns) .
447 bimap (hunreify . toColumns) (hunreify . toColumns) .
453 (\(AHEither a) -> a)
448 (\(AHEither a) -> a)
454− SIdentity ->
449+ SResult ->
456 toColumns .
451 toColumns .
457 bimap (hunreify . toColumns) (hunreify . toColumns) .
452 bimap (hunreify . toColumns) (hunreify . toColumns) .
@@ -482,7 +477,7 @@ smapList :: Congruent a b
482smapList = \case
477smapList = \case
483 SAggregate -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
478 SAggregate -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
484 SExpr -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
479 SExpr -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
485− SIdentity -> \f _ (AHList as) -> AHList (fmap f as)
480+ SResult -> \f _ (AHList as) -> AHList (fmap f as)
486 SInsert -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
481 SInsert -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
487 SName -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
482 SName -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
488 SReify context -> \f g (AHList as) -> AHList (smapList context f g as)
483 SReify context -> \f g (AHList as) -> AHList (smapList context f g as)
@@ -495,7 +490,7 @@ sfromColumnsList :: Table (Reify context) a
495sfromColumnsList = \case
490sfromColumnsList = \case
496 SAggregate -> AHList . ListTable
491 SAggregate -> AHList . ListTable
497 SExpr -> AHList . ListTable
492 SExpr -> AHList . ListTable
498− SIdentity -> AHList . fmap (fromColumns . hreify) . fromColumns . hunreify
493+ SResult -> AHList . fmap (fromColumns . hreify) . fromColumns . hunreify
499 SInsert -> AHList . ListTable
494 SInsert -> AHList . ListTable
500 SName -> AHList . ListTable
495 SName -> AHList . ListTable
501 SReify context ->
496 SReify context ->
@@ -512,7 +507,7 @@ stoColumnsList :: Table (Reify context) a
512stoColumnsList = \case
507stoColumnsList = \case
513 SAggregate -> \(AHList (ListTable a)) -> a
508 SAggregate -> \(AHList (ListTable a)) -> a
514 SExpr -> \(AHList (ListTable a)) -> a
509 SExpr -> \(AHList (ListTable a)) -> a
515− SIdentity ->
510+ SResult ->
516 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHList a) -> a)
511 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHList a) -> a)
517 SInsert -> \(AHList (ListTable a)) -> a
512 SInsert -> \(AHList (ListTable a)) -> a
518 SName -> \(AHList (ListTable a)) -> a
513 SName -> \(AHList (ListTable a)) -> a
@@ -531,7 +526,7 @@ smapMaybe :: ()
531smapMaybe = \case
526smapMaybe = \case
532 SAggregate -> \f (AHMaybe a) -> AHMaybe (fmap f a)
527 SAggregate -> \f (AHMaybe a) -> AHMaybe (fmap f a)
533 SExpr -> \f (AHMaybe a) -> AHMaybe (fmap f a)
528 SExpr -> \f (AHMaybe a) -> AHMaybe (fmap f a)
534− SIdentity -> \f (AHMaybe a) -> AHMaybe (fmap f a)
529+ SResult -> \f (AHMaybe a) -> AHMaybe (fmap f a)
535 SInsert -> \f (AHMaybe a) -> AHMaybe (fmap f a)
530 SInsert -> \f (AHMaybe a) -> AHMaybe (fmap f a)
536 SName -> \f (AHMaybe a) -> AHMaybe (fmap f a)
531 SName -> \f (AHMaybe a) -> AHMaybe (fmap f a)
537 SReify context -> \f (AHMaybe a) -> AHMaybe (smapMaybe context f a)
532 SReify context -> \f (AHMaybe a) -> AHMaybe (smapMaybe context f a)
@@ -544,7 +539,7 @@ sfromColumnsMaybe :: Table (Reify context) a
544sfromColumnsMaybe = \case
539sfromColumnsMaybe = \case
545 SAggregate -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
540 SAggregate -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
546 SExpr -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
541 SExpr -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
547− SIdentity -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
542+ SResult -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
548 SInsert -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
543 SInsert -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
549 SName -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
544 SName -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
550 SReify context ->
545 SReify context ->
@@ -563,7 +558,7 @@ stoColumnsMaybe = \case
563 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
558 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
565 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
560 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
566− SIdentity ->
561+ SResult ->
567 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
562 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
568 SInsert ->
563 SInsert ->
569 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
564 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
@@ -585,7 +580,7 @@ smapNonEmpty :: Congruent a b
585smapNonEmpty = \case
580smapNonEmpty = \case
586 SAggregate -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
581 SAggregate -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
587 SExpr -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
582 SExpr -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
588− SIdentity -> \f _ (AHNonEmpty as) -> AHNonEmpty (fmap f as)
583+ SResult -> \f _ (AHNonEmpty as) -> AHNonEmpty (fmap f as)
589 SInsert -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
584 SInsert -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
590 SName -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
585 SName -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
591 SReify context -> \f g (AHNonEmpty as) -> AHNonEmpty (smapNonEmpty context f g as)
586 SReify context -> \f g (AHNonEmpty as) -> AHNonEmpty (smapNonEmpty context f g as)
@@ -598,7 +593,7 @@ sfromColumnsNonEmpty :: Table (Reify context) a
598sfromColumnsNonEmpty = \case
593sfromColumnsNonEmpty = \case
599 SAggregate -> AHNonEmpty . NonEmptyTable
594 SAggregate -> AHNonEmpty . NonEmptyTable
600 SExpr -> AHNonEmpty . NonEmptyTable
595 SExpr -> AHNonEmpty . NonEmptyTable
601− SIdentity ->
596+ SResult ->
602 AHNonEmpty . fmap (fromColumns . hreify) . fromColumns . hunreify
597 AHNonEmpty . fmap (fromColumns . hreify) . fromColumns . hunreify
603 SInsert -> AHNonEmpty . NonEmptyTable
598 SInsert -> AHNonEmpty . NonEmptyTable
604 SName -> AHNonEmpty . NonEmptyTable
599 SName -> AHNonEmpty . NonEmptyTable
@@ -616,7 +611,7 @@ stoColumnsNonEmpty :: Table (Reify context) a
616stoColumnsNonEmpty = \case
611stoColumnsNonEmpty = \case
617 SAggregate -> \(AHNonEmpty (NonEmptyTable a)) -> a
612 SAggregate -> \(AHNonEmpty (NonEmptyTable a)) -> a
618 SExpr -> \(AHNonEmpty (NonEmptyTable a)) -> a
613 SExpr -> \(AHNonEmpty (NonEmptyTable a)) -> a
619− SIdentity ->
614+ SResult ->
620 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHNonEmpty a) -> a)
615 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHNonEmpty a) -> a)
621 SInsert -> \(AHNonEmpty (NonEmptyTable a)) -> a
616 SInsert -> \(AHNonEmpty (NonEmptyTable a)) -> a
622 SName -> \(AHNonEmpty (NonEmptyTable a)) -> a
617 SName -> \(AHNonEmpty (NonEmptyTable a)) -> a
@@ -636,7 +631,7 @@ sbimapThese :: ()
636sbimapThese = \case
631sbimapThese = \case
637 SAggregate -> \f g (AHThese a) -> AHThese (bimap f g a)
632 SAggregate -> \f g (AHThese a) -> AHThese (bimap f g a)
638 SExpr -> \f g (AHThese a) -> AHThese (bimap f g a)
633 SExpr -> \f g (AHThese a) -> AHThese (bimap f g a)
639− SIdentity -> \f g (AHThese a) -> AHThese (bimap f g a)
634+ SResult -> \f g (AHThese a) -> AHThese (bimap f g a)
640 SInsert -> \f g (AHThese a) -> AHThese (bimap f g a)
635 SInsert -> \f g (AHThese a) -> AHThese (bimap f g a)
641 SName -> \f g (AHThese a) -> AHThese (bimap f g a)
636 SName -> \f g (AHThese a) -> AHThese (bimap f g a)
642 SReify context -> \f g (AHThese a) -> AHThese (sbimapThese context f g a)
637 SReify context -> \f g (AHThese a) -> AHThese (sbimapThese context f g a)
@@ -657,7 +652,7 @@ sfromColumnsThese = \case
657 bimap (fromColumns . hreify) (fromColumns . hreify) .
652 bimap (fromColumns . hreify) (fromColumns . hreify) .
658 fromColumns .
653 fromColumns .
660− SIdentity ->
655+ SResult ->
661 AHThese .
656 AHThese .
662 bimap (fromColumns . hreify) (fromColumns . hreify) .
657 bimap (fromColumns . hreify) (fromColumns . hreify) .
663 fromColumns .
658 fromColumns .
@@ -694,7 +689,7 @@ stoColumnsThese = \case
694 toColumns .
689 toColumns .
695 bimap (hunreify . toColumns) (hunreify . toColumns) .
690 bimap (hunreify . toColumns) (hunreify . toColumns) .
696 (\(AHThese a) -> a)
691 (\(AHThese a) -> a)
697− SIdentity ->
692+ SResult ->
699 toColumns .
694 toColumns .
700 bimap (hunreify . toColumns) (hunreify . toColumns) .
695 bimap (hunreify . toColumns) (hunreify . toColumns) .