<commit_before>package info.hupel.isabelle.internal

import scala.language.experimental.macros
import scala.reflect.macros.whitebox

import cats.instances.list._
import cats.syntax.traverse._

import info.hupel.isabelle._
import info.hupel.isabelle.pure._

import macrocompat.bundle

object Macros {

  def fuse(terms: List[Program[Term]], parts: List[String]): Program[String] = {
    val tps: Program[List[String]] =
      terms.sequence.map { ts =>
        ts zip parts.tail map { case (t, p) =>
          val s = Codec[Term].encode(t).compact
          s"XML\\<open>$s\\<close>$p"
        }
      }

    tps.map(_.mkString("")).map(parts.head + _)
  }

}

@bundle
class Macros(val c: whitebox.Context { type PrefixType <: ExprStringContext.term.type }) {

  import c.universe.{Expr => _, _}

  // FIXME this should be c.prefix, debug MatchError
  private val q"""${_}(${_}(..$parts)).${_}.apply[..${_}](..${_})""" =
    c.macroApplication

  private def embed(arg: Tree): Tree = {
    if (arg.tpe <:< typeOf[Term])
      q"""_root_.info.hupel.isabelle.MLProg.pure($arg)"""
    else if (arg.tpe <:< typeOf[Expr[_]])
      q"""_root_.info.hupel.isabelle.MLProg.pure($arg.term)"""
    else {
      val embeddable = c.inferImplicitValue(appliedType(typeOf[Embeddable[_]], arg.tpe))
      q"""$embeddable.embed($arg)"""
    }
  }
<commit_msg>term quasiquote: catch missing implicits<commit_after>package info.hupel.isabelle.internal

import scala.language.experimental.macros
import scala.reflect.macros.whitebox

import cats.instances.list._
import cats.syntax.traverse._

import info.hupel.isabelle._
import info.hupel.isabelle.pure._

import macrocompat.bundle

object Macros {

  def fuse(terms: List[Program[Term]], parts: List[String]): Program[String] = {
    val tps: Program[List[String]] =
      terms.sequence.map { ts =>
        ts zip parts.tail map { case (t, p) =>
          val s = Codec[Term].encode(t).compact
          s"XML\\<open>$s\\<close>$p"
        }
      }

    tps.map(_.mkString("")).map(parts.head + _)
  }

}

@bundle
class Macros(val c: whitebox.Context { type PrefixType <: ExprStringContext.term.type }) {

  import c.universe.{Expr => _, _}

  // FIXME this should be c.prefix, debug MatchError
  private val q"""${_}(${_}(..$parts)).${_}.apply[..${_}](..${_})""" =
    c.macroApplication

  private def embed(arg: Tree): Tree = {
    if (arg.tpe <:< typeOf[Term])
      q"""_root_.info.hupel.isabelle.MLProg.pure($arg)"""
    else if (arg.tpe <:< typeOf[Expr[_]])
      q"""_root_.info.hupel.isabelle.MLProg.pure($arg.term)"""
    else {
      val embeddable = c.inferImplicitValue(appliedType(typeOf[Embeddable[_]], arg.tpe))
      if (embeddable == EmptyTree)
        c.error(c.enclosingPosition, s"Could not find implicit `Embeddable` for type ${arg.tpe}")
      q"""$embeddable.embed($arg)"""
    }
  }

{'fraction_non_alphanumeric': 0.17452498240675582, 'fraction_numerical': 0.0, 'mean_word_length': 3.3537519142419603, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, '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': '3136084', 'n_tokens_mistral': 1070, 'n_tokens_neox': 1027, 'n_words': 180}