
# The following lets a property escape from local context without
# being kept on the cadabra stack. This is undefined.

def fun():
    {A,B,C}::AntiCommuting.
    ex:= C B A
    sort_product(_)
    print("inside fun")
    print(_)
    return __cdbtmp__

prop=fun()
print(prop)

ex:= C B A
sort_product(_)

# Mixing properties from global and local scope.

{A,B,C}::AntiCommuting.
qg = 42

def fun3():
    print('=== locals in fun3 scope ===')
    # print(q) # this would not work
    print(qg)
    {K,L}::AntiCommuting.
    ex:= C B A
    sort_product(_)
    print(_)
    # This function does not see things declared in fun2,
    # and it should not, because it does not see 'q' above either.
    ex2:= F E D 
    sort_product(_)
    print(_)
    print(locals())

# FIXME: in this problem, even though three local kernels get created,
# only two of them show up: in fun3 & fun4 the kernel is the same. 

def fun2():
    q=3
    {D,E,F}::AntiCommuting.
    print("fun2 scope "+str(properties()))
    ex:= C B A
    sort_product(_)
    print(_)
    ex2:= F E D
    sort_product(_)
    print(_)
    fun3()
    __cdbkernel__ = Kernel()
    print(__cdbkernel__)
    def fun4():
        print('=== locals in fun4 scope ===')
        print("fun4 scope "+str(properties()))
        print(locals());
        print q
        # If you drop this print below, __cdbkernel__ will not be in locals above.
        #print __cdbkernel__
        {Q,R}::AntiCommuting.
        ex4:= R Q
        sort_product(_)
        print(_)
        # This does not see the fun2 scope (but it does of course see
        # the global scope). That's wrong...
        ex5:= D E F
        sort_product(_)
        print(_)
    fun4()
    print("fun2 scope "+str(properties()))
    ex3:= R Q
    sort_product(_)
    print(_)
    print('=== locals in fun2 scope ===')
    print(locals())
    # print('=== globals ===')
    # print(globals())

fun2()
ex3:= F E D
sort_product(_)
print(_)


