Пример #1
0
        public IEnumerable <Solution> Resolve(Statement st, Solution solution)
        {
            List <Expression> call_arguments = null;
            IVariable         lv             = null;
            DatatypeDecl      datatype       = null;

            InitArgs(st, out lv, out call_arguments);
            Contract.Assert(tcce.OfSize(call_arguments, 1), Error.MkErr(st, 0, 1, call_arguments.Count));

            string argType = GetArgumentType(call_arguments[0] as NameSegment);

            foreach (var result in ResolveExpression(call_arguments[0]))
            {
                if (argType == "Element")
                {
                    var       ns           = result as NameSegment;
                    IVariable originalDecl = StaticContext.GetGlobalVariable(ns.Name);
                    Contract.Assert(originalDecl != null, Error.MkErr(st, 9, ((NameSegment)call_arguments[0]).Name));
                    var datatypeName = originalDecl.Type.ToString();
                    datatype = StaticContext.GetGlobal(datatypeName);
                    var lit = IsInductive(datatypeName, datatype);
                    yield return(AddNewLocal(lv, lit));
                }
                else
                {
                    var ctor = result as DatatypeCtor;
                    Contract.Assert(ctor != null, Error.MkErr(st, 1, "Datatype constructor"));
                    var datatypeName = ctor.EnclosingDatatype.Name;

                    LiteralExpr lit = new LiteralExpr(st.Tok, false);

                    foreach (var formal in ctor.Formals)
                    {
                        if (formal.Type.ToString() == datatypeName)
                        {
                            lit = new LiteralExpr(st.Tok, true);
                            break;
                        }
                    }
                    yield return(AddNewLocal(lv, lit));
                }
            }
        }
Пример #2
0
        private IEnumerable <Solution> GenerateMatch(TacnyCasesBlockStmt st)
        {
            NameSegment     casesGuard;
            UserDefinedType datatypeType = null;
            var             isElement    = false;

            var guard = st.Guard as ParensExpression;

            if (guard == null)
            {
                casesGuard = st.Guard as NameSegment;
            }
            else
            {
                casesGuard = guard.E as NameSegment;
            }

            Contract.Assert(casesGuard != null, Error.MkErr(st, 2));

            IVariable tacInput = GetLocalKeyByName(casesGuard);

            Contract.Assert(tacInput != null, Error.MkErr(st, 9, casesGuard.Name));


            if (!(tacInput is Formal))
            {
                tacInput = GetLocalValueByName(casesGuard) as IVariable;
                Contract.Assert(tacInput != null, Error.MkErr(st, 9, casesGuard.Name));
                // the original
                if (tacInput != null)
                {
                    casesGuard = new NameSegment(tacInput.Tok, tacInput.Name, null);
                }
            }
            else
            {
                // get the original declaration inside the method
                casesGuard = GetLocalValueByName(tacInput) as NameSegment;
            }
            string datatypeName = tacInput?.Type.ToString();

            /**
             * TODO cleanup
             * if datatype is Element lookup the formal in global variable registry
             */

            if (datatypeName == "Element")
            {
                isElement = true;
                var val  = GetLocalValueByName(tacInput.Name);
                var decl = val as NameSegment;
                Contract.Assert(decl != null, Error.MkErr(st, 9, tacInput.Name));

                var originalDecl = StaticContext.GetGlobalVariable(decl?.Name);
                if (originalDecl != null)
                {
                    datatypeType = originalDecl.Type as UserDefinedType;
                    datatypeName = datatypeType != null ? datatypeType.Name : originalDecl.Type.ToString();
                }
                else
                {
                    Contract.Assert(false, Error.MkErr(st, 9, tacInput.Name));
                }
            }

            if (!StaticContext.ContainsGlobalKey(datatypeName))
            {
                Contract.Assert(false, Error.MkErr(st, 12, datatypeName));
            }

            var datatype = StaticContext.GetGlobal(datatypeName);

            if (datatype.TypeArgs != null)
            {
                _ctorTypes = new Dictionary <string, Type>();

                if (datatype.TypeArgs.Count == datatypeType.TypeArgs.Count)
                {
                    for (int i = 0; i < datatype.TypeArgs.Count; i++)
                    {
                        var genericType = datatype.TypeArgs[i];
                        var definedType = datatypeType.TypeArgs[i];
                        _ctorTypes.Add(genericType.Name, definedType);
                    }
                }
            }

            if (isElement)
            {
                yield return(GenerateVerifiedStmt(datatype, casesGuard, st));
            }
            else
            {
                foreach (var item in GenerateStmt(datatype, casesGuard, st))
                {
                    yield return(item);
                }
            }
        }