# Path: types_builtin.py
# def app_map(data, appTs):
#     apps = {}
#     for tv, at in ezip(data.tvars, appTs):
#         if isinstance(at, TVar) and at.typeVar is tv:
#             continue
#         apps[tv] = at
#     return apps
# 
# def subst(mapping, t):
#     return map_type_vars(lambda st: mapping.get(st.typeVar, st), t)


from base import *
from bedrock import *
from globs import *
from hashlib import sha256
from os import system
from types_builtin import app_map, subst
        assert isinstance(t, TPrim) and isinstance(t.primType, PBool)
        _write(_encode_int(1 if node else 0))
    elif isinstance(node, int):
        assert isinstance(t, TPrim) and isinstance(t.primType, PInt)
        _write(_encode_int(node))
    elif isinstance(node, float):
        assert isinstance(t, TPrim) and isinstance(t.primType, PFloat)
        _write(_encode_float(node))
    elif isinstance(node, list):
        assert isinstance(t, TArray), "Unexpected array:\n%s\nfor:\n%s" % (
                node, t)
        _write(_encode_int(len(node)))
        et = t.elemType
        if isinstance(et, TWeak):
            for item in node:
                _write_ref(item, et.refType)
        else:
            for item in node:
                _serialize_node(item, et)
    else:
        assert False, "Can't serialize %r" % (node,)

InspectState = DT('InspectState', ('module', '*Module'), ('count', int))

Inspection = new_env('Inspection', InspectState)

def _inspect_node(node, t):
    if isinstance(node, Structured):
        dtform, appTs = match(t, ("TData(dt, apps)", tuple2))
        # Collect instantiations

# Path: types_builtin.py
# def app_map(data, appTs):
#     apps = {}
#     for tv, at in ezip(data.tvars, appTs):
#         if isinstance(at, TVar) and at.typeVar is tv:
#             continue
#         apps[tv] = at
#     return apps
# 
# def subst(mapping, t):
#     return map_type_vars(lambda st: mapping.get(st.typeVar, st), t)


# Path: native.py
from base import *
from bedrock import *
from globs import *
from hashlib import sha256
from os import system
from types_builtin import app_map, subst

        assert isinstance(t, TPrim) and isinstance(t.primType, PBool)
        _write(_encode_int(1 if node else 0))
    elif isinstance(node, int):
        assert isinstance(t, TPrim) and isinstance(t.primType, PInt)
        _write(_encode_int(node))
    elif isinstance(node, float):
        assert isinstance(t, TPrim) and isinstance(t.primType, PFloat)
        _write(_encode_float(node))
    elif isinstance(node, list):
        assert isinstance(t, TArray), "Unexpected array:\n%s\nfor:\n%s" % (
                node, t)
        _write(_encode_int(len(node)))
        et = t.elemType
        if isinstance(et, TWeak):
            for item in node:
                _write_ref(item, et.refType)
        else:
            for item in node:
                _serialize_node(item, et)
    else:
        assert False, "Can't serialize %r" % (node,)

InspectState = DT('InspectState', ('module', '*Module'), ('count', int))

Inspection = new_env('Inspection', InspectState)

def _inspect_node(node, t):
    if isinstance(node, Structured):
        dtform, appTs = match(t, ("TData(dt, apps)", tuple2))
        # Collect instantiations

        apps = app_map(dtform, appTs)