From 6e8254e707ea6bf8e9e240f596f1cb4350a054f1 Mon Sep 17 00:00:00 2001 From: sdual Date: Sun, 15 Dec 2019 14:30:41 +0900 Subject: [PATCH 1/3] add simulacrum dependency --- build.sbt | 2 +- core/src/main/scala/com/github/sdual/stanz/Distribution.scala | 4 ++++ project/Dependencies.scala | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 core/src/main/scala/com/github/sdual/stanz/Distribution.scala diff --git a/build.sbt b/build.sbt index 12530b8..751c632 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ import Dependencies._ -scalacOptions += "-Ypartial-unification" +scalacOptions ++= Seq("-Ypartial-unification", "-Ymacro-annotations") lazy val commonSettings = Seq( organization := "com.github.sdual", diff --git a/core/src/main/scala/com/github/sdual/stanz/Distribution.scala b/core/src/main/scala/com/github/sdual/stanz/Distribution.scala new file mode 100644 index 0000000..4cf796e --- /dev/null +++ b/core/src/main/scala/com/github/sdual/stanz/Distribution.scala @@ -0,0 +1,4 @@ +package com.github.sdual.stanz + +class Distribution { +} diff --git a/project/Dependencies.scala b/project/Dependencies.scala index a466eb5..0e94a86 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -6,6 +6,7 @@ object Dependencies { val scalaMock = "org.scalamock" %% "scalamock" % "4.1.0" val apacheCommonsMath = "org.apache.commons" % "commons-math3" % "3.6.1" val cats = "org.typelevel" %% "cats-core" % "2.0.0" + val simulacrum = "org.typelevel" %% "simulacrum" % "1.0.0" val stanzCommonDependencies = Seq( apacheCommonsMath, @@ -14,7 +15,8 @@ object Dependencies { ) val stanzCoreDependencies = Seq( - cats + cats, + simulacrum ) ++ stanzCommonDependencies } From 71f85a0ce4cf2c1a8827d60b60a2678fa1fcbe04 Mon Sep 17 00:00:00 2001 From: sdual Date: Wed, 18 Dec 2019 17:45:36 +0900 Subject: [PATCH 2/3] add distribtion and its case class --- build.sbt | 2 +- .../com/github/sdual/stanz/Distribution.scala | 4 ---- .../sdual/stanz/distribution/Distribution.scala | 16 ++++++++++++++++ .../distribution/PrimitiveDistribution.scala | 5 +++++ .../sdual/stanz/distribution/package.scala | 5 +++++ 5 files changed, 27 insertions(+), 5 deletions(-) delete mode 100644 core/src/main/scala/com/github/sdual/stanz/Distribution.scala create mode 100644 core/src/main/scala/com/github/sdual/stanz/distribution/Distribution.scala create mode 100644 core/src/main/scala/com/github/sdual/stanz/distribution/PrimitiveDistribution.scala create mode 100644 core/src/main/scala/com/github/sdual/stanz/distribution/package.scala diff --git a/build.sbt b/build.sbt index 751c632..d868f9b 100644 --- a/build.sbt +++ b/build.sbt @@ -18,7 +18,7 @@ lazy val core = (project in file("core")) .settings( commonSettings, name := "stanz", - libraryDependencies ++= stanzCommonDependencies, + libraryDependencies ++= stanzCoreDependencies, ) lazy val prototype = (project in file("prototype")) diff --git a/core/src/main/scala/com/github/sdual/stanz/Distribution.scala b/core/src/main/scala/com/github/sdual/stanz/Distribution.scala deleted file mode 100644 index 4cf796e..0000000 --- a/core/src/main/scala/com/github/sdual/stanz/Distribution.scala +++ /dev/null @@ -1,4 +0,0 @@ -package com.github.sdual.stanz - -class Distribution { -} diff --git a/core/src/main/scala/com/github/sdual/stanz/distribution/Distribution.scala b/core/src/main/scala/com/github/sdual/stanz/distribution/Distribution.scala new file mode 100644 index 0000000..ac290ab --- /dev/null +++ b/core/src/main/scala/com/github/sdual/stanz/distribution/Distribution.scala @@ -0,0 +1,16 @@ +package com.github.sdual.stanz.distribution + +import simulacrum.typeclass + +import scala.util.Random + +@typeclass trait Distribution[A] { + //def sample(dist: Distribution[A])(random: Random): A +} + +object Distribution { + case class Point[P](value: P) extends Distribution[P] + case class FlatMap[P0, P1](dist: Distribution[P0], f: P0 => Distribution[P1]) extends Distribution[P1] + case class Primitive[P](fa: PrimitiveDistribution[P]) extends Distribution[P] + case class Conditional[P](dist: Distribution[P], likelihood: P => Probability) extends Distribution[P] +} \ No newline at end of file diff --git a/core/src/main/scala/com/github/sdual/stanz/distribution/PrimitiveDistribution.scala b/core/src/main/scala/com/github/sdual/stanz/distribution/PrimitiveDistribution.scala new file mode 100644 index 0000000..c3759ad --- /dev/null +++ b/core/src/main/scala/com/github/sdual/stanz/distribution/PrimitiveDistribution.scala @@ -0,0 +1,5 @@ +package com.github.sdual.stanz.distribution + +trait PrimitiveDistribution[P] { + +} diff --git a/core/src/main/scala/com/github/sdual/stanz/distribution/package.scala b/core/src/main/scala/com/github/sdual/stanz/distribution/package.scala new file mode 100644 index 0000000..fb72a4e --- /dev/null +++ b/core/src/main/scala/com/github/sdual/stanz/distribution/package.scala @@ -0,0 +1,5 @@ +package com.github.sdual.stanz + +package object distribution { + type Probability = Double +} From 9e89f0d7dbabea96cf8c87879c861fbf5641133e Mon Sep 17 00:00:00 2001 From: sdual Date: Sat, 8 Aug 2020 03:41:22 +0900 Subject: [PATCH 3/3] add type class using simulacrum --- build.sbt | 5 ++++- .../github/sdual/stanz/distribution/Distribution.scala | 2 +- .../com/github/sdual/stanz/typeclass/Applicative.scala | 8 ++++++++ .../scala/com/github/sdual/stanz/typeclass/Functor.scala | 7 +++++++ .../scala/com/github/sdual/stanz/typeclass/Monad.scala | 8 ++++++++ .../com/github/sdual/stanz/typeclass/Sampleable.scala | 9 +++++++++ project/Dependencies.scala | 2 -- 7 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 core/src/main/scala/com/github/sdual/stanz/typeclass/Applicative.scala create mode 100644 core/src/main/scala/com/github/sdual/stanz/typeclass/Functor.scala create mode 100644 core/src/main/scala/com/github/sdual/stanz/typeclass/Monad.scala create mode 100644 core/src/main/scala/com/github/sdual/stanz/typeclass/Sampleable.scala diff --git a/build.sbt b/build.sbt index d868f9b..2a03ba4 100644 --- a/build.sbt +++ b/build.sbt @@ -9,6 +9,9 @@ lazy val commonSettings = Seq( ) lazy val root = (project in file(".")) + .settings( + name := "stanz" + ) .aggregate( prototype, example @@ -17,7 +20,7 @@ lazy val root = (project in file(".")) lazy val core = (project in file("core")) .settings( commonSettings, - name := "stanz", + name := "core", libraryDependencies ++= stanzCoreDependencies, ) diff --git a/core/src/main/scala/com/github/sdual/stanz/distribution/Distribution.scala b/core/src/main/scala/com/github/sdual/stanz/distribution/Distribution.scala index ac290ab..6eae04f 100644 --- a/core/src/main/scala/com/github/sdual/stanz/distribution/Distribution.scala +++ b/core/src/main/scala/com/github/sdual/stanz/distribution/Distribution.scala @@ -13,4 +13,4 @@ object Distribution { case class FlatMap[P0, P1](dist: Distribution[P0], f: P0 => Distribution[P1]) extends Distribution[P1] case class Primitive[P](fa: PrimitiveDistribution[P]) extends Distribution[P] case class Conditional[P](dist: Distribution[P], likelihood: P => Probability) extends Distribution[P] -} \ No newline at end of file +} diff --git a/core/src/main/scala/com/github/sdual/stanz/typeclass/Applicative.scala b/core/src/main/scala/com/github/sdual/stanz/typeclass/Applicative.scala new file mode 100644 index 0000000..87a77a1 --- /dev/null +++ b/core/src/main/scala/com/github/sdual/stanz/typeclass/Applicative.scala @@ -0,0 +1,8 @@ +package com.github.sdual.stanz.typeclass + +import simulacrum.typeclass + +@typeclass trait Applicative[F[_]] extends Functor[F] { + def pure[A](a: => A): F[A] + def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B] +} diff --git a/core/src/main/scala/com/github/sdual/stanz/typeclass/Functor.scala b/core/src/main/scala/com/github/sdual/stanz/typeclass/Functor.scala new file mode 100644 index 0000000..5b105a2 --- /dev/null +++ b/core/src/main/scala/com/github/sdual/stanz/typeclass/Functor.scala @@ -0,0 +1,7 @@ +package com.github.sdual.stanz.typeclass + +import simulacrum.typeclass + +@typeclass trait Functor[F[_]] { + def map[A, B](fa: F[A])(f: A => B): F[B] +} diff --git a/core/src/main/scala/com/github/sdual/stanz/typeclass/Monad.scala b/core/src/main/scala/com/github/sdual/stanz/typeclass/Monad.scala new file mode 100644 index 0000000..69a7051 --- /dev/null +++ b/core/src/main/scala/com/github/sdual/stanz/typeclass/Monad.scala @@ -0,0 +1,8 @@ +package com.github.sdual.stanz.typeclass + +import simulacrum.typeclass + +@typeclass trait Monad[F[_]] extends Applicative[F] { + def point[A](a: => A): F[A] = pure(a) + def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B] +} diff --git a/core/src/main/scala/com/github/sdual/stanz/typeclass/Sampleable.scala b/core/src/main/scala/com/github/sdual/stanz/typeclass/Sampleable.scala new file mode 100644 index 0000000..d575493 --- /dev/null +++ b/core/src/main/scala/com/github/sdual/stanz/typeclass/Sampleable.scala @@ -0,0 +1,9 @@ +package com.github.sdual.stanz.typeclass + +import simulacrum.typeclass + +import scala.util.Random + +@typeclass trait Sampleable[F[_]] { + def sample[A](fa: F[A])(random: Random): A +} diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 0e94a86..c791051 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -5,7 +5,6 @@ object Dependencies { val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5" val scalaMock = "org.scalamock" %% "scalamock" % "4.1.0" val apacheCommonsMath = "org.apache.commons" % "commons-math3" % "3.6.1" - val cats = "org.typelevel" %% "cats-core" % "2.0.0" val simulacrum = "org.typelevel" %% "simulacrum" % "1.0.0" val stanzCommonDependencies = Seq( @@ -15,7 +14,6 @@ object Dependencies { ) val stanzCoreDependencies = Seq( - cats, simulacrum ) ++ stanzCommonDependencies