Ticket #4009 (closed feature request: invalid)

Opened 3 years ago

Last modified 3 years ago

can newtype be extended to permit GADT-like declarations

Reported by: nr Owned by: simonpj
Priority: normal Milestone: 7.0.1
Component: Compiler (Type checker) Version: 6.12.1
Keywords: newtype GADT Cc:
Operating System: Linux Architecture: x86
Type of failure: None/Unknown Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

I'd like to create the following value constructor without the overhead of data:

data B n e x where
  B :: Body n -> B n C C

However, if I ask for newtype instead of data I'm told that a newtype constructor must have a return type of form T a1 ... an. Is this restriction fundamental? If not, could it be possible to allow GADT-like declarations for newtype?

Change History

Changed 3 years ago by igloo

  • owner set to simonpj
  • milestone set to 6.14.1

Changed 3 years ago by simonpj

  • status changed from new to closed
  • resolution set to invalid

It's fundamental. Suppose we had

data T a where
   MkT :: Int -> T Int

f :: T a -> a -> Int
f (MkT n) m = m+1

blah = f (MkT 4) 6          -- Calling f at type Int, works
boo  = f (error "urk") True -- Calling f at type True, diverges

This is fine. Matching on the MkT ensures that a~Int. The call in blah is fine, and returns 7. The call in boo also type-checks fine, but the pattern match in f will diverge, so we won't get a seg fault when we try to evaluate True + 1.

But if instead we had

newtype T a where
   MkT :: Int -> T Int

f :: T a -> a -> Int
f (MkT n) m = m+1

blah = f (MkT 4) 6          -- Calling f at type Int, works
boo  = f (error "urk") True -- Calling f at type True, diverges

Now we would get a seg-fault by evaluating True + 1. The point is that matching against the MkT constructor does no evaluation.

A System-FC way of saying this is that matching against a data constructor brings into scope an equality coercion variable; but "matching" against a newtype constructor doesn't, and can't.

Simon

Note: See TracTickets for help on using tickets.