singleton-type

Singleton types in Haskell

删除回忆录丶 提交于 2019-12-31 09:11:22
问题 As part of doing a survey on various dependently typed formalization techniques, I have ran across a paper advocating the use of singleton types (types with one inhabitant) as a way of supporting dependently typed programming. Acording to this source, in Haskell, there is a separation betwen runtime values and compile-time types that can be blurred when using singleton types, due to the induced type/value isomorphism. My question is: How do singleton types differ from type-classes or from

working with proofs involving CmpNat and singletons in Haskell

允我心安 提交于 2019-12-24 00:39:33
问题 I'm trying to create some functions to work with the following type. The following code uses the singletons and constraints libraries on GHC-8.4.1: {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-#

What is a Singleton Type exactly?

梦想与她 提交于 2019-12-17 19:34:47
问题 What is a singleton type? what are the applications, the implications ? Examples are more than welcome and layman terms are even more welcome ! 回答1: If you think of a type as a set of values, the singleton type of a value x is the type which only contains this value ( {x} ). Usage examples: Pattern matching: case _: Foo.type checks that the matched object is the same as Foo using eq , where case Foo only checks that it's equal to Foo using equals . It's needed to write down the type of an

Can't prove that singleton types are singleton types while generating type class instance

寵の児 提交于 2019-12-17 08:55:22
问题 Suppose I've got a type class that proves that all the types in a Shapeless coproduct are singleton types: import shapeless._ trait AllSingletons[A, C <: Coproduct] { def values: List[A] } object AllSingletons { implicit def cnilSingletons[A]: AllSingletons[A, CNil] = new AllSingletons[A, CNil] { def values = Nil } implicit def coproductSingletons[A, H <: A, T <: Coproduct](implicit tsc: AllSingletons[A, T], witness: Witness.Aux[H] ): AllSingletons[A, H :+: T] = new AllSingletons[A, H :+: T]

How to return wildcard generic?

时光毁灭记忆、已成空白 提交于 2019-12-11 16:37:41
问题 I have a type alias with parameter and I would like to return the instance of different parameter types from a method: type TC[T] = (ClassTag[T], Option[T]) def gen(x: Int): TC[_] = x match { case 0 => (classTag[Int], Option[Int](0)) case _ => (classTag[String], Option[String]("")) } This does not work and gives me error: error: type mismatch; found : (scala.reflect.ClassTag[_ >: Int with String], Option[Any]) required: TC[ ] (which expands to) (scala.reflect.ClassTag[ $1], Option[_$1])

Adding an Ord instance to 'singleton' package generated naturals

南笙酒味 提交于 2019-12-10 17:45:18
问题 I am using very simple type-level naturals generated with the singletons package. I am now trying to add an Ord instance to them. {-# LANGUAGE MultiParamTypeClasses, TemplateHaskell, KindSignatures, DataKinds, ScopedTypeVariables, GADTs, TypeFamilies, FlexibleInstances, TypeOperators, UndecidableInstances, InstanceSigs #-} module Functions where import Data.Singletons import Data.Singletons.TH import Data.Singletons.Prelude import Data.Promotion.Prelude singletons [d| data Nat = Z | S Nat

Can I get KnownNat n to imply KnownNat (n * 3), etc?

。_饼干妹妹 提交于 2019-12-10 03:14:22
问题 I'm working with data types of this shape, using V from linear : type Foo n = V (n * 3) Double -> Double Having it fixed on n is pretty important, because I want to be able to ensure that I'm passing in the right number of elements at compile-time. This is a part of my program that already works well, independent of what I'm doing here. For any KnownNat n , I can generate a Foo n satisfying the behavior that my program needs. For the purposes of this question it can be something silly like

Scala: “Static values” in traits?

烈酒焚心 提交于 2019-12-08 02:43:10
问题 Let's say I have: trait X { val x: String } Using mix-in, I can define a trait such as trait XPrinter { self: X => def printX: String = "X is: " + x } such that a value/object implementing XPrinter implements x and give its methods such as printX access to the values specified in X such as x . So far, so good. I want to know if there is a way of having a trait in the form of: trait XDependent[T <: X] { def printX: String = ??? } So that XDependent instances have access to the value of T.x ,

Scala: “Static values” in traits?

浪尽此生 提交于 2019-12-06 11:04:06
Let's say I have: trait X { val x: String } Using mix-in, I can define a trait such as trait XPrinter { self: X => def printX: String = "X is: " + x } such that a value/object implementing XPrinter implements x and give its methods such as printX access to the values specified in X such as x . So far, so good. I want to know if there is a way of having a trait in the form of: trait XDependent[T <: X] { def printX: String = ??? } So that XDependent instances have access to the value of T.x , with x assumed to be a "static value" glued with the type definition. Now I understand why T.x can't be

Can I get KnownNat n to imply KnownNat (n * 3), etc?

点点圈 提交于 2019-12-05 04:13:34
I'm working with data types of this shape, using V from linear : type Foo n = V (n * 3) Double -> Double Having it fixed on n is pretty important, because I want to be able to ensure that I'm passing in the right number of elements at compile-time. This is a part of my program that already works well, independent of what I'm doing here. For any KnownNat n , I can generate a Foo n satisfying the behavior that my program needs. For the purposes of this question it can be something silly like mkFoo :: KnownNat (n * 3) => Foo n mkFoo = sum Or for a more meaningful example, it can generate a random