[GHC] #4459: Polymorphic Data.Dynamic
GHC
cvs-ghc at haskell.org
Fri Jul 1 09:09:53 CEST 2011
#4459: Polymorphic Data.Dynamic
-----------------------------------------------------+----------------------
Reporter: vivian | Owner: vivian
Type: feature request | Status: new
Priority: normal | Milestone: 7.4.1
Component: GHC API | Version: 7.1
Keywords: polymorphic, dynamic, class, linking | Testcase:
Blockedby: | Difficulty:
Os: Unknown/Multiple | Blocking: 4316
Architecture: Unknown/Multiple | Failure: None/Unknown
-----------------------------------------------------+----------------------
Comment(by simonpj):
I too am very hazy about what this ticket is asking for. The existing
`Data.Dynamic` looks like this
{{{
data Dynamic = Dynamic TypeRep Obj -- Obj is like HValue; GHC is
inconsistent
toDyn :: Typeable a => a -> Dynamic
fromDynamic :: Typeable a => Dynamic -> Maybe a
dynApply :: Dynamic -> Dynamic -> Maybe Dynamic
}}}
I think that `Dynamic` is what you call `TypedAny`.
In the existing setup you cannot put a polymorphic value in a `Dynamic`.
So if `sort :: Ord a => [a] -> [a]`, you can say
{{{
toDyn (sort :: [Int] -> [Int])
toDyn (sort :: [Bool] -> [Bool])
}}}
but if you say
{{{
toDyn (sort :: forall a. [a] -> [a])
}}}
you'll get an ambiguity error.
I think this polymorphism issue is what you are trying to solve.
I don't think it's easy:
* A `Dynamic` contains a `TypeRep`, so you'd need to elaborate `TypeRep`
to represent polymorphic types.
* You'd need to have an instance
{{{
instance Typable (forall a. ty) where ..
}}}
and I don't know how to do that.
* When unwrapping an overloaded polymorphic value at runtime, you'd need
to build a suitable dictionary to apply it to. This is no easy matter:
you might need the dictionary for `(Ord (Tree [Maybe Int]))`, say. That
is, you need to invoke the full type-constraint solver, in the correct
type environment.
If you wanted to solve the latter problem, I think you could get close
like this
* Construct the `HsSyn` syntax tree for
{{{
\(x :: forall a. Ord a => [a] -> [a]) -> (x :: [ty] -> [ty]
}}}
where `ty` is the type you want to instantiate `sort` at.
* Use GHCi to typecheck and compile this
* Apply it to the polymorphic `sort`. That will give you a sort of
type `[ty] -> [ty]`.
Before embarking on such a project I think it would be good to learn from
the Clean experience, try out their system, and (especially) articulate
use cases. So that you know exactly what it is that you want to build.
Perhaps start a fresh wiki page articulating (a) the motivation (b) use
cases (c) the design as seen by the programmer.
Simon
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/4459#comment:14>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list