Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 5.py
- """
- tool #1
- helper for the generation of model
- model / set of attributions
- { door is close <- True, Lights are on <- True }
- { p1, p2 } # this is a notation that means p1 is True and p2 is False
- """
- def getListOfAlternatingTrueFalse(
- piHowManyValues:int,
- piAlternateEveryN:int=1,
- pbStartValue:bool=True
- ):
- retList = list()
- retList = []
- bCurrentValue = pbStartValue
- while(len(retList)<piHowManyValues):
- retList.append(bCurrentValue)
- bAlternateTime:bool = \
- len(retList)%piAlternateEveryN == 0
- if(bAlternateTime):
- bCurrentValue = not bCurrentValue
- # while
- return retList
- # def getListOfAlternatingTrueFalse
- """
- l1 = getListOfAlternatingTrueFalse(
- 33,
- piAlternateEveryN=4,
- pbStartValue=True
- )
- print (l1)
- """
- """
- 1 prop (1 var) => basta 1 lista q alterne de 1-1
- 2 props (2 vars) => 2 listas (1-1 , 2-2)
- 3 props (3 vars) => 3 listas (1-1 , 2-2, 4-4)
- n props (n vars) => n listas (2**0=1, 2**1=2, ... 2**n-1)
- ferramenta auxiliar para compilar todas as listas
- de combinações possíveis
- """
- def getAllPossibleAlternatingListsForNProps(
- pN:int=2,
- pbStartValue:bool=True
- ):
- retListOfLists = list()
- iHowManyCombosThereAreForNProps = 2**pN
- iCurrentList = 0
- while(len(retListOfLists)<pN):
- listOfAlternatingValues = \
- getListOfAlternatingTrueFalse(
- piHowManyValues=iHowManyCombosThereAreForNProps,
- piAlternateEveryN=2**iCurrentList,
- pbStartValue=pbStartValue
- )
- retListOfLists.append(listOfAlternatingValues)
- iCurrentList+=1
- # while
- return retListOfLists
- # def getAllPossibleAlternatingListsForNProps
- """
- l2 = getAllPossibleAlternatingListsForNProps(3)
- print (l2)
- """
- def getAllModelsForNProps(
- pN:int=2,
- pbStartValue:bool=True
- ):
- retListOfAllPossibleModels = list()
- listAlts =\
- getAllPossibleAlternatingListsForNProps(
- pN=pN,
- pbStartValue=pbStartValue
- )
- for idxValue in range(2**pN):
- currentModel = list()
- for idxListAlt in range(len(listAlts)):
- currentList = listAlts[idxListAlt]
- bCurrentValue:bool = currentList[idxValue]
- currentModel.append(bCurrentValue)
- # for
- # a new model is ready
- currentModel.reverse() # in-place
- retListOfAllPossibleModels.append(
- currentModel
- )
- # for
- return retListOfAllPossibleModels
- # def getAllModelsForNProps
- """
- models = getAllModelsForNProps(3)
- print(models)
- """
- def getAllDictModelsForNamedProps(
- pListNamedProps = ["a", "b", "c"]
- ):
- retListOfDictModels = list()
- iHowManyProps = len(pListNamedProps)
- allModels = getAllModelsForNProps(pN=iHowManyProps)
- for idxModel in range(len(allModels)):
- currentModel = allModels[idxModel]
- newDictModel = dict()
- for idxProp in range(len(pListNamedProps)):
- strCurrentProp = pListNamedProps[idxProp]
- bCurrentValue:bool = currentModel[idxProp]
- newDictModel[strCurrentProp] = bCurrentValue
- # for
- # new model (as dict)
- retListOfDictModels.append(newDictModel)
- # for
- return retListOfDictModels
- # def getAllDictModelsForNamedProps
- def truthTable(
- pListOfPropositions, # a list of proposition
- pF # some boolean function
- ):
- listOfCorrespondences = list()
- listOfAllPossibleModelsForProps =\
- getAllDictModelsForNamedProps(
- pListOfPropositions
- )
- for m in listOfAllPossibleModelsForProps:
- listOfValuesInTheCurrentModel = list()
- for propName in m.keys():
- propValue:bool = m[propName]
- listOfValuesInTheCurrentModel.append(
- propValue
- )
- # for every value in the value
- # now we have the list of values for model m
- resultForTheCurrentModel = pF(
- *listOfValuesInTheCurrentModel
- )
- listOfCorrespondences.append(
- (m, resultForTheCurrentModel)
- )
- # for every model
- return listOfCorrespondences
- # def truthTable
- def headerForTruthTable(
- pT,
- pDescription:str=""
- ):
- strHeader:str=""
- bThereAreResults = len(pT)>0
- if (bThereAreResults):
- theFirstModelAsTuple = pT[0]
- theFirstModel:dict = theFirstModelAsTuple[0] # the model
- # the keys n the 1st result are the same
- # as in any other results
- for k in theFirstModel.keys():
- strHeader+=f"{k}\t"
- # for every proposition
- strHeader+=pDescription
- strHeader+="\n"
- #if
- return strHeader
- # def headerForTruthTable
- def renderTruthTable (pT, pDescription:str=""):
- strTablePresentation:str = headerForTruthTable(
- pT,
- pDescription
- )
- for tupleDictBool in pT:
- theModel:dict = tupleDictBool[0]
- result:bool = tupleDictBool[1]
- strLine = ""
- for prop in theModel.keys():
- v:bool = theModel[prop]
- strLine+=str(v)+"\t"
- # for
- # example: "True\tTrue\t"
- strLine+=f"{result}\n"
- # example: "True\tTrue\tTrue\n"
- strTablePresentation+=strLine
- # for every tuple (dict, result)
- return strTablePresentation
- # def renderTruthTable
- def logicalConsequence (pPropositions, pLeft, pRight)->bool:
- truthLeft = truthTable(pPropositions, pLeft)
- truthRight = truthTable(pPropositions, pRight)
- bDoWeHaveLogicalConsequence = True
- for idxModel in range(len(truthLeft)):
- currentTupleModelResult = truthLeft[idxModel]
- currentLeftResult = currentTupleModelResult[1]
- if(currentLeftResult==True):
- currentRightResult = truthRight[idxModel][1]
- bDoWeHaveLogicalConsequence =\
- bDoWeHaveLogicalConsequence and \
- (currentLeftResult == currentRightResult)
- if (not bDoWeHaveLogicalConsequence):
- return False
- # if
- # if
- # for
- return bDoWeHaveLogicalConsequence
- # def logicalConsequence
- def logicalEquivalence(pProps, pLeftExp, pRightExp)->bool:
- bLeftImpliesRight = logicalConsequence(pProps, pLeftExp, pRightExp)
- bRightImpliesLeft = logicalConsequence(pProps, pRightExp, pLeftExp)
- return bLeftImpliesRight and bRightImpliesLeft
- # def logicalEquivalence
- # (p or q) |= p
- def leftSide(p, q):
- return (p or q)
- # def leftSide
- def rightSide(p, q):
- return p
- # def rightSide
- isRightLogicalConsequenceOfTheLeft =\
- logicalConsequence(["p", "q"], leftSide, rightSide)
- print(isRightLogicalConsequenceOfTheLeft)
Advertisement
Add Comment
Please, Sign In to add comment