what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'?

The attachment compiles OK; yet, I don't understand how the compiler knows what type val_9 has. The compile/run is: <---cut here--- Compilation started at Tue Oct 21 13:17:41 make runghc uniplate.try.hs val_9=Val 9 add_8_9=Add (Val 8) (Val 9) children_add_8_9=[Val 8,Val 9] Compilation finished at Tue Oct 21 13:17:42
---cut here--- I would have expected that I would need to code this:
let val_9 = (Val 9)::(Expr Eo) but apparently not. Could somebody explain? TIA. -Larry

Can you explain why you think you need that annotation? I can't see an ambiguous interpretation of your code. -- _jsn

On Wed, Oct 22, 2008 at 4:10 PM, Jason Dusek
Can you explain why you think you need that annotation? I can't see an ambiguous interpretation of your code.
The confusion is that the 'Val' constructor is for the 'Expr' type, which has a phantom type parameter in its type constructor. Can you load that up into GHCi and type:
:t val_9
which should cause GHCi to print out what it thinks the type of that expression is. -Antoine

Am Mittwoch, 22. Oktober 2008 23:44 schrieb Antoine Latter:
On Wed, Oct 22, 2008 at 4:10 PM, Jason Dusek
wrote: Can you explain why you think you need that annotation? I can't see an ambiguous interpretation of your code.
The confusion is that the 'Val' constructor is for the 'Expr' type, which has a phantom type parameter in its type constructor.
Can you load that up into GHCi and type:
:t val_9
which should cause GHCi to print out what it thinks the type of that expression is.
-Antoine
ghci correctly thinks that has the type Expr e. Much like *Main> :t [] [] :: [a] I think what goes on here is defaulting (deviating from Section 4.3.4 of the report, but it's the same deviation that allows [] to be printed). To print it, ghci picks some default type for e, probably Integer, as the defaut default is (Integer, Double), doesn't influence the result of show. If you muck around with the Show instance, you can easily get compilation errors like "Ambiguous type variable..." (e.g. if you add a (Show e) constraint to the Show instance for (Expr e), but not if you add a (Num e) or an (Integral e) constraint).

On 10/22/08 19:52, Daniel Fischer wrote:
Am Mittwoch, 22. Oktober 2008 23:44 schrieb Antoine Latter:
On Wed, Oct 22, 2008 at 4:10 PM, Jason Dusek
wrote: Can you explain why you think you need that annotation? I can't see an ambiguous interpretation of your code.
The confusion is that the 'Val' constructor is for the 'Expr' type, which has a phantom type parameter in its type constructor.
Can you load that up into GHCi and type:
:t val_9
which should cause GHCi to print out what it thinks the type of that expression is.
-Antoine
ghci correctly thinks that has the type Expr e. Much like *Main> :t [] [] :: [a]
I think what goes on here is defaulting (deviating from Section 4.3.4 of the report, but it's the same deviation that allows [] to be printed). To print it, ghci picks some default type for e, probably Integer, as the defaut default is (Integer, Double), doesn't influence the result of show. If you muck around with the Show instance, you can easily get compilation errors like "Ambiguous type variable..." (e.g. if you add a (Show e) constraint to the Show instance for (Expr e), but not if you add a (Num e) or an (Integral e) constraint).
Thanks Deaniel. The fog in my head begins to clear. I took Antoine's suggestion and got: <---cut here --- GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> :load "/home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs" [1 of 1] Compiling Main ( /home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs, interpreted ) Ok, modules loaded: Main. *Main> let val_9 = Val 9 Loading package mtl-1.1.0.0 ... linking ... done. Loading package array-0.1.0.0 ... linking ... done. Loading package containers-0.1.0.1 ... linking ... done. Loading package uniplate-1.2.0.1 ... linking ... done. *Main> :t val_9 val_9 :: Expr e *Main> print val_9 Val 9 *Main>
---cut here--- I guess the phantom type mentioned in Antoine's post is the e in:
val_9::Expr e ?

Am Donnerstag, 23. Oktober 2008 05:36 schrieb Larry Evans:
Thanks Deaniel. The fog in my head begins to clear. I took Antoine's suggestion and got: <---cut here --- GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> :load "/home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs" [1 of 1] Compiling Main ( /home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs, interpreted ) Ok, modules loaded: Main. *Main> let val_9 = Val 9 Loading package mtl-1.1.0.0 ... linking ... done. Loading package array-0.1.0.0 ... linking ... done. Loading package containers-0.1.0.1 ... linking ... done. Loading package uniplate-1.2.0.1 ... linking ... done. *Main> :t val_9 val_9 :: Expr e *Main> print val_9 Val 9 *Main>
---cut here---
I guess the phantom type mentioned in Antoine's post is the e in:
val_9::Expr e
?
Exactly. That type is never used in any value of type (Expr e), therefore it is called a 'phantom' type. It only serves as a tag to prevent mixing Expr's with different type parameters. Cheers, Daniel
participants (4)
-
Antoine Latter
-
Daniel Fischer
-
Jason Dusek
-
Larry Evans