rédaction calepin : reste pouvoir expression + partie avec lamba-expression Python
This commit is contained in:
parent
7c957049db
commit
30267226b3
4010
lambda_calcul.ipynb
4010
lambda_calcul.ipynb
File diff suppressed because it is too large
Load Diff
954
lambda_calcul.md
954
lambda_calcul.md
File diff suppressed because it is too large
Load Diff
@ -53,17 +53,6 @@ class Lambda_parser(Parser):
|
||||
'''
|
||||
tokens = Lambda_lexer.tokens
|
||||
|
||||
# @_('VAR')
|
||||
# def term(self, p):
|
||||
# return Lambda_terme(0, p[0])
|
||||
|
||||
# @_('LAMBDA VAR POINT term')
|
||||
# def term(self, p):
|
||||
# return Lambda_terme(1, p[1], p.term)
|
||||
|
||||
# @_('LPAR term term RPAR')
|
||||
# def term(self, p):
|
||||
# return Lambda_terme(2, p.term0, p.term1)
|
||||
|
||||
@_('VAR')
|
||||
def term(self, p):
|
||||
@ -77,7 +66,12 @@ class Lambda_parser(Parser):
|
||||
def term(self, p):
|
||||
return (2, p.term0, p.term1)
|
||||
|
||||
|
||||
def error(self, p):
|
||||
if p:
|
||||
raise LambdaSyntaxError('{} inattendu en position {}'.format(p.type, p.index))
|
||||
else:
|
||||
raise LambdaSyntaxError('description inachevée')
|
||||
|
||||
parser = Lambda_parser()
|
||||
|
||||
##################################################
|
||||
@ -126,6 +120,19 @@ class Lambda_terme():
|
||||
>>> print(T5)
|
||||
(λz.(λx.(y x) z) t)
|
||||
|
||||
Sous-termes d'un terme
|
||||
>>> for t in T5.sous_termes():
|
||||
... print(t)
|
||||
(λz.(λx.(y x) z) t)
|
||||
λz.(λx.(y x) z)
|
||||
(λx.(y x) z)
|
||||
λx.(y x)
|
||||
(y x)
|
||||
y
|
||||
x
|
||||
z
|
||||
t
|
||||
|
||||
Autres prédicats
|
||||
>>> [(t.est_redex(), t.est_forme_normale()) for t in (T1, T2, T3, T4, T5)]
|
||||
[(False, True), (False, True), (True, False), (False, False), (True, False)]
|
||||
@ -215,7 +222,20 @@ class Lambda_terme():
|
||||
else:
|
||||
return '({:s} {:s})'.format(str(self._content[1]), str(self._content[2]))
|
||||
|
||||
|
||||
def __eq__(self, terme):
|
||||
if not isinstance(terme, Lambda_terme):
|
||||
raise Lambda_termeError('Comparaison inpossible impossible')
|
||||
if self.est_variable():
|
||||
return terme.est_variable() and self._content[1] == terme._content[1]
|
||||
elif self.est_application():
|
||||
return (terme.est_application() and
|
||||
self._content[1] == terme._content[1] and
|
||||
self._content[2] == terme._content[2])
|
||||
else:
|
||||
return (terme.est_abstraction() and
|
||||
self._content[2] == terme._content[2].subs(terme._content[1],
|
||||
Lambda_terme(self._content[1])))
|
||||
|
||||
def applique(self, terme):
|
||||
if not isinstance(terme, Lambda_terme):
|
||||
raise Lambda_termeError('Application impossible')
|
||||
@ -226,6 +246,31 @@ class Lambda_terme():
|
||||
raise Lambda_termeError("Variable d'Abstraction invalide")
|
||||
return Lambda_terme(1, var, self)
|
||||
|
||||
def variables(self):
|
||||
if self.est_variable():
|
||||
return ({self._content[1]}, set())
|
||||
elif self.est_application():
|
||||
var_libres1, var_liees1 = self._content[1].variables()
|
||||
var_libres2, var_liees2 = self._content[2].variables()
|
||||
return (var_libres1.union(var_libres2),
|
||||
var_liees1.union(var_liees2))
|
||||
else:
|
||||
var_libres, var_liees = self._content[2].variables()
|
||||
if self._content[1] in var_libres:
|
||||
var_libres.discard(self._content[1])
|
||||
var_liees.add(self._content[1])
|
||||
return (var_libres, var_liees)
|
||||
|
||||
def sous_termes(self):
|
||||
if self.est_variable():
|
||||
ss_termes = []
|
||||
elif self.est_abstraction():
|
||||
ss_termes = self._content[2].sous_termes()
|
||||
else:
|
||||
ss_termes = self._content[1].sous_termes()
|
||||
ss_termes.extend(self._content[2].sous_termes())
|
||||
return [self] + ss_termes
|
||||
|
||||
def est_redex(self):
|
||||
return self.est_application() and self._content[1].est_abstraction()
|
||||
|
||||
@ -236,18 +281,7 @@ class Lambda_terme():
|
||||
return self._content[2].est_forme_normale()
|
||||
else:
|
||||
return (not self._content[1].est_abstraction() and
|
||||
all(self._content[k].est_forme_normale() for k in (1, 2)))
|
||||
|
||||
def variables_libres(self):
|
||||
if self.est_variable():
|
||||
return {self._content[1]}
|
||||
elif self.est_application():
|
||||
var_libres = self._content[2].variables_libres()
|
||||
return var_libres.union(self._content[1].variables_libres())
|
||||
else:
|
||||
var_libres = self._content[2].variables_libres()
|
||||
var_libres.discard(self._content[1])
|
||||
return var_libres
|
||||
all(self._content[k].est_forme_normale() for k in (1, 2)))
|
||||
|
||||
def subs(self, var, terme):
|
||||
if not isinstance(var, str):
|
||||
@ -266,13 +300,13 @@ class Lambda_terme():
|
||||
else:
|
||||
var_abstr = self._content[1]
|
||||
corps_abstr = self._content[2]
|
||||
var_libres_corps = corps_abstr.variables_libres()
|
||||
var_libres_corps, _ = corps_abstr.variables()
|
||||
if var == var_abstr or var not in var_libres_corps:
|
||||
return self
|
||||
elif var_abstr not in terme.variables_libres():
|
||||
elif var_abstr not in terme.variables()[0]:
|
||||
return Lambda_terme(1, var_abstr, corps_abstr.subs(var, terme))
|
||||
else:
|
||||
nouvelle_var = _autre_variable(var_abstr, corps_abstr.variables_libres())
|
||||
nouvelle_var = _autre_variable(var_abstr, var_libres_corps)
|
||||
return Lambda_terme(1,
|
||||
nouvelle_var,
|
||||
corps_abstr.subs(var_abstr,
|
||||
|
BIN
tikz_diamant.png
Normal file
BIN
tikz_diamant.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.7 KiB |
21
tikz_diamant.tex
Normal file
21
tikz_diamant.tex
Normal file
@ -0,0 +1,21 @@
|
||||
\documentclass{standalone}
|
||||
|
||||
\usepackage{tikz}
|
||||
\usetikzlibrary{automata,positioning}
|
||||
|
||||
\begin{document}
|
||||
\begin{tikzpicture}[shorten >=1pt,node distance=3cm,on grid,>=stealth,
|
||||
every state/.style={draw=none}]
|
||||
|
||||
\node[state] (T) {$T$};
|
||||
\node[state] (R1) [below left=of T] {$R_1$};
|
||||
\node[state] (R2) [below right=of T] {$R_2$};
|
||||
\node[state] (R) [below right=of R1] {$R$};
|
||||
|
||||
\path [every node/.style={font=\footnotesize, below}]
|
||||
(T) edge[->>] node[near end] {$\beta$} (R1)
|
||||
edge[->>] node[near end] {$\beta$} (R2)
|
||||
(R1) edge[->>] node[near end] {$\beta$} (R)
|
||||
(R2) edge[->>] node[near end] {$\beta$} (R);
|
||||
\end{tikzpicture}
|
||||
\end{document}
|
Loading…
Reference in New Issue
Block a user