From be76f1d7cc6d3fa27422c8d8c393461099b6be48 Mon Sep 17 00:00:00 2001 From: Antonio MATTAR <antonio.mattar@etu.univ-amu.fr> Date: Tue, 28 Jun 2022 16:41:57 +0200 Subject: [PATCH] -Fixed the variables letters problem // -Added operations execution possibilty --- app_functions.py | 29 ++++++++------- logic.py | 93 +++++++++++++++++++++++++++++------------------- main.py | 59 ++++++++++++++++++++++++++++-- 3 files changed, 128 insertions(+), 53 deletions(-) diff --git a/app_functions.py b/app_functions.py index 3fabad7..b311532 100644 --- a/app_functions.py +++ b/app_functions.py @@ -51,25 +51,28 @@ def getSecondFromPair(p): def dec_to_lambda_relative_integers(number): if number >= 0: return pair(TRUE,dec_to_church(number)) - return pair(FALSE, dec_to_church(number)) + return pair(FALSE, dec_to_church(abs(number))) -# def succ(n):#done -# return beta_reduction_totale(new_app(SUCCS,n),None,False) +def succ(n):#done + return beta_reduction_totale(new_app(SUCCS,n),'arithmetic expressions',True) -# def add(n,m):#done -# return beta_reduction_totale(new_app(new_app(ADD,n),m),None,False) +def add(n,m):#done + return beta_reduction_totale(new_app(new_app(ADD,n),m),'arithmetic expressions',True) -# def power(n,m):# done -# return beta_reduction_totale(new_app((new_app(POW, n)),m),None,False) +def power(n,m):# done + return beta_reduction_totale(new_app((new_app(POW, n)),m),'arithmetic expressions',True) -# def multiplication(n,m):#done -# return beta_reduction_totale(new_app(new_app(MUL,n),m),None,False) +def multiplication(n,m):#done + return beta_reduction_totale(new_app(new_app(MUL,n),m),'arithmetic expressions',True) -# def is_zero(n):#done -# return beta_reduction_totale(new_app(IS_ZERO,n),None,False) +def is_zero(n):#done + return beta_reduction_totale(new_app(IS_ZERO,n),'arithmetic expressions',True) -#def predec(n):#done -# return beta_reduction_totale(new_app(PRED, n),None,True) +def predec(n):#done + return beta_reduction_totale(new_app(PRED, n),'arithmetic expressions',True) + +def sub(n,m): + return beta_reduction_totale(new_app(new_app(SUB,n),m),'arithmetic expressions',True) #print(to_string(beta_reduction_totale(new_app(new_app(XOR,TRUE),FALSE),None,False))) #print(to_string(dec_to_lambda_relative_integers(5))) \ No newline at end of file diff --git a/logic.py b/logic.py index c24f4f8..8e8500c 100644 --- a/logic.py +++ b/logic.py @@ -235,56 +235,75 @@ def captureImage(terme, path, counter=True, date= True): image_maker.saveImage(image_maker.createImage(terme),str(counter),path,date) image_counter += 1 +def recognize_term(terme): + if isApplication(terme): + first=getFirstTerm(terme) + second= getSecondTerm(terme) + if isAbstraction(first) and isAbstraction(second) : + a=getOutputFromAbs(first) + b=getOutputFromAbs(second) + if isApplication(a) and isApplication(b): + if isVariable(getFirstTerm(a)) and isVariable(getSecondTerm(a)) and isVariable(getFirstTerm(b)) and isVariable(getSecondTerm(b)): + if getFirstTerm(a)==getSecondTerm(a) and getFirstTerm(b)==getSecondTerm(b): + if getFirstTerm(a)==getInputFromAbs(first) and getFirstTerm(b)==getInputFromAbs(second): + return True + else: + return False + else: + return False + else: + return False + else: + return False + else: + return False + else: + return False + def beta_reduction_totale(terme, path, saveImages=True): - if saveImages==False: + if recognize_term(terme): + print("reduction infinie detectée ") + return terme + elif saveImages==False: if beta_reduction(terme) != None: return beta_reduction_totale(beta_reduction(terme), path, False) return (terme) else: if path == None: - if annotate_reductor(terme) != None: - captureImage(annotate_reductor(terme), None) - else: - captureImage(terme, None) + captureImage(terme, None) else: - if annotate_reductor(terme) != None: - captureImage(annotate_reductor(terme), path) - else: - captureImage(terme,path) + captureImage(terme,path) if beta_reduction(terme) != None: return beta_reduction_totale(beta_reduction(terme), path) return (terme) -numbers_to_letters = { - 0:'x', - 1:'y', - 2:'z', - 3:'q', - 4:'w', - 5:'e', - 6:'r', - 7:'t', - 8:'u', - 9:'p', - 10:'d', - 11:'a', - 12:'k', - 13:'m', - 14:'n', - 15:'v', - 16:'f', - 17:'ç', - 18:'aa', - 19:'bb', - 20:'cc', -} +variables_letters_couples = {} + +import random,string +def random_string(type): + if type == 'lower': + return ''.join(random.choice(string.ascii_lowercase) for i in range(1)) + elif type == 'upper': + return ''.join(random.choice(string.ascii_uppercase) for i in range(1)) + +def associateVariableWithLetter(var): + if var in variables_letters_couples: + return + if len(list(variables_letters_couples.keys())) >= 26 : + x = random_string('upper') + else: + x = random_string('lower') + while x in variables_letters_couples.values(): + x = random_string('lower') + variables_letters_couples[var] = x def to_string_var(terme): assert (isVariable(terme)), 'The argument is not a variable' - return (numbers_to_letters[terme[1]]) + associateVariableWithLetter(terme[1]) + return variables_letters_couples[terme[1]] def to_string_abs(terme): assert (isAbstraction(terme)), 'The argument is not an Abstraction' return "\u03BB"+to_string(getInputFromAbs(terme))+"."+to_string(getOutputFromAbs(terme)) @@ -461,11 +480,13 @@ def beta_reduction_choice_n(terme,n): return new_app(A1,beta_reduction_choice_n(B1,n)) if A2 == None and B2 == None: return None - def beta_reduction_interactive(terme, at): global counters + if recognize_term(terme): + print(" reduction interminable detectee") + return terme if at != None: - print(annotated_to_string(at)) + # print(annotated_to_string(at)) choice = int(input("Choose a beta reduction: ")) while choice <= 0 or choice > counters: print("Invalid choice") @@ -505,5 +526,3 @@ def beta_reduction_interactive_totale(terme,path): - - diff --git a/main.py b/main.py index 3a46111..99f4699 100644 --- a/main.py +++ b/main.py @@ -63,7 +63,9 @@ def delete_images(folder): print(e) def moveImages(src, dest): for filename in os.listdir(src): - shutil.move(src+"/"+filename, dest) + if os.path.isfile(dest+'/'+filename): + os.remove(dest+'/'+filename) + shutil.move(src+"/"+filename, dest) def return_main_menu(): not_pressed = True @@ -75,7 +77,6 @@ def return_main_menu(): clear() time.sleep(1) logic.image_counter = 0 - logic.var_counter = 0 image_maker.variables_colors_couple = {} clear() break @@ -309,6 +310,15 @@ def run_arithmetic_operations_menu(path='arithmetic expressions'): clear() print("Voici le terme: "+ logic.to_string(app_functions.ADD)) logic.captureImage(app_functions.ADD,path,'ADD', False) + choix=(input("do you want to try an exemple? (y/n) : ")) + while choix not in ['y','n']: + choix = input('Invalid choice. Do you want to try an exemple? (y/n): ') + if choix=='y': + clear() + print("you are going to try n+m") + n=int(input("give n : ")) + m=int(input("give m : ")) + app_functions.add(app_functions.dec_to_church(n),app_functions.dec_to_church(m)) save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') @@ -323,7 +333,16 @@ def run_arithmetic_operations_menu(path='arithmetic expressions'): elif choice == 2: clear() print("Voici le terme: "+ logic.to_string(app_functions.SUB)) - logic.captureImage(app_functions.SUB,path,'SUB', False) + logic.captureImage(app_functions.SUB,path+'/SUB','SUB', False) + choix=(input("do you want to try an exemple? (y/n) : ")) + while choix not in ['y','n']: + choix = input('Invalid choice. Do you want to try an exemple? (y/n): ') + if choix=='y': + clear() + print("you are going to try n-m") + n=int(input("give n : ")) + m=int(input("give m : ")) + app_functions.sub(app_functions.dec_to_church(n),app_functions.dec_to_church(m)) save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') @@ -339,6 +358,15 @@ def run_arithmetic_operations_menu(path='arithmetic expressions'): clear() print("Voici le terme: "+ logic.to_string(app_functions.MUL)) logic.captureImage(app_functions.MUL,path,'MUL',False) + choix=(input("do you want to try an exemple? (y/n) : ")) + while choix not in ['y','n']: + choix = input('Invalid choice. Do you want to try an exemple? (y/n): ') + if choix=='y': + clear() + print("you are going to try n*m") + n=int(input("give n : ")) + m=int(input("give m : ")) + app_functions.mul(app_functions.dec_to_church(n),app_functions.dec_to_church(m)) save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') @@ -354,6 +382,15 @@ def run_arithmetic_operations_menu(path='arithmetic expressions'): clear() print("Voici le terme: "+ logic.to_string(app_functions.POW)) logic.captureImage(app_functions.POW,path,'POWER',False) + choix=(input("do you want to try an exemple? (y/n) : ")) + while choix not in ['y','n']: + choix = input('Invalid choice. Do you want to try an exemple? (y/n): ') + if choix=='y': + clear() + print("you are going to try n puiss m") + n=int(input("give n : ")) + m=int(input("give m : ")) + app_functions.power(app_functions.dec_to_church(n),app_functions.dec_to_church(m)) save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') @@ -369,6 +406,14 @@ def run_arithmetic_operations_menu(path='arithmetic expressions'): clear() print("Voici le terme: "+ logic.to_string(app_functions.SUCCS)) logic.captureImage(app_functions.SUCCS,path,'SUCCS',False) + choix=(input("do you want to try an exemple? (y/n) : ")) + while choix not in ['y','n']: + choix = input('Invalid choice. Do you want to try an exemple? (y/n): ') + if choix=='y': + clear() + print("you are going to try n+1") + n=int(input("give n : ")) + app_functions.succ(app_functions.dec_to_church(n)) save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') @@ -384,6 +429,14 @@ def run_arithmetic_operations_menu(path='arithmetic expressions'): clear() print("Voici le terme: "+ logic.to_string(app_functions.PRED)) logic.captureImage(app_functions.PRED,path,'PRED',False) + choix=(input("do you want to try an exemple? (y/n) : ")) + while choix not in ['y','n']: + choix = input('Invalid choice. Do you want to try an exemple? (y/n): ') + if choix=='y': + clear() + print("you are going to try n-1") + n=int(input("give n : ")) + app_functions.predec(app_functions.dec_to_church(n)) save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') -- GitLab