Massimo Nocentini


Abstract
In this document we collect a naive type system based on sets.

In [1]:
from itertools import repeat
from sympy import *
#from type_system import *

In [2]:
%run ../../src/commons.py

In [3]:
%run ./type-system.py


In [4]:
init_printing()

In [5]:
x,y,m,n,t,z = symbols('x y m n t z', commutative=True)
alpha, beta, gamma, eta = symbols(r'\alpha \beta \gamma \eta', commutative=True)
f,g = Function('f'), Function('g')

lists


In [6]:
lst_of_alpha_gf, = lst(tyvar(alpha)[z]).gf()
lst_of_alpha_gf


Out[6]:
$$\mathcal{L}{\left (\alpha,z \right )} = - \frac{1}{\alpha z - 1}$$

In [7]:
occupancy(lst_of_alpha_gf, syms=[alpha], objects='unlike', boxes='unlike').series(z)


Out[7]:
$$\operatorname{gf}{\left (z,\alpha \right )} = - \left(z \alpha - 1\right)^{-1} = 1 + z \alpha + z^{2} \alpha^{2} + z^{3} \alpha^{3} + z^{4} \alpha^{4} + z^{5} \alpha^{5} + O\left(z^{6}\right)$$

In [8]:
occupancy(lst_of_alpha_gf, syms=[alpha], objects='unlike', boxes='like').series(z)


Out[8]:
$$\operatorname{gf}{\left (\alpha,z \right )} = - \frac{1}{\alpha z - 1} = 1 + \alpha z + \alpha^{2} z^{2} + \alpha^{3} z^{3} + \alpha^{4} z^{4} + \alpha^{5} z^{5} + O\left(z^{6}\right)$$

In [9]:
occupancy(lst_of_alpha_gf, syms=[alpha], objects='like', boxes='unlike').series(z)


Out[9]:
$$\operatorname{gf}{\left (z,\bullet \right )} = - \left(z \bullet - 1\right)^{-1} = 1 + z \bullet + z^{2} \bullet^{2} + z^{3} \bullet^{3} + z^{4} \bullet^{4} + z^{5} \bullet^{5} + O\left(z^{6}\right)$$

In [10]:
occupancy(lst_of_alpha_gf, syms=[alpha], objects='like', boxes='like').series(z)


Out[10]:
$$\operatorname{gf}{\left (\circ,z \right )} = - \frac{1}{\circ z - 1} = 1 + \circ z + \circ^{2} z^{2} + \circ^{3} z^{3} + \circ^{4} z^{4} + \circ^{5} z^{5} + O\left(z^{6}\right)$$


In [12]:
lst_of_maybe_of_alpha_gf, = lst(maybe(truth)[z]).gf()
lst_of_maybe_of_alpha_gf


Out[12]:
$$\mathcal{L}{\left (z,w_{1} \right )} = - \frac{1}{w_{1} z + z ␣ - 1}$$

In [13]:
occupancy(lst_of_maybe_of_alpha_gf, syms=[w[1]], objects='unlike', boxes='unlike').series(z, n=5)


Out[13]:
$$\operatorname{gf}{\left (␣,z,w_{1} \right )} = - \left(z w_{1} + z ␣ - 1\right)^{-1} = 1 + z \left(w_{1} + ␣\right) + z^{2} \left(w_{1} ␣ + w_{1}^{2} + ␣ w_{1} + ␣^{2}\right) + z^{3} \left(w_{1} ␣ w_{1} + w_{1} ␣^{2} + w_{1}^{2} ␣ + w_{1}^{3} + ␣ w_{1} ␣ + ␣ w_{1}^{2} + ␣^{2} w_{1} + ␣^{3}\right) + z^{4} \left(w_{1} ␣ w_{1} ␣ + w_{1} ␣ w_{1}^{2} + w_{1} ␣^{2} w_{1} + w_{1} ␣^{3} + w_{1}^{2} ␣ w_{1} + w_{1}^{2} ␣^{2} + w_{1}^{3} ␣ + w_{1}^{4} + ␣ w_{1} ␣ w_{1} + ␣ w_{1} ␣^{2} + ␣ w_{1}^{2} ␣ + ␣ w_{1}^{3} + ␣^{2} w_{1} ␣ + ␣^{2} w_{1}^{2} + ␣^{3} w_{1} + ␣^{4}\right) + O\left(z^{5}\right)$$

In [14]:
occupancy(lst_of_maybe_of_alpha_gf, syms=[w[1]], objects='unlike', boxes='like').series(z, n=5)


Out[14]:
$$\operatorname{gf}{\left (␣,z,w_{1} \right )} = - \left(w_{1} z + z ␣ - 1\right)^{-1} = 1 + z \left(w_{1} + ␣\right) + z^{2} \left(w_{1}^{2} + 2 w_{1} ␣ + ␣^{2}\right) + z^{3} \left(w_{1}^{3} + 3 w_{1}^{2} ␣ + 3 w_{1} ␣^{2} + ␣^{3}\right) + z^{4} \left(w_{1}^{4} + 4 w_{1}^{3} ␣ + 6 w_{1}^{2} ␣^{2} + 4 w_{1} ␣^{3} + ␣^{4}\right) + O\left(z^{5}\right)$$

In [15]:
occupancy(lst_of_maybe_of_alpha_gf, syms=[w[1]], objects='like', boxes='unlike').series(z, n=5)


Out[15]:
$$\operatorname{gf}{\left (␣,z,\bullet \right )} = - \left(z \bullet + z ␣ - 1\right)^{-1} = 1 + z \left(\bullet + ␣\right) + z^{2} \left(\bullet ␣ + \bullet^{2} + ␣ \bullet + ␣^{2}\right) + z^{3} \left(\bullet ␣ \bullet + \bullet ␣^{2} + \bullet^{2} ␣ + \bullet^{3} + ␣ \bullet ␣ + ␣ \bullet^{2} + ␣^{2} \bullet + ␣^{3}\right) + z^{4} \left(\bullet ␣ \bullet ␣ + \bullet ␣ \bullet^{2} + \bullet ␣^{2} \bullet + \bullet ␣^{3} + \bullet^{2} ␣ \bullet + \bullet^{2} ␣^{2} + \bullet^{3} ␣ + \bullet^{4} + ␣ \bullet ␣ \bullet + ␣ \bullet ␣^{2} + ␣ \bullet^{2} ␣ + ␣ \bullet^{3} + ␣^{2} \bullet ␣ + ␣^{2} \bullet^{2} + ␣^{3} \bullet + ␣^{4}\right) + O\left(z^{5}\right)$$

In [16]:
occupancy(lst_of_maybe_of_alpha_gf, syms=[w[1]], objects='like', boxes='like').series(z,n=5)


Out[16]:
$$\operatorname{gf}{\left (\circ,z \right )} = - \frac{1}{\circ z + z - 1} = 1 + z \left(\circ + 1\right) + z^{2} \left(\circ^{2} + 2 \circ + 1\right) + z^{3} \left(\circ^{3} + 3 \circ^{2} + 3 \circ + 1\right) + z^{4} \left(\circ^{4} + 4 \circ^{3} + 6 \circ^{2} + 4 \circ + 1\right) + O\left(z^{5}\right)$$


In [24]:
nats = (lst(truth[z]) * lst(falsehood[z]))
nats_gf, = nats.gf()
nats_gf


Out[24]:
$$\times{\left (w_{0},z,w_{1} \right )} = \frac{1}{\left(w_{0} z - 1\right) \left(w_{1} z - 1\right)}$$

In [25]:
occupancy(nats_gf, syms=[w[1],w[0]], objects='unlike', boxes='unlike').series(z)


Out[25]:
$$\operatorname{gf}{\left (z,w_{1},w_{0} \right )} = \left(z w_{1} - 1\right)^{-1} \left(z w_{0} - 1\right)^{-1} = 1 + z \left(w_{0} + w_{1}\right) + z^{2} \left(w_{0}^{2} + w_{1} w_{0} + w_{1}^{2}\right) + z^{3} \left(w_{0}^{3} + w_{1} w_{0}^{2} + w_{1}^{2} w_{0} + w_{1}^{3}\right) + z^{4} \left(w_{0}^{4} + w_{1} w_{0}^{3} + w_{1}^{2} w_{0}^{2} + w_{1}^{3} w_{0} + w_{1}^{4}\right) + z^{5} \left(w_{0}^{5} + w_{1} w_{0}^{4} + w_{1}^{2} w_{0}^{3} + w_{1}^{3} w_{0}^{2} + w_{1}^{4} w_{0} + w_{1}^{5}\right) + O\left(z^{6}\right)$$

In [19]:
occupancy(nats_gf, syms=[w[1],w[0]], objects='unlike', boxes='like').series(z)


Out[19]:
$$\operatorname{gf}{\left (w_{0},z,w_{1} \right )} = \frac{1}{\left(w_{0} z - 1\right) \left(w_{1} z - 1\right)} = 1 + z \left(w_{0} + w_{1}\right) + z^{2} \left(w_{0}^{2} + w_{0} w_{1} + w_{1}^{2}\right) + z^{3} \left(w_{0}^{3} + w_{0}^{2} w_{1} + w_{0} w_{1}^{2} + w_{1}^{3}\right) + z^{4} \left(w_{0}^{4} + w_{0}^{3} w_{1} + w_{0}^{2} w_{1}^{2} + w_{0} w_{1}^{3} + w_{1}^{4}\right) + z^{5} \left(w_{0}^{5} + w_{0}^{4} w_{1} + w_{0}^{3} w_{1}^{2} + w_{0}^{2} w_{1}^{3} + w_{0} w_{1}^{4} + w_{1}^{5}\right) + O\left(z^{6}\right)$$

In [20]:
occupancy(nats_gf, syms=[w[1],w[0]], objects='like', boxes='unlike').series(z)


Out[20]:
$$\operatorname{gf}{\left (z,\bullet \right )} = \left(z \bullet - 1\right)^{-2} = 1 + 2 z \bullet + 3 z^{2} \bullet^{2} + 4 z^{3} \bullet^{3} + 5 z^{4} \bullet^{4} + 6 z^{5} \bullet^{5} + O\left(z^{6}\right)$$

In [21]:
occupancy(nats_gf, syms=[w[1],w[0]], objects='like', boxes='like').series(z)


Out[21]:
$$\operatorname{gf}{\left (\circ,z \right )} = \frac{1}{\left(\circ z - 1\right)^{2}} = 1 + 2 \circ z + 3 \circ^{2} z^{2} + 4 \circ^{3} z^{3} + 5 \circ^{4} z^{4} + 6 \circ^{5} z^{5} + O\left(z^{6}\right)$$


In [26]:
lst_of_boolean_gf, = lst(boolean[z]).gf()
lst_of_boolean_gf


Out[26]:
$$\mathcal{L}{\left (w_{0},z,w_{1} \right )} = - \frac{1}{w_{0} z + w_{1} z - 1}$$

In [27]:
occupancy(lst_of_boolean_gf, syms=[w[1],w[0]], objects='unlike', boxes='unlike').series(z, n=5)


Out[27]:
$$\operatorname{gf}{\left (z,w_{1},w_{0} \right )} = - \left(z w_{0} + z w_{1} - 1\right)^{-1} = 1 + z \left(w_{0} + w_{1}\right) + z^{2} \left(w_{0} w_{1} + w_{0}^{2} + w_{1} w_{0} + w_{1}^{2}\right) + z^{3} \left(w_{0} w_{1} w_{0} + w_{0} w_{1}^{2} + w_{0}^{2} w_{1} + w_{0}^{3} + w_{1} w_{0} w_{1} + w_{1} w_{0}^{2} + w_{1}^{2} w_{0} + w_{1}^{3}\right) + z^{4} \left(w_{0} w_{1} w_{0} w_{1} + w_{0} w_{1} w_{0}^{2} + w_{0} w_{1}^{2} w_{0} + w_{0} w_{1}^{3} + w_{0}^{2} w_{1} w_{0} + w_{0}^{2} w_{1}^{2} + w_{0}^{3} w_{1} + w_{0}^{4} + w_{1} w_{0} w_{1} w_{0} + w_{1} w_{0} w_{1}^{2} + w_{1} w_{0}^{2} w_{1} + w_{1} w_{0}^{3} + w_{1}^{2} w_{0} w_{1} + w_{1}^{2} w_{0}^{2} + w_{1}^{3} w_{0} + w_{1}^{4}\right) + O\left(z^{5}\right)$$

In [28]:
occupancy(lst_of_boolean_gf, syms=[w[1],w[0]], objects='unlike', boxes='like').series(z, n=5)


Out[28]:
$$\operatorname{gf}{\left (w_{0},z,w_{1} \right )} = - \frac{1}{w_{0} z + w_{1} z - 1} = 1 + z \left(w_{0} + w_{1}\right) + z^{2} \left(w_{0}^{2} + 2 w_{0} w_{1} + w_{1}^{2}\right) + z^{3} \left(w_{0}^{3} + 3 w_{0}^{2} w_{1} + 3 w_{0} w_{1}^{2} + w_{1}^{3}\right) + z^{4} \left(w_{0}^{4} + 4 w_{0}^{3} w_{1} + 6 w_{0}^{2} w_{1}^{2} + 4 w_{0} w_{1}^{3} + w_{1}^{4}\right) + O\left(z^{5}\right)$$

In [29]:
occupancy(lst_of_boolean_gf, syms=[w[1],w[0]], objects='like', boxes='unlike').series(z, n=5)


Out[29]:
$$\operatorname{gf}{\left (z,\bullet \right )} = - \left(2 z \bullet - 1\right)^{-1} = 1 + 2 z \bullet + 4 z^{2} \bullet^{2} + 8 z^{3} \bullet^{3} + 16 z^{4} \bullet^{4} + O\left(z^{5}\right)$$

In [30]:
occupancy(lst_of_boolean_gf, syms=[w[1],w[0]], objects='like', boxes='like').series(z, n=5)


Out[30]:
$$\operatorname{gf}{\left (\circ,z \right )} = - \frac{1}{2 \circ z - 1} = 1 + 2 \circ z + 4 \circ^{2} z^{2} + 8 \circ^{3} z^{3} + 16 \circ^{4} z^{4} + O\left(z^{5}\right)$$


In [31]:
dollar_exchange, = (lst(tyvar(u[1]*x)) * lst(tyvar(u[5]*x**5)) * lst(tyvar(u[10]*x**10)) * 
                    lst(tyvar(u[25]*x**25)) * lst(tyvar(u[50]*x**50))).gf()
dollar_exchange


Out[31]:
$$\times{\left (u_{5},u_{10},u_{25},u_{50},x,u_{1} \right )} = - \frac{1}{\left(u_{1} x - 1\right) \left(u_{10} x^{10} - 1\right) \left(u_{25} x^{25} - 1\right) \left(u_{5} x^{5} - 1\right) \left(u_{50} x^{50} - 1\right)}$$

In [32]:
coins = dollar_exchange.series(x, n=101)

In [33]:
coins.rhs.rhs.coeff(x,42)


Out[33]:
$$u_{1}^{42} + u_{1}^{37} u_{5} + u_{1}^{32} u_{10} + u_{1}^{32} u_{5}^{2} + u_{1}^{27} u_{10} u_{5} + u_{1}^{27} u_{5}^{3} + u_{1}^{22} u_{10}^{2} + u_{1}^{22} u_{10} u_{5}^{2} + u_{1}^{22} u_{5}^{4} + u_{1}^{17} u_{10}^{2} u_{5} + u_{1}^{17} u_{10} u_{5}^{3} + u_{1}^{17} u_{25} + u_{1}^{17} u_{5}^{5} + u_{1}^{12} u_{10}^{3} + u_{1}^{12} u_{10}^{2} u_{5}^{2} + u_{1}^{12} u_{10} u_{5}^{4} + u_{1}^{12} u_{25} u_{5} + u_{1}^{12} u_{5}^{6} + u_{1}^{7} u_{10}^{3} u_{5} + u_{1}^{7} u_{10}^{2} u_{5}^{3} + u_{1}^{7} u_{10} u_{25} + u_{1}^{7} u_{10} u_{5}^{5} + u_{1}^{7} u_{25} u_{5}^{2} + u_{1}^{7} u_{5}^{7} + u_{1}^{2} u_{10}^{4} + u_{1}^{2} u_{10}^{3} u_{5}^{2} + u_{1}^{2} u_{10}^{2} u_{5}^{4} + u_{1}^{2} u_{10} u_{25} u_{5} + u_{1}^{2} u_{10} u_{5}^{6} + u_{1}^{2} u_{25} u_{5}^{3} + u_{1}^{2} u_{5}^{8}$$

In [34]:
assert 292 == coins.rhs.rhs.coeff(x,100).subs( # number of ways to change *one* dollar
    {u[1]:1, u[5]:1, u[10]:1, u[25]:1, u[50]:1})

non-empty lists


In [35]:
nnlst_of_alpha_gf, = nnlst(tyvar(alpha*z)).gf()
nnlst_of_alpha_gf


Out[35]:
$$\mathcal{L}_{+}{\left (\alpha,z \right )} = - \frac{\alpha z}{\alpha z - 1}$$

In [36]:
nnlst_of_alpha_gf.series(z, n=10)


Out[36]:
$$\mathcal{L}_{+}{\left (\alpha,z \right )} = - \frac{\alpha z}{\alpha z - 1} = \alpha z + \alpha^{2} z^{2} + \alpha^{3} z^{3} + \alpha^{4} z^{4} + \alpha^{5} z^{5} + \alpha^{6} z^{6} + \alpha^{7} z^{7} + \alpha^{8} z^{8} + \alpha^{9} z^{9} + O\left(z^{10}\right)$$

In [38]:
nnlst_gf, = nnlst(nnlst(truth[z])*falsehood).gf()
nnlst_gf


Out[38]:
$$\mathcal{L}_{+}{\left (w_{0},z,w_{1} \right )} = - \frac{w_{0} w_{1} z}{w_{0} w_{1} z + w_{1} z - 1}$$

In [39]:
occupancy(nnlst_gf, syms=[w[1],w[0]], objects='unlike', boxes='unlike').series(z, n=5)


Out[39]:
$$\operatorname{gf}{\left (w_{0},z,w_{1} \right )} = - z w_{1} w_{0} \left(z w_{1} + z w_{1} w_{0} - 1\right)^{-1} = z w_{1} w_{0} + z^{2} \left(w_{1} w_{0} w_{1} + w_{1} w_{0} w_{1} w_{0}\right) + z^{3} \left(w_{1} w_{0} w_{1} w_{0} w_{1} + w_{1} w_{0} w_{1} w_{0} w_{1} w_{0} + w_{1} w_{0} w_{1}^{2} + w_{1} w_{0} w_{1}^{2} w_{0}\right) + z^{4} \left(w_{1} w_{0} w_{1} w_{0} w_{1} w_{0} w_{1} + w_{1} w_{0} w_{1} w_{0} w_{1} w_{0} w_{1} w_{0} + w_{1} w_{0} w_{1} w_{0} w_{1}^{2} + w_{1} w_{0} w_{1} w_{0} w_{1}^{2} w_{0} + w_{1} w_{0} w_{1}^{2} w_{0} w_{1} + w_{1} w_{0} w_{1}^{2} w_{0} w_{1} w_{0} + w_{1} w_{0} w_{1}^{3} + w_{1} w_{0} w_{1}^{3} w_{0}\right) + O\left(z^{5}\right)$$

In [40]:
occupancy(nnlst_gf, syms=[w[1],w[0]], objects='unlike', boxes='like').series(z, n=5)


Out[40]:
$$\operatorname{gf}{\left (w_{0},w_{1},z \right )} = - \frac{w_{0} w_{1} z}{w_{0} w_{1} z + w_{1} z - 1} = z^{2} \left(w_{0}^{2} w_{1}^{2} + w_{0} w_{1}^{2}\right) + z^{3} \left(w_{0}^{3} w_{1}^{3} + 2 w_{0}^{2} w_{1}^{3} + w_{0} w_{1}^{3}\right) + z^{4} \left(w_{0}^{4} w_{1}^{4} + 3 w_{0}^{3} w_{1}^{4} + 3 w_{0}^{2} w_{1}^{4} + w_{0} w_{1}^{4}\right) + w_{0} w_{1} z + O\left(z^{5}\right)$$