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
30import Data.Kind ( Type )
31import Data.List.NonEmpty ( NonEmpty )
35import Rel8.Kind.Necessity ( Necessity( Required ), SNecessity( SRequired ) )
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.Null ( Unnullify, NotNull, Nullity( NotNull ) )
44import Rel8.Schema.Spec ( Spec( Spec ), SSpec(..) )
45import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
46import Rel8.Type.Information ( TypeInformation )
49import Data.Zip ( Unzip, Repeat, Zippy(..) )
51import Rel8.Schema.HTable.MapTable
52import GHC.Generics (Generic)
55class Vector list where
56 listNotNull :: proxy a -> Dict NotNull (list a)
57 vectorTypeInformation :: ()
59 -> TypeInformation (Unnullify a)
60 -> TypeInformation (list a)
63instance Vector [] where
65 vectorTypeInformation = listTypeInformation
68instance Vector NonEmpty where
70 vectorTypeInformation = nonEmptyTypeInformation
73type HVectorize :: (Type -> Type) -> K.HTable -> K.HTable
74newtype HVectorize list table context = HVectorize (HMapTable (Vectorize list) table context)
75 deriving stock Generic
76 deriving anyclass HTable
79data Vectorize :: (Type -> Type) -> Spec -> Exp Spec
82type instance Eval (Vectorize list ('Spec labels _necessity a)) = 'Spec labels 'Required (list a)
85instance Vector list => MapSpec (Vectorize list) where
87 SSpec {..} -> case listNotNull @list nullity of
89 { necessity = SRequired
91 , info = vectorTypeInformation nullity info
96hvectorize :: (HTable t, Unzip f, Vector list)
97 => (forall labels necessity a. ()
98 => SSpec ('Spec labels necessity a)
99 -> f (context ('Spec labels necessity a))
100 -> context' ('Spec labels 'Required (list a)))
102 -> HVectorize list t context'
103hvectorize vectorizer as = HVectorize $ htabulate $ \(HMapTableField field) ->
104 case hfield hspecs field of
105 spec@SSpec {} -> vectorizer spec (fmap (`hfield` field) as)
106{-# INLINABLE hvectorize #-}
109hunvectorize :: (HTable t, Repeat f, Vector list)
110 => (forall labels necessity a. ()
111 => SSpec ('Spec labels necessity a)
112 -> context ('Spec labels 'Required (list a))
113 -> f (context' ('Spec labels necessity a)))
114 -> HVectorize list t context
116hunvectorize unvectorizer (HVectorize table) =
117 getZippy $ htabulateA $ \field -> case hfield hspecs field of
118 spec@SSpec{} -> case hfield table (HMapTableField field) of
119 a -> Zippy (unvectorizer spec a)
120{-# INLINABLE hunvectorize #-}
123happend :: (HTable t, Vector list) =>
124 ( forall labels necessity a. ()
126 -> TypeInformation (Unnullify a)
127 -> context ('Spec labels necessity (list a))
128 -> context ('Spec labels necessity (list a))
129 -> context ('Spec labels necessity (list a))
131 -> HVectorize list t context
132 -> HVectorize list t context
133 -> HVectorize list t context
134happend append (HVectorize as) (HVectorize bs) = HVectorize $
135 htabulate $ \field@(HMapTableField j) -> case (hfield as field, hfield bs field) of
136 (a, b) -> case hfield hspecs j of
137 SSpec {nullity, info} -> append nullity info a b
141 ( forall labels necessity a. ()
143 -> TypeInformation (Unnullify a)
144 -> context ('Spec labels necessity [a])
146 -> HVectorize [] t context
147hempty empty = HVectorize $ htabulate $ \(HMapTableField field) -> case hfield hspecs field of
148 SSpec {nullity, info} -> empty nullity info
151instance HLabelable g => HLabelable (Precompose (Vectorize list) g) where
152 hlabeler = Precompose . hlabeler . precomposed
153 hunlabeler = Precompose . hunlabeler . precomposed
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