1{-# language AllowAmbiguousTypes #-}
2{-# language DataKinds #-}
3{-# language DefaultSignatures #-}
4{-# language FlexibleContexts #-}
5{-# language FlexibleInstances #-}
6{-# language MultiParamTypeClasses #-}
7{-# language ScopedTypeVariables #-}
8{-# language StandaloneKindSignatures #-}
9{-# language TypeApplications #-}
10{-# language TypeFamilies #-}
11{-# language UndecidableInstances #-}
13module Rel8.Generic.Rel8able
17 , GColumns, gfromColumns, gtoColumns
24import Data.Kind ( Constraint, Type )
25import Data.Proxy ( Proxy( Proxy ) )
26import Data.Type.Equality ( (:~:)( Refl ) )
27import GHC.Generics ( Generic, Rep, from, to )
31import Rel8.Kind.Context ( Reifiable )
32import Rel8.FCF ( Eval, Exp )
33import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
34import Rel8.Generic.Record ( Record(..) )
35import Rel8.Generic.Table ( GAlgebra )
36import qualified Rel8.Generic.Table.Record as G
37import qualified Rel8.Kind.Algebra as K ( Algebra(..) )
38import Rel8.Schema.Context ( Col )
39import Rel8.Schema.Context.Label ( Labelable )
40import Rel8.Schema.HTable ( HTable )
41import qualified Rel8.Schema.Kind as K
42import Rel8.Schema.Reify ( Reify, UnwrapReify )
43import Rel8.Schema.Result ( Result )
45 ( fromColumns, toColumns, reify, unreify
46 , TTable, TColumns, TContext, TUnreify
51type KRel8able = K.Rel8able
54-- | This type class allows you to define custom 'Table's using higher-kinded
55-- data types. Higher-kinded data types are data types of the pattern:
59-- MyType { field1 :: Column f T1 OR HK1 f
60-- , field2 :: Column f T2 OR HK2 f
62-- , fieldN :: Column f Tn OR HKn f
66-- where @Tn@ is any Haskell type, and @HKn@ is any higher-kinded type.
68-- That is, higher-kinded data are records where all fields in the record are
69-- all either of the type @Column f T@ (for any @T@), or are themselves
76-- Nested { nested1 :: MyType f
77-- , nested2 :: MyType f
81-- The @Rel8able@ type class is used to give us a special mapping operation
82-- that lets us change the type parameter @f@.
84-- [Supplying @Rel8able@ instances]
86-- This type class should be derived generically for all table types in your
87-- project. To do this, enable the @DeriveAnyType@ and @DeriveGeneric@ language
91-- \{\-\# LANGUAGE DeriveAnyClass, DeriveGeneric #-\}
93-- data MyType f = MyType { fieldA :: Column f T }
94-- deriving ( GHC.Generics.Generic, Rel8able )
96type Rel8able :: K.Rel8able -> Constraint
97class HTable (GColumns t) => Rel8able t where
98 type GColumns t :: K.HTable
100 gfromColumns :: (Labelable context, Reifiable context)
101 => GColumns t (Col (Reify context)) -> t (Reify context)
103 gtoColumns :: (Labelable context, Reifiable context)
104 => t (Reify context) -> GColumns t (Col (Reify context))
106 greify :: (Labelable context, Reifiable context)
107 => t context -> t (Reify context)
109 gunreify :: (Labelable context, Reifiable context)
110 => t (Reify context) -> t context
112 type GColumns t = G.GColumns TColumns (GRep t (Reify Result))
114 default gfromColumns :: forall context.
115 ( Generic (Record (t (Reify context)))
116 , G.GTable (TTable (Reify context)) TColumns (Col (Reify context)) (GRep t (Reify context))
117 , G.GColumns TColumns (GRep t (Reify context)) ~ GColumns t
119 => GColumns t (Col (Reify context)) -> t (Reify context)
123 G.gfromColumns @(TTable (Reify context)) @TColumns fromColumns
125 default gtoColumns :: forall context.
126 ( Generic (Record (t (Reify context)))
127 , G.GTable (TTable (Reify context)) TColumns (Col (Reify context)) (GRep t (Reify context))
128 , G.GColumns TColumns (GRep t (Reify context)) ~ GColumns t
130 => t (Reify context) -> GColumns t (Col (Reify context))
132 G.gtoColumns @(TTable (Reify context)) @TColumns toColumns .
136 default greify :: forall context.
137 ( Generic (Record (t context))
138 , Generic (Record (t (Reify context)))
139 , GMappable (TTable (Reify context)) (GRep t (Reify context))
140 , GRep t context ~ GMap TUnreify (GRep t (Reify context))
142 => t context -> t (Reify context)
146 gunmap @(TTable (Reify context)) (Proxy @TUnreify) (reify Refl) .
150 default gunreify :: forall context.
151 ( Generic (Record (t context))
152 , Generic (Record (t (Reify context)))
153 , GMappable (TTable (Reify context)) (GRep t (Reify context))
154 , GRep t context ~ GMap TUnreify (GRep t (Reify context))
156 => t (Reify context) -> t context
160 gmap @(TTable (Reify context)) (Proxy @TUnreify) (unreify Refl) .
165type Algebra :: K.Rel8able -> K.Algebra
166type Algebra t = GAlgebra (GRep t (Reify Result))
169type GRep :: K.Rel8able -> K.Context -> Type -> Type
170type GRep t context = Rep (Record (t context))
173data TUnreifyContext :: Type -> Exp K.Context
174type instance Eval (TUnreifyContext a) = UnwrapReify (Eval (TContext a))
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