@@ -81,51 +81,24 @@ type BaseIndex = 1
81-- positional field.
81-- positional field.
83 ( Generic s
83 ( Generic s
84− , ContainsAt BaseIndex index (Rep s) ~ 'True -- this is needed for the fundep
85 , GHasFieldAt BaseIndex index (Rep s) a
84 , GHasFieldAt BaseIndex index (Rep s) a
86 ) => HasFieldAt index a s where
85 ) => HasFieldAt index a s where
87 itemAt = repIso . gItemAt @BaseIndex @index
86 itemAt = repIso . gItemAt @BaseIndex @index
89−data Choice = GoLeft | GoRight
88+--------------------------------------------------------------------------------
89+type family Not (b :: Bool) where
91−class GHasFieldAtProd offset index a b ret (w :: Choice) | offset index a b w -> ret where
93+type family (<) (x :: Nat) (y :: Nat) :: Bool where
92− prodItemAt :: Lens' ((a :*: b) x) ret
94+ x < y = Not (y <=? x)
94−instance (GHasFieldAt offset index f ret) => GHasFieldAtProd offset index f g ret 'GoLeft where
96+type family If (c :: Bool) (x :: k) (y :: k) :: k where
95− prodItemAt = first . gItemAt @offset @index
97+ If 'True x _ = x
97−instance (GHasFieldAt offset index g ret) => GHasFieldAtProd offset index f g ret 'GoRight where
100+type family Min (a :: Nat) (b :: Nat) :: Nat where
98− prodItemAt = second . gItemAt @offset @index
101+ Min a b = If (a < b) a b
100−--------------------------------------------------------------------------------
102−type family Search offset index f g :: Choice where
103− Search offset index f g = Choose (ContainsAt offset index f) (ContainsAt (offset + Size f) index g)
105−type family Choose f g :: Choice where
106− Choose 'True _ = 'GoLeft
107− Choose _ 'True = 'GoRight
108− Choose _ _ = TypeError ('Text "Could not find type")
110−-- | Try find a field by position in the generic representation.
111−type family ContainsAt (offset :: Nat) (index :: Nat) f :: Bool where
112− ContainsAt offset index (D1 m f)
113− = ContainsAt offset index f
114− ContainsAt n n (S1 _ _)
116− ContainsAt _ _ (S1 _ _)
118− ContainsAt offset index (f :*: g)
119− = ContainsAt offset index f || ContainsAt (Size f + offset) index g
120− ContainsAt offset index (C1 m f)
121− = ContainsAt offset index f
122− ContainsAt offset index (Rec0 _)
124− ContainsAt offset index U1
126− ContainsAt offset index V1
128− ContainsAt offset index t = TypeError ('ShowType offset ':<>: 'Text " " ':<>: 'ShowType index)
130-- | Returns the count of terminal nodes in the generic representation.
103-- | Returns the count of terminal nodes in the generic representation.
131type family Size f :: Nat where
104type family Size f :: Nat where
@@ -133,35 +106,62 @@ type family Size f :: Nat where
134 Size (f :*: g)
107 Size (f :*: g)
135 = Size f + Size g
108 = Size f + Size g
110+ = Min (Size f) (Size g)
136 Size (C1 m f)
111 Size (C1 m f)
138 Size t = 1
113 Size t = 1
116+data Choice = GoLeft | GoRight
118+type family Search offset index f g :: Choice where
119+ Search offset index f g =
121+ (TypeError ('Text "Type does not have specified field."))
122+ (If (index < (Size f + offset))
124+ (If (index < (Size f + Size g + offset))
126+ (TypeError ('Text "Type does not have specified field."))))
140-- | If traversing to the left, offset does not change.
128-- | If traversing to the left, offset does not change.
141-- If traversing to the right, offset is incremented by size of left subtree.
129-- If traversing to the right, offset is incremented by size of left subtree.
142−type family Offset (offset :: Nat) (choice :: Choice) (size :: Nat) :: Nat where
130+type family NewOffset (offset :: Nat) (choice :: Choice) (size :: Nat) :: Nat where
143− Offset n 'GoLeft _ = n
131+ NewOffset n 'GoLeft _ = n
144− Offset n 'GoRight s = n + s
132+ NewOffset n 'GoRight s = n + s
147−type family (a :: Bool) || (b :: Bool) :: Bool where
151--------------------------------------------------------------------------------
134--------------------------------------------------------------------------------
136+class GHasFieldAtProd offset index a b ret (w :: Choice) | offset index a b w -> ret where
137+ prodItemAt :: Lens' ((a :*: b) x) ret
139+instance (GHasFieldAt offset index f ret) => GHasFieldAtProd offset index f g ret 'GoLeft where
140+ prodItemAt = first . gItemAt @offset @index
142+instance (GHasFieldAt offset index g ret) => GHasFieldAtProd offset index f g ret 'GoRight where
143+ prodItemAt = second . gItemAt @offset @index
153-- | Like 'HasFieldAt', but on the generic representation
146-- | Like 'HasFieldAt', but on the generic representation
154class GHasFieldAt (offset :: Nat) (index :: Nat) (s :: Type -> Type) a | offset index s -> a where
147class GHasFieldAt (offset :: Nat) (index :: Nat) (s :: Type -> Type) a | offset index s -> a where
155 gItemAt :: Lens' (s x) a
148 gItemAt :: Lens' (s x) a
158 ( choice ~ (Search offset index s s')
151 ( choice ~ (Search offset index s s')
159− , offset' ~ Offset offset choice (Size s)
152+ , offset' ~ NewOffset offset choice (Size s)
160− , GHasFieldAtProd offset' index s s' a choice
153+ , GHasFieldAtProd offset' index s s' a choice
162 => GHasFieldAt offset index (s :*: s') a where
155 => GHasFieldAt offset index (s :*: s') a where
163 gItemAt = prodItemAt @offset' @index @_ @_ @_ @choice
156 gItemAt = prodItemAt @offset' @index @_ @_ @_ @choice
159+ ( GHasFieldAt offset index s a
160+ , GHasFieldAt offset index s' a
162+ => GHasFieldAt offset index (s :+: s') a where
163+ gItemAt = combine (gItemAt @offset @index @s) (gItemAt @offset @index @s')
165instance GHasFieldAt offset index (K1 R a) a where
165instance GHasFieldAt offset index (K1 R a) a where
166 gItemAt f (K1 x) = fmap K1 (f x)
166 gItemAt f (K1 x) = fmap K1 (f x)