This file is indexed.

/usr/include/flint/flintxx/expression.h is in libflint-dev 2.5.2-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/*=============================================================================

    This file is part of FLINT.

    FLINT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    FLINT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2013 Tom Bachmann

******************************************************************************/

#ifndef CXX_EXPRESSION_H
#define CXX_EXPRESSION_H

// TODO
// * static asserts

#include <iosfwd>
#include <string>
#include <cstdio>

#include "evaluation_tools.h"
#include "expression_traits.h"
#include "mp.h"
#include "rules.h"
#include "traits.h"
#include "tuple.h"

namespace flint {
namespace detail {
// Helper traits used by the "expression" class, in particular the evaluate()
// method. This is the general (i.e. non-immediate) case,
// which requires actual work.
template<class Operation, class Expr, class Data>
struct evaluation_traits
{
    typedef typename Expr::derived_t derived_t;
    typedef typename mp::find_evaluation<
        Operation, Data, false>::type rule_t;
    typedef typename mp::find_evaluation<
        Operation, Data, true>::type temp_rule_t;
    typedef typename rule_t::return_t evaluation_return_t;
    typedef evaluation_return_t evaluated_t;

    static evaluation_return_t evaluate(const derived_t& from)
    {
        evaluated_t res = 
            rules::instantiate_temporaries<derived_t, evaluated_t>::get(from);
        evaluate_into_fresh(res, from);
        return res;
    }

    template<class T>
    static void evaluate_into(T& to, const derived_t& from)
    {
        typedef mp::back_tuple<typename rule_t::temporaries_t> back_t;
        typename back_t::type temps_backing =
            mp::htuples::fill<typename back_t::type>(
                tools::temporaries_filler(from));
        typename rule_t::temporaries_t temps;
        back_t::init(temps, temps_backing, 0);
        rule_t::doit(from._data(), temps, &to);
    }

    static void evaluate_into_fresh(evaluation_return_t& to, const derived_t& from)
    {
        typedef mp::back_tuple<
            typename temp_rule_t::temporaries_t,
            evaluation_return_t
          > back_t;
        typename back_t::type temps_backing =
            mp::htuples::fill<typename back_t::type>(
                tools::temporaries_filler(from));
        typename temp_rule_t::temporaries_t temps;
        back_t::init(temps, temps_backing, &to);
        temp_rule_t::doit(from._data(), temps, &to);
    }
};

// This is the special case of an immediate argument, where "evaluation" is
// at most assignment.
template<class Expr, class Data>
struct evaluation_traits<operations::immediate, Expr, Data>
{
    typedef typename Expr::derived_t derived_t;
    typedef typename Expr::derived_t evaluated_t;
    typedef evaluated_t& evaluation_return_t;

    static evaluated_t& evaluate(derived_t& d) {return d;}
    static const evaluated_t& evaluate(const derived_t& d) {return d;}

    template<class T>
    static void evaluate_into(T& to, const derived_t& from)
    {
        rules::assignment<T, derived_t>::doit(to, from);
    }

    static void evaluate_into_fresh(derived_t& to, const derived_t& from)
    {
        evaluate_into(to, from);
    }
};
} // detail

// The main expression template class.
//
// The argument Derived must have the following form:
// struct derived
// {
//     template<class Operation, class Data>
//     struct type
//     {
//         typedef XYZ result;
//     };
// };
// See derived_wrapper below for a common example.
//
// Note that, while Data does not have to be default constructible,
// it *does* need to be copy-constructible, and have a working destructor.
template<class Derived, class Operation, class Data>
class expression
{
private:
    Data data;

protected:
    explicit expression(const Data& d) : data(d) {}

public:
    // internal -- see is_expression implementation.
    typedef void IS_EXPRESSION_MARKER;

    typedef detail::evaluation_traits<Operation, expression, Data> ev_traits_t;
    typedef typename Derived::template type<Operation, Data>::result derived_t;
    typedef typename ev_traits_t::evaluated_t evaluated_t;
    typedef typename ev_traits_t::evaluation_return_t evaluation_return_t;
    typedef Data data_t;
    typedef Operation operation_t;

private:
    derived_t& downcast() {return *static_cast<derived_t*>(this);}
    const derived_t& downcast() const
    {
        return *static_cast<const derived_t*>(this);
    }

    // Some helpers for initialization, since it is not possible to
    // conditionally enable constructors in C++98
    template<class T>
    static data_t get_data(const T& t,
        typename mp::disable_if<traits::is_lazy_expr<T> >::type* = 0)
    {
        return data_t(t);
    }
    template<class T>
    static data_t get_data(T& t,
        typename mp::disable_if<traits::is_lazy_expr<T> >::type* = 0)
    {
        return data_t(t);
    }
    template<class T>
    static data_t get_data(const T& t,
        typename mp::enable_if<traits::is_lazy_expr<T> >::type* = 0,
        typename mp::disable_if<
            mp::equal_types<typename T::evaluated_t, derived_t> >::type* = 0)
    {
        return data_t(t.evaluate());
    }
    template<class T>
    static data_t get_data(const T& t,
        typename mp::enable_if<traits::is_lazy_expr<T> >::type* = 0,
        typename mp::enable_if<
            mp::equal_types<typename T::evaluated_t, derived_t> >::type* = 0)
    {
        return data_t(t.evaluate()._data());
    }

    // Invoke the data copy constructor when appropriate
    static data_t get_data(const derived_t& o)
    {
        return data_t(o._data());
    }
    static data_t get_data(derived_t& o)
    {
        return data_t(o._data());
    }

    // Having the empty constructor here delays its instantiation, and allows
    // compiling even if data is *not* default constructible.
    static data_t get_data() {return data_t();}

public:
    // forwarded constructors
    template<class T>
    explicit expression(const T& t)
        : data(get_data(t)) {}

    template<class T>
    explicit expression(T& t)
        : data(get_data(t)) {}

    template<class T, class U>
    expression(const T& t, const U& u)
        : data(t, u) {}
    template<class T, class U>
    expression(T& t, const U& u)
        : data(t, u) {}

    template<class T, class U, class V>
    expression(const T& t, const U& u, const V& v)
        : data(t, u, v) {}
    template<class T, class U, class V>
    expression(T& t, const U& u, const V& v)
        : data(t, u, v) {}
    template<class T, class U, class V, class W>
    expression(const T& t, const U& u, const V& v, const W& w)
        : data(t, u, v, w) {}
    template<class T, class U, class V, class W>
    expression(T& t, const U& u, const V& v, const W& w)
        : data(t, u, v, w) {}

    expression() : data(get_data()) {}

    expression& operator=(const expression& o)
    {
        this->set(o.downcast());
        return *this;
    }

    // See rules::instantiate_temporaries for explanation.
    evaluated_t create_temporary() const
    {
        return evaluated_t();
    }

    Data& _data() {return data;}
    const Data& _data() const {return data;}

    void print(std::ostream& o) const
    {
        tools::print_using_str<evaluated_t>::doit(evaluate(), o);
    }

    std::string to_string(int base = 10) const
    {
        return rules::to_string<evaluated_t>::get(evaluate(), base);
    }

    template<class T>
    T to() const
    {
        return rules::conversion<T, evaluated_t>::get(evaluate());
    }

    int print(FILE* f = stdout) const
    {
        return rules::cprint<evaluated_t>::doit(f, evaluate());
    }
    int print_pretty(FILE* f = stdout) const
    {
        return rules::print_pretty<evaluated_t>::doit(f, evaluate());
    }
    template<class T>
    int print_pretty(const T& extra, FILE* f = stdout) const
    {
        return rules::print_pretty<evaluated_t>::doit(f, evaluate(), extra);
    }
    int read(FILE* f = stdin)
    {
        return rules::read<derived_t>::doit(f, downcast());
    }

    typename traits::make_const<evaluation_return_t>::type evaluate() const
    {
        return ev_traits_t::evaluate(downcast());
    }
    evaluation_return_t evaluate() {return ev_traits_t::evaluate(downcast());}

    template<class T>
    void set(const T& t,
            typename mp::enable_if<traits::is_expression<T> >::type* = 0,
            typename mp::enable_if<traits::can_evaluate_into<
                derived_t, typename T::evaluated_t> >::type* = 0)
    {
        T::ev_traits_t::evaluate_into(downcast(), t);
    }
    template<class T>
    void set(const T& t,
            typename mp::enable_if<traits::is_expression<T> >::type* = 0,
            typename mp::disable_if<traits::can_evaluate_into<
                derived_t, typename T::evaluated_t> >::type* = 0)
    {
        rules::assignment<derived_t, typename T::evaluated_t>::doit(
            downcast(), t.evaluate());
    }
    template<class T>
    void set(const T& t,
            typename mp::disable_if<traits::is_expression<T> >::type* = 0)
    {
        rules::assignment<derived_t, T>::doit(downcast(), t);
    }

    template<class T>
    bool equals(const T& t,
            typename mp::enable_if<traits::is_lazy_expr<T> >::type* = 0) const
    {
        return equals(t.evaluate());
    }
    template<class T>
    bool equals(const T& t,
            typename mp::disable_if<traits::is_lazy_expr<T> >::type* = 0) const
    {
        return tools::equals_using_cmp<evaluated_t, T>::get(evaluate(), t);
    }

    template<class Op, class NData>
    struct make_helper
    {
        typedef typename Derived::template type<Op, NData>::result type;
        static type make(const NData& ndata)
        {
            return type(ndata);
        }
    };
};

// If your expression template is of the form
//   template<class Operation, class Data>
//   class my_expression ...
// then derived_wrapper<my_expression> is a valid argument for Derived in
// the expression class above.
template<template<class O, class D> class Derived>
struct derived_wrapper
{
    template<class Operation, class Data>
    struct type
    {
        typedef Derived<Operation, Data> result;
    };
};

// If your expression template is of the form
//   template<class Extra, class Opeartion, class Data>
//   class my_expression2 ...
// where Extra is some extra information which should be passed on unchanged,
// then derived_wrapper2<my_expression2, Extra> is a valid argument for Derived
// in the expression class above.
template<template<class E, class O, class D> class Derived, class Extra>
struct derived_wrapper2
{
    template<class Operation, class Data>
    struct type
    {
        typedef Derived<Extra, Operation, Data> result;
    };
};


// operators

namespace detail {
// These traits determine how arguments of an expression template are stored.
// E.g. (e1 + e2) yields a new expression template with a two-argument tuple
// as Data. If e1 is an immediate, then we want to (usually) store it by
// reference, to avoid copies. If not, we can just store by value (since
// copying e1 just copies the references anyway) and avoid indirection.
// (Similarly for e2.)
template<class Expr>
struct storage_traits
    : mp::if_<
          traits::is_immediate<Expr>,
          typename traits::forwarding<Expr>::type,
          Expr
        > { };
// See tuple.h.
template<>
struct storage_traits<detail::UNUSED> {typedef detail::UNUSED type;};

template<class ev_t, class Op, class type>
struct nary_op_helper_step2
{
    typedef typename ev_t::return_t Expr;
    typedef typename Expr::template make_helper<Op, type> make_helper;
    typedef typename make_helper::type return_t;
};
template<class Op, class type>
struct nary_op_helper_step2<rules::UNIMPLEMENTED, Op, type>
{
    struct return_t { };
    struct make_helper { };
};

// Helper to determine the return type of an expression, where Data is already
// the correct tuple type.
// The step1/step2 splitting above is necessary to avoid compiler errors in
// case there is not actually any rule.
template<class Op, class Data>
struct nary_op_helper
{
    typedef typename mp::find_evaluation<Op, Data, true>::type ev_t;
    typedef nary_op_helper_step2<ev_t, Op, Data> nohs2;
    typedef typename nohs2::return_t return_t;
    typedef typename nohs2::make_helper make_helper;

    typedef traits::is_implemented<ev_t> cond;
    typedef mp::enable_if<cond, return_t> enable;
};

template<class Op, class Maker>
struct nary_op_helper_maker
    : nary_op_helper<Op, typename Maker::type>
{
    typedef Maker maker;
};

#define FLINTXX_NARY_OP_HELPER_MACRO(arg) typename storage_traits< arg >::type

// nary_op_helper<Op, Arg1, Arg2, ...> invokes nary_op_helper with the correct
// tuple type as argument.
template<class Op, FLINTXX_MAKE_TUPLE_TEMPLATE_ARGS>
struct nary_op_helper2
    : nary_op_helper_maker<Op, mp::make_tuple<
          FLINTXX_MAKE_TUPLE_TYPES_APPLYMACRO(FLINTXX_NARY_OP_HELPER_MACRO) > >
{
    typedef nary_op_helper2 noh2;
    static typename noh2::return_t make(FLINTXX_MAKE_TUPLE_FUNC_ARGS)
    {
        return noh2::make_helper::make(noh2::maker::make(
              FLINTXX_MAKE_TUPLE_FUNC_ARG_NAMES));
    }
};

// Special casing for binary operators.
template<class Expr1, class Op, class Expr2>
struct binary_op_helper
    : nary_op_helper2<Op, Expr1, Expr2>
{ };

// Special casing for unary operations.
template<class Op, class Expr>
struct unary_op_helper : nary_op_helper2<Op, Expr> { };

// For unary member operators, determining the return type the normal way can
// lead to cyclic dependencies. See FLINTXX_DEFINE_MEMBER_UNOP_RTYPE.
template<class Ret, class Op, class Expr>
struct unary_op_helper_with_rettype
{
    typedef mp::make_tuple<typename storage_traits<Expr>::type> maker;
    typedef typename Ret::template make_helper<
        Op, typename maker::type>::type return_t;
};

// Common helper for implementing comparison operators.
template<class Expr1, class Expr2>
struct order_op_helper
{
    typedef typename tools::evaluation_helper<Expr1>::type ev1_t;
    typedef typename tools::evaluation_helper<Expr2>::type ev2_t;
    typedef tools::symmetric_cmp<ev1_t, ev2_t> scmp;

    typedef mp::enable_if<
          mp::and_<
            traits::is_implemented<scmp>,
            mp::or_<
                traits::is_expression<Expr1>,
                traits::is_expression<Expr2>
              >
          >,
          bool> enable;

    static int get(const Expr1& e1, const Expr2& e2)
    {
        return scmp::get(tools::evaluation_helper<Expr1>::get(e1),
            tools::evaluation_helper<Expr2>::get(e2));
    }
};
} // detail

template<class Expr>
inline typename mp::enable_if<traits::is_expression<Expr>, std::ostream&>::type
operator<<(std::ostream& o, const Expr& e)
{
    e.print(o);
    return o;
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_expression<Expr1>, bool>::type
operator==(const Expr1& e1, const Expr2& e2)
{
  return e1.equals(e2);
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<mp::and_<
        mp::not_<traits::is_expression<Expr1> >,
        traits::is_expression<Expr2> >,
    bool>::type
operator==(const Expr1& e1, const Expr2& e2)
{
  return e2.equals(e1);
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<mp::or_<
        traits::is_expression<Expr1>,
        traits::is_expression<Expr2> >,
    bool>::type
operator!=(const Expr1& e1, const Expr2& e2)
{
  return !(e1 == e2);
}

template<class Expr1, class Expr2>
inline typename detail::order_op_helper<Expr1, Expr2>::enable::type
operator<(const Expr1& e1, const Expr2& e2)
{
    return detail::order_op_helper<Expr1, Expr2>::get(e1, e2) < 0;
}

template<class Expr1, class Expr2>
inline typename detail::order_op_helper<Expr1, Expr2>::enable::type
operator<=(const Expr1& e1, const Expr2& e2)
{
    return detail::order_op_helper<Expr1, Expr2>::get(e1, e2) <= 0;
}

template<class Expr1, class Expr2>
inline typename detail::order_op_helper<Expr1, Expr2>::enable::type
operator>(const Expr1& e1, const Expr2& e2)
{
    return detail::order_op_helper<Expr1, Expr2>::get(e1, e2) > 0;
}

template<class Expr1, class Expr2>
inline typename detail::order_op_helper<Expr1, Expr2>::enable::type
operator>=(const Expr1& e1, const Expr2& e2)
{
    return detail::order_op_helper<Expr1, Expr2>::get(e1, e2) >= 0;
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::plus, Expr2>::enable::type
operator+(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::plus, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::minus, Expr2>::enable::type
operator-(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::minus, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::times, Expr2>::enable::type
operator*(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::times, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::divided_by, Expr2>::enable::type
operator/(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::divided_by, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::modulo, Expr2>::enable::type
operator%(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::modulo, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::binary_and, Expr2>::enable::type
operator&(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::binary_and, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::binary_or, Expr2>::enable::type
operator|(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::binary_or, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::binary_xor, Expr2>::enable::type
operator^(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::binary_xor, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::shift, Expr2>::enable::type
operator<<(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::shift, Expr2>::make(e1, e2);
}

template<class Expr1, class Expr2>
inline typename detail::binary_op_helper<
    Expr1, operations::shift, Expr2>::enable::type
operator>>(const Expr1& e1, const Expr2& e2)
{
    return detail::binary_op_helper<Expr1, operations::shift, Expr2>::make(e1, -e2);
}

template<class Expr>
inline typename detail::unary_op_helper<operations::negate, Expr>::enable::type
operator-(const Expr& e)
{
    return detail::unary_op_helper<operations::negate, Expr>::make(e);
}

template<class Expr>
inline typename detail::unary_op_helper<operations::complement, Expr>::enable::type
operator~(const Expr& e)
{
    return detail::unary_op_helper<operations::complement, Expr>::make(e);
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_immediate_expr<Expr1>, Expr1&>::type
operator+=(Expr1& e1, const Expr2& e2)
{
    e1.set(e1 + e2);
    return e1;
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_immediate_expr<Expr1>, Expr1&>::type
operator-=(Expr1& e1, const Expr2& e2)
{
    e1.set(e1 - e2);
    return e1;
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_immediate_expr<Expr1>, Expr1&>::type
operator*=(Expr1& e1, const Expr2& e2)
{
    e1.set(e1 * e2);
    return e1;
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_immediate_expr<Expr1>, Expr1&>::type
operator/=(Expr1& e1, const Expr2& e2)
{
    e1.set(e1 / e2);
    return e1;
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_immediate_expr<Expr1>, Expr1&>::type
operator%=(Expr1& e1, const Expr2& e2)
{
    e1.set(e1 % e2);
    return e1;
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_immediate_expr<Expr1>, Expr1&>::type
operator|=(Expr1& e1, const Expr2& e2)
{
    e1.set(e1 | e2);
    return e1;
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_immediate_expr<Expr1>, Expr1&>::type
operator&=(Expr1& e1, const Expr2& e2)
{
    e1.set(e1 & e2);
    return e1;
}

template<class Expr1, class Expr2>
inline typename mp::enable_if<traits::is_immediate_expr<Expr1>, Expr1&>::type
operator^=(Expr1& e1, const Expr2& e2)
{
    e1.set(e1 ^ e2);
    return e1;
}

// c-style IO
template<class T>
typename mp::enable_if<traits::is_implemented<
    rules::cprint<typename T::evaluated_t> >, int>::type
print(const T& t)
{
    return t.print();
}
template<class T>
typename mp::enable_if<traits::is_implemented<
    rules::cprint<typename T::evaluated_t> >, int>::type
print(FILE* f, const T& t)
{
    return t.print(f);
}
template<class T, class U>
typename mp::enable_if<traits::is_implemented<
    rules::print_pretty<typename T::evaluated_t> >, int>::type
print_pretty(const T& t, const U& extra)
{
    return t.print_pretty(extra);
}
template<class T, class U>
typename mp::enable_if<traits::is_implemented<
    rules::print_pretty<typename T::evaluated_t> >, int>::type
print_pretty(FILE* f, const T& t, const U& extra)
{
    return t.print_pretty(extra, f);
}
template<class T>
typename mp::enable_if<traits::is_implemented<
    rules::print_pretty<typename T::evaluated_t> >, int>::type
print_pretty(const T& t)
{
    return t.print_pretty();
}
template<class T>
typename mp::enable_if<traits::is_implemented<
    rules::print_pretty<typename T::evaluated_t> >, int>::type
print_pretty(FILE* f, const T& t)
{
    return t.print_pretty(f);
}
template<class T>
typename mp::enable_if<traits::is_implemented<
    rules::read<typename T::evaluated_t> >, int>::type
read(T& t)
{
    return t.read();
}
template<class T>
typename mp::enable_if<traits::is_implemented<
    rules::read<typename T::evaluated_t> >, int>::type
read(FILE* f, T& t)
{
    return t.read(f);
}

// TODO move to std?
template<class Expr1, class Expr2>
inline typename mp::enable_if<typename traits::is_implemented<
    rules::swap<Expr1, Expr2> > >::type swap(Expr1& e1, Expr2& e2)
{
    rules::swap<Expr1, Expr2>::doit(e1, e2);
}
}

// TODO remove this?
#include "default_rules.h"


////////////////////////////////////////////////////////////////////////
// HELPER MACROS
////////////////////////////////////////////////////////////////////////

// To be called in any namespace

// Make the binary operation "name" available in current namespace
#define FLINT_DEFINE_BINOP_HERE(name) \
template<class T1, class T2> \
inline typename ::flint::detail::binary_op_helper<\
    T1, ::flint::operations::name##_op, T2>::enable::type \
name(const T1& t1, const T2& t2) \
{ \
  return ::flint::detail::binary_op_helper< \
      T1, ::flint::operations::name##_op, T2>::make(t1, t2); \
}

// Make the unary operation "name" available in current namespace
#define FLINT_DEFINE_UNOP_HERE(name) \
template<class T1> \
inline typename ::flint::detail::unary_op_helper<\
    ::flint::operations::name##_op, T1>::enable::type \
name(const T1& t1) \
{ \
  return ::flint::detail::unary_op_helper< ::flint::operations::name##_op, T1>::make(t1); \
}

// Make the threeary operation "name" available in current namespace
#define FLINT_DEFINE_THREEARY_HERE(name) \
template<class T1, class T2, class T3> \
inline typename ::flint::detail::nary_op_helper2<\
    ::flint::operations::name##_op, T1, T2, T3>::enable::type \
name(const T1& t1, const T2& t2, const T3& t3) \
{ \
  return ::flint::detail::nary_op_helper2< \
      ::flint::operations::name##_op, T1, T2, T3>::make(t1, t2, t3); \
}

// Make the threeary operation "name" available in current namespace,
// but with only two arguments, the second of which is of type type1 and
// defaults to val1, and the third argument always (implicitly) of type type2
// and value val2.
// The suggested usage of this macro is to first call FLINT_DEFINE_THREEARY_HERE,
// and then call FLINT_DEFINE_THREEARY_HERE_2DEFAULT. The effect will be an
// operation which can be invoked with 1, 2 or 3 arguments.
#define FLINT_DEFINE_THREEARY_HERE_2DEFAULT(name, type1, val1, type2, val2) \
template<class T1> \
inline typename ::flint::detail::nary_op_helper2<\
    ::flint::operations::name##_op, T1, type1, type2 >::enable::type \
name(const T1& t1, type1 t2 = val1) \
{ \
  return ::flint::detail::nary_op_helper2< \
      ::flint::operations::name##_op, T1, type1, type2>::make(t1, t2, val2); \
}

// Make the fourary operation "name" available in current namespace
#define FLINT_DEFINE_FOURARY_HERE(name) \
template<class T1, class T2, class T3, class T4> \
inline typename ::flint::detail::nary_op_helper2<\
    ::flint::operations::name##_op, T1, T2, T3, T4>::enable::type \
name(const T1& t1, const T2& t2, const T3& t3, const T4& t4) \
{ \
  return ::flint::detail::nary_op_helper2< \
      ::flint::operations::name##_op, T1, T2, T3, T4>::make(t1, t2, t3, t4); \
}

// Make the fiveary operation "name" available in current namespace
#define FLINT_DEFINE_FIVEARY_HERE(name) \
template<class T1, class T2, class T3, class T4, class T5> \
inline typename ::flint::detail::nary_op_helper2<\
    ::flint::operations::name##_op, T1, T2, T3, T4, T5>::enable::type \
name(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) \
{ \
  return ::flint::detail::nary_op_helper2< \
      ::flint::operations::name##_op, T1, T2, T3, T4, T5>::make(t1, t2, t3, t4, t5); \
}

// Make the sixary operation "name" available in current namespace
#define FLINT_DEFINE_SIXARY_HERE(name) \
template<class T1, class T2, class T3, class T4, class T5, class T6> \
inline typename ::flint::detail::nary_op_helper2<\
    ::flint::operations::name##_op, T1, T2, T3, T4, T5, T6>::enable::type \
name(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6) \
{ \
  return ::flint::detail::nary_op_helper2< \
      ::flint::operations::name##_op, T1, T2, T3, T4, T5, T6>::make(t1, t2, t3, t4, t5, t6); \
}

// Make the sevenary operation "name" available in current namespace
#define FLINT_DEFINE_SEVENARY_HERE(name) \
template<class T1, class T2, class T3, class T4, class T5, class T6, class T7> \
inline typename ::flint::detail::nary_op_helper2<\
    ::flint::operations::name##_op, T1, T2, T3, T4, T5, T6, T7>::enable::type \
name(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7) \
{ \
  return ::flint::detail::nary_op_helper2< \
      ::flint::operations::name##_op, T1, T2, T3, T4, T5, T6, T7>::make(t1, t2, t3, t4, t5, t6, t7); \
}

// This set of macros should be called in namespace flint.

// Introduce a new binary operation called "name"
// NB: because of ADL bugs in g++ <= 4.4, the operation tag is called "name_op",
// whereas the function corresponding to it is just called "name"
#define FLINT_DEFINE_BINOP(name) \
namespace operations { \
struct name##_op { }; \
} \
FLINT_DEFINE_BINOP_HERE(name)

// This macro can be used to conditionally enable a function, and is mostly
// used for forwarding.
// A typical usage is
//   template<class T, class U>
//   FLINT_BINOP_ENABLE_RETTYPE(myop, T, U) myop_other(const T& t, const U& u)
//   {
//       // perhaps something more interesting
//       return myop(t, u);
//   }
#define FLINT_BINOP_ENABLE_RETTYPE(name, T1, T2) \
    typename detail::binary_op_helper<T1, operations::name##_op, T2>::enable::type

// Introduce a new unary operation called "name"
#define FLINT_DEFINE_UNOP(name) \
namespace operations { \
struct name##_op { }; \
} \
FLINT_DEFINE_UNOP_HERE(name)

#define FLINT_UNOP_ENABLE_RETTYPE(name, T) \
    typename detail::unary_op_helper<operations::name##_op, T>::return_t

// See FLINTXX_DEFINE_MEMBER_UNOP_RTYPE
#define FLINT_UNOP_BUILD_RETTYPE(name, rettype, T) \
    typename detail::unary_op_helper_with_rettype<rettype, \
        operations::name##_op, T>::return_t

#define FLINT_DEFINE_THREEARY(name) \
namespace operations { \
struct name##_op { }; \
} \
FLINT_DEFINE_THREEARY_HERE(name)

#define FLINT_THREEARY_ENABLE_RETTYPE(name, T1, T2, T3) \
    typename detail::nary_op_helper2<operations::name##_op, T1, T2, T3>::enable::type

#define FLINT_DEFINE_FOURARY(name) \
namespace operations { \
struct name##_op { }; \
} \
FLINT_DEFINE_FOURARY_HERE(name)

#define FLINT_FOURARY_ENABLE_RETTYPE(name, T1, T2, T3, T4) \
    typename detail::nary_op_helper2<operations::name##_op, T1, T2, T3, T4>::enable::type

#define FLINT_DEFINE_FIVEARY(name) \
namespace operations { \
struct name##_op { }; \
} \
FLINT_DEFINE_FIVEARY_HERE(name)

#define FLINT_FIVEARY_ENABLE_RETTYPE(name, T1, T2, T3, T4, T5) \
    typename detail::nary_op_helper2<operations::name##_op, T1, T2, T3, T4, T5>::enable::type

#define FLINT_DEFINE_SIXARY(name) \
namespace operations { \
struct name##_op { }; \
} \
FLINT_DEFINE_SIXARY_HERE(name)

#define FLINT_DEFINE_SEVENARY(name) \
namespace operations { \
struct name##_op { }; \
} \
FLINT_DEFINE_SEVENARY_HERE(name)

#endif