logik2021/code/aussagenlogik/rekursion.py

85 lines
2.7 KiB
Python
Raw Normal View History

2021-05-06 12:44:29 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# IMPORTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from __future__ import annotations
2021-05-06 12:44:29 +02:00
from lark import Tree;
from typing import List;
from aussagenlogik.schema import isAtom;
from aussagenlogik.schema import isBeliebig;
from aussagenlogik.schema import isTrueSymbol;
from aussagenlogik.schema import isFalseSymbol;
from aussagenlogik.schema import isNegation;
from aussagenlogik.schema import isConjunction;
from aussagenlogik.schema import isLongConjunction;
from aussagenlogik.schema import isDisjunction;
from aussagenlogik.schema import isLongDisjunction;
from aussagenlogik.schema import isImplication;
from aussagenlogik.schema import getName;
from aussagenlogik.schema import SyntaxBaum;
2021-05-06 12:44:29 +02:00
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# GLOBALE KONSTANTEN
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHODEN
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def rekursiv_eval(fml: SyntaxBaum, I: List[str]) -> int:
subfml = fml.children;
if isAtom(fml):
name = getName(fml);
return 1 if (name in I) else 0;
if isBeliebig(fml):
name = getName(fml);
return 1 if (name in I) else 0;
elif isTrueSymbol(fml):
return 1;
elif isFalseSymbol(fml):
return 0;
elif isNegation(fml):
val0 = rekursiv_eval(subfml[0], I);
return 1 - val0;
elif isConjunction(fml):
val0 = rekursiv_eval(subfml[0], I);
val1 = rekursiv_eval(subfml[1], I);
return min(val0, val1);
elif isLongConjunction(fml):
values = [rekursiv_eval(t, I) for t in subfml];
return min(values);
elif isDisjunction(fml):
val0 = rekursiv_eval(subfml[0], I);
val1 = rekursiv_eval(subfml[1], I);
return max(val0, val1);
elif isLongDisjunction(fml):
values = [rekursiv_eval(t, I) for t in subfml];
return max(values);
elif isImplication(fml):
val0 = rekursiv_eval(subfml[0], I);
val1 = rekursiv_eval(subfml[1], I);
return 0 if val0 == 1 and val1 == 0 else 1;
raise Exception('Evaluation nicht möglich!');
2021-05-06 12:44:29 +02:00
def rekursiv_atoms(fml: Tree) -> List[str]:
## Herausforderung: schreibe diese Funktion!
return [];
def rekursiv_depth(fml: Tree) -> int:
## Herausforderung: schreibe diese Funktion!
return 0;
def rekursiv_length(fml: Tree) -> int:
## Herausforderung: schreibe diese Funktion!
return 0;
def rekursiv_parentheses(fml: Tree) -> int:
## Herausforderung: schreibe diese Funktion!
return 0