Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue on fail-fast assumption search #21

Merged
merged 1 commit into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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