示例#1
0
        /// <summary>
        /// Note: input program's type definitions wiill be pre-processed to
        /// cater for AC term semantics.
        /// </summary>
        public InterpreterTypeChecker(TrsProgramBlock programIn)
        {
            TypeCheckerPreprocessor preprocessor = new TypeCheckerPreprocessor();

            preprocessor.RewriteTerms(programIn);
            InitializeLookupTables(programIn);
        }
示例#2
0
        private void InitializeLookupTables(TrsProgramBlock programIn)
        {
            // Variable mappings
            foreach (var limitStatement in programIn.Statements.
                     Where(stm => stm is TrsLimitStatement).Cast <TrsLimitStatement>())
            {
                foreach (var variable in limitStatement.Variables)
                {
                    HashSet <TrsTypeDefinitionTypeName> currentTypeList = null;
                    if (!typeMappings.TryGetValue(variable, out currentTypeList))
                    {
                        typeMappings.Add(variable, currentTypeList = new HashSet <TrsTypeDefinitionTypeName>());
                    }
                    currentTypeList.Add(limitStatement.TypeDefinition);
                }
            }

            // Type definitions
            foreach (var typeStatement in programIn.Statements.
                     Where(stm => stm is TrsTypeDefinition).Cast <TrsTypeDefinition>())
            {
                foreach (var acceptedTerm in typeStatement.AcceptedTerms)
                {
                    List <TrsTypeDefinitionTypeName> typeNames = null;
                    if (!transitionFunction.TryGetValue(acceptedTerm, out typeNames))
                    {
                        transitionFunction.Add(acceptedTerm, typeNames = new List <TrsTypeDefinitionTypeName>());
                    }
                    typeNames.Add(typeStatement.Name);
                }
            }
        }
 /// <summary>
 /// Substitutes the type structure in the given program block with one equivilant to the given dictionary.
 /// </summary>
 private void SubstituteDictionaryToTypeStructure(TrsProgramBlock programIn,
                                                  Dictionary <TrsTypeDefinitionTypeName, HashSet <TrsTypeDefinitionTermBase> > typeStructureDictionary)
 {
     programIn.Statements.RemoveAll(stm => stm is TrsTypeDefinition);
     foreach (var typeDefPair in typeStructureDictionary)
     {
         programIn.Statements.Add(new TrsTypeDefinition(typeDefPair.Key, typeDefPair.Value.ToList()));
     }
 }
示例#4
0
 /// <summary>
 /// Rewrites the input type definitions to flatten the AC terms so that
 /// sub-terms with the same AC symbol can also be matched using the non-
 /// deterministic tree automaton.
 /// 
 /// Duplicate term definitions is also removed.
 /// </summary>
 public void RewriteTerms(TrsProgramBlock programIn)
 {
     var typeStructureDictionary = ConvertTypeStructureToDictionary(programIn);
       bool rewriteTookPlace = true;
       // Expand sub-AC subtypes to be consistent with ONF convewrsion for AC terms
       Stack<TypeRewritePair> rewriteStack = new Stack<TypeRewritePair>();
       while (rewriteTookPlace)
       {
     rewriteStack.Clear();
     rewriteTookPlace = false;
     // Compile list of expandable AC types, taking IEnumerable semantics into account
     CalculateChangeList(typeStructureDictionary, rewriteStack);
     // Expand the types
     rewriteTookPlace = RewriteTypeDefinitions(typeStructureDictionary, rewriteTookPlace, rewriteStack);
       }
       // Rebuild
       SubstituteDictionaryToTypeStructure(programIn, typeStructureDictionary);
 }
示例#5
0
 /// <summary>
 /// Gets a graph for validating AC Term Type definitions against cycles.
 /// </summary>
 public Dictionary<TrsTypeDefinitionTypeName, List<TrsTypeDefinitionTypeName>> GetCycleTestGraph(TrsProgramBlock programBlockIn)
 {
     var typeGraphIn = ConvertTypeStructureToDictionary(programBlockIn);
       Stack<TypeRewritePair> rewriteStack = new Stack<TypeRewritePair>();
       CalculateChangeList(typeGraphIn, rewriteStack);
       Dictionary<TrsTypeDefinitionTypeName, List<TrsTypeDefinitionTypeName>> retVal = new Dictionary<TrsTypeDefinitionTypeName, List<TrsTypeDefinitionTypeName>>();
       while (rewriteStack.Count > 0)
       {
     var current = rewriteStack.Pop();
     List<TrsTypeDefinitionTypeName> nextStates = null;
     if (!retVal.TryGetValue(current.ParentTypeName, out nextStates))
       retVal.Add(current.ParentTypeName, nextStates = new List<TrsTypeDefinitionTypeName>());
     foreach (var term in current.SubstitutionTerms)
       nextStates.AddRange(term.OnfArgumentTypes.Where(arg => arg.Term is TrsTypeDefinitionTypeName
     && typeGraphIn.ContainsKey((TrsTypeDefinitionTypeName)arg.Term)).Select(arg => arg.Term as TrsTypeDefinitionTypeName));
       }
       return retVal;
 }
        /// <summary>
        /// Rewrites the input type definitions to flatten the AC terms so that
        /// sub-terms with the same AC symbol can also be matched using the non-
        /// deterministic tree automaton.
        ///
        /// Duplicate term definitions is also removed.
        /// </summary>
        public void RewriteTerms(TrsProgramBlock programIn)
        {
            var  typeStructureDictionary = ConvertTypeStructureToDictionary(programIn);
            bool rewriteTookPlace        = true;
            // Expand sub-AC subtypes to be consistent with ONF convewrsion for AC terms
            Stack <TypeRewritePair> rewriteStack = new Stack <TypeRewritePair>();

            while (rewriteTookPlace)
            {
                rewriteStack.Clear();
                rewriteTookPlace = false;
                // Compile list of expandable AC types, taking IEnumerable semantics into account
                CalculateChangeList(typeStructureDictionary, rewriteStack);
                // Expand the types
                rewriteTookPlace = RewriteTypeDefinitions(typeStructureDictionary, rewriteTookPlace, rewriteStack);
            }
            // Rebuild
            SubstituteDictionaryToTypeStructure(programIn, typeStructureDictionary);
        }
示例#7
0
        /// <summary>
        /// Creates a new interpreter
        /// </summary>
        public Interpreter(TrsProgramBlock programBlock, List <ITrsNativeFunction> nativeFunctions = null,
                           List <ITrsUnifierCalculation> customUnifiersCalculations = null)
        {
            if (nativeFunctions == null)
            {
                nativeFunctions = new List <ITrsNativeFunction>();
            }
            else
            {
                this.nativeFunctions = nativeFunctions;
            }
            mguCalculation      = new MguCalculation(customUnifiersCalculations);
            this.initialProgram = programBlock;
            var validator = new TrsProgramBlockValidator();

            validator.Validate(this.initialProgram);
            ValidationMessages = new List <InterpreterResultMessage>();
            ValidationMessages.AddRange(validator.ValidationMessages);
            this.hasErrors = ValidationMessages.Where(msg => msg.MessageType == InterpreterMessageType.Error).Count() > 0;
            if (!hasErrors)
            {
                ClassifyInput();
            }
        }
        /// <summary>
        /// Gets a graph for validating AC Term Type definitions against cycles.
        /// </summary>
        public Dictionary <TrsTypeDefinitionTypeName, List <TrsTypeDefinitionTypeName> > GetCycleTestGraph(TrsProgramBlock programBlockIn)
        {
            var typeGraphIn = ConvertTypeStructureToDictionary(programBlockIn);
            Stack <TypeRewritePair> rewriteStack = new Stack <TypeRewritePair>();

            CalculateChangeList(typeGraphIn, rewriteStack);
            Dictionary <TrsTypeDefinitionTypeName, List <TrsTypeDefinitionTypeName> > retVal = new Dictionary <TrsTypeDefinitionTypeName, List <TrsTypeDefinitionTypeName> >();

            while (rewriteStack.Count > 0)
            {
                var current = rewriteStack.Pop();
                List <TrsTypeDefinitionTypeName> nextStates = null;
                if (!retVal.TryGetValue(current.ParentTypeName, out nextStates))
                {
                    retVal.Add(current.ParentTypeName, nextStates = new List <TrsTypeDefinitionTypeName>());
                }
                foreach (var term in current.SubstitutionTerms)
                {
                    nextStates.AddRange(term.OnfArgumentTypes.Where(arg => arg.Term is TrsTypeDefinitionTypeName &&
                                                                    typeGraphIn.ContainsKey((TrsTypeDefinitionTypeName)arg.Term)).Select(arg => arg.Term as TrsTypeDefinitionTypeName));
                }
            }
            return(retVal);
        }
        /// <summary>
        /// Gets the type system as a lookup table, in the process removing duplicates
        /// and merging types with the same name
        /// </summary>
        private Dictionary <TrsTypeDefinitionTypeName, HashSet <TrsTypeDefinitionTermBase> > ConvertTypeStructureToDictionary(TrsProgramBlock programIn)
        {
            var retVal = new Dictionary <TrsTypeDefinitionTypeName, HashSet <TrsTypeDefinitionTermBase> >();

            foreach (TrsTypeDefinition typeDef in programIn.Statements.Where(stm => stm is TrsTypeDefinition))
            {
                HashSet <TrsTypeDefinitionTermBase> vOut = null;
                if (!retVal.TryGetValue(typeDef.Name, out vOut))
                {
                    retVal.Add(typeDef.Name, vOut = new HashSet <TrsTypeDefinitionTermBase>());
                }
                foreach (var subType in typeDef.AcceptedTerms)
                {
                    vOut.Add(subType);
                }
            }
            return(retVal);
        }
示例#10
0
   /// <summary>
   /// Creates a new interpreter
   /// </summary>
   public Interpreter(TrsProgramBlock programBlock, List<ITrsNativeFunction> nativeFunctions = null,
 List<ITrsUnifierCalculation> customUnifiersCalculations = null)
   {
       if (nativeFunctions == null) nativeFunctions = new List<ITrsNativeFunction>();
         else this.nativeFunctions = nativeFunctions;
         mguCalculation = new MguCalculation(customUnifiersCalculations);
         this.initialProgram = programBlock;
         var validator = new TrsProgramBlockValidator();
         validator.Validate(this.initialProgram);
         ValidationMessages = new List<InterpreterResultMessage>();
         ValidationMessages.AddRange(validator.ValidationMessages);
         this.hasErrors = ValidationMessages.Where(msg => msg.MessageType == InterpreterMessageType.Error).Count() > 0;
         if (!hasErrors) ClassifyInput();
   }
示例#11
0
 /// <summary>
 /// Note: input program's type definitions wiill be pre-processed to 
 /// cater for AC term semantics.
 /// </summary>
 public InterpreterTypeChecker(TrsProgramBlock programIn)
 {
     TypeCheckerPreprocessor preprocessor = new TypeCheckerPreprocessor();
       preprocessor.RewriteTerms(programIn);
       InitializeLookupTables(programIn);
 }
示例#12
0
   /// <summary>
   /// Substitutes the type structure in the given program block with one equivilant to the given dictionary.
   /// </summary>
   private void SubstituteDictionaryToTypeStructure(TrsProgramBlock programIn,
 Dictionary<TrsTypeDefinitionTypeName, HashSet<TrsTypeDefinitionTermBase>> typeStructureDictionary)
   {
       programIn.Statements.RemoveAll(stm => stm is TrsTypeDefinition);
         foreach (var typeDefPair in typeStructureDictionary)
       programIn.Statements.Add(new TrsTypeDefinition(typeDefPair.Key, typeDefPair.Value.ToList()));
   }
示例#13
0
 /// <summary>
 /// Gets the type system as a lookup table, in the process removing duplicates
 /// and merging types with the same name
 /// </summary>
 private Dictionary<TrsTypeDefinitionTypeName, HashSet<TrsTypeDefinitionTermBase>> ConvertTypeStructureToDictionary(TrsProgramBlock programIn)
 {
     var retVal = new Dictionary<TrsTypeDefinitionTypeName, HashSet<TrsTypeDefinitionTermBase>>();
       foreach (TrsTypeDefinition typeDef in programIn.Statements.Where(stm => stm is TrsTypeDefinition))
       {
     HashSet<TrsTypeDefinitionTermBase> vOut = null;
     if (!retVal.TryGetValue(typeDef.Name, out vOut))
       retVal.Add(typeDef.Name, vOut = new HashSet<TrsTypeDefinitionTermBase>());
     foreach (var subType in typeDef.AcceptedTerms) vOut.Add(subType);
       }
       return retVal;
 }
示例#14
0
 public static TrsProgramBlock Convert(this AstProgramBlock astIn)
 {
     var trsProgramBlock = new TrsProgramBlock(astIn);
       foreach (var statement in astIn.Statements)
       {
     if (statement is AstTermBase) trsProgramBlock.Statements.Add(ConvertAstTermBase((AstTermBase)statement));
     else if (statement is AstReductionRule) trsProgramBlock.Statements.Add(((AstReductionRule)statement).Convert());
     else if (statement is AstTypeDefinitionStatement) trsProgramBlock.Statements.Add(((AstTypeDefinitionStatement)statement).Convert());
     else if (statement is AstLimitStatement) trsProgramBlock.Statements.Add(((AstLimitStatement)statement).Convert());
     else throw new ArgumentException("Enexpected AST type: " + statement.GetType().FullName);
       }
       return trsProgramBlock;
 }
示例#15
0
 public InterpreterResult(TrsProgramBlock programIn)
 {
     Messages   = new List <InterpreterResultMessage>();
     ProgramIn  = programIn;
     ProgramOut = new TrsProgramBlock();
 }
示例#16
0
 public InterpreterResult(TrsProgramBlock programIn)
 {
     Messages = new List<InterpreterResultMessage>();
       ProgramIn = programIn;
       ProgramOut = new TrsProgramBlock();
 }
示例#17
0
        private void InitializeLookupTables(TrsProgramBlock programIn)
        {
            // Variable mappings
              foreach (var limitStatement in programIn.Statements.
            Where(stm => stm is TrsLimitStatement).Cast<TrsLimitStatement>())
              {
            foreach (var variable in limitStatement.Variables)
            {
              HashSet<TrsTypeDefinitionTypeName> currentTypeList = null;
              if (!typeMappings.TryGetValue(variable, out currentTypeList))
            typeMappings.Add(variable, currentTypeList = new HashSet<TrsTypeDefinitionTypeName>());
              currentTypeList.Add(limitStatement.TypeDefinition);
            }
              }

              // Type definitions
              foreach (var typeStatement in programIn.Statements.
            Where(stm => stm is TrsTypeDefinition).Cast<TrsTypeDefinition>())
              {
            foreach (var acceptedTerm in typeStatement.AcceptedTerms)
            {
              List<TrsTypeDefinitionTypeName> typeNames = null;
              if (!transitionFunction.TryGetValue(acceptedTerm, out typeNames))
            transitionFunction.Add(acceptedTerm, typeNames = new List<TrsTypeDefinitionTypeName>());
              typeNames.Add(typeStatement.Name);
            }
              }
        }