aboutsummaryrefslogtreecommitdiff
path: root/ch12_12.i.hs
blob: cf3ddcf932c18ee6d5fa245f39d4d9fb6d770d9e (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE TypeFamilies #-}

import Data.Kind (Constraint, Type)
import Data.Proxy (Proxy (Proxy))
import Data.Vector (Vector, cons, empty, ifilter, unsafeIndex, (//))
import Fcf qualified as F
import Fcf.Data.List qualified as F (Cons)
import Fcf.Data.Nat qualified as F (Nat)
import GHC.OverloadedLabels (IsLabel, fromLabel)
import GHC.TypeLits qualified as T
import Unsafe.Coerce (unsafeCoerce)

data Any (f :: k -> Type) where
  Any :: f t -> Any f

data OpenProduct (f :: k -> Type) (ts :: [(T.Symbol, k)]) where
  OpenProduct :: Vector (Any f) -> OpenProduct f ts

type UniqueKey (key :: k) (ts :: [(k, t)]) =
  F.Null F.=<< F.Filter (F.TyEq key F.<=< F.Fst) ts

type family
  RequireUniqueKey
    (result :: Bool)
    (key :: T.Symbol)
    (t :: k)
    (ts :: [(T.Symbol, k)])
    :: Constraint
  where
  RequireUniqueKey 'True key t ts = ()
  RequireUniqueKey 'False key t ts =
    T.TypeError
      ( 'T.Text "Attempting to add a field named `"
          'T.:<>: 'T.Text key
          'T.:<>: 'T.Text "' with type "
          'T.:<>: 'T.ShowType t
          'T.:<>: 'T.Text " to an OpenProduct."
          'T.:$$: 'T.Text "But the OpenProduct already has a field `"
            'T.:<>: 'T.Text key
            'T.:<>: 'T.Text "' with type "
            'T.:<>: 'T.ShowType (F.Eval (LookupType key ts))
          'T.:$$: 'T.Text "Consider using `update' instead of `insert'."
      )

type FindIndex (key :: T.Symbol) (ts :: [(T.Symbol, k)]) =
  F.FindIndex (F.TyEq key F.<=< F.Fst) ts

-- Exercise 12-i
-- Add helpful type errors to `OpenProduct`'s `update` and `delete` functions.

-- My take is a bit different than Sandy's solution. `FindElemWithBase`
-- generalizes element finding, so we can re-implement the original `FindElem`
-- (which gets stuck) plus a new `FindElemDuring` (which provides the helpful
-- error message) using it.
type FindElemWithBase (key :: T.Symbol) (ts :: [(T.Symbol, k)]) (base :: T.Nat) =
  F.Eval (F.FromMaybe base F.=<< FindIndex key ts)

type FindElem (key :: T.Symbol) (ts :: [(T.Symbol, k)]) =
  FindElemWithBase key ts F.Stuck

type FindElemDuring (action :: T.Symbol) (key :: T.Symbol) (ts :: [(T.Symbol, k)]) =
  FindElemWithBase
    key
    ts
    ( T.TypeError
        ( 'T.Text "Attempting to "
            'T.:<>: 'T.Text action
            'T.:<>: 'T.Text " a field named `"
            'T.:<>: 'T.Text key
            'T.:<>: 'T.Text "' in an OpenProduct."
            'T.:$$: 'T.Text "But the OpenProduct does not have a field `"
              'T.:<>: 'T.Text key
              'T.:<>: 'T.Text "'."
        )
    )

type FindableDuring action key ts =
  ( T.KnownNat (FindElemDuring action key ts)
  , T.KnownNat (FindElem key ts)
  )

type LookupType (key :: k) (ts :: [(k, t)]) =
  F.FromMaybe F.Stuck F.=<< F.Lookup key ts

type UpdateElem (key :: T.Symbol) (t :: k) (ts :: [(T.Symbol, k)]) =
  F.SetIndex (FindElem key ts) '(key, t) ts

type DeleteElem (key :: T.Symbol) (ts :: [(T.Symbol, k)]) =
  F.Filter (F.Not F.<=< F.TyEq key F.<=< F.Fst) ts

type UpsertElem (key :: T.Symbol) (t :: k) (ts :: [(T.Symbol, k)]) =
  F.UnMaybe
    (F.Cons '(key, t) ts)
    (F.ConstFn (F.Eval (UpdateElem key t ts)))
    F.=<< FindIndex key ts

data Key (key :: T.Symbol) = Key

instance key ~ key' => IsLabel key (Key key') where
  fromLabel = Key

class MaybeIndex (m :: Maybe F.Nat) where
  maybeIndex :: Maybe Int

instance MaybeIndex 'Nothing where
  maybeIndex = Nothing

instance T.KnownNat a => MaybeIndex ('Just a) where
  maybeIndex = Just $ fromIntegral $ T.natVal $ Proxy @a

nil :: OpenProduct f '[]
nil = OpenProduct empty

insert
  :: RequireUniqueKey (F.Eval (UniqueKey key ts)) key t ts
  => Key key
  -> f t
  -> OpenProduct f ts
  -> OpenProduct f ('(key, t) ': ts)
insert _ ft (OpenProduct v) = OpenProduct $ cons (Any ft) v

findElem
  :: forall key ts
   . T.KnownNat (FindElem key ts)
  => Int
findElem = fromIntegral $ T.natVal $ Proxy @(FindElem key ts)

get
  :: forall key ts f
   . FindableDuring "get" key ts
  => Key key
  -> OpenProduct f ts
  -> f (F.Eval (LookupType key ts))
get _ (OpenProduct v) = unAny $ unsafeIndex v $ findElem @key @ts
 where
  unAny (Any a) = unsafeCoerce a

update
  :: forall key ts t f
   . FindableDuring "update" key ts
  => Key key
  -> f t
  -> OpenProduct f ts
  -> OpenProduct f (F.Eval (UpdateElem key t ts))
update _ ft (OpenProduct v) = OpenProduct $ v // [(findElem @key @ts, Any ft)]

delete
  :: forall key ts f
   . FindableDuring "delete" key ts
  => Key key
  -> OpenProduct f ts
  -> OpenProduct f (F.Eval (DeleteElem key ts))
delete _ (OpenProduct v) = OpenProduct $ flip ifilter v $ curry $ (findElem @key @ts ==) . fst

upsert
  :: forall key ts t f
   . MaybeIndex (F.Eval (FindIndex key ts))
  => Key key
  -> f t
  -> OpenProduct f ts
  -> OpenProduct f (F.Eval (UpsertElem key t ts))
upsert _ ft (OpenProduct v) =
  case maybeIndex @(F.Eval (FindIndex key ts)) of
    Nothing -> OpenProduct $ cons (Any ft) v
    Just i -> OpenProduct $ v // [(i, Any ft)]