75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
"""
|
|
Library containing all the basic functions
|
|
"""
|
|
|
|
# from ..proof import Context, Proof
|
|
from ..props import Eq, Or, Prop
|
|
from ..terms import App, Const, EqT, Lambda, Term, Var, register_known_const
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Identity function
|
|
# -----------------------------------------------------------------------------
|
|
# Always returns its input.
|
|
register_known_const("identity", Lambda(Var(0)))
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Equality
|
|
# -----------------------------------------------------------------------------
|
|
equality = Lambda(Lambda(EqT(lhs=Var(1), rhs=Var(0))))
|
|
|
|
register_known_const("eq", equality)
|
|
register_known_const("==", equality)
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Bool
|
|
# -----------------------------------------------------------------------------
|
|
|
|
bool_t = Lambda(Lambda(Var(1)))
|
|
bool_f = Lambda(Lambda(Var(0)))
|
|
if_statement = Lambda(Var(0))
|
|
not_statement = Lambda(App(f=App(f=Var(0), x=bool_f), x=bool_t))
|
|
|
|
register_known_const("Bool.False", bool_f)
|
|
register_known_const("Bool.True", bool_t)
|
|
register_known_const("Bool.elim", if_statement)
|
|
register_known_const("Bool.if", if_statement)
|
|
register_known_const("Bool.not", not_statement)
|
|
|
|
def is_bool(x : Term) -> Prop:
|
|
return Or(Eq(x=x, y=bool_f), Eq(x=x, y=bool_t))
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Nat
|
|
# -----------------------------------------------------------------------------
|
|
|
|
zero = Const("Nat.Zero")
|
|
succ = Const("Nat.Succ")
|
|
|
|
def kernel_from_int(n : int) -> Term:
|
|
if n < 0:
|
|
raise ValueError(
|
|
"Int is not Nat"
|
|
)
|
|
elif n == 0:
|
|
return zero
|
|
else:
|
|
return App(f=succ, x=kernel_from_int(n-1))
|
|
|
|
def is_nat(x : Term) -> Prop:
|
|
return Or(
|
|
Eq(x=x, y=zero),
|
|
Eq(bool_f, bool_t) # TODO: Construct x = Succ n where isNat(n),
|
|
)
|
|
|
|
register_known_const("Nat.add", Lambda(Lambda(
|
|
App(f=App( # If-statement: is the first argument zero?
|
|
f=EqT(lhs=Var(1), rhs=zero),
|
|
# If so, return the second argument
|
|
x=Var(0),
|
|
),
|
|
# If not, return a recursive solution of the add function
|
|
# TODO: Not correct yet
|
|
x=App(f=succ, x=App(f=App(f=Const("Nat.add"), x=Var(1)), x=Var(0)))
|
|
)
|
|
)))
|