Goose  Artifact [05409971eb]

Artifact 05409971eb27c6958466f63d53dd8a4661bd5f99117fcb18d632c89daaf0cf59:

  • File bs/sema/substitute.cpp — part of check-in [b64ea47f6b] at 2020-06-27 22:05:05 on branch trunk — Clearly separate the type checking rules and the unification rules, instead of lumping them all together in a single set of patterns which is increasingly confusing. (user: achavasse size: 970)

#include "sema.h"

namespace goose::sema
{
    Term Substitute( const Term& src, const TypeCheckingContext& context )
    {
        if( auto optHole = HoleFromIRExpr( src ) )
        {
            const auto& hole = *optHole;

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

            const auto& optVal = context.getValue( hole.name.id() );
            if( !optVal )
                return HOLE( "_"_sid );

            return Substitute( *optVal, context );
        }

        if( !holds_alternative< pvec >( src ) )
            return src;

        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;
    }
}