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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

use crate::functional::base::*;
use crate::functional::type_app::*;

use super::structs::*;

pub trait RowCon
  : Sized + Send + 'static
{}

pub trait RowApp < F >
  : RowCon
where
  F: TyCon,
{
  type Applied: Send + 'static;
}

pub trait UncloakRow < F >
  : RowApp < F >
where
  F: TyCon,
{
  type Uncloaked: Send + 'static;

  fn full_cloak_row
    ( row: Self::Uncloaked )
    -> Self::Applied
  ;

  fn full_uncloak_row
    ( row: AppliedSum < Self, F > )
    -> Self::Uncloaked
  ;
}

pub trait HasRow < Row, F >
  : Send
{
  fn get_row
    ( self: Box < Self > )
    -> Box < Row::Applied >
  where
    F: TyCon,
    Row: RowApp < F >,
  ;

  fn get_row_borrow < 'a >
    ( &'a self )
    -> &'a Row::Applied
  where
    F: TyCon,
    Row: RowApp < F >,
  ;

  fn get_row_borrow_mut < 'a >
    ( &'a mut self )
    -> &'a mut Row::Applied
  where
    F: TyCon,
    Row: RowApp < F >,
  ;
}

pub trait ExtractRow < T >
{
  fn extract (self)
    -> T
  ;
}

pub trait RowWitnessCont < Row, F, K >
{
  fn on_row_witness
    ( self: Box < Self >,
      row: Box < Row::Applied >
    ) -> K
  where
    F: TyCon,
    Row: RowApp < F >,
  ;
}

pub trait HasRowWitness < Row, F, K >
  : HasRow < Row, F >
{
  fn with_witness
    ( self: Box < Self >,
      cont: Box < dyn RowWitnessCont < Row, F, K > >
    ) -> K
  ;
}

pub trait SplitRow : Sized + RowCon
{
  fn split_row
    < F1, F2 >
    ( row:
        AppliedSum <
          Self,
          Merge < F1, F2 >
        >
    ) ->
      ( AppliedSum < Self, F1 >,
        AppliedSum < Self, F2 >
      )
  where
    F1: TyCon,
    F2: TyCon,
  ;
}

pub trait SumFunctor
  : RowCon
{
  fn lift_sum
     < T, F1, F2 >
    ( sum: AppliedSum < Self, F1 > )
    -> AppliedSum < Self, F2 >
  where
    F1: TyCon,
    F2: TyCon,
    T: NaturalTransformation < F1, F2 >
  ;
}

pub trait InjectLift < Root >
{
  type SourceF: TyCon;
  type TargetF: TyCon;
  type InjectF: TyCon;

  fn lift_field < A >
    ( self,
      inject:
        impl Fn
          ( Applied < Self::TargetF, A > )
          -> Root
        + Send + 'static,
      row:
        Applied < Self::SourceF, A >
    ) ->
      Applied < Self::InjectF, A >
  where
    A: Send + 'static,
  ;
}

pub trait SumFunctorInject
  : RowCon
{
  fn lift_sum_inject < L, Root, Inject >
    ( ctx: L,
      inject: Inject,
      sum: AppliedSum < Self, L::SourceF >,
    ) ->
      AppliedSum < Self, L::InjectF >
  where
    L: InjectLift < Root >,
    Inject:
      Fn ( AppliedSum < Self, L::TargetF > )
        -> Root
        + Send + 'static,
  ;
}

pub trait IntersectSum : RowCon
{
  fn intersect_sum
    < F1, F2 >
    ( row1: AppliedSum < Self, F1 >,
      row2: AppliedSum < Self, F2 >,
    ) ->
      Option <
        AppliedSum <
          Self,
          Merge < F1, F2 >
        >
      >
  where
    F1: TyCon,
    F2: TyCon,
  ;
}

pub trait ElimField < F, R >
where
  F : TyCon
{
  fn elim_field < A >
    ( self,
      a : Applied < F, A >
    ) ->
      R
  where
    A: Send + 'static,
  ;
}

pub trait ElimSum : RowCon
{
  fn elim_sum
    < F, E, R >
    ( elim_field: E,
      row: AppliedSum < Self, F >
    ) ->
      R
  where
    F: TyCon,
    E: ElimField < F, R >,
  ;
}

pub trait Prism < Row >
where
  Row : RowCon,
{
  type Elem;

  fn inject_elem < F >
    ( elem : Applied < F, Self::Elem > )
    -> AppliedSum < Row, F >
  where
    F: TyCon,
  ;

  fn extract_elem < F >
    ( row : AppliedSum < Row, F > )
    ->
      Option <
        Applied < F, Self::Elem >
      >
  where
    F: TyCon,
  ;
}