variable (python sample)
See also
This page shows how to declare free-variables in a universe-of-discourse.
Usage 1
The preferred approach to declare a variable is to call the with_variable method on the instance of the UniverseOfDiscourse :
The scope of the variable is managed automatically.
u = pu.create_universe()
with u.with_variable() as x, u.with_variable() as y,
...
# some formula elaboration code
...
Usage 2
If you want to manage the scope of your variables expressly, you may call the declare_variable method on the instance of the UniverseOfDiscourse :
u = pu.create_universe()
x = u.declare_variable()
y = u.with_variable()
...
# some theory derivation code
...
phi.lock_variable_scope()
Sample code
Code output
𝖫𝖾𝗍 ⌜𝒰₇₆⌝ 𝖻𝖾 𝖺 𝑢𝑛𝑖𝑣𝑒𝑟𝑠𝑒-𝑜𝑓-𝑑𝑖𝑠𝑐𝑜𝑢𝑟𝑠𝑒.
𝑓(𝐱)
((𝑔(𝐱) + (𝑓(𝐱) + 𝑔(𝐲))) + 𝐱)
(𝑓(𝐱) + 𝐱)
import punctilious as pu
# Create a universe-of-discourse.
u = pu.create_universe_of_discourse(echo=True)
o1 = u.o.declare()
o2 = u.o.declare()
o3 = u.o.declare()
pu.configuration.auto_index = False
f = u.r.declare(symbol='f')
g = u.r.declare(symbol='g')
plus = u.r.declare(symbol='+', formula_rep=pu.Formula.infix)
# FreeVariables have a scope. But often, when we write mathematical formulas,
# their scope is implicit. Typically, we use "x" multiple times and imply
# that different occurrences of "x" or not necessarily the same variable "x".
# With punctilious, a simple way to declare variables with a precise
# scope is to use the with_variable() method on an instance of UniverseOfDiscourse.
# Here are two examples:
# SAMPLE 1
# Declare a variable and declare a formula that uses it.
# In this example, the scope of the variable "x" is the formula "f(x)".
with u.with_variable(symbol='x') as x:
phi = f(x)
print(phi)
# SAMPLE 2
# Declare two variables and declare a formula that uses them.
# In this example, the scope of the variable "x" is the formula "((g(x) + (f(x) + g(y))) + x)".
with u.with_variable(symbol='x') as x, u.with_variable(symbol='y') as y:
psi = (g(x) | u.r.plus | (f(x) | u.r.plus | g(y))) | u.r.plus | x
print(psi)
# Note that we created above two distinct "x" variable objects with two distinct scopes:
# the one in SAMPLE 1 and the one in SAMPLE 2.
# This illustrates that two distinct objects may be assigned exactly the same name,
# which may create confusion sometime. And this is often the case with variables.
# The with_variable() method uses the python @contextlib.contextmanager
# to manage automatically the variable scope.
# Alternatively, you may wish to declare variables and define their scope expressly.
# SAMPLE 3
# Declare a variable and close its scope expressly.
x = u.declare_variable(symbol='x')
omega = f(x) | u.r.plus | x
omega.lock_variable_scope()
print(omega)
# When you call the lock_variable_scope() on an instance of Formula, the scope
# of all the variables contained in the formula is locked and defined as the most
# parent containing formula.
Let "U75" be a universe-of-discourse.
f(x)
((g(x) + (f(x) + g(y))) + x)
(f(x) + x)
Will be provided in a future version.
Will be provided in a future version.