Goose  Artifact [8d37ef60f2]

Artifact 8d37ef60f28b62e33cf627057f65377423d1d048bf72e2cf3d1dcf0a272dbe2c:

  • File bs/sema/substitute.cpp — part of check-in [849a7f99e5] at 2019-01-04 14:11:36 on branch trunk — Implement hole substitution algorithm. (user: achavasse size: 1314)

#include "sema.h"

namespace empathy::sema
{
    Term Substitute( const Term& src, const UnificationContext& context )
    {
        if( !holds_alternative< pvec >( src.content() ) )
            return src;

        auto optHole = HoleFromIRExpr( src );
        if( optHole )
        {
            const auto& hole = *optHole;

            // We only substitute indexed holes. If we encounter a named hole,
            // output it as is.
            if( !holds_alternative< uint32_t >( hole ) )
                return src;

            const auto& optVal = context.getValue( get< uint32_t >( hole ) );
            if( !optVal )
                return src;

            return Substitute( *optVal, context );
        }

        const auto& vec = *get< pvec >( src.content() );
        immer::vector< Term > outputTerms;
        auto vt = outputTerms.transient();
        bool faulty = false;

        for( auto&& t : vec.terms() )
        {
            auto newT = Substitute( t, context );
            faulty = faulty || newT.isFaulty();
            vt.push_back( move( newT ) );
        }

        // TODO: the number of holes in terms is probably not going to end up being useful,
        // so just remove it.
        return Term( src.location(), make_shared< Vector >( vt.persistent(), faulty, 0 ) );
    }
}