// Chaque règle renvoie les faits proposés avec son nom et les faits parents.
Dictionary<string, (string rule, string[] parents)> rule_midp_def(Dictionary<string, (string rule, string[] parents)> basis)
{
var output = new Dictionary<string, (string rule, string[] parents)>();
foreach (var fact in basis.Keys)
{
var f = fact.Split('\u001f');
if (f[0] == "midp")
output[canon("cong", f[2], f[1], f[1], f[3])] = ("midp_def", new[] { fact });
}
return output;
}
// Un milieu appartient à la droite de ses extrémités.
Dictionary<string, (string rule, string[] parents)> rule_midp_coll(Dictionary<string, (string rule, string[] parents)> basis)
{
var output = new Dictionary<string, (string rule, string[] parents)>();
foreach (var fact in basis.Keys)
{
var f = fact.Split('\u001f');
if (f[0] == "midp")
output[canon("coll", f[2], f[1], f[3])] = ("midp_coll", new[] { fact });
}
return output;
}
// Relier les segments par les égalités existantes, puis parcourir les chemins de longueur deux.
Dictionary<string, (string rule, string[] parents)> rule_cong_trans(Dictionary<string, (string rule, string[] parents)> basis)
{
var output = new Dictionary<string, (string rule, string[] parents)>();
var byEdge = new Dictionary<(string, string), HashSet<(string fact, (string, string) edge)>>();
foreach (var fact in basis.Keys)
{
var f = fact.Split('\u001f');
if (f[0] != "cong") continue;
var e1 = (f[1], f[2]);
var e2 = (f[3], f[4]);
if (!byEdge.ContainsKey(e1)) byEdge[e1] = new HashSet<(string, (string, string))>();
if (!byEdge.ContainsKey(e2)) byEdge[e2] = new HashSet<(string, (string, string))>();
byEdge[e1].Add((fact, e2));
byEdge[e2].Add((fact, e1));
}
foreach (var entry in byEdge)
foreach (var first in entry.Value)
foreach (var second in byEdge[first.edge])
if (second.edge != entry.Key)
output[canon("cong", entry.Key.Item1, entry.Key.Item2, second.edge.Item1, second.edge.Item2)] = ("cong_trans", new[] { first.fact, second.fact });
return output;
}
// Deux droites perpendiculaires à une même droite sont parallèles.
// La droite commune est indexée : elle peut occuper l'une ou l'autre position dans chaque fait.
Dictionary<string, (string rule, string[] parents)> rule_perp_para(Dictionary<string, (string rule, string[] parents)> basis)
{
var output = new Dictionary<string, (string rule, string[] parents)>();
var byPivot = new Dictionary<(string, string), HashSet<(string fact, (string, string) line)>>();
foreach (var fact in basis.Keys)
{
var f = fact.Split('\u001f');
if (f[0] != "perp") continue;
var l1 = (f[1], f[2]);
var l2 = (f[3], f[4]);
if (!byPivot.ContainsKey(l1)) byPivot[l1] = new HashSet<(string, (string, string))>();
if (!byPivot.ContainsKey(l2)) byPivot[l2] = new HashSet<(string, (string, string))>();
byPivot[l1].Add((fact, l2));
byPivot[l2].Add((fact, l1));
}
foreach (var pivot in byPivot)
foreach (var first in pivot.Value)
foreach (var second in pivot.Value)
if (second.fact != first.fact && second.line != first.line)
output[canon("para", first.line.Item1, first.line.Item2, second.line.Item1, second.line.Item2)] = ("perp_para", new[] { first.fact, second.fact });
return output;
}
// Propager une perpendicularité en remplaçant un point par un point aligné.
Dictionary<string, (string rule, string[] parents)> rule_coll_perp(Dictionary<string, (string rule, string[] parents)> basis)
{
var output = new Dictionary<string, (string rule, string[] parents)>();
var colls = basis.Keys.Where(f => f.Split('\u001f')[0] == "coll").ToArray();
var perps = basis.Keys.Where(f => f.Split('\u001f')[0] == "perp").ToArray();
foreach (var fc in colls)
{
var points = fc.Split('\u001f').Skip(1).ToHashSet();
foreach (var fp in perps)
{
var f = fp.Split('\u001f');
foreach (var pair in new[] { (1, 2), (3, 4) })
{
var p = f[pair.Item1];
var q = f[pair.Item2];
if (!points.Contains(p) || !points.Contains(q)) continue;
foreach (var r in points.Where(x => x != p && x != q))
{
var other1 = pair.Item1 == 1 ? f[3] : f[1];
var other2 = pair.Item1 == 1 ? f[4] : f[2];
output[canon("perp", r, q, other1, other2)] = ("coll_perp", new[] { fc, fp });
}
}
}
}
return output;
}
var RULES = new List<Func<Dictionary<string, (string rule, string[] parents)>, Dictionary<string, (string rule, string[] parents)>>>
{
rule_midp_def, rule_midp_coll, rule_cong_trans, rule_perp_para, rule_coll_perp
};
if (RULES.Count != 5) throw new Exception("Écart sur le nombre de règles : référence Python citée = 5.");
// Témoin de la règle : sur quatre bases de deux faits, la droite commune (M, N) occupe
// chacune des quatre positions possibles — première ou seconde dans chaque fait.
// Le dernier cas est celui qui échappe à un parcours limité à la première position.
int temoinPositions = 0;
var temoinCouvertes = new HashSet<(int, int)>();
var temoinCas = new (string[] faits, string attendu)[]
{
(new[] { "perp M N P Q", "perp M N Y Z" }, "para P Q Y Z"),
(new[] { "perp M N P Q", "perp A B M N" }, "para A B P Q"),
(new[] { "perp A B M N", "perp M N P Q" }, "para A B P Q"),
(new[] { "perp A B M N", "perp C D M N" }, "para A B C D"),
};
var temoinPartage = ("M", "N");
foreach (var temoinCasCourant in temoinCas)
{
var temoinBase = new Dictionary<string, (string rule, string[] parents)>();
foreach (var temoinFait in temoinCasCourant.faits)
temoinBase[canon(temoinFait.Split(' '))] = ("hypothese", Array.Empty<string>());
var temoinSortie = rule_perp_para(temoinBase);
var temoinCle = canon(temoinCasCourant.attendu.Split(' '));
if (!temoinSortie.ContainsKey(temoinCle) || temoinSortie[temoinCle].rule != "perp_para")
throw new Exception("Écart de la règle perp_para par rapport à la référence Python citée.");
// La position lue est celle du fait canonique, pas celle de la saisie.
var temoinPlacement = new List<int>();
foreach (var temoinFait in temoinCasCourant.faits)
{
var temoinF = canon(temoinFait.Split(' ')).Split('\u001f');
temoinPlacement.Add((temoinF[1], temoinF[2]) == temoinPartage ? 1 : 2);
}
temoinCouvertes.Add((temoinPlacement[0], temoinPlacement[1]));
temoinPositions++;
}
if (!temoinCouvertes.SetEquals(new[] { (1, 1), (1, 2), (2, 1), (2, 2) }))
throw new Exception("Écart sur les positions couvertes par la règle perp_para.");
Console.WriteLine($"corpus : {RULES.Count.ToString(CultureInfo.InvariantCulture)} regles elementaires");
Console.WriteLine($"temoin perp_para : {temoinPositions.ToString(CultureInfo.InvariantCulture)} positions sur 4");