Goose  Check-in [f2ca82dfce]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Simplified ir::Vector, use plain std::vector to store the terms. immer::vector was pointless for that since we pretty much never actually take advantage of them being CoW in this case.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f2ca82dfce6ffddae6f7221ad620b43026d48c361a3456475e6587733ea544a7
User & Date: achavasse 2019-08-16 23:52:52.491
Context
2019-08-17
14:49
  • Intrinsics automatically set their domain depending on the domain restrictions of the builtin types that they use.
  • Declaring a local variable of a compile-time only type is now properly rejected during codegen.
  • Improved error messages for operators and extension point invocations.
check-in: 48a020a1fa user: achavasse tags: trunk
2019-08-16
23:52
Simplified ir::Vector, use plain std::vector to store the terms. immer::vector was pointless for that since we pretty much never actually take advantage of them being CoW in this case. check-in: f2ca82dfce user: achavasse tags: trunk
20:33
Implemented variable declarations with local type inference. check-in: 5a361c8b86 user: achavasse tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to bs/builtins/types/func/build.cpp.
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
#include "builtins/builtins.h"

namespace empathy::builtins
{
    FuncType BuildFuncType( const Term& domain, const Value& returnType, const Value& params )
    {
        immer::vector< Term > tv;
        auto tvt = tv.transient();

        ForEachInTuple( params, [&]( auto&& param )
        {
            if( IsDecl( param ) )
            {
                auto decl = *FromValue< Decl >( param );
                tvt.push_back( ValueToIRExpr( ValuePattern( MkHole( "_"_sid ), move( decl.type() ), MkHole( "_"_sid ) ) ) );
            }
            else if( param.isConstant() )
                tvt.push_back( ValueToIRExpr( param ) );

            return true;
        } );

        return FuncType( domain, ValueToIRExpr( returnType ),
            TERM( make_shared< Vector >( tvt.persistent() ) ) );
    }

    Func BuildExternalFunc( const FuncType& funcType, const string& symbol )
    {
        return Func( funcType, symbol);
    }







|
|






|


|




|
<







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
#include "builtins/builtins.h"

namespace empathy::builtins
{
    FuncType BuildFuncType( const Term& domain, const Value& returnType, const Value& params )
    {
        auto tv = make_shared< Vector >();
        tv->reserve( TupleSize( params ) );

        ForEachInTuple( params, [&]( auto&& param )
        {
            if( IsDecl( param ) )
            {
                auto decl = *FromValue< Decl >( param );
                tv->append( ValueToIRExpr( ValuePattern( MkHole( "_"_sid ), move( decl.type() ), MkHole( "_"_sid ) ) ) );
            }
            else if( param.isConstant() )
                tv->append( ValueToIRExpr( param ) );

            return true;
        } );

        return FuncType( domain, ValueToIRExpr( returnType ), tv );

    }

    Func BuildExternalFunc( const FuncType& funcType, const string& symbol )
    {
        return Func( funcType, symbol);
    }

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
        return Func( funcType, unparsedBody, pFuncLLR.get() );
    }

    Term BuildCallPatternFromFuncType( const Value& funcType )
    {
        auto ftype = *FromValue< FuncType >( funcType );

        immer::vector< Term > apv;
        auto apvt = apv.transient();


        ForEachInVectorTerm( ftype.params(), [&]( auto&& param )
        {
            auto result = Decompose( param,
                Vec(
                    Lit( "value"_sid ),
                    SubTerm(),
                    SubTerm(),
                    SubTerm(),
                    Val< LocationId >()
                )
            );
            assert( result );

            auto&& [sort, type, val, locId] = *result;

            if( sort == TSID( constant ) )
                apvt.push_back( param );
            else
                apvt.push_back( ValueToIRExpr( ValuePattern( TSID( computed ), type, TERM( ptr< void >() ) ) ) );

            return true;
        } );

        return VEC(
            ftype.domain(),
            TERM( make_shared< Vector >( apvt.persistent() ) ),
            ftype.returnType()
        );
    }
}







<
|
>

















|

|




<
<
<
|
<


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
        return Func( funcType, unparsedBody, pFuncLLR.get() );
    }

    Term BuildCallPatternFromFuncType( const Value& funcType )
    {
        auto ftype = *FromValue< FuncType >( funcType );


        auto apv = make_shared< Vector >();
        apv->reserve( VecSize( ftype.params() ) );

        ForEachInVectorTerm( ftype.params(), [&]( auto&& param )
        {
            auto result = Decompose( param,
                Vec(
                    Lit( "value"_sid ),
                    SubTerm(),
                    SubTerm(),
                    SubTerm(),
                    Val< LocationId >()
                )
            );
            assert( result );

            auto&& [sort, type, val, locId] = *result;

            if( sort == TSID( constant ) )
                apv->append( param );
            else
                apv->append( ValueToIRExpr( ValuePattern( TSID( computed ), type, TERM( ptr< void >() ) ) ) );

            return true;
        } );




        return VEC( ftype.domain(), apv,ftype.returnType() );

    }
}
Changes to bs/builtins/types/func/func.cpp.
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

        pFuncLLR->body() = cfg;
        return true;
    }

    Term BuildArgListForCall( const FuncType& ft, const Term& unifiedArgs )
    {
        immer::vector< Term > av;
        auto tav = av.transient();

        ForEachInVectorTerms( ft.params(), unifiedArgs, [&]( auto&& p, auto&& a )
        {
            auto vp = ValuePatternFromIRExpr( p );
            if( vp->val() == MkHole( "_"_sid ) )
                tav.push_back( a );

            return true;
        } );

        return TERM( make_shared< Vector >( tav.persistent() ) );
    }
}

namespace empathy::ir
{
    const Term& Bridge< FuncType >::Type()
    {







|
|





|




|







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

        pFuncLLR->body() = cfg;
        return true;
    }

    Term BuildArgListForCall( const FuncType& ft, const Term& unifiedArgs )
    {
        auto av = make_shared< Vector >();
        av->reserve( VecSize( ft.params() ) );

        ForEachInVectorTerms( ft.params(), unifiedArgs, [&]( auto&& p, auto&& a )
        {
            auto vp = ValuePatternFromIRExpr( p );
            if( vp->val() == MkHole( "_"_sid ) )
                av->append( a );

            return true;
        } );

        return av;
    }
}

namespace empathy::ir
{
    const Term& Bridge< FuncType >::Type()
    {
Changes to bs/builtins/types/template/build.cpp.
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
            return nullopt;

        return TDecl( *typeSig, name );
    }

    TFuncType BuildTFuncType( const Term& domain, const Value& returnType, const Value& params )
    {
        immer::vector< Term > v;
        auto vt = v.transient();

        ForEachInTuple( params, [&]( auto&& param )
        {
            vt.push_back( ValueToIRExpr( param ) );
            return true;
        } );

        return TFuncType( domain, ValueToIRExpr( returnType ), TERM( make_shared< Vector >( vt.persistent() ) ) );
    }

    optional< Term > BuildTFuncSignature( const Context& c, const TFuncType& tft )
    {
        immer::vector< Term > v;
        auto vt = v.transient();

        bool success = true;
        ForEachInVectorTerm( tft.params(), [&]( auto&& param )
        {
            auto teSig = BuildTemplateSignature( c, param );
            if( !teSig )
            {
                DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( param )->locationId(),
                    "Invalid template parameter." );
                success = false;
                return false;
            }

            vt.push_back( move( *teSig ) );
            return true;
        } );

        if( !success )
            return nullopt;

        auto rtSig = BuildTemplateSignature( c, tft.returnType() );
        if( !rtSig )
        {
            DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( tft.returnType() )->locationId(),
                "Invalid template return type or texpr." );
            return nullopt;
        }

        return VEC(
            tft.domain(),
            TERM( make_shared< Vector >( vt.persistent() ) ),
            *rtSig );
    }

    Value BuildTFunc( const Context& c, const TFuncType& tft, const Term& identity, const Value& params, ptr< void > body )
    {
        auto sig = BuildTFuncSignature( c, tft );
        if( !sig )
            return PoisonValue();

        return ToValue( TFunc( tft, *sig, identity, body ) );
    }

    optional< Term > BuildArgPatternFromTFuncType( const Context& c, const Value& tfuncType )
    {
        const auto& ftype = FromValue< TFuncType >( tfuncType );
        assert( ftype );

        immer::vector< Term > apv;
        auto apvt = apv.transient();


        bool success = true;
        ForEachInVectorTerm( ftype->params(), [&]( auto&& param )
        {
            auto teArgPat = BuildTemplateArgPattern( c, param );
            if( !teArgPat )
            {
                DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( param )->locationId(),
                    "Invalid template parameter." );
                success = false;
                return false;
            }

            apvt.push_back( move( *teArgPat ) );
            return true;
        } );

        if( !success )
            return nullopt;

        auto rtArgPat = BuildTemplateArgPattern( c, ftype->returnType() );
        if( !rtArgPat )
        {
            DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( ftype->returnType() )->locationId(),
                "Invalid template return type or texpr." );
            return nullopt;
        }

        return VEC(
            ftype->domain(),
            TERM( make_shared< Vector >( apvt.persistent() ) ),
            *rtArgPat
        );
    }
}







|
|



|



|




|
|













|














|
<
<
<
















<
|
>













|














|
<
<
<
<


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
            return nullopt;

        return TDecl( *typeSig, name );
    }

    TFuncType BuildTFuncType( const Term& domain, const Value& returnType, const Value& params )
    {
        auto v = make_shared< Vector >();
        v->reserve( TupleSize( params ) );

        ForEachInTuple( params, [&]( auto&& param )
        {
            v->append( ValueToIRExpr( param ) );
            return true;
        } );

        return TFuncType( domain, ValueToIRExpr( returnType ), v );
    }

    optional< Term > BuildTFuncSignature( const Context& c, const TFuncType& tft )
    {
        auto v = make_shared< Vector >();
        v->reserve( VecSize( tft.params() ) );

        bool success = true;
        ForEachInVectorTerm( tft.params(), [&]( auto&& param )
        {
            auto teSig = BuildTemplateSignature( c, param );
            if( !teSig )
            {
                DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( param )->locationId(),
                    "Invalid template parameter." );
                success = false;
                return false;
            }

            v->append( move( *teSig ) );
            return true;
        } );

        if( !success )
            return nullopt;

        auto rtSig = BuildTemplateSignature( c, tft.returnType() );
        if( !rtSig )
        {
            DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( tft.returnType() )->locationId(),
                "Invalid template return type or texpr." );
            return nullopt;
        }

        return VEC( tft.domain(), v, *rtSig );



    }

    Value BuildTFunc( const Context& c, const TFuncType& tft, const Term& identity, const Value& params, ptr< void > body )
    {
        auto sig = BuildTFuncSignature( c, tft );
        if( !sig )
            return PoisonValue();

        return ToValue( TFunc( tft, *sig, identity, body ) );
    }

    optional< Term > BuildArgPatternFromTFuncType( const Context& c, const Value& tfuncType )
    {
        const auto& ftype = FromValue< TFuncType >( tfuncType );
        assert( ftype );


        auto apv = make_shared< Vector >();
        apv->reserve( VecSize( ftype->params() ) );

        bool success = true;
        ForEachInVectorTerm( ftype->params(), [&]( auto&& param )
        {
            auto teArgPat = BuildTemplateArgPattern( c, param );
            if( !teArgPat )
            {
                DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( param )->locationId(),
                    "Invalid template parameter." );
                success = false;
                return false;
            }

            apv->append( move( *teArgPat ) );
            return true;
        } );

        if( !success )
            return nullopt;

        auto rtArgPat = BuildTemplateArgPattern( c, ftype->returnType() );
        if( !rtArgPat )
        {
            DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( ftype->returnType() )->locationId(),
                "Invalid template return type or texpr." );
            return nullopt;
        }

        return VEC( ftype->domain(), apv,*rtArgPat );




    }
}
Changes to bs/builtins/types/template/rules.cpp.
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
    class TVecTemplateRule : public TemplateRule
    {
        optional< Term > buildSignature( const Context& c, const Value& val ) const final
        {
            auto tvec = FromValue< TVec >( val );
            assert( tvec );

            immer::vector< Term > v;
            auto vt = v.transient();

            for( auto&& x : tvec->content()->terms() )
            {
                if( auto s = BuildTemplateSignature( c, x ) )
                    vt.push_back( move( *s ) );
                else
                    vt.push_back( x );
            }

            return TERM( make_shared< Vector >( vt.persistent() ) );
        }

        Value buildParamDecl( const Context& c, const Value& param, const Value& arg ) const final
        {
            return arg;
        }








|
|




|

|


|







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
    class TVecTemplateRule : public TemplateRule
    {
        optional< Term > buildSignature( const Context& c, const Value& val ) const final
        {
            auto tvec = FromValue< TVec >( val );
            assert( tvec );

            auto v = make_shared< Vector >();
            v->reserve( tvec->content()->terms().size() );

            for( auto&& x : tvec->content()->terms() )
            {
                if( auto s = BuildTemplateSignature( c, x ) )
                    v->append( move( *s ) );
                else
                    v->append( x );
            }

            return v;
        }

        Value buildParamDecl( const Context& c, const Value& param, const Value& arg ) const final
        {
            return arg;
        }

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
        }

        optional< Term > buildArgPattern( const Context& c, const Value& val ) const final
        {
            auto tvec = FromValue< TVec >( val );
            assert( tvec );

            immer::vector< Term > v;
            auto vt = v.transient();

            for( auto&& x : tvec->content()->terms() )
            {
                auto s = BuildTemplateArgPattern( c, x );
                if( !s )
                    return nullopt;
                vt.push_back( move( *s ) );
            }

            return TERM( make_shared< Vector >( vt.persistent() ) );
        }
    };

    class ValueTemplateRule : public TemplateRule
    {
        optional< Term > buildSignature( const Context& c, const Value& val ) const final
        {







|
|






|


|







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
        }

        optional< Term > buildArgPattern( const Context& c, const Value& val ) const final
        {
            auto tvec = FromValue< TVec >( val );
            assert( tvec );

            auto v = make_shared< Vector >();
            v->reserve( tvec->content()->terms().size() );

            for( auto&& x : tvec->content()->terms() )
            {
                auto s = BuildTemplateArgPattern( c, x );
                if( !s )
                    return nullopt;
                v->append( move( *s ) );
            }

            return v;
        }
    };

    class ValueTemplateRule : public TemplateRule
    {
        optional< Term > buildSignature( const Context& c, const Value& val ) const final
        {
Changes to bs/codegen/type.cpp.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        if( !rt )
            return nullopt;

        vector< llvm::Type* > paramTypes;
        const auto& paramVec = *get< pvec >( ft.params() );
        paramTypes.reserve( paramVec.terms().size() );

        immer::vector< Term > pv;
        auto tpv = pv.transient();

        bool success = true;
        ForEachInVectorTerm( ft.params(), [&]( auto&& p )
        {
            // Params are not stored explicitely, but as patterns
            // for expediency (where the value is either a hole or an actual
            // value depending on whether this is a specialization param),







|
|







38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        if( !rt )
            return nullopt;

        vector< llvm::Type* > paramTypes;
        const auto& paramVec = *get< pvec >( ft.params() );
        paramTypes.reserve( paramVec.terms().size() );

        auto pv = make_shared< Vector >();
        pv->reserve( VecSize( ft.params() ) );

        bool success = true;
        ForEachInVectorTerm( ft.params(), [&]( auto&& p )
        {
            // Params are not stored explicitely, but as patterns
            // for expediency (where the value is either a hole or an actual
            // value depending on whether this is a specialization param),
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
                    success = false;
                    return false;
                }

                paramTypes.emplace_back( GetLLVMType( *type ) );

                vp->type() = ValueToIRExpr( *type );
                tpv.push_back( ValueToIRExpr( *vp ) );
            }
            else
                tpv.push_back( p );

            return true;
        } );

        if( !success )
            return nullopt;

        auto* pLLVMType = llvm::FunctionType::get( GetLLVMType( *rt ), paramTypes, false );
        if( !pLLVMType )
            return nullopt;

        return FuncType( ft.domain(), ValueToIRExpr( *rt ),
            TERM( make_shared< Vector >( tpv.persistent() ) ), pLLVMType );
    }
}







|


|











|
<


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
                    success = false;
                    return false;
                }

                paramTypes.emplace_back( GetLLVMType( *type ) );

                vp->type() = ValueToIRExpr( *type );
                pv->append( ValueToIRExpr( *vp ) );
            }
            else
                pv->append( p );

            return true;
        } );

        if( !success )
            return nullopt;

        auto* pLLVMType = llvm::FunctionType::get( GetLLVMType( *rt ), paramTypes, false );
        if( !pLLVMType )
            return nullopt;

        return FuncType( ft.domain(), ValueToIRExpr( *rt ), pv, pLLVMType );

    }
}
Changes to bs/ir/helpers.h.
1
2
3
4
5





6
7
8
9
10
11
12
#ifndef EMPATHY_IR_HELPERS_H
#define EMPATHY_IR_HELPERS_H

namespace empathy::ir
{





    template< typename... T >
    Term AppendToVectorTerm( const Term& vectorTerm, T&&... t );

    extern Term ConcatenateVectorTerms( const Term& vector1, const Term& vector2 );

    extern Term TakeVectorTerm( const Term& vectorTerm, size_t n );
    extern Term DropVectorTerm( const Term& vectorTerm, size_t n );





>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef EMPATHY_IR_HELPERS_H
#define EMPATHY_IR_HELPERS_H

namespace empathy::ir
{
    static inline auto VecSize( const Term& vectorTerm )
    {
        return get< pvec >( vectorTerm )->terms().size();
    }

    template< typename... T >
    Term AppendToVectorTerm( const Term& vectorTerm, T&&... t );

    extern Term ConcatenateVectorTerms( const Term& vector1, const Term& vector2 );

    extern Term TakeVectorTerm( const Term& vectorTerm, size_t n );
    extern Term DropVectorTerm( const Term& vectorTerm, size_t n );
Changes to bs/ir/vector.h.
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

    class Vector
    {
        public:
            Vector() {}

            template< typename T >
            using container_type = immer::flex_vector< T >;

            const auto& terms() const { return m_terms; }

            uint32_t complexity() const { return m_complexity; }
            void setComplexity( uint32_t c ) { m_complexity = c; }






            template< typename... T >
            static auto Make( T&&... terms )
            {


                immer::vector< Term > v;
                auto vt = v.transient();




                auto complexity = Append( vt, forward< T >( terms )... );

                auto pVec = make_shared< Vector >( vt.persistent() );

                pVec->setComplexity( complexity + 1 );

                pVec->setupRepetition( forward< T >( terms )... );




                return pVec;
            }

            template< typename... T >
            static Vector MakeAppend( const Vector& vec, T&&... terms )
            {
                auto v = vec.m_terms;

                auto vt = v.transient();
                auto complexity = Append( vt, forward< T >( terms )... );

                Vector newVec( vt.persistent() );
                newVec.setComplexity( vec.complexity() + complexity );
                newVec.setupRepetition( forward< T >( terms )... );

                return newVec;
            }

            template< typename... T >
            static Vector MakeConcat( const Vector& vec1, const Vector& vec2 )
            {

                auto v = vec1.m_terms;

                auto vt = v.transient();
                for( auto&& t : vec2.terms() )
                    Append( vt, t );

                auto result = Vector( vt.persistent() );
                result.setComplexity( vec1.complexity() + vec2.complexity() );
                return result;
            }

            friend ostream& ToString( ostream& out, const pvec& v );

            Vector( container_type< Term >&& terms ) :
                m_terms( move( terms ) )
            {}







|


>



>
>
>
>
>
|
|

>
>
|
<
>
>
>
>
|
|
<
|
|
>
|
|
>
>
>
|





<
|
<
<
|
<
<
<
|
|





>
|

<

|

<
<
|







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

    class Vector
    {
        public:
            Vector() {}

            template< typename T >
            using container_type = vector< T >;

            const auto& terms() const { return m_terms; }
            auto& terms() { return m_terms; }
            uint32_t complexity() const { return m_complexity; }
            void setComplexity( uint32_t c ) { m_complexity = c; }

            void reserve( size_t n )
            {
                m_terms.reserve( n );
            }

            template< typename T >
            void append( T&& term )
            {
                if constexpr( !is_same_v< Repetition, remove_cvref_t< T > > )
                {
                    m_complexity += GetComplexity( term ),

                    m_terms.emplace_back( forward< T >( term ) );
                }
                else
                {
                    setRepetitionTerm( forward< T >( term ) );
                }

            }

            template< typename... T >
            static auto Make( T&&... terms )
            {
                auto v = make_shared< Vector >();
                v->reserve( sizeof... ( T ) );
                ( v->append( terms ), ... );
                return v;
            }

            template< typename... T >
            static Vector MakeAppend( const Vector& vec, T&&... terms )
            {

                Vector v( vec );


                v.reserve( v.m_terms.size() + sizeof... ( T ) );



                ( v.append( terms ), ... );
                return v;
            }

            template< typename... T >
            static Vector MakeConcat( const Vector& vec1, const Vector& vec2 )
            {
                Vector v( vec1 );
                v.reserve( vec1.m_terms.size() + vec2.m_terms.size() );


                for( auto&& t : vec2.terms() )
                    v.append( t );



                return v;
            }

            friend ostream& ToString( ostream& out, const pvec& v );

            Vector( container_type< Term >&& terms ) :
                m_terms( move( terms ) )
            {}
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
                if( m_repetitionTerm )
                    co_yield *m_repetitionTerm;
            }

            template< typename F >
            pvec transform( F&& func ) const
            {
                immer::vector< Term > v;
                auto vt = v.transient();

                uint32_t c = 0;

                for( auto&& x : m_terms )
                {
                    auto newX = func( x );
                    if( !newX )
                        return nullptr;

                    c += GetComplexity( *newX );
                    vt.push_back( move( *newX ) );
                }

                auto result = make_shared< Vector >( vt.persistent() );
                result->setComplexity( c + 1 );
                return result;
            }

            Vector take( size_t n ) const
            {
                auto result = Vector( m_terms.take( n ) );


                uint32_t c = 0;
                for( auto&& t : result.m_terms )
                    c += GetComplexity( t );



                result.setComplexity( c + 1 );
                return result;
            }

            Vector drop( size_t n ) const
            {
                auto result = Vector( m_terms.drop( n ) );




                uint32_t c = 0;
                for( auto&& t : result.m_terms )
                    c += GetComplexity( t );

                result.setComplexity( c + 1 );
                return result;
            }

            template< typename T >
            void setRepetitionTerm( T&& term )
            {
                uint32_t c = complexity();
                if( m_repetitionTerm )







|
|









<
|


<
<
|




|
>

<
|
|
>
>

<
|




|
>

>
>
|
|
|

<
|







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
                if( m_repetitionTerm )
                    co_yield *m_repetitionTerm;
            }

            template< typename F >
            pvec transform( F&& func ) const
            {
                auto v = make_shared< Vector >();
                v->reserve( m_terms.size() );

                uint32_t c = 0;

                for( auto&& x : m_terms )
                {
                    auto newX = func( x );
                    if( !newX )
                        return nullptr;


                    v->append( move( *newX ) );
                }



                return v;
            }

            Vector take( size_t n ) const
            {
                Vector v;
                v.reserve( max( n, m_terms.size() ) );


                auto it = m_terms.begin();

                while( n-- )
                    v.append( *it++ );


                return v;
            }

            Vector drop( size_t n ) const
            {
                Vector v;
                v.reserve( m_terms.size() > n ? m_terms.size() - n : 0 );

                auto it = m_terms.begin();
                advance( it, n );

                while( it != m_terms.end() )
                    v.append( *it++ );


                return v;
            }

            template< typename T >
            void setRepetitionTerm( T&& term )
            {
                uint32_t c = complexity();
                if( m_repetitionTerm )
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

            const auto& operator[]( size_t i ) const
            {
                return m_terms[i];
            }

        private:
            template< typename V, typename H, typename... T >
            static uint32_t Append( V& vt, H&& head, T&&... tail )
            {
                uint32_t complexity = Append( vt, forward< H >( head ) );
                complexity += Append( vt, forward< T >( tail )... );
                return complexity;
            }

            template< typename V, typename H >
            static uint32_t Append( V& vt, H&& head )
            {
                if constexpr( !is_same_v< Repetition, remove_cvref_t< H > > )
                {
                    uint32_t complexity = GetComplexity( head );
                    vt.push_back( forward< H >( head ) );
                    return complexity;
                }
                else
                    return 0;
            }

            template< typename V >
            static uint32_t Append( V& vt )
            {
                return 0;
            }

            template< typename H, typename... T >
            void setupRepetition( H&& head, T&&... tail )
            {
                if constexpr( is_same_v< Repetition, remove_cvref_t< H > > )
                    setupRepetition( forward< H >( head ) );
                else
                    setupRepetition( forward< T >( tail )... );
            }

            template< typename H >
            void setupRepetition( H&& head )
            {
                if constexpr( is_same_v< Repetition, remove_cvref_t< H > > )
                    setRepetitionTerm( forward< H >( head ).m_term );
            }

            void setupRepetition()
            {}

            container_type< Term >  m_terms;
            optional< Term >        m_repetitionTerm;
            uint32_t                m_complexity = 1;
    };

    template< typename T >
    static inline auto&& SetComplexity( T&& term, uint32_t complexity )







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







182
183
184
185
186
187
188














































189
190
191
192
193
194
195

            const auto& operator[]( size_t i ) const
            {
                return m_terms[i];
            }

        private:














































            container_type< Term >  m_terms;
            optional< Term >        m_repetitionTerm;
            uint32_t                m_complexity = 1;
    };

    template< typename T >
    static inline auto&& SetComplexity( T&& term, uint32_t complexity )
Changes to bs/sema/domain.cpp.
15
16
17
18
19
20
21
22

23
24
25
    Term InjectDomainIntoIdentity( const Term& identity, const Term& domain )
    {
        const auto& vec = get< pvec >( identity );

        assert( vec );
        assert( !vec->empty() );

        auto newVec = make_shared< Vector >( vec->terms().set( 0, domain ) );

        return TERM( move( newVec ) );
    }
}







|
>
|


15
16
17
18
19
20
21
22
23
24
25
26
    Term InjectDomainIntoIdentity( const Term& identity, const Term& domain )
    {
        const auto& vec = get< pvec >( identity );

        assert( vec );
        assert( !vec->empty() );

        auto newVec = make_shared< Vector >( *vec );
        newVec->terms().front() = domain;
        return newVec;
    }
}
Changes to bs/sema/postprocess.cpp.
44
45
46
47
48
49
50
51


52
53
54
55
56
57
58
59
60
61
62
63
64
            if( !val )
                return nullopt;

            return ( *optPP->second )( *val, uc );
        }

        const auto& vec = *get< pvec >( src );
        immer::vector< Term > outputTerms;


        auto vt = outputTerms.transient();
        for( auto&& t : vec.terms() )
        {
            auto newT = Postprocess( t, uc );
            if( !newT )
                return nullopt;

            vt.push_back( move( *newT ) );
        }

        return Term( make_shared< Vector >( vt.persistent() ) );
    }
}







|
>
>
|






|


|


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
            if( !val )
                return nullopt;

            return ( *optPP->second )( *val, uc );
        }

        const auto& vec = *get< pvec >( src );

        auto outputTerms = make_shared< Vector >();
        outputTerms->reserve( vec.terms().size() );

        for( auto&& t : vec.terms() )
        {
            auto newT = Postprocess( t, uc );
            if( !newT )
                return nullopt;

            outputTerms->append( move( *newT ) );
        }

        return outputTerms;
    }
}
Changes to bs/sema/substitute.cpp.
20
21
22
23
24
25
26
27


28
29
30
31
32
33
34
35
36
37
38
39
            if( !optVal )
                return MkHole( "_"_sid );

            return Substitute( *optVal, context );
        }

        const auto& vec = *get< pvec >( src );
        immer::vector< Term > outputTerms;


        auto vt = outputTerms.transient();
        for( auto&& t : vec.terms() )
            vt.push_back( Substitute( t, context ) );

        return Term( make_shared< Vector >( vt.persistent() ) );
    }

    Term SubstituteNamed( const Term& src, const UnificationContext& context )
    {
        if( !holds_alternative< pvec >( src ) )
            return src;








|
>
>
|

|

|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
            if( !optVal )
                return MkHole( "_"_sid );

            return Substitute( *optVal, context );
        }

        const auto& vec = *get< pvec >( src );

        auto outputTerms = make_shared< Vector >();
        outputTerms->reserve( vec.terms().size() );

        for( auto&& t : vec.terms() )
            outputTerms->append( Substitute( t, context ) );

        return outputTerms;
    }

    Term SubstituteNamed( const Term& src, const UnificationContext& context )
    {
        if( !holds_alternative< pvec >( src ) )
            return src;

61
62
63
64
65
66
67
68


69
70
71
72
73
74
75
                    return MkHole( "_"_sid );

                return SubstituteNamed( *optVal, context );
            }
        }

        const auto& vec = *get< pvec >( src );
        immer::vector< Term > outputTerms;


        auto vt = outputTerms.transient();
        for( auto&& t : vec.terms() )
            vt.push_back( SubstituteNamed( t, context ) );

        return Term( make_shared< Vector >( vt.persistent() ) );
    }
}







|
>
>
|

|

|


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
                    return MkHole( "_"_sid );

                return SubstituteNamed( *optVal, context );
            }
        }

        const auto& vec = *get< pvec >( src );

        auto outputTerms = make_shared< Vector >();
        outputTerms->reserve( vec.terms().size() );

        for( auto&& t : vec.terms() )
            outputTerms->append( SubstituteNamed( t, context ) );

        return outputTerms;
    }
}