├── .gitignore ├── .travis.yml ├── CREDITS.txt ├── LICENSE.txt ├── README.md ├── build.sbt ├── common └── src │ └── main │ └── scala │ └── common.scala ├── project ├── build.properties └── plugins.sbt ├── scalacheck └── src │ ├── main │ └── scala │ │ └── package.scala │ └── test │ └── scala │ └── ShrinkSpec.scala ├── scripts ├── dot-sbtrc └── travis-publish.sh ├── spire └── src │ ├── main │ └── scala │ │ ├── package.scala │ │ └── typeclass.scala │ └── test │ └── scala │ └── ProductTest.scala └── version.sbt /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | tags 4 | 5 | # sbt specific 6 | .lib/ 7 | dist/* 8 | target/ 9 | lib_managed/ 10 | src_managed/ 11 | project/boot/ 12 | project/plugins/project/ 13 | 14 | # IDE specific 15 | .scala_dependencies 16 | .*.swp 17 | .*.swo 18 | .idea* 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: scala 2 | 3 | scala: 4 | - 2.10.6 5 | - 2.11.7 6 | - 2.12.2 7 | 8 | jdk: 9 | - oraclejdk8 10 | 11 | cache: 12 | directories: 13 | - $HOME/.ivy2/cache 14 | - $HOME/.sbt 15 | 16 | script: 17 | - scripts/travis-publish.sh 18 | 19 | env: 20 | global: 21 | - secure: jFz1aWhx2zBWz1jNTeqxxfy2+4v25SiJIlO2HGd0Iu3921Rd7lJp+4fnSZlwxcDpIS5/laqu7jAxOR2o2QecBNQBtBMwSFfTFsAnJNHi6c2qB4Ns2HBJrYjoQ/sZFEqoJw62M5/ECU2qK+TzLdDYdULqlF4inlkIrVEj2av7oyw= 22 | - secure: yK4+dMyjKhoNOvDvUsGR/Q9iYeP4pe6ibKX7o4WkHyTYb9ohS4CrrcpSF2N8eSqAQK7I/CW66tLH01HFPp2vQ4L+zlmJi6bLkxEZCvXXbMBbUMHdaiLvTsvJeu+DlwA1BlGPkAGLrD7SvGK38R7A7NQT3AutEz3V6XUMWWd2CDs= 23 | 24 | sudo: false 25 | 26 | notifications: 27 | webhooks: 28 | urls: 29 | - https://webhooks.gitter.im/e/acd320c0b11f34dcc2a0 30 | -------------------------------------------------------------------------------- /CREDITS.txt: -------------------------------------------------------------------------------- 1 | sbt launcher script 2 | =================== 3 | 4 | Copyright (c) 2011, Paul Phillips. 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 11 | * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14 | 15 | 16 | scalaz 17 | ====== 18 | 19 | Copyright (c) 2009-2012 Tony Morris, Runar Bjarnason, Tom Adams, Kristian Domagala, Brad Clow, Ricky Clarkson, Paul Chiusano, Trygve Laugstøl, Nick Partridge, Jason Zaugg 20 | All rights reserved. 21 | 22 | Redistribution and use in source and binary forms, with or without 23 | modification, are permitted provided that the following conditions 24 | are met: 25 | 1. Redistributions of source code must retain the above copyright 26 | notice, this list of conditions and the following disclaimer. 27 | 2. Redistributions in binary form must reproduce the above copyright 28 | notice, this list of conditions and the following disclaimer in the 29 | documentation and/or other materials provided with the distribution. 30 | 3. The name of the author may not be used to endorse or promote products 31 | derived from this software without specific prior written permission. 32 | 33 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 34 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 35 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 36 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 37 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 38 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 39 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 40 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 41 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 42 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 43 | 44 | 45 | shapeless 46 | ========= 47 | 48 | Copyright (c) 2012 Miles Sabin 49 | 50 | 51 | Apache License 52 | Version 2.0, January 2004 53 | http://www.apache.org/licenses/ 54 | 55 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 56 | 57 | 1. Definitions. 58 | 59 | "License" shall mean the terms and conditions for use, reproduction, 60 | and distribution as defined by Sections 1 through 9 of this document. 61 | 62 | "Licensor" shall mean the copyright owner or entity authorized by 63 | the copyright owner that is granting the License. 64 | 65 | "Legal Entity" shall mean the union of the acting entity and all 66 | other entities that control, are controlled by, or are under common 67 | control with that entity. For the purposes of this definition, 68 | "control" means (i) the power, direct or indirect, to cause the 69 | direction or management of such entity, whether by contract or 70 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 71 | outstanding shares, or (iii) beneficial ownership of such entity. 72 | 73 | "You" (or "Your") shall mean an individual or Legal Entity 74 | exercising permissions granted by this License. 75 | 76 | "Source" form shall mean the preferred form for making modifications, 77 | including but not limited to software source code, documentation 78 | source, and configuration files. 79 | 80 | "Object" form shall mean any form resulting from mechanical 81 | transformation or translation of a Source form, including but 82 | not limited to compiled object code, generated documentation, 83 | and conversions to other media types. 84 | 85 | "Work" shall mean the work of authorship, whether in Source or 86 | Object form, made available under the License, as indicated by a 87 | copyright notice that is included in or attached to the work 88 | (an example is provided in the Appendix below). 89 | 90 | "Derivative Works" shall mean any work, whether in Source or Object 91 | form, that is based on (or derived from) the Work and for which the 92 | editorial revisions, annotations, elaborations, or other modifications 93 | represent, as a whole, an original work of authorship. For the purposes 94 | of this License, Derivative Works shall not include works that remain 95 | separable from, or merely link (or bind by name) to the interfaces of, 96 | the Work and Derivative Works thereof. 97 | 98 | "Contribution" shall mean any work of authorship, including 99 | the original version of the Work and any modifications or additions 100 | to that Work or Derivative Works thereof, that is intentionally 101 | submitted to Licensor for inclusion in the Work by the copyright owner 102 | or by an individual or Legal Entity authorized to submit on behalf of 103 | the copyright owner. For the purposes of this definition, "submitted" 104 | means any form of electronic, verbal, or written communication sent 105 | to the Licensor or its representatives, including but not limited to 106 | communication on electronic mailing lists, source code control systems, 107 | and issue tracking systems that are managed by, or on behalf of, the 108 | Licensor for the purpose of discussing and improving the Work, but 109 | excluding communication that is conspicuously marked or otherwise 110 | designated in writing by the copyright owner as "Not a Contribution." 111 | 112 | "Contributor" shall mean Licensor and any individual or Legal Entity 113 | on behalf of whom a Contribution has been received by Licensor and 114 | subsequently incorporated within the Work. 115 | 116 | 2. Grant of Copyright License. Subject to the terms and conditions of 117 | this License, each Contributor hereby grants to You a perpetual, 118 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 119 | copyright license to reproduce, prepare Derivative Works of, 120 | publicly display, publicly perform, sublicense, and distribute the 121 | Work and such Derivative Works in Source or Object form. 122 | 123 | 3. Grant of Patent License. Subject to the terms and conditions of 124 | this License, each Contributor hereby grants to You a perpetual, 125 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 126 | (except as stated in this section) patent license to make, have made, 127 | use, offer to sell, sell, import, and otherwise transfer the Work, 128 | where such license applies only to those patent claims licensable 129 | by such Contributor that are necessarily infringed by their 130 | Contribution(s) alone or by combination of their Contribution(s) 131 | with the Work to which such Contribution(s) was submitted. If You 132 | institute patent litigation against any entity (including a 133 | cross-claim or counterclaim in a lawsuit) alleging that the Work 134 | or a Contribution incorporated within the Work constitutes direct 135 | or contributory patent infringement, then any patent licenses 136 | granted to You under this License for that Work shall terminate 137 | as of the date such litigation is filed. 138 | 139 | 4. Redistribution. You may reproduce and distribute copies of the 140 | Work or Derivative Works thereof in any medium, with or without 141 | modifications, and in Source or Object form, provided that You 142 | meet the following conditions: 143 | 144 | (a) You must give any other recipients of the Work or 145 | Derivative Works a copy of this License; and 146 | 147 | (b) You must cause any modified files to carry prominent notices 148 | stating that You changed the files; and 149 | 150 | (c) You must retain, in the Source form of any Derivative Works 151 | that You distribute, all copyright, patent, trademark, and 152 | attribution notices from the Source form of the Work, 153 | excluding those notices that do not pertain to any part of 154 | the Derivative Works; and 155 | 156 | (d) If the Work includes a "NOTICE" text file as part of its 157 | distribution, then any Derivative Works that You distribute must 158 | include a readable copy of the attribution notices contained 159 | within such NOTICE file, excluding those notices that do not 160 | pertain to any part of the Derivative Works, in at least one 161 | of the following places: within a NOTICE text file distributed 162 | as part of the Derivative Works; within the Source form or 163 | documentation, if provided along with the Derivative Works; or, 164 | within a display generated by the Derivative Works, if and 165 | wherever such third-party notices normally appear. The contents 166 | of the NOTICE file are for informational purposes only and 167 | do not modify the License. You may add Your own attribution 168 | notices within Derivative Works that You distribute, alongside 169 | or as an addendum to the NOTICE text from the Work, provided 170 | that such additional attribution notices cannot be construed 171 | as modifying the License. 172 | 173 | You may add Your own copyright statement to Your modifications and 174 | may provide additional or different license terms and conditions 175 | for use, reproduction, or distribution of Your modifications, or 176 | for any such Derivative Works as a whole, provided Your use, 177 | reproduction, and distribution of the Work otherwise complies with 178 | the conditions stated in this License. 179 | 180 | 5. Submission of Contributions. Unless You explicitly state otherwise, 181 | any Contribution intentionally submitted for inclusion in the Work 182 | by You to the Licensor shall be under the terms and conditions of 183 | this License, without any additional terms or conditions. 184 | Notwithstanding the above, nothing herein shall supersede or modify 185 | the terms of any separate license agreement you may have executed 186 | with Licensor regarding such Contributions. 187 | 188 | 6. Trademarks. This License does not grant permission to use the trade 189 | names, trademarks, service marks, or product names of the Licensor, 190 | except as required for reasonable and customary use in describing the 191 | origin of the Work and reproducing the content of the NOTICE file. 192 | 193 | 7. Disclaimer of Warranty. Unless required by applicable law or 194 | agreed to in writing, Licensor provides the Work (and each 195 | Contributor provides its Contributions) on an "AS IS" BASIS, 196 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 197 | implied, including, without limitation, any warranties or conditions 198 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 199 | PARTICULAR PURPOSE. You are solely responsible for determining the 200 | appropriateness of using or redistributing the Work and assume any 201 | risks associated with Your exercise of permissions under this License. 202 | 203 | 8. Limitation of Liability. In no event and under no legal theory, 204 | whether in tort (including negligence), contract, or otherwise, 205 | unless required by applicable law (such as deliberate and grossly 206 | negligent acts) or agreed to in writing, shall any Contributor be 207 | liable to You for damages, including any direct, indirect, special, 208 | incidental, or consequential damages of any character arising as a 209 | result of this License or out of the use or inability to use the 210 | Work (including but not limited to damages for loss of goodwill, 211 | work stoppage, computer failure or malfunction, or any and all 212 | other commercial damages or losses), even if such Contributor 213 | has been advised of the possibility of such damages. 214 | 215 | 9. Accepting Warranty or Additional Liability. While redistributing 216 | the Work or Derivative Works thereof, You may choose to offer, 217 | and charge a fee for, acceptance of support, warranty, indemnity, 218 | or other liability obligations and/or rights consistent with this 219 | License. However, in accepting such obligations, You may act only 220 | on Your own behalf and on Your sole responsibility, not on behalf 221 | of any other Contributor, and only if You agree to indemnify, 222 | defend, and hold each Contributor harmless for any liability 223 | incurred by, or claims asserted against, such Contributor by reason 224 | of your accepting any such warranty or additional liability. 225 | 226 | END OF TERMS AND CONDITIONS 227 | 228 | APPENDIX: How to apply the Apache License to your work. 229 | 230 | To apply the Apache License to your work, attach the following 231 | boilerplate notice, with the fields enclosed by brackets "[]" 232 | replaced with your own identifying information. (Don't include 233 | the brackets!) The text should be enclosed in the appropriate 234 | comment syntax for the file format. We also recommend that a 235 | file or class name and description of purpose be included on the 236 | same "printed page" as the copyright notice for easier 237 | identification within third-party archives. 238 | 239 | Copyright [yyyy] [name of copyright owner] 240 | 241 | Licensed under the Apache License, Version 2.0 (the "License"); 242 | you may not use this file except in compliance with the License. 243 | You may obtain a copy of the License at 244 | 245 | http://www.apache.org/licenses/LICENSE-2.0 246 | 247 | Unless required by applicable law or agreed to in writing, software 248 | distributed under the License is distributed on an "AS IS" BASIS, 249 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 250 | See the License for the specific language governing permissions and 251 | limitations under the License. 252 | 253 | 254 | spire 255 | ===== 256 | 257 | Copyright (c) 2011-2012 Erik Osheim, Tom Switzer 258 | 259 | Permission is hereby granted, free of charge, to any person obtaining a copy of 260 | this software and associated documentation files (the "Software"), to deal in 261 | the Software without restriction, including without limitation the rights to 262 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 263 | of the Software, and to permit persons to whom the Software is furnished to do 264 | so, subject to the following conditions: 265 | 266 | The above copyright notice and this permission notice shall be included in all 267 | copies or substantial portions of the Software. 268 | 269 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 270 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 271 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 272 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 273 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 274 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 275 | SOFTWARE. 276 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 Lars Hupel 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | shapeless-contrib 2 | ================= 3 | 4 | Interoperability libraries for Shapeless 5 | 6 | [![Build Status](https://travis-ci.org/typelevel/shapeless-contrib.png?branch=master)](http://travis-ci.org/typelevel/shapeless-contrib) 7 | 8 | 9 | Usage 10 | ----- 11 | 12 | This library is currently available for Scala 2.10, 2.11, and 2.12. 13 | 14 | To use the latest version, include the following in your `build.sbt`: 15 | 16 | ```scala 17 | libraryDependencies ++= Seq( 18 | "org.typelevel" %% "shapeless-scalacheck" % "0.6.1", 19 | "org.typelevel" %% "shapeless-spire" % "0.6.1", 20 | "org.typelevel" %% "shapeless-scalaz" % "0.6.1" 21 | ) 22 | ``` 23 | 24 | 25 | What does this library do? 26 | -------------------------- 27 | 28 | `shapeless-contrib` aims to provide smooth interoperability between [Shapeless](https://github.com/milessabin/shapeless), and [Spire](https://github.com/non/spire). The most prominent feature is automatic derivation of type class instances for case classes, but there is more. If you've got a cool idea for more stuff, please open an issue. 29 | 30 | 31 | Examples 32 | -------- 33 | 34 | ### Derive type classes 35 | 36 | Consider a simple case class with an addition operation: 37 | 38 | ```scala 39 | case class Vector3(x: Int, y: Int, z: Int) { 40 | def +(other: Vector3) = Vector3(this.x + other.x, this.y + other.y, this.z + other.z) 41 | } 42 | ``` 43 | 44 | If we want to use that in a generic setting, e.g. in an algorithm which requires a `Monoid`, we can define an instance for `spire.algebra.Monoid` like so: 45 | 46 | ```scala 47 | implicit object Vector3Monoid extends Monoid[Vector3] { 48 | def id = Vector3(0, 0, 0) 49 | def op(x: Vector3, y: Vector3) = x + y 50 | } 51 | ``` 52 | 53 | This will work nicely for that particular case. However, it requires repetition: addition on `Vector3` is just pointwise addition of its elements, and the null vector consists of three zeroes. We do not want to repeat that sort of code for all our case classes, and want to derive that automatically. Luckily, this library provides exactly that: 54 | 55 | ```scala 56 | import spire.implicits._ 57 | import shapeless.contrib.spire._ 58 | 59 | // Define the `Vector3` case class without any operations 60 | case class Vector3(x: Int, y: Int, z: Int) 61 | 62 | // That's it! `Vector3` is an `AdditiveMonoid` now. 63 | Vector3(1, 2, 3) + Vector3(-1, 3, 0) 64 | ``` 65 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | import com.typesafe.sbt.pgp.PgpKeys.publishSigned 2 | import org.scalajs.sbtplugin.cross.{ CrossProject, CrossType } 3 | import ReleaseTransformations._ 4 | 5 | import com.typesafe.tools.mima.plugin.MimaPlugin.mimaDefaultSettings 6 | import com.typesafe.tools.mima.plugin.MimaKeys 7 | import MimaKeys.{ previousArtifacts, binaryIssueFilters } 8 | 9 | import com.typesafe.sbt.SbtGit._ 10 | import GitKeys._ 11 | 12 | lazy val buildSettings = Seq( 13 | organization := "org.typelevel", 14 | scalaVersion := "2.12.2", 15 | crossScalaVersions := Seq("2.10.6", "2.11.8", "2.12.2") 16 | ) 17 | 18 | addCommandAlias("root", ";project rootJVM") 19 | addCommandAlias("common", ";project commonJVM") 20 | addCommandAlias("scalacheck", ";project scalacheckJVM") 21 | addCommandAlias("scalaz", ";project scalazJVM") 22 | addCommandAlias("spire", ";project spireJVM") 23 | 24 | addCommandAlias("validate", ";root;compile;test") 25 | addCommandAlias("validateJVM", ";project rootJVM;compile;test") 26 | addCommandAlias("validateJS", ";project rootJS;compile;test") 27 | 28 | addCommandAlias("releaseAll", ";root;release skip-tests") 29 | 30 | val shapelessVersion = "2.3.2" 31 | val scalacheckVersion = "1.13.5" 32 | val spireVersion = "0.13.0" 33 | val scalatestVersion = "3.0.1" 34 | 35 | lazy val commonSettings = Seq( 36 | scalacOptions := Seq( 37 | "-feature", 38 | "-language:higherKinds", 39 | "-language:implicitConversions", 40 | "-Xfatal-warnings", 41 | "-deprecation", 42 | "-unchecked" 43 | ), 44 | 45 | resolvers ++= Seq( 46 | Resolver.sonatypeRepo("releases"), 47 | Resolver.sonatypeRepo("snapshots") 48 | ), 49 | 50 | scalacOptions in console in Compile -= "-Xfatal-warnings", 51 | scalacOptions in console in Test -= "-Xfatal-warnings", 52 | 53 | scmInfo := 54 | Some(ScmInfo( 55 | url("https://github.com/typelevel/shapeless-contrib"), 56 | "scm:git:git://github.com/typelevel/shapeless-contrib.git" 57 | )) 58 | ) ++ scalaMacroDependencies 59 | 60 | lazy val commonJsSettings = Seq( 61 | parallelExecution in Test := false 62 | ) 63 | 64 | lazy val commonJvmSettings = Seq( 65 | parallelExecution in Test := false 66 | ) 67 | 68 | lazy val coreSettings = buildSettings ++ commonSettings ++ publishSettings 69 | 70 | lazy val root = project.in(file(".")) 71 | .aggregate(rootJS, rootJVM) 72 | .dependsOn(rootJS, rootJVM) 73 | .settings(coreSettings:_*) 74 | .settings(noPublishSettings) 75 | 76 | lazy val rootJVM = project.in(file(".rootJVM")) 77 | .aggregate(commonJVM, scalacheckJVM, spireJVM) 78 | .dependsOn(commonJVM, scalacheckJVM, spireJVM) 79 | .settings(coreSettings:_*) 80 | .settings(noPublishSettings) 81 | 82 | lazy val rootJS = project.in(file(".rootJS")) 83 | .aggregate(commonJS, scalacheckJS, spireJS) 84 | .dependsOn(commonJS, scalacheckJS, spireJS) 85 | .settings(coreSettings:_*) 86 | .settings(noPublishSettings) 87 | 88 | lazy val common = crossProject.crossType(CrossType.Pure) 89 | .settings(moduleName := "shapeless-contrib-common") 90 | .settings(coreSettings:_*) 91 | .settings(mimaSettings:_*) 92 | .settings( 93 | libraryDependencies ++= Seq( 94 | "com.chuusai" %%% "shapeless" % shapelessVersion 95 | ) 96 | ) 97 | .jsSettings(commonJsSettings:_*) 98 | .jvmSettings(commonJvmSettings:_*) 99 | 100 | lazy val commonJVM = common.jvm 101 | lazy val commonJS = common.js 102 | 103 | lazy val scalacheck = crossProject.crossType(CrossType.Pure) 104 | .dependsOn(common) 105 | .settings(moduleName := "shapeless-scalacheck") 106 | .settings(coreSettings:_*) 107 | .settings(mimaSettings:_*) 108 | .settings( 109 | libraryDependencies ++= Seq( 110 | "com.chuusai" %%% "shapeless" % shapelessVersion, 111 | "org.scalacheck" %%% "scalacheck" % scalacheckVersion 112 | ) 113 | ) 114 | .jsSettings(commonJsSettings:_*) 115 | .jvmSettings(commonJvmSettings:_*) 116 | 117 | lazy val scalacheckJVM = scalacheck.jvm 118 | lazy val scalacheckJS = scalacheck.js 119 | 120 | lazy val spire = crossProject.crossType(CrossType.Pure) 121 | .dependsOn(common, scalacheck % "test") 122 | .settings(moduleName := "shapeless-spire") 123 | .settings(coreSettings:_*) 124 | .settings(mimaSettings:_*) 125 | .settings( 126 | libraryDependencies ++= Seq( 127 | "com.chuusai" %%% "shapeless" % shapelessVersion, 128 | scalaOrganization.value % "scala-reflect" % scalaVersion.value % "provided", 129 | "org.spire-math" %%% "spire" % spireVersion, 130 | "org.scalatest" %%% "scalatest" % scalatestVersion % "test", 131 | "org.scalacheck" %%% "scalacheck" % scalacheckVersion % "test", 132 | "org.spire-math" %%% "spire-laws" % spireVersion % "test" 133 | ) 134 | ) 135 | .jsSettings(commonJsSettings:_*) 136 | .jvmSettings(commonJvmSettings:_*) 137 | 138 | lazy val spireJVM = spire.jvm 139 | lazy val spireJS = spire.js 140 | 141 | lazy val scalaMacroDependencies: Seq[Setting[_]] = Seq( 142 | libraryDependencies ++= { 143 | CrossVersion.partialVersion(scalaVersion.value) match { 144 | case Some((2, scalaMajor)) if scalaMajor >= 11 => Seq() 145 | case Some((2, 10)) => 146 | Seq(compilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.patch)) 147 | } 148 | } 149 | ) 150 | 151 | lazy val publishSettings = Seq( 152 | releaseCrossBuild := true, 153 | releasePublishArtifactsAction := PgpKeys.publishSigned.value, 154 | publishMavenStyle := true, 155 | publishArtifact in Test := false, 156 | pomIncludeRepository := { _ => false }, 157 | publishTo := version { (v: String) => 158 | val nexus = "https://oss.sonatype.org/" 159 | if (v.trim.endsWith("SNAPSHOT")) 160 | Some("snapshots" at nexus + "content/repositories/snapshots") 161 | else 162 | Some("releases" at nexus + "service/local/staging/deploy/maven2") 163 | }.value, 164 | homepage := Some(url("http://typelevel.org/")), 165 | licenses := Seq("MIT" -> url("http://www.opensource.org/licenses/mit-license.php")), 166 | scmInfo := 167 | Some(ScmInfo( 168 | url("https://github.com/typelevel/shapeless-contrib"), 169 | "scm:git:git://github.com/typelevel/shapeless-contrib.git" 170 | )), 171 | pomExtra := ( 172 | 173 | 174 | larsrh 175 | Lars Hupel 176 | https://github.com/larsrh 177 | 178 | 179 | ) 180 | ) 181 | 182 | lazy val noPublishSettings = Seq( 183 | publish := (), 184 | publishLocal := (), 185 | publishArtifact := false 186 | ) 187 | 188 | lazy val mimaSettings = mimaDefaultSettings ++ Seq( 189 | previousArtifacts := Set(), // Set(organization.value %% moduleName.value % "2.3.0"), 190 | 191 | binaryIssueFilters ++= { 192 | import com.typesafe.tools.mima.core._ 193 | import com.typesafe.tools.mima.core.ProblemFilters._ 194 | 195 | // Filtering the methods that were added since the checked version 196 | // (these only break forward compatibility, not the backward one) 197 | Seq( 198 | ) 199 | } 200 | ) 201 | 202 | lazy val sharedReleaseProcess = Seq( 203 | releaseProcess := Seq[ReleaseStep]( 204 | checkSnapshotDependencies, 205 | inquireVersions, 206 | runClean, 207 | runTest, 208 | setReleaseVersion, 209 | commitReleaseVersion, 210 | tagRelease, 211 | publishArtifacts, 212 | setNextVersion, 213 | commitNextVersion, 214 | ReleaseStep(action = Command.process("sonatypeReleaseAll", _)), 215 | pushChanges 216 | ) 217 | ) 218 | 219 | credentials ++= (for { 220 | username <- Option(System.getenv().get("SONATYPE_USERNAME")) 221 | password <- Option(System.getenv().get("SONATYPE_PASSWORD")) 222 | } yield Credentials("Sonatype Nexus Repository Manager", "oss.sonatype.org", username, password)).toSeq 223 | -------------------------------------------------------------------------------- /common/src/main/scala/common.scala: -------------------------------------------------------------------------------- 1 | package shapeless.contrib 2 | 3 | import shapeless._ 4 | 5 | trait Product[+C[_], F, T <: HList] { 6 | def F: C[F] 7 | def T: C[T] 8 | 9 | type λ = F :: T 10 | } 11 | 12 | trait Sum[+C[_], L, R <: Coproduct] { 13 | def L: C[L] 14 | def R: C[R] 15 | 16 | type λ = L :+: R 17 | } 18 | 19 | trait Isomorphic[+C[_], A, B] { 20 | def B: C[B] 21 | def to: A => B 22 | def from: B => A 23 | } 24 | 25 | // vim: expandtab:ts=2:sw=2 26 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.13 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | scalacOptions += "-deprecation" 2 | libraryDependencies += "org.slf4j" % "slf4j-nop" % "1.7.15" 3 | 4 | addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.1.8") 5 | addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5") 6 | addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.16") 7 | addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.0") 8 | addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") 9 | addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1") 10 | -------------------------------------------------------------------------------- /scalacheck/src/main/scala/package.scala: -------------------------------------------------------------------------------- 1 | package shapeless.contrib 2 | 3 | import shapeless._ 4 | 5 | import org.scalacheck.{Gen, Arbitrary, Shrink} 6 | 7 | object scalacheck { 8 | object ArbitraryDerivedOrphans extends TypeClassCompanion[Arbitrary] { 9 | object typeClass extends TypeClass[Arbitrary] { 10 | // TODO this is terrible 11 | private lazy val _emptyCoproduct: Gen[Nothing] = Gen.fail 12 | 13 | def emptyProduct = Arbitrary(Gen.const(HNil: HNil)) 14 | 15 | def product[H, T <: HList](h: Arbitrary[H], t: Arbitrary[T]) = 16 | Arbitrary(Gen.sized { size => 17 | if (size == 0) 18 | Gen.fail 19 | else { 20 | val resizedH = Gen.resize(size.abs/2, h.arbitrary) 21 | val resizedT = Gen.resize(size.abs - size.abs/2, t.arbitrary) 22 | for { h <- resizedH; t <- resizedT } 23 | yield h :: t 24 | }}) 25 | 26 | def emptyCoproduct = Arbitrary[CNil](_emptyCoproduct) 27 | 28 | def coproduct[L, R <: Coproduct](l: => Arbitrary[L], r: => Arbitrary[R]) = { 29 | val rGen = r.arbitrary 30 | val gens: List[Gen[L :+: R]] = 31 | l.arbitrary.map(Inl(_): L :+: R) :: 32 | (if (rGen == _emptyCoproduct) Nil else List(rGen.map(Inr(_): L :+: R))) 33 | Arbitrary(Gen.oneOf(gens).flatMap(identity)) 34 | } 35 | 36 | def project[A, B](b: => Arbitrary[B], ab: A => B, ba: B => A) = 37 | Arbitrary(b.arbitrary.map(ba)) 38 | } 39 | } 40 | 41 | object ShrinkDerivedOrphans extends TypeClassCompanion[Shrink] { 42 | object typeClass extends TypeClass[Shrink] { 43 | def emptyProduct = Shrink(_ => Stream.empty) 44 | 45 | def product[F, T <: HList](f: Shrink[F], t: Shrink[T]) = Shrink { case a :: b ⇒ 46 | f.shrink(a).map( _ :: b) append 47 | t.shrink(b).map(a :: _) 48 | } 49 | 50 | def emptyCoproduct: Shrink[CNil] = Shrink(_ => Stream.empty) 51 | 52 | def coproduct[L, R <: Coproduct](sl: => Shrink[L], sr: => Shrink[R]) = Shrink { lr => 53 | lr match { 54 | case Inl(l) ⇒ sl.shrink(l).map(Inl.apply) 55 | case Inr(r) ⇒ sr.shrink(r).map(Inr.apply) 56 | } 57 | } 58 | 59 | def project[A, B](b: => Shrink[B], ab: A => B, ba: B => A) = Shrink { a => 60 | b.shrink(ab(a)).map(ba) 61 | } 62 | } 63 | } 64 | 65 | implicit def deriveArbitrary[T] 66 | (implicit orphan: Orphan[Arbitrary, ArbitraryDerivedOrphans.type, T]): Arbitrary[T] = orphan.instance 67 | 68 | implicit def deriveShrink[T] 69 | (implicit orphan: Orphan[Shrink, ShrinkDerivedOrphans.type, T]): Shrink[T] = orphan.instance 70 | } 71 | -------------------------------------------------------------------------------- /scalacheck/src/test/scala/ShrinkSpec.scala: -------------------------------------------------------------------------------- 1 | package shapeless.contrib.scalachecktests 2 | 3 | import org.scalacheck.{Arbitrary,Gen,Properties,Shrink,Test} 4 | import org.scalacheck.Prop.forAll 5 | 6 | import shapeless.contrib.scalacheck._ 7 | 8 | object ArbitrarySpec extends Properties("Arbitrary") { 9 | private val ok = (_: Any) => true 10 | 11 | sealed trait Tree[A] 12 | case class Node[A](left: Tree[A], right: Tree[A]) extends Tree[A] 13 | case class Leaf[A](item: A) extends Tree[A] 14 | 15 | property("leaf") = { 16 | forAll(implicitly[Arbitrary[Leaf[Int]]].arbitrary)(ok) 17 | } 18 | 19 | property("tree") = { 20 | forAll(implicitly[Arbitrary[Tree[Int]]].arbitrary)(ok) 21 | } 22 | } 23 | 24 | object ShrinkSpec extends Properties("Shrink") { 25 | 26 | case class ShrinkTest(one: String, 27 | two: String) 28 | 29 | private def shrinkClosure[T : Shrink](x: T): Stream[T] = { 30 | val xs = Shrink.shrink[T](x) 31 | if(xs.isEmpty) xs 32 | else xs.append(xs.take(1).map(shrinkClosure[T]).flatten) 33 | } 34 | 35 | val emptyShrinkTest = ShrinkTest("","") 36 | 37 | property("derived shrink") = forAll {(shrinkMe: ShrinkTest) => 38 | shrinkMe == emptyShrinkTest || shrinkClosure(shrinkMe).contains(emptyShrinkTest) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /scripts/dot-sbtrc: -------------------------------------------------------------------------------- 1 | alias boot = ;reload ;project rootJVM ;iflast shell 2 | -------------------------------------------------------------------------------- /scripts/travis-publish.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Example setting to use at command line for testing: 4 | # export TRAVIS_SCALA_VERSION=2.10.5;export TRAVIS_PULL_REQUEST="false";export TRAVIS_BRANCH="master" 5 | 6 | SBT="sbt ++${TRAVIS_SCALA_VERSION}" 7 | 8 | if [[ "${TRAVIS_PULL_REQUEST}" == "false" && 9 | "${TRAVIS_BRANCH}" == "master" && 10 | $(cat version.sbt) =~ "-SNAPSHOT" 11 | ]]; then 12 | PUBLISH=publish 13 | else 14 | PUBLISH=publishLocal 15 | fi 16 | 17 | ${SBT} validateJS 18 | ${SBT} validateJVM 19 | -------------------------------------------------------------------------------- /spire/src/main/scala/package.scala: -------------------------------------------------------------------------------- 1 | package shapeless.contrib 2 | 3 | import _root_.spire.math._ 4 | import _root_.spire.algebra._ 5 | 6 | import shapeless._ 7 | 8 | package object spire { 9 | 10 | // Instances 11 | 12 | object EqDerivedOrphans extends ProductTypeClassCompanion[Eq] { 13 | object typeClass extends ProductTypeClass[Eq] with Empty { 14 | def product[F, T <: HList](f: Eq[F], t: Eq[T]) = 15 | new ProductEq[F, T] { def F = f; def T = t } 16 | def project[A, B](b: => Eq[B], ab: A => B, ba: B => A) = 17 | b on ab 18 | } 19 | } 20 | 21 | object OrderDerivedOrphans extends ProductTypeClassCompanion[Order] { 22 | object typeClass extends ProductTypeClass[Order] with Empty { 23 | def product[F, T <: HList](f: Order[F], t: Order[T]) = 24 | new ProductOrder[F, T] { def F = f; def T = t } 25 | def project[A, B](b: => Order[B], ab: A => B, ba: B => A) = 26 | b on ab 27 | } 28 | } 29 | 30 | object SemigroupDerivedOrphans extends ProductTypeClassCompanion[Semigroup] { 31 | object typeClass extends ProductTypeClass[Semigroup] with Empty { 32 | def product[F, T <: HList](f: Semigroup[F], t: Semigroup[T]) = 33 | new ProductSemigroup[F, T] { def F = f; def T = t } 34 | def project[A, B](b: => Semigroup[B], ab: A => B, ba: B => A) = 35 | new IsomorphicSemigroup[A, B] { def B = b; def to = ab; def from = ba } 36 | } 37 | } 38 | 39 | object MonoidDerivedOrphans extends ProductTypeClassCompanion[Monoid] { 40 | object typeClass extends ProductTypeClass[Monoid] with Empty { 41 | def product[F, T <: HList](f: Monoid[F], t: Monoid[T]) = 42 | new ProductMonoid[F, T] { def F = f; def T = t } 43 | def project[A, B](b: => Monoid[B], ab: A => B, ba: B => A) = 44 | new IsomorphicMonoid[A, B] { def B = b; def to = ab; def from = ba } 45 | } 46 | } 47 | 48 | object GroupDerivedOrphans extends ProductTypeClassCompanion[Group] { 49 | object typeClass extends ProductTypeClass[Group] with Empty { 50 | def product[F, T <: HList](f: Group[F], t: Group[T]) = 51 | new ProductGroup[F, T] { def F = f; def T = t } 52 | def project[A, B](b: => Group[B], ab: A => B, ba: B => A) = 53 | new IsomorphicGroup[A, B] { def B = b; def to = ab; def from = ba } 54 | } 55 | } 56 | 57 | object AbGroupDerivedOrphans extends ProductTypeClassCompanion[AbGroup] { 58 | object typeClass extends ProductTypeClass[AbGroup] with Empty { 59 | def product[F, T <: HList](f: AbGroup[F], t: AbGroup[T]) = 60 | new ProductAbGroup[F, T] { def F = f; def T = t } 61 | def project[A, B](b: => AbGroup[B], ab: A => B, ba: B => A) = 62 | new IsomorphicAbGroup[A, B] { def B = b; def to = ab; def from = ba } 63 | } 64 | } 65 | 66 | object AdditiveSemigroupDerivedOrphans extends ProductTypeClassCompanion[AdditiveSemigroup] { 67 | object typeClass extends ProductTypeClass[AdditiveSemigroup] with Empty { 68 | def product[F, T <: HList](f: AdditiveSemigroup[F], t: AdditiveSemigroup[T]) = 69 | new ProductAdditiveSemigroup[F, T] { def F = f; def T = t } 70 | def project[A, B](b: => AdditiveSemigroup[B], ab: A => B, ba: B => A) = 71 | new IsomorphicAdditiveSemigroup[A, B] { def B = b; def to = ab; def from = ba } 72 | } 73 | } 74 | 75 | object AdditiveMonoidDerivedOrphans extends ProductTypeClassCompanion[AdditiveMonoid] { 76 | object typeClass extends ProductTypeClass[AdditiveMonoid] with Empty { 77 | def product[F, T <: HList](f: AdditiveMonoid[F], t: AdditiveMonoid[T]) = 78 | new ProductAdditiveMonoid[F, T] { def F = f; def T = t } 79 | def project[A, B](b: => AdditiveMonoid[B], ab: A => B, ba: B => A) = 80 | new IsomorphicAdditiveMonoid[A, B] { def B = b; def to = ab; def from = ba } 81 | } 82 | } 83 | 84 | object AdditiveGroupDerivedOrphans extends ProductTypeClassCompanion[AdditiveGroup] { 85 | object typeClass extends ProductTypeClass[AdditiveGroup] with Empty { 86 | def product[F, T <: HList](f: AdditiveGroup[F], t: AdditiveGroup[T]) = 87 | new ProductAdditiveGroup[F, T] { def F = f; def T = t } 88 | def project[A, B](b: => AdditiveGroup[B], ab: A => B, ba: B => A) = 89 | new IsomorphicAdditiveGroup[A, B] { def B = b; def to = ab; def from = ba } 90 | } 91 | } 92 | 93 | object AdditiveAbGroupDerivedOrphans extends ProductTypeClassCompanion[AdditiveAbGroup] { 94 | object typeClass extends ProductTypeClass[AdditiveAbGroup] with Empty { 95 | def product[F, T <: HList](f: AdditiveAbGroup[F], t: AdditiveAbGroup[T]) = 96 | new ProductAdditiveAbGroup[F, T] { def F = f; def T = t } 97 | def project[A, B](b: => AdditiveAbGroup[B], ab: A => B, ba: B => A) = 98 | new IsomorphicAdditiveAbGroup[A, B] { def B = b; def to = ab; def from = ba } 99 | } 100 | } 101 | 102 | object MultiplicativeSemigroupDerivedOrphans extends ProductTypeClassCompanion[MultiplicativeSemigroup] { 103 | object typeClass extends ProductTypeClass[MultiplicativeSemigroup] with Empty { 104 | def product[F, T <: HList](f: MultiplicativeSemigroup[F], t: MultiplicativeSemigroup[T]) = 105 | new ProductMultiplicativeSemigroup[F, T] { def F = f; def T = t } 106 | def project[A, B](b: => MultiplicativeSemigroup[B], ab: A => B, ba: B => A) = 107 | new IsomorphicMultiplicativeSemigroup[A, B] { def B = b; def to = ab; def from = ba } 108 | } 109 | } 110 | 111 | object MultiplicativeMonoidDerivedOrphans extends ProductTypeClassCompanion[MultiplicativeMonoid] { 112 | object typeClass extends ProductTypeClass[MultiplicativeMonoid] with Empty { 113 | def product[F, T <: HList](f: MultiplicativeMonoid[F], t: MultiplicativeMonoid[T]) = 114 | new ProductMultiplicativeMonoid[F, T] { def F = f; def T = t } 115 | def project[A, B](b: => MultiplicativeMonoid[B], ab: A => B, ba: B => A) = 116 | new IsomorphicMultiplicativeMonoid[A, B] { def B = b; def to = ab; def from = ba } 117 | } 118 | } 119 | 120 | object MultiplicativeGroupDerivedOrphans extends ProductTypeClassCompanion[MultiplicativeGroup] { 121 | object typeClass extends ProductTypeClass[MultiplicativeGroup] with Empty { 122 | def product[F, T <: HList](f: MultiplicativeGroup[F], t: MultiplicativeGroup[T]) = 123 | new ProductMultiplicativeGroup[F, T] { def F = f; def T = t } 124 | def project[A, B](b: => MultiplicativeGroup[B], ab: A => B, ba: B => A) = 125 | new IsomorphicMultiplicativeGroup[A, B] { def B = b; def to = ab; def from = ba } 126 | } 127 | } 128 | 129 | object MultiplicativeAbGroupDerivedOrphans extends ProductTypeClassCompanion[MultiplicativeAbGroup] { 130 | object typeClass extends ProductTypeClass[MultiplicativeAbGroup] with Empty { 131 | def product[F, T <: HList](f: MultiplicativeAbGroup[F], t: MultiplicativeAbGroup[T]) = 132 | new ProductMultiplicativeAbGroup[F, T] { def F = f; def T = t } 133 | def project[A, B](b: => MultiplicativeAbGroup[B], ab: A => B, ba: B => A) = 134 | new IsomorphicMultiplicativeAbGroup[A, B] { def B = b; def to = ab; def from = ba } 135 | } 136 | } 137 | 138 | implicit def deriveEq[T] 139 | (implicit orphan: Orphan[Eq, EqDerivedOrphans.type, T]): Eq[T] = orphan.instance 140 | 141 | implicit def deriveOrder[T] 142 | (implicit orphan: Orphan[Order, OrderDerivedOrphans.type, T]): Order[T] = orphan.instance 143 | 144 | implicit def deriveSemigroup[T] 145 | (implicit orphan: Orphan[Semigroup, SemigroupDerivedOrphans.type, T]): Semigroup[T] = orphan.instance 146 | 147 | implicit def deriveMonoid[T] 148 | (implicit orphan: Orphan[Monoid, MonoidDerivedOrphans.type, T]): Monoid[T] = orphan.instance 149 | 150 | implicit def deriveGroup[T] 151 | (implicit orphan: Orphan[Group, GroupDerivedOrphans.type, T]): Group[T] = orphan.instance 152 | 153 | implicit def deriveAbGroup[T] 154 | (implicit orphan: Orphan[AbGroup, AbGroupDerivedOrphans.type, T]): AbGroup[T] = orphan.instance 155 | 156 | implicit def deriveAdditiveSemigroup[T] 157 | (implicit orphan: Orphan[AdditiveSemigroup, AdditiveSemigroupDerivedOrphans.type, T]): AdditiveSemigroup[T] = 158 | orphan.instance 159 | 160 | implicit def deriveAdditiveMonoid[T] 161 | (implicit orphan: Orphan[AdditiveMonoid, AdditiveMonoidDerivedOrphans.type, T]): AdditiveMonoid[T] = 162 | orphan.instance 163 | 164 | implicit def deriveAdditiveGroup[T] 165 | (implicit orphan: Orphan[AdditiveGroup, AdditiveGroupDerivedOrphans.type, T]): AdditiveGroup[T] = 166 | orphan.instance 167 | 168 | implicit def deriveAdditiveAbGroup[T] 169 | (implicit orphan: Orphan[AdditiveAbGroup, AdditiveAbGroupDerivedOrphans.type, T]): AdditiveAbGroup[T] = 170 | orphan.instance 171 | 172 | implicit def deriveMultiplicativeSemigroup[T] 173 | (implicit orphan: Orphan[MultiplicativeSemigroup, MultiplicativeSemigroupDerivedOrphans.type, T]): 174 | MultiplicativeSemigroup[T] = orphan.instance 175 | 176 | implicit def deriveMultiplicativeMonoid[T] 177 | (implicit orphan: Orphan[MultiplicativeMonoid, MultiplicativeMonoidDerivedOrphans.type, T]): 178 | MultiplicativeMonoid[T] = orphan.instance 179 | 180 | implicit def deriveMultiplicativeGroup[T] 181 | (implicit orphan: Orphan[MultiplicativeGroup, MultiplicativeGroupDerivedOrphans.type, T]): 182 | MultiplicativeGroup[T] = orphan.instance 183 | 184 | implicit def deriveMultiplicativeAbGroup[T] 185 | (implicit orphan: Orphan[MultiplicativeAbGroup, MultiplicativeAbGroupDerivedOrphans.type, T]): 186 | MultiplicativeAbGroup[T] = orphan.instance 187 | } 188 | -------------------------------------------------------------------------------- /spire/src/main/scala/typeclass.scala: -------------------------------------------------------------------------------- 1 | package shapeless.contrib.spire 2 | 3 | import spire.math._ 4 | import spire.algebra._ 5 | 6 | import shapeless._ 7 | import shapeless.contrib._ 8 | 9 | trait Empty { 10 | 11 | def emptyProduct = new Order[HNil] with AbGroup[HNil] with AdditiveAbGroup[HNil] with MultiplicativeAbGroup[HNil] { 12 | override def eqv(x: HNil, y: HNil) = true 13 | override def neqv(x: HNil, y: HNil) = false 14 | def compare(x: HNil, y: HNil) = 0 15 | def op(x: HNil, y: HNil) = HNil 16 | def id = HNil 17 | def inverse(a: HNil) = HNil 18 | def plus(x: HNil, y: HNil) = HNil 19 | def zero = HNil 20 | def negate(x: HNil) = HNil 21 | override def minus(x: HNil, y: HNil) = HNil 22 | override def additive: AbGroup[HNil] = this 23 | def times(x: HNil, y: HNil) = HNil 24 | def one = HNil 25 | override def reciprocal(x: HNil) = HNil 26 | def div(x: HNil, y: HNil) = HNil 27 | override def multiplicative: AbGroup[HNil] = this 28 | } 29 | 30 | } 31 | 32 | 33 | // Products 34 | 35 | trait ProductEq[F, T <: HList] 36 | extends Eq[F :: T] 37 | with Product[Eq, F, T] { 38 | 39 | def eqv(x: λ, y: λ) = 40 | F.eqv(x.head, y.head) && T.eqv(x.tail, y.tail) 41 | 42 | override def neqv(x: λ, y: λ) = 43 | F.neqv(x.head, y.head) || T.neqv(x.tail, y.tail) 44 | 45 | } 46 | 47 | trait ProductOrder[F, T <: HList] 48 | extends ProductEq[F, T] 49 | with Order[F :: T] 50 | with Product[Order, F, T] { 51 | 52 | override def eqv(x: λ, y: λ) = 53 | super[ProductEq].eqv(x, y) 54 | 55 | def compare(x: λ, y: λ) = { 56 | val headOrder = F.compare(x.head, y.head) 57 | if (headOrder < 0) 58 | headOrder 59 | else 60 | T.compare(x.tail, y.tail) 61 | } 62 | 63 | } 64 | 65 | trait ProductSemigroup[F, T <: HList] 66 | extends Semigroup[F :: T] 67 | with Product[Semigroup, F, T] { 68 | 69 | def op(x: λ, y: λ) = 70 | F.op(x.head, y.head) :: T.op(x.tail, y.tail) 71 | 72 | } 73 | 74 | trait ProductMonoid[F, T <: HList] 75 | extends ProductSemigroup[F, T] 76 | with Monoid[F :: T] 77 | with Product[Monoid, F, T] { 78 | 79 | def id = F.id :: T.id 80 | 81 | } 82 | 83 | trait ProductGroup[F, T <: HList] 84 | extends ProductMonoid[F, T] 85 | with Group[F :: T] 86 | with Product[Group, F, T] { 87 | 88 | def inverse(a: λ) = 89 | F.inverse(a.head) :: T.inverse(a.tail) 90 | 91 | } 92 | 93 | trait ProductAbGroup[F, T <: HList] 94 | extends ProductGroup[F, T] 95 | with AbGroup[F :: T] 96 | 97 | trait ProductAdditiveSemigroup[F, T <: HList] 98 | extends AdditiveSemigroup[F :: T] 99 | with Product[AdditiveSemigroup, F, T] { 100 | 101 | def plus(x: λ, y: λ) = 102 | F.plus(x.head, y.head) :: T.plus(x.tail, y.tail) 103 | 104 | } 105 | 106 | trait ProductAdditiveMonoid[F, T <: HList] 107 | extends ProductAdditiveSemigroup[F, T] 108 | with AdditiveMonoid[F :: T] 109 | with Product[AdditiveMonoid, F, T] { 110 | 111 | def zero = F.zero :: T.zero 112 | 113 | } 114 | 115 | trait ProductAdditiveGroup[F, T <: HList] 116 | extends ProductAdditiveMonoid[F, T] 117 | with AdditiveGroup[F :: T] 118 | with Product[AdditiveGroup, F, T] { 119 | 120 | def negate(a: λ) = 121 | F.negate(a.head) :: T.negate(a.tail) 122 | 123 | override def minus(x: λ, y: λ) = 124 | F.minus(x.head, y.head) :: T.minus(x.tail, y.tail) 125 | 126 | } 127 | 128 | trait ProductAdditiveAbGroup[F, T <: HList] 129 | extends ProductAdditiveGroup[F, T] 130 | with AdditiveAbGroup[F :: T] 131 | 132 | trait ProductMultiplicativeSemigroup[F, T <: HList] 133 | extends MultiplicativeSemigroup[F :: T] 134 | with Product[MultiplicativeSemigroup, F, T] { 135 | 136 | def times(x: λ, y: λ) = 137 | F.times(x.head, y.head) :: T.times(x.tail, y.tail) 138 | 139 | } 140 | 141 | trait ProductMultiplicativeMonoid[F, T <: HList] 142 | extends ProductMultiplicativeSemigroup[F, T] 143 | with MultiplicativeMonoid[F :: T] 144 | with Product[MultiplicativeMonoid, F, T] { 145 | 146 | def one = F.one :: T.one 147 | 148 | } 149 | 150 | trait ProductMultiplicativeGroup[F, T <: HList] 151 | extends ProductMultiplicativeMonoid[F, T] 152 | with MultiplicativeGroup[F :: T] 153 | with Product[MultiplicativeGroup, F, T] { 154 | 155 | override def reciprocal(a: λ) = 156 | F.reciprocal(a.head) :: T.reciprocal(a.tail) 157 | 158 | def div(x: λ, y: λ) = 159 | F.div(x.head, y.head) :: T.div(x.tail, y.tail) 160 | 161 | } 162 | 163 | trait ProductMultiplicativeAbGroup[F, T <: HList] 164 | extends ProductMultiplicativeGroup[F, T] 165 | with MultiplicativeAbGroup[F :: T] 166 | 167 | 168 | // Isos 169 | 170 | trait IsomorphicSemigroup[A, B] 171 | extends Semigroup[A] 172 | with Isomorphic[Semigroup, A, B] { 173 | 174 | def op(x: A, y: A) = 175 | from(B.op(to(x), to(y))) 176 | 177 | } 178 | 179 | trait IsomorphicMonoid[A, B] 180 | extends IsomorphicSemigroup[A, B] 181 | with Monoid[A] 182 | with Isomorphic[Monoid, A, B] { 183 | 184 | def id = from(B.id) 185 | 186 | } 187 | 188 | trait IsomorphicGroup[A, B] 189 | extends IsomorphicMonoid[A, B] 190 | with Group[A] 191 | with Isomorphic[Group, A, B] { 192 | 193 | def inverse(a: A) = 194 | from(B.inverse(to(a))) 195 | 196 | } 197 | 198 | trait IsomorphicAbGroup[A, B] 199 | extends IsomorphicGroup[A, B] 200 | with AbGroup[A] 201 | 202 | trait IsomorphicAdditiveSemigroup[A, B] 203 | extends AdditiveSemigroup[A] 204 | with Isomorphic[AdditiveSemigroup, A, B] { 205 | 206 | def plus(x: A, y: A) = 207 | from(B.plus(to(x), to(y))) 208 | 209 | } 210 | 211 | trait IsomorphicAdditiveMonoid[A, B] 212 | extends IsomorphicAdditiveSemigroup[A, B] 213 | with AdditiveMonoid[A] 214 | with Isomorphic[AdditiveMonoid, A, B] { 215 | 216 | def zero = from(B.zero) 217 | 218 | } 219 | 220 | trait IsomorphicAdditiveGroup[A, B] 221 | extends IsomorphicAdditiveMonoid[A, B] 222 | with AdditiveGroup[A] 223 | with Isomorphic[AdditiveGroup, A, B] { 224 | 225 | def negate(a: A) = 226 | from(B.negate(to(a))) 227 | 228 | override def minus(x: A, y: A) = 229 | from(B.minus(to(x), to(y))) 230 | 231 | } 232 | 233 | trait IsomorphicAdditiveAbGroup[A, B] 234 | extends IsomorphicAdditiveGroup[A, B] 235 | with AdditiveAbGroup[A] 236 | 237 | trait IsomorphicMultiplicativeSemigroup[A, B] 238 | extends MultiplicativeSemigroup[A] 239 | with Isomorphic[MultiplicativeSemigroup, A, B] { 240 | 241 | def times(x: A, y: A) = 242 | from(B.times(to(x), to(y))) 243 | 244 | } 245 | 246 | trait IsomorphicMultiplicativeMonoid[A, B] 247 | extends IsomorphicMultiplicativeSemigroup[A, B] 248 | with MultiplicativeMonoid[A] 249 | with Isomorphic[MultiplicativeMonoid, A, B] { 250 | 251 | def one = from(B.one) 252 | 253 | } 254 | 255 | trait IsomorphicMultiplicativeGroup[A, B] 256 | extends IsomorphicMultiplicativeMonoid[A, B] 257 | with MultiplicativeGroup[A] 258 | with Isomorphic[MultiplicativeGroup, A, B] { 259 | 260 | override def reciprocal(a: A) = 261 | from(B.reciprocal(to(a))) 262 | 263 | def div(x: A, y: A) = 264 | from(B.div(to(x), to(y))) 265 | 266 | } 267 | 268 | trait IsomorphicMultiplicativeAbGroup[A, B] 269 | extends IsomorphicMultiplicativeGroup[A, B] 270 | with MultiplicativeAbGroup[A] 271 | 272 | // vim: expandtab:ts=2:sw=2 273 | -------------------------------------------------------------------------------- /spire/src/test/scala/ProductTest.scala: -------------------------------------------------------------------------------- 1 | package shapeless.contrib.spire 2 | 3 | import shapeless.contrib.scalacheck._ 4 | 5 | import spire.algebra._ 6 | import spire.std.int._ 7 | import spire.std.long._ 8 | import spire.laws._ 9 | 10 | import org.typelevel.discipline.scalatest.Discipline 11 | 12 | import org.scalatest.FunSuite 13 | 14 | class ProductTest extends FunSuite with Discipline { 15 | 16 | case class OneElem(n: Int) 17 | 18 | checkAll("one element", GroupLaws[OneElem].additiveAbGroup) 19 | checkAll("one element", RingLaws[OneElem].multiplicativeSemigroup) 20 | 21 | case class TwoElem(n: Int, m: Long) 22 | 23 | checkAll("two elements", GroupLaws[TwoElem].additiveAbGroup) 24 | checkAll("two elements", RingLaws[OneElem].multiplicativeSemigroup) 25 | 26 | } 27 | -------------------------------------------------------------------------------- /version.sbt: -------------------------------------------------------------------------------- 1 | version in ThisBuild := "0.7-SNAPSHOT" --------------------------------------------------------------------------------