
package uk.ac.man.cs.lethe.internal.fol.scan

import uk.ac.man.cs.lethe.internal.fol.datatypes._

object Fol2ScanConverter { 

  def convert(formula: Expression): String = { 
    def inner(formula: Expression): String = formula match { 
      case f: Formula if f.tautology => "true" 
      case f: Formula if f.contradiction => "false"
      case Variable(v) => v
      case Constant(c) => c
      case Function(f, args) => f + args.map(inner).mkString("(", ", ", ")")
      case SimpleProposition(name) => name
      case IdentityPredicate(a1, a2) => a1 + " = " + a2
      case GenericPredicate(p, args) => p + args.map(inner).mkString("(", ", ", ")")
      case Literal(true, predicate) => inner(predicate)
      case Literal(false, predicate) => "-" + inner(predicate)
      case Negation(formula) => "-" + inner(formula)
      case Disjunction(formulas) => formulas.map(inner).mkString("(", " | ", ")")
      case Conjunction(formulas) => formulas.map(inner).mkString("(", " & ", ")")
      case Implication(formula1, formula2) => "(" + inner(formula1) + " -> " + inner(formula2) + ")"
      case Equivalence(formula1, formula2) => "(" + inner(formula1) + " <-> " + inner(formula2) + ")"
      case ExistentialQuantification(Variable(v), formula) => "(exists " + v + " (" + inner(formula) + "))"
      case UniversalQuantification(Variable(v), formula) => "(all " + v + " (" + inner(formula) + "))"
    } 

    inner(formula).replaceAllLiterally("'", "_") + "."
  }
}
