Skip to content
Snippets Groups Projects
Commit aa02cfe2 authored by Tamazouzt AIT ELDJOUDI's avatar Tamazouzt AIT ELDJOUDI
Browse files

added beta_reduction_interactive and beta-reduction_interactive_totale and fixed to_String

parent 2b4992ea
No related branches found
No related tags found
No related merge requests found
......@@ -204,10 +204,21 @@ def beta_reduction_totale(terme):
return beta_reduction_totale(beta_reduction(terme))
return (terme)
numbers_to_letters = {
0:'x',
1:'y',
2:'z',
3:'q',
4:'w',
5:'e',
6:'r',
7:'t',
8:'u',
}
def to_string_var(terme):
assert (isVariable(terme)), 'The argument is not a variable'
return str(terme[1])
return (numbers_to_letters[terme[1]])
def to_string_abs(terme):
assert (isAbstraction(terme)), 'The argument is not an Abstraction'
return "#"+to_string(getInputFromAbs(terme))+"."+to_string(getOutputFromAbs(terme))
......@@ -341,3 +352,60 @@ def annotated_to_string(terme):
return String_term
def beta_reduction_choice_n(terme,n):
if isVariable(terme): return None
elif isAbstraction(terme):
A = getOutputFromAbs(terme)
B = beta_reduction_choice_n(A,n)
if B != None:
return new_abs(getInputFromAbs(terme), beta_reduction_choice_n((A),n))
else:
return None
elif isApplication(terme):
if len(terme) == 5:
if terme[4] == n:
return terme[3]
else:
terme = terme[:3]
A1 = getFirstTerm(terme)
B1 = getSecondTerm(terme)
A2 = beta_reduction_choice_n(A1,n)
B2 = beta_reduction_choice_n(B1,n)
if A2 != None and B2 != None:
return new_app(beta_reduction_choice_n(A1,n),beta_reduction_choice_n(B1,n))
if A2 != None and B2 == None:
return new_app(beta_reduction_choice_n(A1,n),B1)
if A2 == None and B2 != None:
return new_app(A1,beta_reduction_choice_n(B1,n))
if A2 == None and B2 == None:
return None
def beta_reduction_interactive(terme):
at = annotate_beta_reduction(terme)
if at != None:
print(annotated_to_string(at))
choice = int(input("Choose a beta reduction: "))
while choice <= 0 or choice > counters:
print("Invalid choice")
choice = int(input("Choose a beta reduction: "))
return beta_reduction_choice_n(at,choice)
else:
return terme
def beta_reduction_interactive_totale(terme):
captureImage(terme)
if beta_reduction((terme)) != None:
print(to_string(terme))
choix=int(input("voulez-vous faire la reduction tapez sur 1 pour oui tapez sur 2 pour non "))
if choix==1:
return beta_reduction_interactive_totale(beta_reduction_interactive(terme))
else:
print("C'est fini, le terme obtenu est : "+to_string(terme))
return (terme)
from logic import *
x=new_var(freshVar())
y=new_var(freshVar())
z=new_var(freshVar())
w = new_var(freshVar())
print(isVariable(x) or isVariable(y) or isVariable(z))
print(getVariables(z))
# x=new_var(freshVar())
# y=new_var(freshVar())
# z=new_var(freshVar())
# w = new_var(freshVar())
# print(isVariable(x) or isVariable(y) or isVariable(z))
# print(getVariables(z))
# X = new_abs(x, new_app(x, y))
# Y = new_abs(y, new_app(y, y))
# Z = new_abs(z, new_app(z, x))
X = new_abs(x, new_app(x, y))
Y = new_abs(y, new_app(y, y))
Z = new_abs(z, new_app(z, x))
# print(isAbstraction(X) or isAbstraction(Y) or isAbstraction(Z))
# print(getVariables(Z))
print(isAbstraction(X) or isAbstraction(Y) or isAbstraction(Z))
print(getVariables(Z))
# app1 = new_app(x, y)
# app2 = new_app(y, z)
# app3 = new_app(z, X)
app1 = new_app(x, y)
app2 = new_app(y, z)
app3 = new_app(z, X)
# print(isApplication(app1) or isApplication(app2) or isApplication(app3))
# print(getFirstTerm(app1))
# print(getVariables(app3))
print(isApplication(app1) or isApplication(app2) or isApplication(app3))
print(getFirstTerm(app1))
print(getVariables(app3))
# print(getCommonVariables(Y,Z))
# print(isCommonVariables(X,Z))
# print(getOutputVariablesFromAbs(X))
print(getCommonVariables(Y,Z))
print(isCommonVariables(X,Z))
print(getOutputVariablesFromAbs(X))
# print(X)
# print(substitute(x,z,X))
print(X)
print(substitute(x,z,X))
# print(getBoundVariables(new_abs(x,new_app(new_app(x,y),new_abs(new_var('j'),new_var('j'))))))
print(getBoundVariables(new_abs(x,new_app(new_app(x,y),new_abs(new_var('j'),new_var('j'))))))
# Z = new_abs(x,new_abs(y,new_abs(x, y)))
# Z1 = alpha_rename(Z,y)
# Z2 = alpha_rename(Z1,x)
# S = new_app(new_abs(x,x),new_abs(y,x))
# S1 = alpha_rename(S,y)
# S2 = alpha_rename(S1,x)
# print(S2)
Z = new_abs(x,new_abs(y,new_abs(x, y)))
Z1 = alpha_rename(Z,y)
Z2 = alpha_rename(Z1,x)
S = new_app(new_abs(x,x),new_abs(y,x))
S1 = alpha_rename(S,y)
S2 = alpha_rename(S1,x)
print(S2)
# A=new_abs(x,new_app(new_abs(y,new_app(x,y)),y))
# B=new_app(new_abs(x,new_app(x,y)),x)
# C = new_app(new_abs(x,new_abs(y,x)),y)
# D = new_app(new_abs(x,new_abs(y,new_app(x,y))),new_abs(y,y))
# E = new_app(new_abs(x,new_abs(y,new_app(x,y))),new_abs(y,new_abs(z,new_app(x,z))))
# F = new_app(new_app(new_abs(x,x),y),new_abs(z,z))
# Y_COMBINATOR = new_app(new_abs(x,new_app(x,x)),new_abs(y,new_app(y,y)))
# print('/n')
# print(to_string(E))
# print('/n')
# print((beta_reduction(E)))
# print(x)
# print(beta_reduction_totale(E))
# print(to_string([APP, [APP, [ABS, x, y], y] , z]))
# # print(beta_reduction_totale(Y_COMBINATOR))
A=new_abs(x,new_app(new_abs(y,new_app(x,y)),y))
B=new_app(new_abs(x,new_app(x,y)),x)
C = new_app(new_abs(x,new_abs(y,x)),y)
D = new_app(new_abs(x,new_abs(y,new_app(x,y))),new_abs(y,y))
E = new_app(new_abs(x,new_abs(y,new_app(x,y))),new_abs(y,new_abs(z,new_app(x,z))))
F = new_app(new_app(new_abs(x,x),y),new_abs(z,z))
Y_COMBINATOR = new_app(new_abs(x,new_app(x,x)),new_abs(y,new_app(y,y)))
print('/n')
print(to_string(E))
print('/n')
print((beta_reduction(E)))
print(x)
print(beta_reduction_totale(E))
print(to_string([APP, [APP, [ABS, x, y], y] , z]))
# print(beta_reduction_totale(Y_COMBINATOR))
# #############################################################################################################################
# import parser
# # A = "#x.(x)(y)"
# # print(getTermsFromParantheses("(AAAAAA)(BBBBBBBBB)(CCCCCCCCC)"))
# # print(buildTerm(A))
# # print(remove_first_and_last_spaces('ll'))
# # print(isAbstraction(A))
# # print(isApplication("(x)(y)(z)"))
# # print(checkType("(#x.x)"))
# # print(buildTerm('(#x.x)(#y.y)'))
# # print(remove_inutile_spaces('(#x.x)(#y.y)'))
# # print(open_parantheses_counter('((((((((((('))
# # print(close_parantheses_counter('((((((((((()'))
# # print(buildAbs("#x.ABC"))
# # print(checkType("(#x.xx)(fds)"))
# # print(findClosingParanthesesIndex('()',0))
#############################################################################################################################
import parser
# A = "#x.(x)(y)"
# print(getTermsFromParantheses("(AAAAAA)(BBBBBBBBB)(CCCCCCCCC)"))
# print(buildTerm(A))
# print(remove_first_and_last_spaces('ll'))
# print(isAbstraction(A))
# print(isApplication("(x)(y)(z)"))
# print(checkType("(#x.x)"))
# print(buildTerm('(#x.x)(#y.y)'))
# print(remove_inutile_spaces('(#x.x)(#y.y)'))
# print(open_parantheses_counter('((((((((((('))
# print(close_parantheses_counter('((((((((((()'))
# print(buildAbs("#x.ABC"))
# print(checkType("(#x.xx)(fds)"))
# print(findClosingParanthesesIndex('()',0))
\ No newline at end of file
x=new_var(freshVar())
y=new_var(freshVar())
z=new_var(freshVar())
term= new_app(new_abs(x,x),new_app(new_abs(y,y),z))
# #print(annotated_to_string(annotate_beta_reduction(term)))
# print(annotated_to_string(beta_reduction_choice_n(annotate_beta_reduction(term),1)))
import parsing
#print(annotated_to_string(x))
#beta_reduction_interactive_totale(parsing.parseTerm(input("Enter a term: ")))
beta_reduction_interactive_totale(term)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment