I can't seem to add values into the dictionary. I am trying to extend the dictionary recursively but keep on getting the error/warning: "Cannot convert from 'void' to 'System.Collections.Generic.Dictionary<string, bool>'". I am a beginner so I am not sure how to resolve this error.
I am getting the error on the last line in the return statement specifically the model.Add(p,true) part.
namespace InferenceEngine
{
internal class TruthTable
{
TruthTable() { }
private List<string> ExtractSymbol(Sentence KB)
{
List<string> result = new List<string>();
if(KB.symbol != null)
{
result.Add(KB.symbol);
}
else
{
foreach(Sentence s in KB.child)
{
result.Concat(ExtractSymbol(s));
}
}
return result;
}
private bool PL_True(Sentence KB, Dictionary<string,bool> model)
{
if(KB.symbol != null)
{
return model[KB.symbol];
}
else if ( KB.connetives == "AND")
{
foreach (Sentence s in KB.child)
{
if(PL_True(s, model) == false)
{
return false;
}
return true;
}
}
else if (KB.connetives == "OR")
{
foreach (Sentence s in KB.child)
{
if (PL_True(s, model) == true)
{
return true;
}
return false;
}
}
else if (KB.connetives == "AND")
{
foreach (Sentence s in KB.child)
{
if (PL_True(s, model) == false)
{
return false;
}
return true;
}
}
else if (KB.connetives == "IF")
{
Sentence left = KB.child[0];
Sentence right = KB.child[KB.child.Count - 1];
if (PL_True(left,model) == true || PL_True(right,model) == false)
{
return false;
}
return true;
}
else if (KB.connetives == "IFF")
{
Sentence left = KB.child[0];
Sentence right = KB.child[KB.child.Count - 1];
if (PL_True(left, model) == true && PL_True(right, model) == false)
{
return false;
}
return true;
}
else if (KB.connetives == "NOT")
{
Sentence opposite = KB.child[0];
if (PL_True(opposite, model) == true)
{
return false;
}
return true;
}
return false;
}
public bool TT_Entails(Sentence KB, Sentence Alpha)
{
List<string> symbol1 = ExtractSymbol(KB);
List<string> symbol2 = ExtractSymbol(Alpha);
List<string> symbols = new List<string>();
symbols.Concat(symbol1);
symbols.Concat(symbol2);
Dictionary<string, bool> table = new();
return TT_Check_All(KB, Alpha, symbols, table );
}
private bool TT_Check_All(Sentence KB, Sentence Alpha, List<string> symbol, Dictionary<string, bool> model)
{
if(symbol.Count == 0)
{
if(PL_True(KB, model) == true)
{
return PL_True(Alpha, model);
}
return false;
}
string p = symbol[0];
symbol.RemoveAt(0);
List<string> rest = symbol;
return TT_Check_All(KB, Alpha, rest, model.Add(p,true)) && TT_Check_All(KB, Alpha, rest, model.Add(p, false));
}
}
}
CodePudding user response:
The Add
method on the dictionary returns void
. So when you pass the result of model.Add(p, false)
into TT_Check_All
recursively, you are passing a void
rather than the expected Dictionary<string, bool>
type.
You can solve this issue by doing the Add
before the recursive call and passing in model.