summaryrefslogtreecommitdiffstats
path: root/sca-cpp/branches/cpp-contrib/kernel/monad.hpp
blob: 8aa4bc1662ef0a7a94af6e5d157d7431e167eb81 (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
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#ifndef tuscany_monad_hpp
#define tuscany_monad_hpp

/**
 * Simple monad implementations.
 */

#include "function.hpp"
#include "string.hpp"
#include "stream.hpp"

namespace tuscany
{

/**
 * Identity monad. Just wraps a value.
 * To get the value in the monad, just cast it to the value type.
 */
template<typename V> class id {
public:
    id(const V& v) : v(v) {
    }

    const id<V>& operator=(const id<V>& m) {
        if(this == &m)
            return *this;
        v = m.v;
        return *this;
    }

    const bool operator!=(const id<V>& m) const {
        return !this->operator==(m);
    }

    const bool operator==(const id<V>& m) const {
        if (&m == this)
            return true;
        return v == m.v;
    }

private:
    const V v;

    template<typename X> friend const X content(const id<X>& m);
};

/**
 * Write an identity monad to a stream.
 */
template<typename V> ostream& operator<<(ostream& out, const id<V>& m) {
    out << content(m);
    return out;
}

/**
 * Returns the content of an identity monad.
 */
template<typename V> const V content(const id<V>& m) {
    return m.v;
}

/**
 * Return an identity monad from a value.
 */
template<typename V> const id<V> mkunit(const V& v) {
    return id<V>(v);
}

template<typename V> const lambda<id<V>(const V)> unit() {
    return mkunit<V>;
}

/**
 * Bind a function to an identity monad. Pass the value in the monad to the function.
 */
template<typename R, typename V> const id<R> operator>>(const id<V>& m, const lambda<id<R>(const V)>& f) {
    return f(content(m));
}

template<typename R, typename V> const id<R> operator>>(const id<V>& m, const id<R> (* const f)(const V)) {
    return f(content(m));
}

/**
 * Maybe monad. Used to represent an optional value, which may be there or not.
 * To get the value in the monad, just cast it to the value type.
 */
template<typename V> class maybe {
public:
    maybe(const V& v) : hasv(true), v(v) {
    }

    maybe() : hasv(false) {
    }

    const maybe<V>& operator=(const maybe<V>& m) {
        if(this == &m)
            return *this;
        hasv = m.hasv;
        if (hasv)
            v = m.v;
        return *this;
    }

    const bool operator!=(const maybe<V>& m) const {
        return !this->operator==(m);
    }

    const bool operator==(const maybe<V>& m) const {
        if (this == &m)
            return true;
        if (!hasv)
            return !m.hasv;
        return m.hasv && v == m.v;
    }

private:
    const bool hasv;
    V v;

    template<typename X> friend const bool hasContent(const maybe<X>& m);
    template<typename X> friend const X content(const maybe<X>& m);
};

/**
 * Write a maybe monad to a stream.
 */
template<typename V> ostream& operator<<(ostream& out, const maybe<V>& m) {
    if (!hasContent(m)) {
        out << "nothing";
        return out;
    }
    out << content(m);
    return out;
}

/**
 * Return a maybe monad with a value in it.
 */
template<typename V> const maybe<V> mkjust(const V& v) {
    return maybe<V>(v);
}

template<typename V> const lambda<maybe<V>(const V)> just() {
    return mkjust<V>;
}

/**
 * Returns true if a maybe monad contains a content.
 */
template<typename V> const bool hasContent(const maybe<V>& m) {
    return m.hasv;
}

/**
 * Returns the content of a maybe monad.
 */
template<typename V> const V content(const maybe<V>& m) {
    return m.v;
}

/**
 * Bind a function to a maybe monad. Passes the value in the monad to the function
 * if present, or does nothing if there's no value.
 */
template<typename R, typename V> const maybe<R> operator>>(const maybe<V>& m, const lambda<maybe<R>(const V)>& f) {
    if (!hasContent(m))
        return m;
    return f(content(m));
}

template<typename R, typename V> const maybe<R> operator>>(const maybe<V>& m, const maybe<R> (* const f)(const V)) {
    if (!hasContent(m))
        return m;
    return f(content(m));
}

/**
 * Failable monad. Used to represent either a success value or a failure.
 * To get the value in the monad, just cast it to the value type.
 * To get the failure in the monad, cast it to the failure type.
 */
template<typename V, typename F = string> class failable {
public:
    failable() : hasv(false) {
    }

    failable(const V& v) : hasv(true), v(v) {
    }

    failable(const failable<V, F>& m) : hasv(m.hasv), v(m.v), f(m.f) {
    }

    const failable<V, F>& operator=(const failable<V, F>& m) {
        if (&m == this)
            return *this;
        hasv = m.hasv;
        v = m.v;
        f = m.f;
        return *this;
    }

    const bool operator!=(const failable<V, F>& m) const {
        return !this->operator==(m);
    }

    const bool operator==(const failable<V, F>& m) const {
        if (this == &m)
            return true;
        if (!hasv)
            return !m.hasv && f == m.f;
        return m.hasv && v == m.v;
    }

private:
    failable(const bool hasv, const F& f) : hasv(hasv), f(f) {
    }

    template<typename A, typename B> friend const bool hasContent(const failable<A, B>& m);
    template<typename A, typename B> friend const A content(const failable<A, B>& m);
    template<typename A, typename B> friend const B reason(const failable<A, B>& m);
    template<typename A, typename B> friend const failable<A, B> mkfailure(const B& f);
    template<typename A> friend const failable<A, string> mkfailure();

    bool hasv;
    V v;
    F f;
};

/**
 * Write a failable monad to a stream.
 */
template<typename V, typename F> ostream& operator<<(ostream& out, const failable<V, F>& m) {
    if (!hasContent(m)) {
        out << reason(m);
        return out;
    }
    out << content(m);
    return out;
}

/**
 * Returns a failable monad with a success value in it.
 */
template<typename V, typename F> const failable<V, F> mksuccess(const V& v) {
    return failable<V, F>(v);
}

template<typename V, typename F> const lambda<failable<V, F>(const V)> success() {
    return mksuccess<V, F>;
}

/**
 * Returns a failable monad with a failure in it.
 */
template<typename V, typename F> const failable<V, F> mkfailure(const F& f) {
    debug(f, "failable::mkfailure");
    return failable<V, F>(false, f);
}

template<typename V> const failable<V> mkfailure(const char* f) {
    return mkfailure<V, string>(string(f));
}

template<typename V> const failable<V> mkfailure() {
    return failable<V, string>(false, string());
}

template<typename V, typename F> const lambda<failable<V, F>(const V)> failure() {
    return mkfailure<V, F>;
}

/**
 * Returns true if the monad contains a content.
 */
template<typename V, typename F> const bool hasContent(const failable<V, F>& m) {
    return m.hasv;
}

/**
 * Returns the content of a failable monad.
 */
template<typename V, typename F> const V content(const failable<V, F>& m) {
    return m.v;
}

/**
 * Returns the reason for failure of a failable monad.
 */
template<typename V, typename F> const F reason(const failable<V, F>& m) {
    return m.f;
}

/**
 * Bind a function to a failable monad. Passes the success value in the monad to the function
 * if present, or does nothing if there's no value and a failure instead.
 */
template<typename R, typename FR, typename V, typename FV>
const failable<R, FR> operator>>(const failable<V, FV>& m, const lambda<failable<R, FR>(const V)>& f) {
    if (!hasContent(m))
        return m;
    return f(content(m));
}

template<typename R, typename FR, typename V, typename FV>
const failable<R, FR> operator>>(const failable<V, FV>& m, const failable<R, FR> (* const f)(const V)) {
    if (!hasContent(m))
        return m;
    return f(content(m));
}

/**
 * State + content pair data type used by the state monad.
 */
template<typename S, typename V> class scp {
public:
    scp(const S& s, const V& v) : s(s), v(v) {
    }

    operator const S() const {
        return s;
    }

    operator const V() const {
        return v;
    }

    const scp<S, V>& operator=(const scp<S, V>& p) {
        if(this == &p)
            return *this;
        s = p.s;
        v = p.v;
        return *this;
    }

    const bool operator!=(const scp<S, V>& p) const {
        return !this->operator==(p);
    }

    const bool operator==(const scp<S, V>& p) const {
        if (this == &p)
            return true;
        return s == p.s && v == p.v;
    }

private:
    const S s;
    const V v;

    template<typename A, typename B> friend const A scpstate(const scp<A, B>& m);
    template<typename A, typename B> friend const B content(const scp<A, B>& m);
};

/**
 * Returns the state of a state-content pair.
 */
template<typename S, typename V> const S scpstate(const scp<S, V>& m) {
    return m.s;
}

/**
 * Returns the content of a state-content pair.
 */
template<typename S, typename V> const S content(const scp<S, V>& m) {
    return m.v;
}

/**
 * State monad. Used to represent the combination of a state and a content.
 */
template<typename S, typename V> class state {
public:
    state(const lambda<scp<S, V>(const S)>& f) : f(f) {
    }

    const scp<S, V> operator()(const S& s) const {
        return f(s);
    }

    const state<S, V>& operator=(const state<S, V>& m) {
        if(this == &m)
            return *this;
        f = m.f;
        return *this;
    }

    const bool operator!=(const state<S, V>& m) const {
        return !this->operator==(m);
    }

    const bool operator==(const state<S, V>& m) const {
        if (this == &m)
            return true;
        return f == m.f;
    }

private:
    const lambda<scp<S, V>(const S)> f;
};

/**
 * Write a state monad to a stream.
 */
template<typename S, typename V> ostream& operator<<(ostream& out, const state<S, V>& m) {
    const S s = m;
    const V v = m;
    out << '(' << s << ' ' << v << ')';
    return out;
}

/**
 * Return a state monad carrying a result content.
 */
template<typename S, typename V> struct returnState {
    const V v;
    returnState(const V& v) : v(v) {
    }
    const scp<S, V> operator()(const S& s) const {
        return scp<S, V>(s, v);
    }
};

template<typename S, typename V> const state<S, V> result(const V& v) {
    return state<S, V>(returnState<S, V>(v));
}

/**
 * Return a state monad with a transformer function.
 * A transformer function takes a state and returns an scp pair carrying a content and a
 * new (transformed) state.
 */
template<typename S, typename V> const state<S, V> transformer(const lambda<scp<S, V>(const S)>& f) {
    return state<S, V>(f);
}

/**
 * Bind a function to a state monad. The function takes a content and returns a state
 * monad carrying a return content.
 */
template<typename S, typename A, typename B> struct stateBind {
    const state<S, A> st;
    const lambda<state<S, B>(const A)>f;

    stateBind(const state<S, A>& st, const lambda<state<S, B>(const A)>& f) : st(st), f(f) {
    }

    const scp<S, B> operator()(const S& is) const {
        const scp<S, A> iscp = st(is);
        const state<S, B> m = f((A)iscp);
        return m((S)iscp);
    }
};

template<typename S, typename A, typename B>
const state<S, B> operator>>(const state<S, A>& st, const lambda<state<S, B>(const A)>& f) {
    return state<S, B>(stateBind<S, A , B>(st, f));
}

template<typename S, typename A, typename B>
const state<S, B> operator>>(const state<S, A>& st, const state<S, B> (* const f)(const A)) {
    return state<S, B>(stateBind<S, A , B>(st, f));
}

}
#endif /* tuscany_monad_hpp */