enum Expr:
case Num(value: Int)
case Bool(value: Boolean)
case Var(name: String)
case Add(lhs: Expr, rhs: Expr)
case Gt(lhs: Expr, rhs: Expr)
case Let(name: String, value: Expr, body: Expr)
// - Elem -------------------------------------------------------------------------------------------------------------- | |
// --------------------------------------------------------------------------------------------------------------------- | |
sealed trait Elem[X, XS <: Tuple]: | |
def get(haystack: XS): X | |
object Elem: | |
case class Here[X, XS <: Tuple]() extends Elem [X, X *: XS]: | |
def get(haystack: X *: XS) = haystack.head |
def test[LA, LB, RA, RB, F[_, _]]( | |
eqA: LA =:= RA, | |
eqB: LB =:= RB | |
): (F[LA, LB] =:= F[RA, RB]) = { | |
// We start from F[LA, LB] and prove equality of the "left" part. | |
val step1: F[LA, LB] =:= F[RA, LB] = eqA.liftCo[F[_, LB]] | |
// We start from F[RA, LB] and prove equality of the "right" part. | |
val step2: F[RA, LB] =:= F[RA, RB] = eqB.liftCo[F[RA, _]] |
Inductive letter : Type := A | B| C | D | E | F. | |
Inductive modifier : Type := Plus | Natural | Minus. | |
Inductive grade : Type := Grade (l: letter) (m: modifier). | |
Inductive comparison: Set := | |
| Eq: comparison | |
| Lt: comparison | |
| Gt: comparison. |
import java.util.List; | |
import java.util.Arrays; | |
import java.util.Collections; | |
class Demo { | |
static class NotComparable {} | |
public static void main(String[] args) { | |
List<NotComparable> list = Arrays.asList(new NotComparable(), new NotComparable()); |
Hello, my name is Nicolas Rinaudo and I’m here today because I’d like to talk to you about one of the core features of our favourite language, one that we, as OOP programmers, should be intimately familiar with: inheritance.
Yes, I’m aware that there’s a fringe of the community that refuses to use inheritance on philosophical grounds, and I respect that. I just think it’s a bit of a shame because honestly, I couldn’t work without algebraic data types, and, well, Scala does them with inheritance. Or without type classes, for that matter - anybody that knows me knows I love my type classes - and they’re also done with inheritance in Haskell. I mean in Scala. Well, in both, really.
Don’t believe me? Here’s what the Haskell documentation has to say on the subject (https://www.haskell.org/tutorial/classes.html):
We say that Eq is a superclass of Ord (conversely, Ord is a subclass of Eq), and any type which is an instance of Ord must also be an instance of Eq.
I was recently asked to explain why I felt disappointed by Haskell, as a language. And, well. Crucified for crucified, I might as well criticise Haskell publicly.
First though, I need to make it explicit that I claim no particular skill with the language - I will in fact vehemently (and convincingly!) argue that I'm a terrible Haskell programmer. And what I'm about to explain is not meant as The Truth, but my current understanding, potentially flawed, incomplete, or flat out incorrect. I welcome any attempt at proving me wrong, because when I dislike something that so many clever people worship, it's usually because I missed an important detail.
Another important point is that this is not meant to convey the idea that Haskell is a bad language. I do feel, however, that the vocal, and sometimes aggressive, reverence in which it's held might lead people to have unreasonable expectations. It certainly was my case, and the reason I'm writing this.
I love the concept of type class
EXTENDS Integers, TLC, Sequences | |
CONSTANTS Books, | |
People, | |
NumCopies | |
ASSUME NumCopies \in Nat \ {0} | |
PT == INSTANCE PT | |
(*--algorithm library |
------------------------------ MODULE imageaug ------------------------------ | |
EXTENDS Integers, FiniteSets, TLC | |
CONSTANTS Documents, | |
Models, | |
MaxRetries, | |
NULL | |
ASSUME MaxRetries \in Nat \ {0} | |
ASSUME Cardinality(Documents) /= 0 |
import monocle.macros._ | |
import org.scalacheck._ | |
import scalaz.scalacheck.ScalaCheckBinding._ | |
// Deep hierarchy of product types. | |
case class Document(author: Author) | |
case class Author(firstName: String, lastName: String, city: City) | |
case class City(name: String, country: Country) | |
case class Country(name: String, continent: Continent) | |
case class Continent(name: String) |