├── project ├── build.properties └── plugins.sbt ├── .gitignore ├── sonatype.sbt ├── src ├── main │ └── scala │ │ └── yausl │ │ ├── default.scala │ │ ├── package.scala │ │ ├── representation.scala │ │ ├── generators.scala │ │ ├── types.scala │ │ └── scalar.scala └── test │ └── scala │ └── YauslTests.scala ├── README.md └── LICENSE /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 0.13.8 -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | logLevel := Level.Warn 2 | 3 | addSbtPlugin("de.heikoseeberger" % "sbt-header" % "1.5.0") 4 | 5 | addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "0.4.0") 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | 4 | # sbt specific 5 | .cache 6 | .history 7 | .lib/ 8 | dist/* 9 | target/ 10 | lib_managed/ 11 | src_managed/ 12 | project/boot/ 13 | project/plugins/project/ 14 | 15 | # Scala-IDE specific 16 | .scala_dependencies 17 | .worksheet 18 | .idea -------------------------------------------------------------------------------- /sonatype.sbt: -------------------------------------------------------------------------------- 1 | Sonatype.sonatypeSettings 2 | 3 | sonatypeProfileName := "baccata" 4 | 5 | publishMavenStyle := true 6 | 7 | publishTo := { 8 | val nexus = "https://oss.sonatype.org/" 9 | if (isSnapshot.value) 10 | Some("snapshots" at nexus + "content/repositories/snapshots") 11 | else 12 | Some("releases" at nexus + "service/local/staging/deploy/maven2") 13 | } 14 | 15 | pomIncludeRepository := { _ => false } 16 | 17 | // To sync with Maven central, you need to supply the following information: 18 | pomExtra := { 19 | https://github.com/baccata/yausl 20 | 21 | 22 | Apache 2 23 | http://www.apache.org/licenses/LICENSE-2.0.txt 24 | 25 | 26 | 27 | git@github.com:baccata/yausl.git 28 | scm:git:git@github.com:baccata/yausl.git 29 | 30 | 31 | 32 | baccata 33 | Olivier Mélois 34 | http://baccata.github.io 35 | 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/main/scala/yausl/default.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Olivier Mélois 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package yausl 18 | 19 | object default { 20 | trait Time extends BaseQuantity 21 | trait Length extends BaseQuantity 22 | trait Mass extends BaseQuantity 23 | trait Temperature extends BaseQuantity 24 | trait AmountOfSubstance extends BaseQuantity 25 | trait Current extends BaseQuantity 26 | trait Intensity extends BaseQuantity 27 | 28 | trait metre extends UnitM[Length] 29 | trait second extends UnitM[Time] 30 | trait kilogram extends UnitM[Mass] 31 | trait kelvin extends UnitM[Temperature] 32 | trait mole extends UnitM[AmountOfSubstance] 33 | trait ampere extends UnitM[Current] 34 | trait candela extends UnitM[Intensity] 35 | } 36 | -------------------------------------------------------------------------------- /src/main/scala/yausl/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Olivier Mélois 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package object yausl { 18 | 19 | type +[L <: Integer, R <: Integer] = L#add[R] 20 | type -[L <: Integer, R <: Integer] = L#sub[R] 21 | type *[L <: Integer, R <: Integer] = L#prod[R] 22 | type /[L <: Integer, R <: NonZeroInt] = L#div[R] 23 | type <[L <: Integer, R <: Integer] = L#lt[R] 24 | type <=[L <: Integer, R <: Integer] = L#lteq[R] 25 | type >[L <: Integer, R <: Integer] = R#lt[L] 26 | type >=[L <: Integer, R <: Integer] = R#lteq[L] 27 | type ==[L <: Integer, R <: Integer] = L#eq[R] 28 | type min[L <: Integer, R <: Integer] = (L < R)#branch[Integer, L, R] 29 | type minp[L <: NonNegInt, R <: NonNegInt] = (L < R)#branch[NonNegInt, L, R] 30 | 31 | type p1 = _0#succ 32 | type p2 = p1#succ 33 | type p3 = p2#succ 34 | type p4 = p3#succ 35 | type p5 = p4#succ 36 | type p6 = p5#succ 37 | type p7 = p6#succ 38 | type p8 = p7#succ 39 | type p9 = p8#succ 40 | 41 | type n1 = _0#pred 42 | type n2 = n1#pred 43 | type n3 = n2#pred 44 | type n4 = n3#pred 45 | type n5 = n4#pred 46 | type n6 = n5#pred 47 | type n7 = n6#pred 48 | type n8 = n7#pred 49 | type n9 = n8#pred 50 | 51 | // BoolOps: 52 | type ||[L <: Bool, R <: Bool] = L#or[R] 53 | type &&[L <: Bool, R <: Bool] = L#and[R] 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/scala/yausl/representation.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Olivier Mélois 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package yausl 18 | 19 | import shapeless.{::, HNil, HList} 20 | 21 | /** 22 | * Simple typeclass giving a name/description/symbol to a type. 23 | */ 24 | trait Show[T] { 25 | def apply() : String 26 | } 27 | 28 | object Show { 29 | implicit def show0[I <: Integer](implicit ev : ToInt[I]) : Show[I] = new Show[I]{def apply() = ev().toString} 30 | implicit def show1[Units <: HList, Dims <: HList](implicit unitL : ToList[Units, String], dimL : ToList[Dims, Int]) 31 | : Show[Scalar[Units, Dims]] = new Show[Scalar[Units, Dims]] { 32 | def apply(): String = (unitL() zip dimL()) 33 | .filter(_._2 != 0) 34 | .map(x => x._1 + (if (x._2 == 1) "" else "^" + x._2)).mkString(".") 35 | } 36 | def apply[A](a : A)(implicit ev : Show[A]) = ev() 37 | } 38 | 39 | /** 40 | * Typeclass that unlifts a typelevel list to a list of something... 41 | */ 42 | trait ToList[L <: HList, Out]{ 43 | def apply() : List[Out] 44 | } 45 | 46 | object ToList { 47 | import scala.reflect.runtime.universe._ 48 | implicit def toList0[Out] : ToList[HNil, Out] = new ToList[HNil, Out] {def apply() = Nil} 49 | implicit def toList1[H <: Integer, T <: HList](implicit toInt : ToInt[H], toList : ToList[T, Int]) 50 | : ToList[H :: T, Int] = new ToList[H :: T, Int]{def apply() = toInt() :: toList()} 51 | implicit def toList2[H <: UnitM[_], T <: HList](implicit tag : TypeTag[H], toList :ToList[T, String]) 52 | : ToList[H :: T, String] = new ToList[H :: T, String]{def apply() = tag.tpe.typeSymbol.name.toString :: toList()} 53 | } 54 | 55 | /** 56 | * Unlifts a typelevel Integer to a value-level Int. 57 | */ 58 | trait ToInt[I <: Integer]{ 59 | def apply() : Int 60 | } 61 | 62 | object ToInt { 63 | implicit val toInt0 = new ToInt[_0]{ def apply() = 0} 64 | implicit def toIntPos[N <: NonNegInt](implicit toInt: ToInt[N]) : ToInt[++[N]] 65 | = new ToInt[++[N]]{ def apply() = toInt() + 1} 66 | implicit def toIntNeg[N <: NonPosInt](implicit toInt: ToInt[N]) : ToInt[--[N]] 67 | = new ToInt[--[N]]{ def apply() = toInt() - 1} 68 | } 69 | -------------------------------------------------------------------------------- /src/test/scala/YauslTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Olivier Mélois 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package yausl 17 | 18 | import java.util.Calendar 19 | 20 | import org.specs2._ 21 | import org.specs2.matcher.{Expectable, Matcher} 22 | import shapeless._ 23 | import shapeless.test.illTyped 24 | import yausl.default.{second, metre} 25 | 26 | /** 27 | * Typeclass that checks the type equality of two types and that unlifts the result 28 | * to a runtime value. 29 | */ 30 | trait TypeEqualityCheck[A, B]{ 31 | def check : Boolean 32 | } 33 | object TypeEqualityCheck { 34 | def apply[A, B](a : A, b : B)(implicit test : TypeEqualityCheck[A, B]) = test.check 35 | 36 | implicit def tec0[A, B](implicit ev : A =:= B) 37 | : TypeEqualityCheck[A, B] = new TypeEqualityCheck[A, B]{def check = true} 38 | 39 | implicit def tec1[A, B](implicit ev : A =:!= B) 40 | : TypeEqualityCheck[A, B] = new TypeEqualityCheck[A, B]{def check = false} 41 | } 42 | 43 | class YauslUsabilitySpec extends Specification { 44 | 45 | import yausl.default._ 46 | import scala.language.reflectiveCalls 47 | 48 | def is = s2""" 49 | 50 | The usability requirements for yausl are the following : 51 | REQ_YAUSL_USE_001 : systems of units should be creatable from a HList $e1 52 | REQ_YAUSL_USE_002 : "natural constructors" should be derived from the HList $e2 53 | REQ_YAUSL_USE_003 : summing values of different dimensions should not compile $e3 54 | REQ_YAUSL_USE_004 : summing values of equal dimensions should compile and work $e4 55 | REQ_YAUSL_USE_005 : multiplication should be commutative(type-wise and value-wise) $e5 56 | REQ_YAUSL_USE_006 : values should be printable with their types $e6 57 | REQ_YAUSL_USE_007 : Scalar multiplicative operators should work with doubles $e7 58 | REQ_YAUSL_USE_008 : doubles should be usable in prefix position of scalar operators $e8 59 | """ 60 | 61 | val system = SystemGenerator.fromHList[metre :: second :: HNil] 62 | import system._ 63 | 64 | def e1 = system must not be_==null 65 | 66 | def e2 = { 67 | TypeEqualityCheck(1 metre, 1 second) must beFalse 68 | } 69 | 70 | def e3 = { 71 | illTyped("(1 metre) + (1 second)") 72 | ok 73 | } 74 | 75 | val a = (5 metre) * (3 second) 76 | val b = (3 second) * (5 metre) 77 | 78 | def e4 = { 79 | (a + b).value must be equalTo(30) 80 | } 81 | 82 | def e5 = { 83 | TypeEqualityCheck(a, b) must beTrue 84 | } and { 85 | a.value must be equalTo(15) 86 | } and { 87 | b.value must be equalTo(15) 88 | } 89 | 90 | def e6 = { 91 | ((1 metre) / (1 second)).show must be equalTo "1.0 metre.second^-1" 92 | } 93 | 94 | def e7 = { 95 | ((1 metre) * 2) must be equalTo (2.scalar * (1 metre)) 96 | } and { 97 | ((3 metre) / 2) must be equalTo (1.5 metre) 98 | } 99 | 100 | def e8 = { 101 | ((1 second) / (1 metre)) must be equalTo (1.scalar / ((1 metre) / (1 second))) 102 | } 103 | } 104 | 105 | class YauslPerformanceSpec extends Specification { 106 | import scala.language.reflectiveCalls 107 | 108 | def is = s2""" 109 | 110 | The runtime performance requirements for yausl are the following : 111 | REQ_YAUSL_PERF_001 : performances of yausl Scalar instances should be similar 112 | to the ones of unboxed doubles $e1 113 | """ 114 | 115 | val system = SystemGenerator.fromHList[metre :: second :: HNil] 116 | import system._ 117 | 118 | val a = 1.metre 119 | var varA = a 120 | val b : Double = 1 121 | var varB = b 122 | 123 | val scalarThen = Calendar.getInstance().getTime().getTime 124 | for (x <- 1 to 1000000000){varA = varA + a} 125 | var scalarNow = Calendar.getInstance().getTime().getTime 126 | 127 | val doubleThen = Calendar.getInstance().getTime().getTime 128 | for (x <- 1 to 1000000000){varB = varB + b} 129 | val doubleNow = Calendar.getInstance().getTime().getTime 130 | 131 | val ratio : Double = ((scalarNow - scalarThen) / (doubleNow - doubleThen)) 132 | val finalValue = varA.value 133 | 134 | def e1 = {ratio should beCloseTo(1, 0.1)} and {finalValue should beEqualTo(varB)} 135 | } -------------------------------------------------------------------------------- /src/main/scala/yausl/generators.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Olivier Mélois 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package yausl 18 | 19 | import shapeless.HList.ListCompat._ 20 | import shapeless.LUBConstraint.<<: 21 | import shapeless.{CaseClassMacros, LUBConstraint, HList} 22 | 23 | import scala.annotation.StaticAnnotation 24 | import scala.language.experimental.macros 25 | import scala.reflect.macros.whitebox 26 | import scala.reflect.macros.whitebox.Context 27 | 28 | object SystemGenerator { 29 | 30 | 31 | /** 32 | * A helper method that reads code from the "body" and uses it at macro expansion. 33 | * In our case it circumvents the reflective calls usually made by macro-generated types 34 | * (which are structural types). Not only does this reduces the performance loss due to 35 | * reflection, but creating instances of value classes cannot be performed by reflective 36 | * calls, because these instances are not boxes (the values are not boxed). 37 | * 38 | * This trick is called "vampire methods". 39 | */ 40 | def method_impl[Units <: HList, Dims <: HList](c: whitebox.Context) : c.Expr[yausl.Scalar[Units, Dims]] = { 41 | import c.universe._ 42 | import org.scalamacros.resetallattrs._ 43 | 44 | val prefixVal = TermName(c.freshName()) 45 | val prefixVal2 = TermName(c.freshName()) 46 | 47 | object replaceThises extends Transformer { 48 | override def transform(tree: Tree) = tree match { 49 | case This(qual) if qual.decodedName.toString.startsWith("System") => Ident(prefixVal) 50 | case This(qual) if qual.decodedName.toString.startsWith("unitExtension") => Ident(prefixVal2) 51 | case other => super.transform(other) 52 | } 53 | } 54 | 55 | val body = c.macroApplication.symbol.annotations.find( 56 | _.tree.tpe <:< typeOf[body] 57 | ).flatMap { x => 58 | x.tree.children.tail.collectFirst { 59 | case body : Tree => c.resetAllAttrs(body) 60 | } 61 | }.getOrElse(c.abort(c.enclosingPosition, "Annotation body not provided!")) 62 | 63 | c.Expr( 64 | q""" 65 | val $prefixVal = ${c.prefix} 66 | val $prefixVal2 = ${c.prefix} 67 | ${replaceThises.transform(body)} 68 | """ 69 | ) 70 | } 71 | 72 | def fromHList[Units <: HList](implicit ev: LUBConstraint[Units, UnitM[_]]): System[Units] = 73 | macro SystemMacros.fromHListImpl[Units] 74 | 75 | class SystemMacros(val c: whitebox.Context) extends CaseClassMacros { 76 | 77 | /** 78 | * This macro definition generates a type that is not vampired. Therefore, its use will 79 | * trigger a warning at compile time. 80 | */ 81 | def fromHListImpl[Units <: HList : c.WeakTypeTag](ev: c.Expr[LUBConstraint[Units, UnitM[_]]]) = { 82 | import c.universe._ 83 | val tpe = c.weakTypeOf[Units] 84 | val integer_1 = c.weakTypeOf[p1] 85 | val integer_0 = c.weakTypeOf[_0] 86 | val units = hlistElements(tpe) 87 | 88 | def dimsOf(t : Type) = mkHListTpe(units map {u => if (u == t) integer_1 else integer_0}) 89 | 90 | val className = TypeName(c.freshName("System")) 91 | val extensionName = TypeName(c.freshName("unitExtension")) 92 | 93 | c.Expr[System[Units]]( 94 | 95 | q""" 96 | class $className extends yausl.System[$tpe] { 97 | 98 | implicit class $extensionName(val value : Double){ 99 | ..${ units.map { t => 100 | val name = t.typeSymbol.name.toString 101 | val dims = dimsOf(t) 102 | q""" 103 | @yausl.body(yausl.System.scalar[$tpe, $dims](value)) 104 | def ${TermName(name)} : yausl.Scalar[$tpe, $dims] 105 | = macro SystemGenerator.method_impl[$tpe, $dims] 106 | """ 107 | } 108 | } 109 | } 110 | 111 | } 112 | new $className {} 113 | """ 114 | ) 115 | } 116 | } 117 | 118 | } 119 | 120 | class body(tree: Any) extends StaticAnnotation 121 | 122 | /** 123 | * A system is a list of accepted units. 124 | */ 125 | trait System[Units <: HList] { 126 | 127 | def measure[T <: UnitM[_]](value: Double)(implicit dimOf: DimensionsOf[Units, T]) 128 | : Scalar[Units, dimOf.result] = new Scalar[Units, dimOf.result](value) 129 | 130 | /** 131 | * This conversion allows to use a double in prefix position of a Scalar operator, by converting it to a 132 | * dimensionless Scalar. 133 | */ 134 | implicit class toScalarExtension(value : Double) { 135 | def scalar(implicit zeros : Zeros[Units]) : Scalar[Units, zeros.result] 136 | = new Scalar[Units, zeros.result](value) 137 | } 138 | 139 | 140 | } 141 | 142 | /** 143 | * This object is mearly used to keep the Scalar constructor protected. The measure method is 144 | * by the vampire methods that are generated by the fromHList macro, thus allowing the user 145 | * to write things like "3 meter / 2 second" 146 | */ 147 | object System { 148 | def measure[Units <: HList, T <: UnitM[_]](value: Double)(implicit dimOf: DimensionsOf[Units, T]) 149 | : Scalar[Units, dimOf.result] = new Scalar[Units, dimOf.result](value) 150 | 151 | def scalar[Units <: HList, Dims <: HList](value : Double) 152 | (implicit lub1 : LUBConstraint[Units, UnitM[_]], 153 | lub2 : LUBConstraint[Dims, Integer], 154 | sameSize : SameSize[Units, Dims]) 155 | : Scalar[Units, Dims] = new Scalar[Units, Dims](value) 156 | } 157 | -------------------------------------------------------------------------------- /src/main/scala/yausl/types.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Olivier Mélois 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package yausl 18 | 19 | /** 20 | * Typelevel representation of booleans. 21 | * 22 | * Copied from Grant Beaty's Scunits 23 | */ 24 | trait Bool { 25 | type branch[B,T <: B, E <: B] <: B // typelevel if-then-else 26 | type not <: Bool 27 | type or[R <: Bool] <: Bool 28 | type and[R <: Bool] <: Bool 29 | type Xor[R <: Bool] <: Bool 30 | } 31 | /** 32 | * Implementation of the typelevel true. 33 | */ 34 | trait True extends Bool { 35 | type not = False 36 | type branch[B,T <: B, E <: B] = T 37 | type or[R <: Bool] = True 38 | type and[R <: Bool] = R 39 | type Xor[R <: Bool] = R#not 40 | } 41 | /** 42 | * Implementation of the typelevel false. 43 | */ 44 | trait False extends Bool { 45 | type not = True 46 | type branch[B,T <: B, E <: B] = E 47 | type or[R <: Bool] = R 48 | type and[R <: Bool] = False 49 | type Xor[R <: Bool] = R 50 | } 51 | 52 | /** 53 | * Typelevel representation of integers, with several arithmetic operators. These allows to write 54 | * things like "p1 + p3" in type parameters, which is imho more readable than an implementation through 55 | * implicits 56 | * 57 | * Inspired from Grant Beaty's Scunits 58 | */ 59 | sealed trait Integer { 60 | type N <: Integer 61 | type isZero <: Bool 62 | type isPos <: Bool 63 | type isNeg <: Bool 64 | type abs <: NonNegInt 65 | type sameSign[R <: Integer] = (isPos && R#isPos) || (isNeg && R#isNeg) 66 | 67 | type succ <: Integer 68 | type pred <: Integer 69 | 70 | type add[N <: Integer] <: Integer 71 | type sub[N <: Integer] <: Integer 72 | type prod[N <: Integer] <: Integer 73 | type div[N <: NonZeroInt] <: Integer 74 | 75 | type neg <: Integer 76 | 77 | type loop[B,F[_ <: B] <: B, Res <: B] <: B 78 | 79 | type lt[N <: Integer] = sub[N]#isNeg 80 | 81 | type lteq[N <: Integer] = ({ 82 | type minus = sub[N] 83 | type res = minus#isNeg || minus#isZero 84 | })#res 85 | 86 | type eq[N <: Integer] = sub[N]#isZero 87 | } 88 | 89 | /** 90 | * Refinement of integers that matches the [0, +infinity[ subset. 91 | */ 92 | sealed trait NonNegInt extends Integer { 93 | type N <: NonNegInt 94 | type abs = N 95 | type isNeg = False 96 | type succ <: PosInt 97 | type neg <: NonPosInt 98 | type predNat <: NonNegInt 99 | type addNat[R <: NonNegInt] <: NonNegInt 100 | type subNat[R <: NonNegInt] <: NonNegInt 101 | type divNat[D <: PosInt] <: NonNegInt 102 | } 103 | /** 104 | * Refinement of integers that matches the ]-infinity, 0] subset. 105 | */ 106 | sealed trait NonPosInt extends Integer { 107 | type N <: NonPosInt 108 | type isPos = False 109 | type pred <: NegInt 110 | type neg <: NonNegInt 111 | type loop[B,F[_ <: B] <: B, Res <: B] = Res 112 | } 113 | 114 | /** 115 | * Refinement of integers that matches the ]-infinity, 0[ U ]0, + infinity[ subset. 116 | */ 117 | sealed trait NonZeroInt extends Integer { 118 | type N <: NonZeroInt 119 | type isZero = False 120 | type abs <: PosInt 121 | type div[R <: NonZeroInt] = ({ 122 | type aRes = abs#divNat[R#abs] 123 | type result = sameSign[R]#branch[Integer, aRes, aRes#neg] 124 | })#result 125 | } 126 | 127 | /** 128 | * Refinement of integers that matches the ]-infinity, 0[ 129 | */ 130 | sealed trait NegInt extends NonPosInt with NonZeroInt { 131 | type N <: NegInt 132 | type isNeg = True 133 | type neg <: PosInt 134 | type abs = neg 135 | type succ <: NonPosInt 136 | type pred <: NegInt 137 | } 138 | /** 139 | * Refinement of integers that matches the ]0, +infinity[ 140 | */ 141 | sealed trait PosInt extends NonNegInt with NonZeroInt { 142 | type N <: PosInt 143 | type isPos = True 144 | type neg <: NegInt 145 | type pred <: NonNegInt 146 | type succ <: PosInt 147 | type loop[B,F[_ <: B] <: B, Res <: B] = pred#loop[B,F,F[Res]] 148 | } 149 | 150 | /** 151 | * Typelevel implementation of positive integers : each of them is either a successor of Zero or 152 | * a successor of another positive integer. 153 | */ 154 | case class ++[P <: NonNegInt]() extends PosInt { 155 | type N = ++[P] 156 | type succ = ++[++[P]] 157 | type add[R <: Integer] = P#add[R#succ] 158 | type pred = P 159 | type sub[R <: Integer] = P#sub[R#pred] 160 | type prod[R <: Integer] = P#prod[R]#add[R] 161 | type neg = --[P#neg] 162 | type predNat = P 163 | type addNat[R <: NonNegInt] = P#addNat[R#succ] 164 | type subNat[R <: NonNegInt] = (R#isZero)#branch[NonNegInt, N, P#subNat[R#predNat]] 165 | type divNat[D <: PosInt] = lt[D]#branch[NonNegInt, _0, p1#addNat[subNat[D]#divNat[D]]] 166 | } 167 | 168 | /** 169 | * Typelevel implementation of negative integers : each of them is either a predecessor of Zero or 170 | * a predecessor of another negative integer. 171 | */ 172 | case class --[S <: NonPosInt]() extends NegInt { 173 | type N = --[S] 174 | type succ = S 175 | type add[N <: Integer] = S#add[N#pred] 176 | type pred = --[--[S]] 177 | type sub[N <: Integer] = S#sub[N#succ] 178 | type prod[N <: Integer] = (neg#prod[N])#neg 179 | type neg = ++[S#neg] 180 | } 181 | 182 | /** 183 | * Typelevel implementation of Zero. 184 | */ 185 | class _0 extends NonNegInt with NonPosInt { 186 | type N = _0 187 | type isZero = True 188 | type succ = ++[_0] 189 | type add[R <: Integer] = R 190 | type addNat[R <: NonNegInt] = R 191 | type subNat[R <: NonNegInt] = _0 192 | type divNat[R <: PosInt] = _0 193 | type pred = --[_0] 194 | type predNat = _0 195 | type sub[R <: Integer] = R#neg 196 | type prod[R <: Integer] = _0 197 | type div[R <: NonZeroInt] = _0 198 | type neg = _0 199 | } 200 | -------------------------------------------------------------------------------- /src/main/scala/yausl/scalar.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Olivier Mélois 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package yausl 18 | 19 | import shapeless._ 20 | 21 | import scala.annotation.implicitNotFound 22 | 23 | /** 24 | * Yausl uses this value-class to avoid boxing at runtime. It models the fact that a measure 25 | * is a value associated with a unit-system (U) and a list of dimensions in this system. 26 | * So T is a type-level List of Integers, of the same size as the list of units in the system, 27 | * 28 | * If U =:= meter :: second :: HNil, a speed value (meter.second^^-1) is a 29 | * Scalar[meter::second::HNil, 1 :: -1 :: HNil] 30 | */ 31 | class Scalar[U <: HList, T <: HList] protected[yausl](val value: Double) extends AnyVal { 32 | 33 | /** 34 | * Multiplies the scalar with a double 35 | */ 36 | def *(d : Double) : Scalar[U, T] = new Scalar(value * d) 37 | 38 | /** 39 | * Divides the scalar with a double 40 | */ 41 | def /(d : Double) : Scalar[U, T] = new Scalar(value / d) 42 | 43 | /** 44 | * Adds two scalars with the same dimensions. 45 | */ 46 | def +(s: Scalar[U, T]): Scalar[U, T] = new Scalar(value + s.value) 47 | 48 | /** 49 | * Substracts two scalars with the same dimensions. 50 | */ 51 | def -(s: Scalar[U, T]): Scalar[U, T] = new Scalar(value + s.value) 52 | 53 | /** 54 | * Multiplies two scalars with the same dimensions. 55 | */ 56 | def *[T2 <: HList](s: Scalar[U, T2])(implicit product: Product[T, T2]): Scalar[U, product.result] = 57 | new Scalar[U, product.result](value * s.value) 58 | 59 | /** 60 | * Divides two scalars with the same dimensions. 61 | */ 62 | def /[T2 <: HList](s: Scalar[U, T2])(implicit division: Division[T, T2]): Scalar[U, division.result] = 63 | new Scalar[U, division.result](value / s.value) 64 | 65 | /** 66 | * A "toString" method that relies on an instance generated at compile time. 67 | */ 68 | def show(implicit s : Show[Scalar[U, T]]) = value + " " + s() 69 | } 70 | 71 | /** 72 | * Typeclass that allows to find the dimensions resulting from the multiplication of two values. 73 | */ 74 | @implicitNotFound("Could not derive a multiplication from the provided values. " + 75 | "Are you trying to multiply values from different unit systems ?") 76 | trait Product[T1 <: HList, T2 <: HList] { 77 | type result <: HList 78 | } 79 | 80 | object Product { 81 | type Aux[HL1 <: HList, HL2 <: HList, Out <: HList] = Product[HL1, HL2]{type result = Out} 82 | implicit val product1: Aux[HNil, HNil, HNil] = new Product[HNil, HNil] { 83 | type result = HNil 84 | } 85 | 86 | implicit def product2[H1 <: Integer, R1 <: HList, H2 <: Integer, R2 <: HList](implicit prodAux: Product[R1, R2]) 87 | : Aux[H1 :: R1, H2 :: R2, ((H1 + H2) :: prodAux.result)] = 88 | new Product[H1 :: R1, H2 :: R2] { 89 | type result = (H1 + H2) :: prodAux.result 90 | } 91 | } 92 | 93 | /** 94 | * Typeclass that allows to find the dimensions resulting from the division of two values. 95 | */ 96 | @implicitNotFound("Could not derive a division from the provided values. " + 97 | "Are you trying to divide values from different unit systems ?") 98 | trait Division[T1 <: HList, T2 <: HList] { 99 | type result <: HList 100 | } 101 | 102 | object Division { 103 | type Aux[HL1 <: HList, HL2 <: HList, Out <: HList] = Division[HL1, HL2]{type result = Out} 104 | implicit val div1: Aux[HNil, HNil, HNil] = new Division[HNil, HNil] { 105 | type result = HNil 106 | } 107 | 108 | implicit def div2[H1 <: Integer, R1 <: HList, H2 <: Integer, R2 <: HList](implicit divAux: Division[R1, R2]) 109 | : Aux[H1 :: R1, H2 :: R2, (H1 - H2) :: divAux.result] = new Division[H1 :: R1, H2 :: R2] { 110 | type result = (H1 - H2) :: divAux.result 111 | } 112 | } 113 | 114 | /** 115 | * A quantity can represent something like quality, substance, change. It could be Length, Time, Mass ... 116 | */ 117 | trait BaseQuantity 118 | 119 | /** 120 | * A unit of measurement is a definite magnitude of a physical quantity. In our case, of base quantity. 121 | */ 122 | trait UnitM[M <: BaseQuantity] 123 | 124 | 125 | /** 126 | * Typeclass used by a system to give dimensions to a measure in a system of units. 127 | * For instance, if a System is meter::second::HNil, the dimensions of a time value in this system would be : 128 | * 0::1::HNil 129 | */ 130 | trait DimensionsOf[Units <: HList, U <: UnitM[_]] { 131 | type result <: HList 132 | } 133 | 134 | object DimensionsOf { 135 | type Aux[Units <: HList, U <: UnitM[_], Out <: HList] = DimensionsOf[Units, U]{type result = Out} 136 | 137 | implicit def dimOf1[U <: UnitM[_]]: Aux[HNil, U, HNil] = new DimensionsOf[HNil, U] { 138 | type result = HNil 139 | } 140 | 141 | implicit def dimOf2[R <: HList, U <: UnitM[_]](implicit r: DimensionsOf[R, U]): Aux[U :: R, U, p1 :: r.result] = 142 | new DimensionsOf[U :: R, U] { 143 | type result = p1 :: r.result 144 | } 145 | 146 | implicit def dimOf3[R <: HList, U <: UnitM[_], U2 <: UnitM[_]](implicit r: DimensionsOf[R, U], ev: U =:!= U2) 147 | : Aux[U2 :: R, U, _0 :: r.result] = new DimensionsOf[U2 :: R, U] { 148 | type result = _0 :: r.result 149 | } 150 | } 151 | 152 | /** 153 | * Typeclass that computes a list of Zeros from a unit list. 154 | */ 155 | trait Zeros[D <: HList]{ 156 | type result <: HList 157 | } 158 | 159 | object Zeros { 160 | type Aux[D <: HList, Out <:HList] = Zeros[D]{type result = Out} 161 | 162 | implicit val inv0 : Aux[HNil, HNil] = new Zeros[HNil] {type result = HNil} 163 | implicit def inv1[H <: UnitM[_], T <: HList, R <: HList](implicit zeros : Zeros.Aux[T, R]) : Aux[H :: T, _0 :: R] 164 | = new Zeros[H :: T]{type result = _0 :: R} 165 | } 166 | 167 | /** 168 | * Typeclass that computes a list of Zeros from a unit list. 169 | */ 170 | trait SameSize[A <: HList, B <: HList] 171 | 172 | object SameSize { 173 | implicit val ss0 : SameSize[HNil, HNil] = new SameSize[HNil, HNil]{} 174 | implicit def ss1[A1, A <: HList, B1, B <: HList](implicit ss : SameSize[A,B]) 175 | : SameSize[A1::A, B1::B] = new SameSize[A1::A, B1::B]{} 176 | } 177 | 178 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Yausl : Yet Another Unit System Library 3 | 4 | The goal of this library is to provide a way to generically create **Systems of Units** such as the 5 | [International System of Units](http://en.wikipedia.org/wiki/International_System_of_Units), with as little 6 | boilerplate as possible. The types originating from these systems are checked at compile time when 7 | performing operations such as additions and multiplications, and the values of these types should have 8 | performances similar to unboxed doubles, thanks to [value classes](http://docs.scala-lang.org/overviews/core/value-classes.html). 9 | 10 | **THIS LIBRARY IS EXPERIMENTAL, USE IT AT YOUR OWN RISK** 11 | 12 | This library was partly inspired from Grant Beaty's [Scunits](https://github.com/gbeaty/scunits), from which I 13 | got some class names and the implementation for typelevel integers based on type-projections (though I enriched it 14 | for my own knowledge). What differs from it is mainly the fact that I wanted to write things such as 15 | *1.metre / 1.second* without having to manually write these extension methods, 16 | and the fact that I didn't care so much about (not) using implicits for type-level computations. 17 | 18 | ## Usage 19 | 20 | Yausl is built using SBT and published on the oss sonatype repositories. To use it, 21 | The following dependencies should be added to your build (see the double %% that differentiates 22 | maven-built artifacts from SBT-built artifacts). 23 | 24 | resolvers += Resolver.sonatypeRepo("snapshots") 25 | 26 | libraryDependencies += "org.scalamacros" % "resetallattrs_2.11" % "1.0.0" 27 | 28 | libraryDependencies += "com.github.baccata" %% "yausl" % "0.1.0-SNAPSHOT" 29 | 30 | 31 | ## Example code 32 | 33 | 34 | ```scala 35 | import yausl._ 36 | import shapeless._ 37 | 38 | trait Length extends Quantity //defining a quantity 39 | trait Time extends Quantity 40 | trait metre extends UnitM[Length] //defining a unit for this quantity 41 | trait second extends UnitM[Time] 42 | 43 | val system = SystemGenerator.fromHList[metre :: second :: HNil] //generating a system of units 44 | import system._ // importing extension methods generated during the system creation. 45 | 46 | // The pX type is the "type-level positive integer of value X" and 47 | // nX is the "type-level" negative integer of value -X" 48 | 49 | val a = 5 metre // compiles, creates an instance of Scalar[metre :: second :: HNil, p1 :: _0 :: HNil] 50 | val b = 3 second // compiles, creates an instance of Scalar[metre :: second :: HNil, _0 :: p1 :: HNil] 51 | val c = (5 metre) / (3 second) // compiles, creates an instance of 52 | //Scalar[metre :: second :: HNil, p1 :: n1 :: HNil] 53 | val d = (8 metre) + (2 metre) // compiles 54 | // val e = c + d // does not compile as you cannot sum m et m.s^-1 55 | 56 | val f = measure[metre](3) // scalars can also be initialized like this. 57 | 58 | val g = 1.scalar / 4.second // compiles, creates an instance of s^-1 Scalar 59 | 60 | println(a.show) //prints "8.0 metre.second^-1" 61 | ``` 62 | 63 | 64 | ## Requirements 65 | 66 | When this library was imagined, a certain number of requirements were conceptualized. As the code evolves, 67 | I will keep a formal list of requirements and provide a test for each of them. However, some tests 68 | are hard to be written, as performing them means checking whether a piece of code compiles. 69 | 70 | 71 | The usability requirements are the following : 72 | 73 | 74 | Requirements ID | Description 75 | ------------------| ---------------------------------------------------------------- 76 | REQ_YAUSL_USE_001 | systems of units should be creatable from a list of types (HList) 77 | REQ_YAUSL_USE_002 | "natural constructors" must be generated to allow a very natural writing style 78 | REQ_YAUSL_USE_003 | summing/substracting values of different dimensions must not compile 79 | REQ_YAUSL_USE_004 | summing/substracting values of equal dimensions must compile and work 80 | REQ_YAUSL_USE_005 | multiplication should be commutative(type-wise and value-wise) 81 | REQ_YAUSL_USE_006 | values should be printable with their types (for debugging purposes) 82 | REQ_YAUSL_USE_007 | Scalar multiplicative operators should work with doubles 83 | REQ_YAUSL_USE_008 | doubles should be usable in prefix position of scalar operators, through conversion to Scalar 84 | 85 | 86 | The performance requirements are the following : 87 | 88 | 89 | Requirements ID | Description 90 | -------------------|----------------------------------------------------------------- 91 | REQ_YAUSL_PERF_001 | performances of yausl Scalar instances should be similar to the ones of unboxed doubles 92 | 93 | ## Implementation 94 | 95 | The implementation relies on implicit-expansion based type-level programming 96 | Basically, what happens is that a value (yausl.Scalar) is accompanied at compile 97 | time with a type-level list (shapeless.HList) of Units (yausl.UnitM) and a typelevel list of integers that represents, 98 | for each unit, a associated dimension. Therefore, a Scalar[metre::second::HNil, 1 :: -1 :: HNil] is a speed value. 99 | 100 | I really wanted the system to be as generic as possible and did not make assumption on how this should be used, 101 | so you can have values that are m^-3, which does not physically represent anything ... 102 | 103 | The automatic creation of "natural constructors" (when writing "1 meter" for instance) is performed using a 104 | type generator macro that uses vampire methods. Calling the macro 105 | 106 | SystemGenerator.fromHList[metre::second::HNil] 107 | 108 | creates a System instance with an embedded implicit extension that adds the methods "metre" and "second" to the 109 | Double primitive type, each of them instantiating a Scalar of their respective type representation within the 110 | system. 111 | 112 | ## Limitations / TODOs 113 | 114 | There is a number of things you would probably like to do with yausl which have not yet been implemented : 115 | 116 | - Since we use fancy things such as type-level programming and whitebox, macros, your ide will probably show 117 | false positives when looking for type errors. 118 | - Initializing values with composite types : you cannot currently write "1 metre / second", you have 119 | to write "(1 metre) / (1 second)" or "1.metre / 1.second". 120 | - Values (yausl.Scalar) from different systems cannot be summed, even if they represent the same quantities. 121 | - Speaking of quantities : right now my quantities are absolutely useless. 122 | - Values are instances of the yausl.Scalar value class, and as such cannot be used inside a usual Collection, 123 | I will provide a custom implementation of Array at some point that will deal with this problem. 124 | - Multiplying/dividing/showing values requires some typelevel computation that will increase your 125 | compile time. I personally don' mind, the compiler works for me and the end user should see any performance problem. 126 | - If you want your units to have symbols, you should create your own instance of the yausl.Show typeclass. 127 | 128 | ## Thank you note 129 | 130 | I'd like to thank Miles Sabin and the guys behind the [Shapeless](https://github.com/milessabin/shapeless) 131 | library (although yausl doesn't use a lot of Shapeless, their CaseClassMacros trait was very useful) 132 | and the team that works to make macros writable/readable by humans, 133 | especially Eugene Burmako and Travis Brown for their [type providers examples](https://github.com/travisbrown/type-provider-examples) 134 | 135 | ## Finally 136 | 137 | On a personal note, I'm currently looking for a job and am ready to move anywhere. 138 | If you are in need of a young, eager to learn, Scala developer who's ready to fight boilerplate through 139 | type-level programming and macros, please ask me for a CV. 140 | 141 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2015 Olivier Mélois 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------