<commit_before>/*
 * Copyright (c) 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package shapeless.examples

import shapeless._

sealed trait Super
case class Foo(i : Int, s : String) extends Super
case class Bar(i : Int) extends Super
case class BarRec(i : Int, rec: Super) extends Super

object Super {
  implicit val instance = TypeClass[Show, Super]
}

sealed trait MutualA
case class MutualA1(x: Int) extends MutualA
case class MutualA2(b: MutualB) extends MutualA

object MutualA {
  implicit val aInstance: Show[MutualA] = TypeClass[Show, MutualA]
}

sealed trait MutualB
case class MutualB1(x: Int) extends MutualB
case class MutualB2(b: MutualA) extends MutualB

object MutualB {
  implicit val bInstance: Show[MutualB] = TypeClass[Show, MutualB]
}

object ShowExamples extends App {
  import ShowSyntax._

  val bar: Super = Bar(0)
  val rec: Super = BarRec(1, Foo(0, "foo"))

  assert(bar.show == "Inl(0)")
  assert(rec.show == "Inr(Inl(1 :: Inr(Inr(0 :: foo :: HNil)) :: HNil))")

  val mutual: MutualA = MutualA2(MutualB2(MutualA1(0)))

  assert(mutual.show == "Inr(Inr(Inl(0)))")
}

trait ShowSyntax {
  def show : String
}

object ShowSyntax {
  implicit def showSyntax[T](a : T)(implicit st : Show[T]) : ShowSyntax = new ShowSyntax {
    def show = st.show(a)
  }
}

trait Show[T] {
  def show(t: T): String
}

object Show extends TypeClassCompanion[Show] {
  implicit def stringShow: Show[String] = new Show[String] {
    def show(t: String) = t
  }

  implicit def intShow: Show[Int] = new Show[Int] {
    def show(n: Int) = n.toString
  }

  implicit def showInstance: TypeClass[Show] = new TypeClass[Show] {
    def emptyProduct = new Show[HNil] {
      def show(t: HNil) = "HNil"
    }

    def product[F, T <: HList](FHead : Show[F], FTail : Show[T]) = new Show[F :: T] {
      def show(ft: F :: T) = s"${FHead.show(ft.head)} :: ${FTail.show(ft.tail)}"
    }

    def coproduct[L, R <: Coproduct](CL: => Show[L], CR: => Show[R]) = new Show[L :+: R] {
      def show(lr: L :+: R) = lr match {
        case Inl(l) => s"Inl(${CL.show(l)})"
        case Inr(r) => s"Inr(${CR.show(r)})"
      }
    }<commit_msg>override `coproduct1` in `Show` example to make output less confusing<commit_after>/*
 * Copyright (c) 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package shapeless.examples

import shapeless._

sealed trait Super
case class Foo(i : Int, s : String) extends Super
case class Bar(i : Int) extends Super
case class BarRec(i : Int, rec: Super) extends Super

object Super {
  implicit val instance = TypeClass[Show, Super]
}

sealed trait MutualA
case class MutualA1(x: Int) extends MutualA
case class MutualA2(b: MutualB) extends MutualA

object MutualA {
  implicit val aInstance: Show[MutualA] = TypeClass[Show, MutualA]
}

sealed trait MutualB
case class MutualB1(x: Int) extends MutualB
case class MutualB2(b: MutualA) extends MutualB

object MutualB {
  implicit val bInstance: Show[MutualB] = TypeClass[Show, MutualB]
}

object ShowExamples extends App {
  import ShowSyntax._

  val bar: Super = Bar(0)
  val rec: Super = BarRec(1, Foo(0, "foo"))

  assert(bar.show == "Inl(0)")
  assert(rec.show == "Inr(Inl(1 :: Inr(Inr(Inl(0 :: foo :: HNil))) :: HNil))")

  val mutual: MutualA = MutualA2(MutualB2(MutualA1(0)))

  assert(mutual.show == "Inr(Inl(Inr(Inl(Inl(0)))))")
}

trait ShowSyntax {
  def show : String
}

object ShowSyntax {
  implicit def showSyntax[T](a : T)(implicit st : Show[T]) : ShowSyntax = new ShowSyntax {
    def show = st.show(a)
  }
}

trait Show[T] {
  def show(t: T): String
}

object Show extends TypeClassCompanion[Show] {
  implicit def stringShow: Show[String] = new Show[String] {
    def show(t: String) = t
  }

  implicit def intShow: Show[Int] = new Show[Int] {
    def show(n: Int) = n.toString
  }

  implicit def showInstance: TypeClass[Show] = new TypeClass[Show] {
    def emptyProduct = new Show[HNil] {
      def show(t: HNil) = "HNil"
    }

    def product[F, T <: HList](FHead : Show[F], FTail : Show[T]) = new Show[F :: T] {
      def show(ft: F :: T) = s"${FHead.show(ft.head)} :: ${FTail.show(ft.tail)}"
    }

    override def coproduct1[L](CL: => Show[L]) = new Show[L :+: CNil] {
      def show(l: L :+: CNil) = l match {
        case Inl(l) => s"Inl(${CL.show(l)})"
        case Inr(_) => sys.error("absurd")
      }
    }

    def coproduct[L, R <: Coproduct](CL: => Show[L], CR: => Show[R]) = new Show[L :+: R] {
      def show(lr: L :+: R) = lr match {
        case Inl(l) => s"Inl(${CL.show(l)})"
        case Inr(r) => s"Inr(${CR.show(r)})"
      }
    }
{'fraction_non_alphanumeric': 0.1368665239114918, 'fraction_numerical': 0.008565310492505354, 'mean_word_length': 3.620774938169827, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3310553', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1950, 'n_words': 620}