on 0272bcc help sign in
rel8/src/Rel8/Schema/HTable/Vectorize.hs
142 lines 4.2 kB history raw
1{-# language AllowAmbiguousTypes #-}
2{-# language ConstraintKinds #-}
3{-# language DataKinds #-}
4{-# language DeriveAnyClass #-}
5{-# language DeriveGeneric #-}
6{-# language DerivingStrategies #-}
7{-# language FlexibleContexts #-}
8{-# language FlexibleInstances #-}
9{-# language GADTs #-}
10{-# language LambdaCase #-}
11{-# language MultiParamTypeClasses #-}
12{-# language RankNTypes #-}
13{-# language RecordWildCards #-}
14{-# language ScopedTypeVariables #-}
15{-# language StandaloneKindSignatures #-}
16{-# language TypeApplications #-}
17{-# language TypeFamilies #-}
18{-# language UndecidableInstances #-}
20module Rel8.Schema.HTable.Vectorize
21 ( HVectorize
22 , hvectorize, hunvectorize
23 , happend, hempty
24 , hproject
25 , hcolumn
26 )
27where
29-- base
30import Data.Kind ( Type )
31import Data.List.NonEmpty ( NonEmpty )
32import GHC.Generics (Generic)
33import Prelude
35-- rel8
36import Rel8.FCF ( Eval, Exp )
37import Rel8.Schema.Dict ( Dict( Dict ) )
38import qualified Rel8.Schema.Kind as K
39import Rel8.Schema.HTable ( HTable, hfield, htabulate, htabulateA, hspecs )
40import Rel8.Schema.HTable.Identity ( HIdentity( HIdentity ) )
41import Rel8.Schema.HTable.MapTable
42 ( HMapTable( HMapTable ), HMapTableField( HMapTableField )
43 , MapSpec, mapInfo
44 , Precompose( Precompose )
45 )
46import qualified Rel8.Schema.HTable.MapTable as HMapTable ( hproject )
47import Rel8.Schema.Null ( Unnullify, NotNull, Nullity( NotNull ) )
48import Rel8.Schema.Spec ( Spec(..) )
49import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
50import Rel8.Type.Information ( TypeInformation )
52-- semialign
53import Data.Zip ( Unzip, Zip, Zippy(..) )
56class Vector list where
57 listNotNull :: proxy a -> Dict NotNull (list a)
58 vectorTypeInformation :: ()
59 => Nullity a
60 -> TypeInformation (Unnullify a)
61 -> TypeInformation (list a)
64instance Vector [] where
65 listNotNull _ = Dict
66 vectorTypeInformation = listTypeInformation
69instance Vector NonEmpty where
70 listNotNull _ = Dict
71 vectorTypeInformation = nonEmptyTypeInformation
74type HVectorize :: (Type -> Type) -> K.HTable -> K.HTable
75newtype HVectorize list table context = HVectorize (HMapTable (Vectorize list) table context)
76 deriving stock Generic
77 deriving anyclass HTable
80data Vectorize :: (Type -> Type) -> Type -> Exp Type
83type instance Eval (Vectorize list a) = list a
86instance Vector list => MapSpec (Vectorize list) where
87 mapInfo = \case
88 Spec {..} -> case listNotNull @list nullity of
89 Dict -> Spec
90 { nullity = NotNull
91 , info = vectorTypeInformation nullity info
92 , ..
93 }
96hvectorize :: (HTable t, Unzip f, Vector list)
97 => (forall a. Spec a -> f (context a) -> context' (list a))
98 -> f (t context)
99 -> HVectorize list t context'
100hvectorize vectorizer as = HVectorize $ htabulate $ \(HMapTableField field) ->
101 case hfield hspecs field of
102 spec -> vectorizer spec (fmap (`hfield` field) as)
103{-# INLINABLE hvectorize #-}
106hunvectorize :: (HTable t, Zip f, Vector list)
107 => (forall a. Spec a -> context (list a) -> f (context' a))
108 -> HVectorize list t context
109 -> f (t context')
110hunvectorize unvectorizer (HVectorize table) =
111 getZippy $ htabulateA $ \field -> case hfield hspecs field of
112 spec -> case hfield table (HMapTableField field) of
113 a -> Zippy (unvectorizer spec a)
114{-# INLINABLE hunvectorize #-}
117happend :: (HTable t, Vector list)
118 => (forall a. Spec a -> context (list a) -> context (list a) -> context (list a))
119 -> HVectorize list t context
120 -> HVectorize list t context
121 -> HVectorize list t context
122happend append (HVectorize as) (HVectorize bs) = HVectorize $
123 htabulate $ \field@(HMapTableField j) -> case (hfield as field, hfield bs field) of
124 (a, b) -> case hfield hspecs j of
125 spec -> append spec a b
128hempty :: HTable t
129 => (forall a. Spec a -> context [a])
130 -> HVectorize [] t context
131hempty empty = HVectorize $ htabulate $ \(HMapTableField field) ->
132 empty (hfield hspecs field)
135hproject :: ()
136 => (forall ctx. t ctx -> t' ctx)
137 -> HVectorize list t context -> HVectorize list t' context
138hproject f (HVectorize a) = HVectorize (HMapTable.hproject f a)
141hcolumn :: HVectorize list (HIdentity a) context -> context (list a)
142hcolumn (HVectorize (HMapTable (HIdentity (Precompose a)))) = a