1{-# language AllowAmbiguousTypes #-}
2{-# language ConstraintKinds #-}
3{-# language DataKinds #-}
4{-# language DeriveAnyClass #-}
5{-# language DeriveGeneric #-}
6{-# language DerivingStrategies #-}
7{-# language FlexibleContexts #-}
8{-# language FlexibleInstances #-}
10{-# language LambdaCase #-}
11{-# language MultiParamTypeClasses #-}
12{-# language NamedFieldPuns #-}
13{-# language QuantifiedConstraints #-}
14{-# language RankNTypes #-}
15{-# language RecordWildCards #-}
16{-# language ScopedTypeVariables #-}
17{-# language StandaloneKindSignatures #-}
18{-# language TypeApplications #-}
19{-# language TypeFamilies #-}
20{-# language UndecidableInstances #-}
22module Rel8.Schema.HTable.Vectorize
24 , hvectorize, hunvectorize
31import Data.Kind ( Type )
32import Data.List.NonEmpty ( NonEmpty )
36import Rel8.Schema.Context.Label ( HLabelable, hlabeler, hunlabeler )
37import Rel8.Schema.Dict ( Dict( Dict ) )
38import qualified Rel8.Schema.Kind as K
39import Rel8.Schema.HTable
41 , hfield, htabulate, htabulateA, hspecs
43import Rel8.Schema.Nullability
46 , Nullability( NonNullable )
48import Rel8.Schema.Spec ( Spec( Spec ), SSpec(..) )
49import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
50import Rel8.Type.Information ( TypeInformation )
53import Data.Zip ( Unzip, Repeat, Zippy(..) )
55import Rel8.Schema.HTable.MapTable
56import GHC.Generics (Generic)
59class Vector list where
60 listNotNull :: proxy a -> Dict NotNull (list a)
61 vectorTypeInformation :: ()
63 -> TypeInformation (Unnullify a)
64 -> TypeInformation (list a)
67instance Vector [] where
69 vectorTypeInformation = listTypeInformation
72instance Vector NonEmpty where
74 vectorTypeInformation = nonEmptyTypeInformation
77type HVectorize :: (Type -> Type) -> K.HTable -> K.HTable
78newtype HVectorize list table context = HVectorize (HMapTable (Vectorize list) table context)
79 deriving stock Generic
80 deriving anyclass HTable
83data Vectorize :: (Type -> Type) -> Spec -> Exp Spec
86type instance Eval (Vectorize list ('Spec labels necessity a)) = 'Spec labels necessity (list a)
89instance Vector list => MapSpec (Vectorize list) where
91 SSpec {..} -> case listNotNull @list nullability of
93 { nullability = NonNullable
94 , info = vectorTypeInformation nullability info
99hvectorize :: (HTable t, Unzip f, Vector list)
100 => (forall labels necessity a. ()
101 => SSpec ('Spec labels necessity a)
102 -> f (context ('Spec labels necessity a))
103 -> context' ('Spec labels necessity (list a)))
105 -> HVectorize list t context'
106hvectorize vectorizer as = HVectorize $ htabulate $ \(HMapTableField field) ->
107 case hfield hspecs field of
108 spec@SSpec {} -> vectorizer spec (fmap (`hfield` field) as)
109{-# INLINABLE hvectorize #-}
112hunvectorize :: (HTable t, Repeat f, Vector list)
113 => (forall labels necessity a. ()
114 => SSpec ('Spec labels necessity a)
115 -> context ('Spec labels necessity (list a))
116 -> f (context' ('Spec labels necessity a)))
117 -> HVectorize list t context
119hunvectorize unvectorizer (HVectorize table) =
120 getZippy $ htabulateA $ \field -> case hfield hspecs field of
121 spec@SSpec{} -> case hfield table (HMapTableField field) of
122 a -> Zippy (unvectorizer spec a)
123{-# INLINABLE hunvectorize #-}
126happend :: (HTable t, Vector list) =>
127 ( forall labels necessity a. ()
129 -> TypeInformation (Unnullify a)
130 -> context ('Spec labels necessity (list a))
131 -> context ('Spec labels necessity (list a))
132 -> context ('Spec labels necessity (list a))
134 -> HVectorize list t context
135 -> HVectorize list t context
136 -> HVectorize list t context
137happend append (HVectorize as) (HVectorize bs) = HVectorize $
138 htabulate $ \field@(HMapTableField j) -> case (hfield as field, hfield bs field) of
139 (a, b) -> case hfield hspecs j of
140 SSpec {nullability, info} -> append nullability info a b
144 ( forall labels necessity a. ()
146 -> TypeInformation (Unnullify a)
147 -> context ('Spec labels necessity [a])
149 -> HVectorize [] t context
150hempty empty = HVectorize $ htabulate $ \(HMapTableField field) -> case hfield hspecs field of
151 SSpec {nullability, info} -> empty nullability info
154instance HLabelable g => HLabelable (Precompose (Vectorize list) g) where
155 hlabeler = Precompose . hlabeler . precomposed
156 hunlabeler = Precompose . hunlabeler . precomposed
159hrelabel :: HLabelable context
160 => (forall ctx. HLabelable ctx => t ctx -> u ctx)
161 -> HVectorize list t context
162 -> HVectorize list u context
163hrelabel f (HVectorize (HMapTable table)) = HVectorize (HMapTable (f table))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list