<commit_before>package scalapplcodefest.example

import scalapplcodefest.compiler.{WolfeCompilerPlugin2, WolfeTransformer, StringCompiler}
import scala.language.implicitConversions
import scala.tools.nsc.Global
import scala.reflect.internal.Symbols

/**
 * @author <NAME>
 */
object MLECompileExample {

  val source =
    """
      |package scalapplcodefest.example
      |
      |import math.log
      |import scalapplcodefest._
      |import scala.language.reflectiveCalls
      |import scala.language.implicitConversions
      |
      |/**
      | * @author <NAME>
      | */
      |object MLEExampleWithLinearModel extends App {
      |
      |  import Wolfe._
      |
      |  type Coin = Symbol
      |
      |  //elements of the domain
      |  val coins = Set('H, 'T)
      |
      |  //training data
      |  val data = Seq('H, 'T, 'T, 'T)
      |
      |  @Objective.Categorical
      |  def s(params: Vector)(coin: Coin) = ft(coin) dot params
      |
      |  @Objective.JointLoglikelihood
      |  def ll(data: Seq[Coin])(w: Vector) = sum(data) {c => logZ(coins)(s(w)) - s(w)(c)}
      |
      |  //a subset of possible weights to make brute force search tractable
      |  val myReals = Set(0.0, 0.25, 0.75, 1.0).map(math.exp)
      |
      |  //val w = argmin(vectors)(ll(data)) //this won't run without compilation
      |  val w = argmin(vectors(coins, myReals)) {ll(data)} //this should run w/o compilation
      |
      |  //this is how the compiled expression should look like
      |  val compiled = (data map (ft(_))).sum.mapValues(w => log(w / data.size))
      |
      |  println(compiled)
      |  println(w)
      |
      |  def fun = {
      |     val x = 5
      |     x
      |  }
      |
      |}
      |    """.stripMargin

  def main(args: Array[String]) {
    val path = dirPathOfClass(getClass.getName)
    var browsed = false
    val compiler = new StringCompiler(
      additionalClassPath = List(path),
      runsAfter = List("typer"),
      transformer = Some(new WolfeTransformer {

        def transformTree[T <: Global#Tree](global: Global,env:WolfeCompilerPlugin2#Environment, tree: T) = {
          import global._
          tree match {
            case t @ PackageDef(_,_) =>
              //global.treeBrowser.browse(t.asInstanceOf[global.Tree])
            case i @ Ident(name) =>
              println(i)
            case s @ Select(_,name) if name.encoded == "argmin" =>
              val symbol = s.symbol
              println(symbol)
              val sig = symbol.owner.typeSignature.members
              val members = symbol.owner.tpe.members
              val children = symbol.owner.children
              println(members)
              println(env.implementations)
              println(env.implementations.get(s.symbol))

            case _ =>
          }
          tree
        }
      }))
    compiler.compileCode(source)
  }

  object MLETransformer extends WolfeTransformer {

    def transformTree[T <: Global#Tree](global: Global,env:WolfeCompilerPlugin2#Environment, tree: T) = {
      tree match {
        case global.Ident(name) =>
      }
      tree
    }
  }

  private def dirPathOfClass(className: String) = try {
    val resource = className.split('.').mkString("/", "/", ".class")<commit_msg>Work on MLE compiler<commit_after>package scalapplcodefest.example

import scalapplcodefest.compiler.{WolfeCompilerPlugin2, WolfeTransformer, StringCompiler}
import scala.language.implicitConversions
import scala.tools.nsc.Global
import scala.reflect.internal.Symbols
import scalapplcodefest.Wolfe

/**
 * @author <NAME>
 */
object MLECompileExample {

  val source =
    """
      |package scalapplcodefest.example
      |
      |import math.log
      |import scalapplcodefest._
      |import scala.language.reflectiveCalls
      |import scala.language.implicitConversions
      |
      |/**
      | * @author <NAME>
      | */
      |object MLEExampleWithLinearModel extends App {
      |
      |  import Wolfe._
      |
      |  type Coin = Symbol
      |
      |  //elements of the domain
      |  val coins = Set('H, 'T)
      |
      |  //training data
      |  val data = Seq('H, 'T, 'T, 'T)
      |
      |  @Objective.Categorical
      |  def s(params: Vector)(coin: Coin) = ft(coin) dot params
      |
      |  @Objective.JointLoglikelihood
      |  def ll(data: Seq[Coin])(w: Vector) = sum(data) {c => logZ(coins)(s(w)) - s(w)(c)}
      |
      |  //a subset of possible weights to make brute force search tractable
      |  val myReals = Set(0.0, 0.25, 0.75, 1.0).map(math.exp)
      |
      |  //val w = argmin(vectors)(ll(data)) //this won't run without compilation
      |  val w = argmin(vectors(coins, myReals)) {ll(data)} //this should run w/o compilation
      |
      |  //this is how the compiled expression should look like
      |  val compiled = (data map (ft(_))).sum.mapValues(w => log(w / data.size))
      |
      |  println(compiled)
      |  println(w)
      |
      |  def fun = {
      |     val x = 5
      |     x
      |  }
      |
      |}
      |    """.stripMargin

  def main(args: Array[String]) {
    val path = dirPathOfClass(getClass.getName)
    var browsed = false
    val compiler = new StringCompiler(
      additionalClassPath = List(path),
      runsAfter = List("typer"),
      transformer = Some(new WolfeTransformer {

        def transformTree[T <: Global#Tree](global: Global,env:WolfeCompilerPlugin2#Environment, tree: T) = {
          import global._
          tree match {
            case t @ PackageDef(_,_) =>
              global.treeBrowser.browse(t.asInstanceOf[global.Tree])
              println(env.implementations)
            case i @ Ident(name) =>
//              println(i)
            case valdef @ ValDef(_,_,_,app@Apply(f,_)) =>
//              println(f.getClass)
            case app @ Apply(Apply(TypeApply(s@Select(_,name),_),_),a) =>
              println(name)
              println(s.symbol)
              println(s.symbol.annotations)
              println(env.implementations.get(s.symbol))
              for (annotation <- s.symbol.annotations) {
                if (annotation.atp.toString() == "scalapplcodefest.Wolfe.Operator.Argmin") {
                  println("Translating...")
                }
                //if (annotation.symbol.name)
              }
//              println("Fun: " + f)
//              println("Arg: " + a)
//              println(name.encoded)
//              println(name2.encoded)
            case s @ Select(_,name) if name.encoded == "argmin" =>
              val symbol = s.symbol
//              println(symbol)
              val sig = symbol.owner.typeSignature.members
              val members = symbol.owner.tpe.members
              val children = symbol.owner.children
//              println(members)
//              println(env.implementations)
              val map = env.implementations
//              println(env.implementations.get(s.symbol))

            case _ =>
          }
          tree
        }
      }))
    compiler.compileCode(source)
  }

  object MLETransformer extends WolfeTransformer {

    def transformTree[T <: Global#Tree](global: Global,env:WolfeCompilerPlugin2#Environment, tree: T) = {
      tree match {
        case global.Ident(name) =>
      }
      tree
    }
  }

  private def dirPathOfClass(className: String) = try {
    val resource = className.split('.').mkString("/", "/", ".class")
{'fraction_non_alphanumeric': 0.12361822593691021, 'fraction_numerical': 0.003909409544351578, 'mean_word_length': 1.955776892430279, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '531771', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2240, 'n_words': 472}