2017-03-08 64 views
0

類型Prop模型命題句子(或者更簡潔的命題)。命題符號(變量)被編碼爲形式P(n)的項,其中n是整數標識符。 Interpr類型將命題解釋實現爲命題符號ID(無重複)的Scala列表。當且僅當n出現在i中時,命題符號P(n)在解釋中才是真實的。斯卡拉的命題邏輯

編寫一個Scala方法,意思是(i:Interpr,p:Prop):布爾,接受解釋i和命題p,並返回斯卡拉布爾值,如果解釋根據命題邏輯的語義滿足命題,否則返回false值。

我很困惑,我可以如何使用模式匹配和遞歸來實現這一點。任何指導將不勝感激!

// Example case 
meaning(List(1,2), And(P(1), P(2))) is true 

// Example case 
meaning(List(1), And(P(1), P(2))) is false 




type PVar = Int 

type Interpr = List[PVar] 

sealed abstract class Prop 
case object True extends Prop 
case object False extends Prop 
case class P(id: PVar) extends Prop     // propositional   variable 
case class Not(sub: Prop) extends Prop 
case class Or(sub1: Prop, sub2: Prop) extends Prop // inclusive or 
case class Xor(sub1: Prop, sub2: Prop) extends Prop // exclusive or 
case class And(sub1: Prop, sub2: Prop) extends Prop 
case class Impl(sub1: Prop, sub2: Prop) extends Prop // simple implication => 
case class Iff(sub1: Prop, sub2: Prop) extends Prop // double implication <=> 


sealed abstract class Answer 
case object Yes extends Answer 
case object No extends Answer 

def meaning(i: Interpr, p: Prop): Boolean = p match { 
    // solution here 
} 
+0

「//解決方案」堆棧溢出,新的做你的作業服務 – pedrofurla

+0

來吧,告訴我們你已經嘗試了什麼或者你在想什麼解決方案。這是一項任務,你應該從中學習。提交其他人的解決方案有什麼意義? –

回答

1

我很困惑你到底需要什麼。該方法的含義是否應該能夠評估複合命題? List(1,2)究竟如何評估爲真?

然而對於您的使用遞歸的評價,我建議你看看菲利普·沃德勒的論文單子函數式編程問題

http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf

這是一個最好的例子編寫表達式求值在功能語言中。

+0

高級翻譯:)我不反對你的答案,如果問題真的是關於如何做表達式評估,那應該是一個很好的答案。儘管如此,它將完全在Scala初學者的頭頂。所以upvoted。 –

+0

「List(1,2)如何評估爲真」。不是。這是第二個被評估爲真的參數 - 它是關於「解釋」的一個陳述 - 也就是列表。 –

+0

答覆並不意味着是一個巨魔。但是一個方向的指針。 – Utsav