示例#1
0
文件: App.cs 项目: hwacha/SemInC
    public App(LogicalForm f, LogicalForm x) : base(null)
    {
        ISemanticType fType = f.GetSemanticType();
        ISemanticType xType = x.GetSemanticType();

        if (fType.GetType() != typeof(Arrow))
        {
            System.Console.WriteLine(
                "App failed: function argument not function type");
            return;
        }
        Arrow aType = (Arrow)fType;

        if (!aType.GetInputType().Equals(xType))
        {
            // error
            System.Console.WriteLine("App failed: type mismatch");
            // throw new InvalidTypeException();
            return;
        }

        this.type = aType.GetOutputType();

        this.isFormula = this.type.GetType() == typeof(T);

        this.freeVariables = f.MergeVariables(x);

        this.f = f;
        this.x = x;
    }
示例#2
0
文件: FType.cs 项目: hwacha/SemInC
    public FType(ISemanticType basetype, LogicalForm formula)
    {
        if (!formula.IsFormula() || formula.IsClosed())
        {
            System.Console.WriteLine("FType failed: not a formula, or not free");
            return;
            // error
        }
        HashSet <Variable> .Enumerator vars = formula.GetFreeVariables().GetEnumerator();
        vars.MoveNext();
        Variable first = vars.Current;

        if (vars.MoveNext())
        {
            System.Console.WriteLine("FType failed: more than one free variable");
            return;
            // error (more than one free variable)
        }

        if (!first.GetSemanticType().Equals(basetype))
        {
            System.Console.WriteLine(first.GetSemanticType());
            System.Console.WriteLine(basetype);

            System.Console.WriteLine("FType failed: types aren't equal");
            return;
            // error
        }
        this.basetype = basetype;
        this.formula  = formula;
    }
示例#3
0
    static void TestLambda()
    {
        System.Console.WriteLine("=====================");
        System.Console.WriteLine("Testing Lambda:");
        Lambda l = new Lambda(
            new Variable(new E(), 0),
            new App(
                new Variable(new Arrow(new E(), new T()), 8),
                new Variable(new E(), 0)));

        System.Console.WriteLine(l);

        LogicalForm noChange = l.Bind(0, new Constant(new E(), 40));

        System.Console.WriteLine(noChange);

        LogicalForm variableReplace =
            l.Bind(8, new Constant(new Arrow(new E(), new T()), 60));

        System.Console.WriteLine(variableReplace);

        System.Console.WriteLine(l.Apply(new Constant(new E(), 40)));

        System.Console.WriteLine("=====================");
    }
示例#4
0
    public Variable(ISemanticType type, int id) : base(type)
    {
        this.id        = id;
        this.isFormula = LogicalForm.IsFormulaType(type);

        this.freeVariables = new HashSet <Variable>();
        this.freeVariables.Add(this);
    }
示例#5
0
 public override LogicalForm Bind(int id, LogicalForm lf)
 {
     if (id == v.GetID())
     {
         return(this);
     }
     return(new Lambda(v, l.Bind(id, lf)));
 }
示例#6
0
    public Lambda(Variable v, LogicalForm l)
        : base(new Arrow(v.GetSemanticType(), l.GetSemanticType()))
    {
        this.v = v;
        this.l = l;

        this.freeVariables = l.CloneVariables();
        freeVariables.Remove(v);
    }
示例#7
0
文件: Model.cs 项目: hwacha/SemInC
    // Updates the Model (M) such that
    // l has the truth value v in M
    private UpdateInfo Make(LogicalForm l, TruthValue.T v)
    {
        if (l.IsClosed() && l.IsFormula())
        {
            return(UpdateInfo.NoChange);
        }

        return(l.Make(this, v));
    }
示例#8
0
 public ToDesirability(LogicalForm sentence) : base(new W())
 {
     if (sentence.GetType() != typeof(TWG))
     {
         // error!
     }
     this.sentence      = sentence;
     this.isFormula     = true;
     this.freeVariables = sentence.GetFreeVariables();
 }
示例#9
0
文件: Not.cs 项目: hwacha/SemInC
 public Not(LogicalForm sub) : base(sub.GetSemanticType())
 {
     if (!sub.IsFormula())
     {
         // error!
         return;
     }
     this.sub           = sub;
     this.isFormula     = true;
     this.freeVariables = sub.GetFreeVariables();
 }
示例#10
0
 public override LogicalForm Bind(int id, LogicalForm l)
 {
     if (this.id == id && l.GetSemanticType().Equals(this.GetSemanticType()))
     {
         return(l);
     }
     else
     {
         return(this);
     }
 }
示例#11
0
    public override LogicalForm Bind(int id, LogicalForm l)
    {
        HashSet <LogicalForm> newSubs = new HashSet <LogicalForm>();

        foreach (LogicalForm sub in this.subs)
        {
            newSubs.Add(sub.Bind(id, l));
        }

        return(new Or(newSubs));
    }
示例#12
0
文件: Model.cs 项目: hwacha/SemInC
    // Super compatible
    private HashSet <int> GetDomain(ISemanticType t)
    {
        Model currentModel = super;

        if (t.GetType() == typeof(FType))
        {
            FType       fType   = (FType)t;
            LogicalForm formula = fType.GetFormula();
            int         varID   = formula.GetFreeVariables().Single <Variable>().GetID();

            ISemanticType baseType = fType.GetBaseType();

            // populating the set with all semantic values
            // (including those in super models)
            // possible error: basetype is undefined in M
            Dictionary <int, ISemanticValue> .KeyCollection baseSet = model[baseType].Keys;

            while (currentModel != null)
            {
                // possible error: basetype is undefined in M
                baseSet.Union <int>(currentModel.model[baseType].Keys);
            }

            HashSet <int> finalSet = new HashSet <int>();

            foreach (int i in baseSet)
            {
                if (Satisfies(formula.Bind(varID, new Constant(baseType, i))))
                {
                    finalSet.Add(i);
                }
            }
            return(finalSet);
        }

        HashSet <int> theSet = new HashSet <int>();

        foreach (int i in model[t].Keys)
        {
            theSet.Add(i);
        }

        while (currentModel != null)
        {
            // possible error: t is undefined in M
            theSet.UnionWith(currentModel.model[t].Keys);
        }

        return(theSet);
    }
示例#13
0
文件: And.cs 项目: hwacha/SemInC
    public And NegateAndReplaceWith(LogicalForm from, LogicalForm to)
    {
        HashSet <LogicalForm> newSubs = new HashSet <LogicalForm>();

        foreach (LogicalForm sub in this.subs)
        {
            if (!sub.Equals(from))
            {
                newSubs.Add(sub.Negate());
            }
        }
        newSubs.Add(to);
        return(new And(newSubs));
    }
示例#14
0
文件: Model.cs 项目: hwacha/SemInC
    // makes the inference prescribed by r in this model
    // returns true if there was a change to the model,
    // false otherwise
    public bool MakeInference(Rule r)
    {
        if (!r.IsClosed())
        {
            return(false);
        }

        LogicalForm inference = r.GetInference(this);

        if (inference != null)
        {
            sentenceRules.Remove(r);

            if (!activeRules.ContainsKey(inference))
            {
                activeRules[inference] =
                    Tuple.Create(new HashSet <Rule>(),
                                 new HashSet <Rule>());
            }

            activeRules[inference].Item2.Add(r);

            UpdateInfo info = MakeTrue(inference);

            // what was inferred was already true. Boring!
            if (info == UpdateInfo.NoChange)
            {
                return(false);
            }

            // we've inferred something which turned out
            // to be inconsistent. We need to resolve it!
            if (info == UpdateInfo.Warning)
            {
                ResolveInconsistency(inference);
            }

            // we've either resolved an inconsistency or
            // inferred something without a hitch, so the
            // model has changed.
            return(true);
        }

        return(false);
    }
示例#15
0
		public static String GetFormName(LogicalForm paramInt)
		{
			switch (paramInt)
			{
				case LogicalForm.LogicUnclassified:
					return "Not any Normal Form";
				case LogicalForm.LogicNnf:
					return "Negation Normal Form";
				case LogicalForm.LogicCNF:
					return "Conjunctive Normal Form (CNF)";
				case LogicalForm.LogicDNF:
					return "Disjunctive Normal Form (DNF)";
				case LogicalForm.LogicCNF | LogicalForm.LogicDNF:
					return "Conjunctive and Disjunctive Normal Form (CNF & DNF)";
			}

			return "Invalid Form passed";
		}
示例#16
0
    public ModelBind(LogicalForm sentence, LogicalForm model)
        : base(new TWG())
    {
        if (!sentence.IsFormula())
        {
            // error!
        }
        if (model.GetType() != typeof(Model))
        {
            // error!
        }
        this.sentence = sentence;

        this.model = model;

        this.isFormula     = false;
        this.freeVariables = sentence.MergeVariables(model);
    }
示例#17
0
文件: Model.cs 项目: hwacha/SemInC
    // if there is an inconsistency with l and -l,
    // then we need to decide between them.
    // l is the new thing.
    private Policy ResolveInconsistency(LogicalForm l)
    {
        LogicalForm notL = l.Negate();

        // okay. new plan:
        // 1. have two helper methods:
        //    BestRemoval() and BestInsertion()
        // 2. recursively call BestRemoval() and
        //    BestInsertion() and conjoin sentences
        //    along best removal path
        // 3. (when there's a negation, check to see)
        //    whether, in the current rule, there's A
        //    on top or ~A on bottom
        // 4. problem for BestInsertion(): have to
        //    make copy of model to make hypothetical
        //    inferences
        // 5. ResolveInconsistency calls BestRemoval()
        //    on P and ~P, and then removes whichever
        //    wins out.
        // 6. Question simply becomes what to yield
        //    as output for BestInsertion() and BestRemoval()

        Policy bestL    = BestRemoval(l);
        Policy bestNotL = BestRemoval(notL);

        int comparison = CompareLikelihood(BestRemoval(l), BestRemoval(notL));

        if (comparison > 0)
        {
            // TODO perform the removal strategy encoded by bestL
            return(bestL);
        }

        if (comparison < 0)
        {
            // TODO perform the removal strategy encoded by bestNotL
            return(bestNotL);
        }

        // TODO pick arbitrary one???
        // rn we're conservative - we pick the older one
        return(bestNotL);
    }
示例#18
0
    public Rule Bind(int id, LogicalForm replace)
    {
        HashSet <LogicalForm> newTop = new HashSet <LogicalForm>();

        HashSet <LogicalForm>[] newBot = new HashSet <LogicalForm> [bot.Length];

        foreach (LogicalForm l in top)
        {
            newTop.Add(l.Bind(id, replace));
        }
        for (int i = 0; i < bot.Length; i++)
        {
            foreach (LogicalForm l in bot[i])
            {
                newBot[i].Add(l.Bind(id, replace));
            }
        }

        return(new Rule(newTop, newBot));
    }
示例#19
0
文件: Model.cs 项目: hwacha/SemInC
 // super compatible
 public bool Satisfies(LogicalForm l)
 {
     if (l.IsClosed() && l.IsFormula())
     {
         ISemanticValue s = l.Denotation(this);
         if (s.GetType() == typeof(TruthValue))
         {
             TruthValue t = (TruthValue)s;
             if (t.IsUnknown() && (super != null))
             {
                 return(super.Satisfies(l));
             }
             return(t.IsTrue());
         }
         if (super != null)
         {
             return(super.Satisfies(l));
         }
         return(false);
     }
     return(false);
 }
示例#20
0
    public HashSet <Variable> MergeVariables(LogicalForm l)
    {
        HashSet <Variable> newVs = new HashSet <Variable>();

        if (freeVariables != null)
        {
            foreach (Variable v in freeVariables)
            {
                newVs.Add(v);
            }
        }

        if (l.GetFreeVariables() != null)
        {
            foreach (Variable v in l.GetFreeVariables())
            {
                newVs.Add(v);
            }
        }

        return(newVs);
    }
示例#21
0
 public abstract LogicalForm Bind(int id, LogicalForm l);
示例#22
0
 public LogicalForm Apply(LogicalForm x)
 {
     return(l.Bind(v.GetID(), x));
 }
示例#23
0
 public ToTruth(LogicalForm sentence) : base(new T())
 {
     this.sentence      = sentence;
     this.isFormula     = true;
     this.freeVariables = sentence.GetFreeVariables();
 }
示例#24
0
文件: Constant.cs 项目: hwacha/SemInC
 public Constant(ISemanticType type, int id) : base(type)
 {
     this.type      = type;
     this.id        = id;
     this.isFormula = LogicalForm.IsFormulaType(type);
 }
示例#25
0
文件: Constant.cs 项目: hwacha/SemInC
 public override LogicalForm Bind(int id, LogicalForm l)
 {
     return(this);
 }
示例#26
0
文件: App.cs 项目: hwacha/SemInC
 public override LogicalForm Bind(int id, LogicalForm l)
 {
     return(new App(f.Bind(id, l), x.Bind(id, l)));
 }
示例#27
0
文件: Model.cs 项目: hwacha/SemInC
    private Policy BestRemoval(LogicalForm l)
    {
        // first, we find what supports l.
        if (!activeRules.ContainsKey(l))
        {
            // this means l was not inferred.
            // What should we do here?
            return(new Remove(l));
        }

        foreach (Rule r in activeRules[l].Item2)
        {
            HashSet <LogicalForm>   top = r.GetTop();
            HashSet <LogicalForm>[] bot = r.GetBottom();

            // all the rules where l is on bottom
            // we can either side-step or remove a top one
            Policy bestRemoval = null;
            Policy bestShift   = null;

            if (top.Count > 0)
            {
                bestRemoval = BestRemoval(top.First());

                // determine best top to remove
                foreach (LogicalForm parent in top)
                {
                    Policy contender = BestRemoval(parent);
                    if (CompareLikelihood(bestRemoval, contender) < 0)
                    {
                        bestRemoval = contender;
                    }
                }
            }

            // THIS IS ALL THE SIDE-STEP STUFF
            for (int i = 0; i < bot.Length; i++)
            {
                HashSet <LogicalForm> currentTier = bot[i];

                foreach (LogicalForm sibling in currentTier)
                {
                    if (!Satisfies(sibling.Negate()))
                    {
                        if (bestShift == null)
                        {
                            bestShift = BestAddition(sibling);
                        }
                        else
                        {
                            int comparitor = CompareLikelihood(bestShift, BestAddition(sibling));
                            if (comparitor < 0)
                            {
                                bestShift = BestAddition(sibling);
                            }
                        }
                    }
                }

                if (bestShift != null)
                {
                    break;
                }
            }

            // bestRemoval = bestRemoval.Concat(new Remove(l));

            bestShift = bestRemoval.GetDual().Concat(bestShift);

            Policy winner = (CompareLikelihood(bestRemoval, bestShift) > 0) ? bestRemoval : bestShift;

            return(winner.Concat(new Remove(l)));
        }

        return(null);
        // TODO collect best results from different rules
    }
示例#28
0
文件: Model.cs 项目: hwacha/SemInC
 private Policy BestAddition(LogicalForm l)
 {
     // TODO make this involve inferences too
     return(new Add(l));
 }
示例#29
0
 public override LogicalForm Bind(int id, LogicalForm l)
 {
     return(new ToTruth(sentence.Bind(id, l)));
 }
示例#30
0
 public Add(LogicalForm l)
 {
     this.l = l;
 }
示例#31
0
文件: Model.cs 项目: hwacha/SemInC
 // Makes s true in M
 public UpdateInfo MakeTrue(LogicalForm s)
 {
     return(Make(s, TruthValue.T.True));
 }