existential-type

Can an existentially quantified type variable be forced to have only a single type?

為{幸葍}努か 提交于 2019-12-30 10:12:17
问题 Consider the following code trait Foo[T] { def one: Foo[_ >: T] def two: T def three(x: T) } def test[T](f: Foo[T]) = { val b = f.one b.three(b.two) } The method test fails to type check. It says: found : (some other)_$1(in value b) required: _$1(in value b) val x = b.three(b.two) If I am interpreting this correctly, the compiler thinks that b in method test has a type that looks like this (not legal syntax, but hopefully clearer): trait Foo { def two: X where ∃ X >: T def three(x: X where ∃

Type abstraction in GHC Haskell

荒凉一梦 提交于 2019-12-30 09:05:05
问题 I'd love to get the following example to type-check: {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} module Foo where f :: Int -> (forall f. Functor f => Secret f) -> Int f x _ = x g :: (forall f. Functor f => Secret f) -> Int g = f 4 type family Secret (f :: * -> *) :: * where Secret f = Int I get it that it's probably not possible to infer and check g s type (even though in this specific case it's obvious

Could not deduce KnownNat in two existentials with respect to the singletons library

徘徊边缘 提交于 2019-12-29 07:33:10
问题 I was experimenting with the singletons library and I found a case that I don't understand. {-# LANGUAGE GADTs, StandaloneDeriving, RankNTypes, ScopedTypeVariables, FlexibleInstances, KindSignatures, DataKinds, StandaloneDeriving #-} import Data.Singletons.Prelude import Data.Singletons.TypeLits data Foo (a :: Nat) where Foo :: Foo a deriving Show data Thing where Thing :: KnownNat a => Foo a -> Thing deriving instance Show Thing afoo1 :: Foo 1 afoo1 = Foo afoo2 :: Foo 2 afoo2 = Foo athing ::

Existential Types. Writing the instance of a class for an heterogeneous map

只谈情不闲聊 提交于 2019-12-24 17:39:25
问题 Using the following type and class definitions, I do not understand why I get and error when creating the instance below. I need MyMap to hold a map of heterogeneous values. {-# LANGUAGE ExistentialQuantification #-} module Scratch.SO_ExtistentialTypes where import Data.Map type MyMap a = Map String a class MyClass c where getMyMap :: forall a. c -> MyMap a data MyData = forall a. MyData { myMap :: MyMap a } instance MyClass MyData where getMyMap = myMap -- <= ERROR 回答1: For one thing, the

Correlate two type parameters

試著忘記壹切 提交于 2019-12-24 10:27:50
问题 Suppose, I have a sequence of operations, some of which depend on some of the results of previous ones. Something like this: type Results = List[(Operation[_], Any)] // ??? trait Operation[Out] { type Result = Out def apply(results: Results): Out } class SomeOp extends Operation[String] { def apply(results: Results) = "foo" } class OtherOp extends Operation[String] { def apply(results: Results) = results .collectFirst { case (_: SomeOp, x: String) => x } .getOrElse("") + "bar" } def applyAll(

Type variance error in Scala when doing a foldLeft over Traversable views

倾然丶 夕夏残阳落幕 提交于 2019-12-24 00:39:58
问题 I am trying concatenate a series of Traversable views in Scala using a foldLeft operator and am hitting type variance errors that I don't understand. I can use reduce to concatenate a list of Traversable views like so. val xs = List(1,2,3,4).map(Traversable(_).view).reduce((a: TraversableView[Int, Traversable[_]], b: TraversableView[Int, Traversable[_]]) => a ++ b) // TraversableView[Int,Traversable[_]] // xs.force returns Traversable[Int] = List(1, 2, 3, 4) (Note that I have to write the

Is there special meaning to an underscore (_) in Type Bounds?

左心房为你撑大大i 提交于 2019-12-21 02:22:11
问题 I'm trying to understand Scala's existential types. Is there any difference between: def foo[X <: Bar] = 3 and def foo[_ <: Bar] = 3 or are they something more than just unnamed type parameters? 回答1: Here _ is indeed just an unnamed type parameter, no more, no less. There is no difference between def foo[_ <: Bar] = 3 and def foo[X <: Bar] = 3 where X is unused. UPDATE : In response to: "I can't think of a use case for an unused type, I'd be grateful for one": Note that this is pretty much

Why can't I use record selectors with an existentially quantified type?

妖精的绣舞 提交于 2019-12-20 11:07:33
问题 When using Existential types, we have to use a pattern-matching syntax for extracting the forall ed value. We can't use the ordinary record selectors as functions. GHC reports an error and suggest using pattern-matching with this definition of yALL : {-# LANGUAGE ExistentialQuantification #-} data ALL = forall a. Show a => ALL { theA :: a } -- data ok xALL :: ALL -> String xALL (ALL a) = show a -- pattern matching ok -- ABOVE: heaven -- BELOW: hell yALL :: ALL -> String yALL all = show $ theA

Deconstructing an existential type

荒凉一梦 提交于 2019-12-19 10:19:05
问题 I am using an existential type as a wrapper. At a point in my code where I know the enclosed type, I want to do something with it that is specific to the enclosed type. This is the closest I can get: {-# LANGUAGE ExistentialQuantification #-} class Agent a where agentId :: a -> String speciesId :: a -> String -- plus other functions that all agents support -- | A wrapper allowing my daemon to read and write agents of any species. -- (Agents are stored in files that contain a tag so I know

How to define an existential higher kinded type in Scala

不打扰是莪最后的温柔 提交于 2019-12-19 03:24:41
问题 I was trying to define a type that accept an existential higher kinded type in Scala. Unfortunately Scalac does not allow it. Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45). Type in expressions to have them evaluated. Type :help for more information. scala> :paste // Entering paste mode (ctrl-D to finish) trait H[F[_, _]] trait T[A, B] val h:H[T] = null val e:H[F] forSome { type F[A, B] } = h // Exiting paste mode, now interpreting. <console>:13: error: