internal Optional LearnDupLet()

in dsl-samples/MergeConflictsResolution/MergeConflictsResolution/WitnessFunctions.cs [292:361]


        internal Optional<ProgramSet> LearnDupLet(SynthesisEngine engine, LetRule rule,
                                                  LearningTask<DisjunctiveExamplesSpec> task,
                                                  CancellationToken cancel)
        {
            var examples = task.Spec;
            List<string[]> pathsArr = new List<string[]>();
            foreach (KeyValuePair<State, IEnumerable<object>> example in examples.DisjunctiveExamples)
            {
                State inputState = example.Key;
                var input = example.Key[Grammar.InputSymbol] as MergeConflict;
                List<string> idx = new List<string>();
                foreach (IReadOnlyList<Node> output in example.Value)
                {
                    foreach (Node n in Semantics.Concat(input.Upstream, input.Downstream))
                    {
                        bool flag = false;
                        n.Attributes.TryGetValue(Path, out string inPath);
                        foreach (Node node in output)
                        {
                            node.Attributes.TryGetValue(Path, out string outputPath);
                            if (inPath == outputPath)
                            {
                                flag = true;
                            }
                        }
                        if (!flag)
                        {
                            idx.Add(inPath);
                        }
                    }
                }

                pathsArr.Add(idx.ToArray());
            }

            pathsArr.Add(new string[1] { string.Empty });
            List<ProgramSet> programSetList = new List<ProgramSet>();

            foreach (string[] path in pathsArr)
            {
                NonterminalRule findMatchRule = Grammar.Rule(nameof(Semantics.FindMatch)) as NonterminalRule;
                ProgramSet letValueSet =
                    ProgramSet.List(
                        Grammar.Symbol("find"),
                        new NonterminalNode(
                            findMatchRule,
                            new VariableNode(Grammar.InputSymbol),
                            new LiteralNode(Grammar.Symbol("paths"), path)));

                var bodySpec = new Dictionary<State, IEnumerable<object>>();
                foreach (KeyValuePair<State, IEnumerable<object>> kvp in task.Spec.DisjunctiveExamples)
                {
                    State input = kvp.Key;
                    MergeConflict x = (MergeConflict)input[Grammar.InputSymbol];
                    List<IReadOnlyList<Node>> dupValue = Semantics.FindMatch(x, path);

                    State newState = input.Bind(rule.Variable, dupValue);
                    bodySpec[newState] = kvp.Value;
                }

                LearningTask bodyTask = task.Clone(rule.LetBody, new DisjunctiveExamplesSpec(bodySpec));
                ProgramSet bodyProgramSet = engine.Learn(bodyTask, cancel);

                var dupLetProgramSet = ProgramSet.Join(rule, letValueSet, bodyProgramSet);
                programSetList.Add(dupLetProgramSet);
            }

            ProgramSet ps = new UnionProgramSet(rule.Head, programSetList.ToArray());
            return ps.Some();
        }