Opened 2 years ago

Last modified 2 months ago

#7495 new feature request

generalizing overloaded list syntax to Sized Lists, HLists, HRecords, etc

Reported by: nwf Owned by: carter
Priority: normal Milestone: 7.12.1
Component: Compiler Version: 7.6.1
Keywords: Cc: giorgidze@…, info@…, jeroen.weijers@…, eir@…, gershomb@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking: #9883
Related Tickets: #9883 Differential Revisions:

Description

First, sorry if I've missed an earlier request for this in trac; a few searches did not turn up anything relevant.

I've recently taken to doing a lot of work with heterogenous lists (thanks to the DataKinds work) and find the forced-cons-and-nil style of writing lists (e.g. "a:+b:+c:+HN") to be sort of unpleasant.

Would it be possible to allow rebinding list-literal syntax? Off the top of my head I think something like the following might be workable, if only I could stop [] and (:) from being in scope, even with -XNoImplicitPrelude. (Example requires -XDataKinds -XFlexibleInstances -XGADTs -XMultiParamTypeClasses -XTypeOperators)

class HasNil a where
  ([])  :: a
  isNil :: a -> Bool

class HasCons e l l' | e l -> l', l' -> e l where
  (:)    :: e -> l -> l'
  uncons :: l' -> Maybe (e,l)

-- For homogeneous lists...
instance HasNil [a] where
  ([])  = ([])
  isNil = null

instance (a ~ a1, a ~ a2) => HasCons a [a1] [a2] where
  (:)           = (:)
  uncons []     = Nothing
  uncons (x:xs) = Just (x,xs)

-- For HLists...
data HList as where
  HN   :: HList '[]
  (:+) :: a -> HList as -> HList (a ': as)

instance HasNil (HList '[]) where
  ([])  = HN
  isNil = const True

instance (a ~ a1, as ~ as1) => HasCons a (HList as) (HList (a1 ': as1)) where
  (:)              = (:+)
  uncons (a :+ as) = Just (a,as)

Change History (10)

comment:1 Changed 2 years ago by simonpj

  • Cc giorgidze@… info@… jeroen.weijers@… added
  • difficulty set to Unknown

Yes, and an implementation is well advanced; see OverloadedLists. I've cc'd the George, Achim, and Wejers, who are doing it.

Simon

comment:2 Changed 2 years ago by monoidal

Should this be marked as fixed?

comment:3 Changed 2 years ago by nwf

I don't think the extant proposal at OverloadedLists addresses the heterogeneous types I was after?

comment:4 Changed 2 years ago by goldfire

  • Cc eir@… added

I agree with nwf. It seems the current implementation can't deal with heterogeneous lists. I, too, would enjoy being able to use nicer notation for my lists.

comment:5 Changed 2 years ago by igloo

  • Milestone set to 7.8.1

comment:6 Changed 12 months ago by thoughtpolice

  • Milestone changed from 7.8.3 to 7.10.1

Moving to 7.10.1

comment:7 Changed 7 months ago by carter

  • Owner set to carter
  • Summary changed from Rebindable list syntax? to generalizing overloaded list syntax to Sized Lists, HLists, HRecords, etc

I've been working on designing a set of classes that subsumes this proposal.
the work in progress in the class design can be found https://github.com/cartazio/HetList/blob/master/HetList.hs

It doesn't yet have the analogues machinery to support the older fromList style instances, but thats easy to add.

As part of the dev I'm making sure I can write useful instances for the fancy HList libs that are in the while, like HList and Vinyl

after another week or two of polish, I hope to have a candidate patch with the right desugaring (plus some extra work to support current style instances if possible)

comment:8 Changed 5 months ago by carter

  • Blocking 9883 added

comment:9 Changed 4 months ago by thoughtpolice

  • Milestone changed from 7.10.1 to 7.12.1

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:10 Changed 2 months ago by gershomb

  • Cc gershomb@… added
Note: See TracTickets for help on using tickets.