Skip to content

Commit

Permalink
Fix issue on fail-fast assumption search
Browse files Browse the repository at this point in the history
  • Loading branch information
melvic-ybanez committed Mar 26, 2022
1 parent 54e5ed7 commit 4f00958
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/main/scala/com/melvic/chi/Evaluate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,14 @@ import com.melvic.chi.ast.Proposition._
import com.melvic.chi.ast.{Definition, Proof, Proposition, Signature}
import com.melvic.chi.env.Environment
import com.melvic.chi.env.Environment.Environment
import com.melvic.chi.env.implicits._

object Evaluate {
//noinspection SpellCheckingInspection
def proposition(proposition: Proposition)(implicit env: Environment): Result[Proof] =
proposition match {
case PUnit => Result.success(TUnit)
case atom: Atom =>
Environment
.findAtom(atom)
.toRight(Fault.cannotProve(atom))
.flatMap {
case variable @ Variable(name, `atom`) => Result.success(variable)
case variable @ Variable(f, Implication(antecedent, _)) =>
Evaluate.proposition(antecedent)(env.filterNot(_ == variable)).map {
case TUnit => Application(f, Nil)
case Proof.Conjunction(terms) => Application(f, terms)
case param => Application(f, List(param))
}
}
case atom: Atom => applyAssumptionRule(atom)
case Conjunction(components) => conjunctionIntroduction(components)
case Disjunction(left, right) =>
Evaluate
Expand Down Expand Up @@ -74,4 +63,22 @@ object Evaluate {
val evaluatedComponents = recurse(Nil, components)
evaluatedComponents.map(Proof.Conjunction)
}

def applyAssumptionRule(atom: Atom)(implicit env: Environment): Result[Proof] =
Environment
.findAtom(atom)(env)
.toRight(Fault.cannotProve(atom))
.flatMap {
case variable @ Variable(name, `atom`) => Result.success(variable)
case variable @ Variable(f, Implication(antecedent, _)) =>
val newEnv: Environment = env.discharge(variable)
Evaluate
.proposition(antecedent)(newEnv)
.map {
case TUnit => Application(f, Nil)
case Proof.Conjunction(terms) => Application(f, terms)
case param => Application(f, List(param))
}
.orElse(applyAssumptionRule(atom)(newEnv))
}
}
3 changes: 3 additions & 0 deletions src/main/scala/com/melvic/chi/env/Environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ object Environment {
case _ => false
}

def discharge(env: Environment, proof: Proof): Environment =
env.filterNot(_ == proof)

/**
* Assigns a variable to the proposition and registers it into the environment
*/
Expand Down
11 changes: 11 additions & 0 deletions src/main/scala/com/melvic/chi/env/implicits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.melvic.chi.env

import com.melvic.chi.ast.Proof
import com.melvic.chi.env.Environment.Environment

object implicits {
implicit class EnvironmentOps(env: Environment) {
def discharge(proof: Proof): Environment =
Environment.discharge(env, proof)
}
}
12 changes: 12 additions & 0 deletions src/test/scala/com/melvic/chi/tests/ScalaDefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ class ScalaDefSpec extends AnyFlatSpec with should.Matchers {
)
}

"all assumptions" should "be considered" in {
generateAndShow("def foo[A, B, C]: (A => C) => (B => C) => B => C") should be(
"""def foo[A, B, C]: ((A => C) => ((B => C) => (B => C))) =
| f => g => b => g(b)""".stripMargin
)

generateAndShow("def foo[A, B, C]: (B => C) => (A => C) => B => C") should be(
"""def foo[A, B, C]: ((B => C) => ((A => C) => (B => C))) =
| f => g => b => f(b)""".stripMargin
)
}

"Unknown propositions" should "not be allowed" in {
generateAndShow("def foo[A]: A => B") should be(
"Unknown propositions: B"
Expand Down

0 comments on commit 4f00958

Please sign in to comment.