# 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 globs import *
from types_builtin import app_map, subst

def orig_loc(obj):
    # Ugh, I don't like the conditional check...
    if has_extrinsic(Original, obj):
        obj = extrinsic(Original, obj)
    return extrinsic(Location, obj)

def original(extr, obj):
    return extrinsic(extr, extrinsic(Original, obj))

def original_has(extr, obj):
    if not has_extrinsic(Original, obj):
        return False
    return has_extrinsic(extr, extrinsic(Original, obj))

def in_vat(func):
    return in_env(VAT, VatContents([], {}, False), func)

# Clone structured data, recording information about its clone in the vat
def clone(src, extrinsics):
    env(VAT).copiedExtrinsics = extrinsics
    return clone_structured(src)

def clone_structured(src, apps=None):
    ctor = instance_ctor(src)
    fs = []
    for field in ctor.fields:
        fnm = extrinsic(Name, field)
        ft = field.type
        if apps:

# 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: vat.py
from base import *
from globs import *
from types_builtin import app_map, subst


def orig_loc(obj):
    # Ugh, I don't like the conditional check...
    if has_extrinsic(Original, obj):
        obj = extrinsic(Original, obj)
    return extrinsic(Location, obj)

def original(extr, obj):
    return extrinsic(extr, extrinsic(Original, obj))

def original_has(extr, obj):
    if not has_extrinsic(Original, obj):
        return False
    return has_extrinsic(extr, extrinsic(Original, obj))

def in_vat(func):
    return in_env(VAT, VatContents([], {}, False), func)

# Clone structured data, recording information about its clone in the vat
def clone(src, extrinsics):
    env(VAT).copiedExtrinsics = extrinsics
    return clone_structured(src)

def clone_structured(src, apps=None):
    ctor = instance_ctor(src)
    fs = []
    for field in ctor.fields:
        fnm = extrinsic(Name, field)
        ft = field.type
        if apps:

            ft = subst(apps, ft)