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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright (c) 2020-2022  David Sorokin <david.sorokin@gmail.com>, based in Yoshkar-Ola, Russia
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::marker::PhantomData;

#[cfg(feature="cons_mode")]
use std::sync::Arc;

#[cfg(feature="cons_mode")]
use std::slice;

#[cfg(feature="cons_mode")]
use std::mem;

#[cfg(feature="cons_mode")]
use std::ptr;

#[cfg(feature="cons_mode")]
use libc::*;

use crate::simulation;
use crate::simulation::point::Point;

#[cfg(feature="cons_mode")]
use crate::simulation::error::*;

#[cfg(feature="cons_mode")]
use crate::simulation::utils::byte_vec::*;

/// Return a new `Observer` computation by the specified pure value.
#[inline]
pub fn return_observer<M, T>(val: T) -> Return<M, T>
    where T: Clone
{
    Return { val: val, _phantom: PhantomData }
}

/// Delay the `Observer` computation.
#[inline]
pub fn delay_observer<F, O>(f: F) -> Delay<F, O>
    where F: Fn() -> O,
          O: Observer
{
    Delay { f: f, _phantom: PhantomData }
}

/// Construct a new `Observer` computation by the specified function.
#[inline]
pub fn cons_observer<F, M, T>(f: F) -> Cons<F, M, T>
    where F: Fn(&M, &Point) -> simulation::Result<T>
{
     Cons { f: f, _phantom1: PhantomData, _phantom2: PhantomData }
}

/// Return a new `Observer` computation that returns the outer message which it should react to.
#[inline]
pub fn message_observer<M>() -> Message<M>
    where M: Clone
{
    Message { _phantom: PhantomData }
}

/// Trace the computation.
#[inline]
pub fn trace_observer<O>(msg: String, comp: O) -> Trace<O>
    where O: Observer
{
    Trace { comp: comp, msg: msg}
}

/// The computation that receives notifications.
pub trait Observer {

    /// The outer message that the observer reacts to.
    type Message;

    /// The type of the item that the computation returns.
    type Item;

    /// Call the computation in the current modeling time to react to the specified message.
    #[doc(hidden)]
    fn call_observer(&self, m: &Self::Message, p: &Point) -> simulation::Result<Self::Item>;

    /// Bind the current computation with its continuation within the resulting computation.
    #[inline]
    fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
        where Self: Sized,
              U: Observer<Message = Self::Message>,
              F: Fn(Self::Item) -> U,
    {
        FlatMap { comp: self, f: f, _phantom: PhantomData }
    }

    /// Map the current computation using the specified transform.
    #[inline]
    fn map<B, F>(self, f: F) -> Map<Self, B, F>
        where Self: Sized,
              F: Fn(Self::Item) -> B,
    {
        Map { comp: self, f: f, _phantom: PhantomData }
    }

    /// Zip the current computation with another one within the resulting computation.
    #[inline]
    fn zip<U>(self, other: U) -> Zip<Self, U>
        where Self: Sized,
              U: Observer<Message = Self::Message>
    {
        Zip { comp: self, other: other }
    }

    /// The function application.
    #[inline]
    fn ap<U, B>(self, other: U) -> Ap<Self, U, B>
        where Self: Sized,
              Self::Item: Fn(U::Item) -> B,
              U: Observer<Message = Self::Message>
    {
        Ap { comp: self, other: other, _phantom: PhantomData }
    }

    /// Convert into a boxed value.
    #[inline]
    fn into_boxed(self) -> ObserverBox<Self::Message, Self::Item>
        where Self: Sized + 'static
    {
        ObserverBox::new(move |m: &Self::Message, p: &Point| {
            self.call_observer(m, p)
        })
    }
}

/// Allows converting to `Observer` computations.
pub trait IntoObserver {

    /// The target computation.
    type Observer: Observer<Message = Self::Message, Item = Self::Item>;

    /// The type of messages that are processed by the observer.
    type Message;

    /// The type of items that the observer returns.
    type Item;

    /// Convert to the `Observer` computation.
    fn into_observer(self) -> Self::Observer;
}

impl<M: Observer> IntoObserver for M {

    type Observer = M;

    type Message = M::Message;

    type Item = M::Item;

    #[inline]
    fn into_observer(self) -> Self::Observer {
        self
    }
}

/// It represents the boxed `Observer` computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
pub struct ObserverBox<M, T> {
    f: Box<dyn Fn(&M, &Point) -> simulation::Result<T>>
}

impl<M, T> ObserverBox<M, T> {

    /// Create a new boxed computation.
    #[doc(hidden)]
    #[inline]
    fn new<F>(f: F) -> Self
        where F: Fn(&M, &Point) -> simulation::Result<T> + 'static
    {
        ObserverBox {
            f: Box::new(f)
        }
    }

    /// Call the boxed function.
    #[doc(hidden)]
    #[inline]
    pub fn call(&self, m: &M, p: &Point) -> simulation::Result<T> {
        let ObserverBox { f } = self;
        f(m, p)
    }
}

impl<M, T> Observer for ObserverBox<M, T> {

    type Message = M;
    type Item = T;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &M, p: &Point) -> simulation::Result<T> {
        self.call(m, p)
    }

    #[inline]
    fn into_boxed(self) -> ObserverBox<Self::Message, Self::Item>
        where Self: Sized + 'static
    {
        self
    }
}

/// It represents a raw trait object.
#[cfg(feature="cons_mode")]
#[repr(C)]
#[derive(Copy, Clone)]
struct ObserverTraitObject {

    field1: *mut c_void,
    field2: *mut c_void
}

/// A C-friendly representaton of the `Observer` computation.
#[cfg(feature="cons_mode")]
#[repr(C)]
pub struct ObserverRepr {

    /// Delete the object.
    delete: unsafe extern "C" fn(obj: *mut ObserverTraitObject),

    /// The callback.
    callback: unsafe extern "C" fn(obj: *const ObserverTraitObject, message: *const u8, count: usize, p: *const Point) -> *mut ErrorRepr,

    /// The trait object.
    trait_object: ObserverTraitObject
}

#[cfg(feature="cons_mode")]
impl Drop for ObserverRepr {

    fn drop(&mut self) {
        unsafe {
            (self.delete)(&mut self.trait_object);
        }
    }
}

#[cfg(feature="cons_mode")]
impl ObserverRepr {

    /// Convert to a C-friendly representation.
    #[inline]
    pub fn into_repr(comp: ObserverBox<&[u8], ()>) -> ObserverRepr {
        unsafe {
            ObserverRepr {
                delete: delete_observer_repr,
                callback: call_observer_repr,
                trait_object: mem::transmute(comp)
            }
        }
    }

    /// Call the representation.
    #[inline]
    fn call_repr(&self, m: &[u8], p: &Point) -> *mut ErrorRepr {
        unsafe {
            (self.callback)(&self.trait_object, m.as_ptr(), m.len(), p)
        }
    }
}

/// Call the `ObserverBox` representation.
#[cfg(feature="cons_mode")]
unsafe extern "C" fn call_observer_repr(comp: *const ObserverTraitObject, m: *const u8, count: usize, p: *const Point) -> *mut ErrorRepr {
    let comp: ObserverBox<&[u8], ()> = mem::transmute(*comp);
    let m = slice::from_raw_parts(m, count);
    match comp.call_observer(&m, &*p) {
        Result::Ok(()) => {
            mem::forget(comp);
            ptr::null_mut()
        },
        Result::Err(e) => {
            mem::forget(comp);
            let e = ErrorRepr::new(e);
            Box::into_raw(Box::new(e))
        }
    }
}

/// Delete the `ObserverBox` representation.
#[cfg(feature="cons_mode")]
unsafe extern "C" fn delete_observer_repr(comp: *mut ObserverTraitObject) {
    let _: ObserverBox<&[u8], ()> = mem::transmute(*comp);
}

#[cfg(feature="cons_mode")]
impl Observer for ObserverRepr {

    type Message = Arc<ByteVecRepr>;

    type Item = ();

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &Self::Message, p: &Point) -> simulation::Result<Self::Item> {
        unsafe {
            let m = m.slice();
            let e = self.call_repr(m, p);
            if e == ptr::null_mut() {
                Result::Ok(())
            } else {
                let e = ffi_error_repr_into_error(e);
                Result::Err(e)
            }
        }
    }
}

/// Allows creating the `Observer` computation from a pure value.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Return<M, T> {

    /// Return a pure value, which is then transformed to the computation.
    val: T,

    /// To keep the type parameter.
    _phantom: PhantomData<M>
}

impl<M, T> Observer for Return<M, T>
    where T: Clone
{
    type Message = M;
    type Item = T;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, _: &M, _: &Point) -> simulation::Result<T> {
        Result::Ok(self.val.clone())
    }
}

/// Allows delaying the `Observer` computation by the specified function.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Delay<F, O> {

    /// Return the computation.
    f: F,

    /// To keep the type parameter.
    _phantom: PhantomData<O>
}

impl<F, O> Observer for Delay<F, O>
    where F: Fn() -> O,
          O: Observer
{
    type Message = O::Message;
    type Item = O::Item;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &O::Message, p: &Point) -> simulation::Result<O::Item> {
        let Delay { f, _phantom } = self;
        f().call_observer(m, p)
    }
}

/// Allows constructing the `Observer` computation by the specified function.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Cons<F, M, T> {

    /// The function of time point.
    f: F,

    /// To keep the type parameter.
    _phantom1: PhantomData<M>,

    /// To keep the type parameter.
    _phantom2: PhantomData<T>
}

impl<F, M, T> Observer for Cons<F, M, T>
    where F: Fn(&M, &Point) -> simulation::Result<T>
{
    type Message = M;
    type Item = T;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &M, p: &Point) -> simulation::Result<T> {
        let Cons { f, _phantom1, _phantom2 } = self;
        f(m, p)
    }
}

/// Allows creating the `Observer` computation that returns the outer message.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Message<M> {

    /// To keep the type parameter.
    _phantom: PhantomData<M>
}

impl<M> Observer for Message<M>
    where M: Clone
{
    type Message = M;
    type Item = M;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &Self::Message, _: &Point) -> simulation::Result<Self::Item> {
        Result::Ok(m.clone())
    }
}

/// The monadic bind for the `Observer` computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct FlatMap<O, U, F> {

    /// The current computation.
    comp: O,

    /// The continuation of the current computation.
    f: F,

    /// To keep the type parameter.
    _phantom: PhantomData<U>
}

impl<O, U, F> Observer for FlatMap<O, U, F>
    where O: Observer,
          U: Observer<Message = O::Message>,
          F: Fn(O::Item) -> U,
{
    type Message = U::Message;
    type Item = U::Item;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &Self::Message, p: &Point) -> simulation::Result<Self::Item> {
        let FlatMap { comp, f, _phantom } = self;
        match comp.call_observer(m, p) {
            Result::Ok(a) => {
                let u = f(a);
                u.call_observer(m, p)
            },
            Result::Err(e) => {
                Result::Err(e)
            }
        }
    }
}

/// The functor for the `Observer` computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Map<O, B, F> {

    /// The current computation.
    comp: O,

    /// The transform.
    f: F,

    /// To keep the type parameter.
    _phantom: PhantomData<B>
}

impl<O, B, F> Observer for Map<O, B, F>
    where O: Observer,
          F: Fn(O::Item) -> B,
{
    type Message = O::Message;
    type Item = B;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &Self::Message, p: &Point) -> simulation::Result<Self::Item> {
        let Map { comp, f, _phantom } = self;
        match comp.call_observer(m, p) {
            Result::Ok(a) => Result::Ok(f(a)),
            Result::Err(e) => Result::Err(e)
        }
    }
}

/// The zip of two `Observer` computations.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Zip<O, U> {

    /// The current computation.
    comp: O,

    /// Another computation.
    other: U,
}

impl<O, U> Observer for Zip<O, U>
    where O: Observer,
          U: Observer<Message = O::Message>
{
    type Message = O::Message;
    type Item = (O::Item, U::Item);

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &Self::Message, p: &Point) -> simulation::Result<Self::Item> {
        let Zip { comp, other } = self;
        match comp.call_observer(m, p) {
            Result::Ok(a) => {
                match other.call_observer(m, p) {
                    Result::Ok(b) => Result::Ok((a, b)),
                    Result::Err(e) => Result::Err(e)
                }
            },
            Result::Err(e) => Result::Err(e)
        }
    }
}

/// The function application for the `Observer` computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Ap<O, U, B> {

    /// The current computation.
    comp: O,

    /// The continuation of the current computation.
    other: U,

    /// To keep the type parameter.
    _phantom: PhantomData<B>
}

impl<O, U, B> Observer for Ap<O, U, B>
    where O: Observer,
          U: Observer<Message = O::Message>,
          O::Item: Fn(U::Item) -> B,
{
    type Message = O::Message;
    type Item = B;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &Self::Message, p: &Point) -> simulation::Result<Self::Item> {
        let Ap { comp, other, _phantom } = self;
        match comp.call_observer(m, p) {
            Result::Ok(f) => {
                match other.call_observer(m, p) {
                    Result::Ok(a) => Result::Ok(f(a)),
                    Result::Err(e) => Result::Err(e)
                }
            },
            Result::Err(e) => Result::Err(e)
        }
    }
}

/// Trace the computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Trace<O> {

    /// The computation.
    comp: O,

    /// The message to print.
    msg: String
}

impl<O> Observer for Trace<O>
    where O: Observer
{
    type Message = O::Message;
    type Item = O::Item;

    #[doc(hidden)]
    #[inline]
    fn call_observer(&self, m: &Self::Message, p: &Point) -> simulation::Result<Self::Item> {
        let Trace { comp, msg } = self;
        p.trace(&msg);
        comp.call_observer(m, p)
    }
}