-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathCase.hs
More file actions
51 lines (41 loc) · 1.54 KB
/
Case.hs
File metadata and controls
51 lines (41 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# language FlexibleContexts #-}
{-# language FlexibleInstances #-}
{-# language NamedFieldPuns #-}
{-# language TypeFamilies #-}
{-# language UndecidableInstances #-}
{-# language ViewPatterns #-}
module Rel8.Table.Case
( Case
, case_
, undefined
)
where
-- base
import Prelude hiding ( undefined )
-- rel8
import Rel8.Expr ( Expr )
import Rel8.Expr.Bool ( caseExpr )
import Rel8.Expr.Null ( snull, unsafeUnnullify )
import Rel8.Schema.HTable ( hfield, htabulate, hspecs )
import Rel8.Schema.Null ( Nullity( Null, NotNull ) )
import Rel8.Schema.Spec ( Spec(..) )
import Rel8.Table ( Table, fromColumns, toColumns )
class Case a where
-- | Produce a table expression from a list of alternatives. Returns the
-- first table where the @Expr Bool@ expression is @True@. If no
-- alternatives are true, the given default is returned.
case_ :: [(Expr Bool, a)] -> a -> a
undefined :: a
instance {-# INCOHERENT #-} Table Expr a => Case a where
case_ (map (fmap toColumns) -> branches) (toColumns -> fallback) =
fromColumns $ htabulate $ \field -> case hfield fallback field of
fallbackExpr ->
case map (fmap (`hfield` field)) branches of
branchExprs -> caseExpr branchExprs fallbackExpr
undefined = fromColumns $ htabulate $ \field -> case hfield hspecs field of
Spec {nullity, info} -> case nullity of
Null -> snull info
NotNull -> unsafeUnnullify (snull info)
instance Case b => Case (a -> b) where
case_ branches fallback a = case_ (map (fmap ($ a)) branches) (fallback a)
undefined = const undefined