Re: [Haskell-cafe] Prolog-style list syntax?

Ok thank you for the feedback, I get the message not to re-purpose currently valid syntax. For that reason, this won't fly: I see no technical issues in allowing
something like `[ x, y || ys]`
`||` is already an operator in the Prelude.
[x, y ,: ys ] -- ? not currently valid
We could make list syntax work harder
[x, y ,:+ ys ]
Means desugar the commas by applying constructor `:+` instead of `:`. That could be in general any constructor starting `:`. Or indeed could be a pattern synonym starting `:`, which is a 'smart constructor' to build the list maintaining some invariant.

Ok so I implemented both ideas:
elem2 _ [] = False elem2 x [y,: ys] = x == y || elem2 x ys
let [x,y,z,w,: ws] = "hello there" in ws -- yields "o there"
In fact it was easier to implement both ideas than make a special case of `(:)`. So these work
let [x,:+ xs] = [1,2,3,:+ Nily] in xs
let [x,`ConsSet` xs] = [1,2,3,`ConsSet` NilSet] in xs
with decls
infixr 5 :+ data Listy a = Nily | a :+ (Listy a) deriving (Eq, Show, Read)
infixr 5 `ConsSet` data Set a = NilSet | ConsSet a (Set a) deriving (Eq, Show, Read)
On Wed, 30 Jun 2021 at 19:00, Anthony Clayden
Ok thank you for the feedback, I get the message not to re-purpose currently valid syntax.
[x, y ,: ys ] -- ? not currently valid
We could make list syntax work harder
[x, y ,:+ ys ]
Means desugar the commas by applying constructor `:+` instead of `:`. That could be in general any constructor starting `:`.
Or indeed could be a pattern synonym starting `:`, which is a 'smart constructor' to build the list maintaining some invariant.

I note that (1) Clean uses ":" in lists the way Prolog uses "|". (2) Before Prolog switched to "|", Edinburgh Prolog used ",.." (two tokens). (3) There doesn't seem to be any reason why [p1,p2,p3]++pr could not be a pattern, where p1, p2, p3 match elements and pr the rest. Erlang has <string literal> ++ <pattern>, but forbids <list pattern> ++ <pattern>, presumably because it uses "|" like Prolog.

On Jul 8, 2021, at 5:05 AM, Richard O'Keefe
wrote: (3) There doesn't seem to be any reason why [p1,p2,p3]++pr could not be a pattern, where p1, p2, p3 match elements and pr the rest.
The problem with that one is that: [p1,p2,p3]++pr = list is interpreted as defining “++”. If the outermost expression isn’t a constructor or some new syntax (i.e., previously a syntax error) then I think you’ll have that problem. Jeff

Thanks Richard, see the discussion last month (same Subject line) that
considered/rejected various ideas
On Fri, 9 Jul 2021 at 00:05, Richard O'Keefe
I note that (1) Clean uses ":" in lists the way Prolog uses "|".
Yeah that's where I started. [x : xs] is already valid Haskell, meaning [ (x: xs) ].
(2) Before Prolog switched to "|", Edinburgh Prolog used ",.." (two tokens).
Haskell already has [1, 2 .. 5] for arithmetic sequences. `,..` Wouldn't quite be ambiguous, but is dangerously close. (3) There doesn't seem to be any reason why [p1,p2,p3]++pr could not be
a pattern, where p1, p2, p3 match elements and pr the rest.
Oh but `++` is an operator, not a constructor. Syntactically that's not a pattern. So [p1, p2]++pr++pq is a valid expression; what would it mean as a pattern? Somebody in last month's discussion wanted similar, especially for strings "prefix"++pr. But it would be valid in a pattern only if the lhs were a pattern, and we made a special syntactic case for ++. Then what if there's a user-defined override for ++? Also see my OP last month, I want the list to be the thing in [ ], not also some trailing expression.
Erlang has <string literal> ++ <pattern>, but forbids <list pattern> ++ <pattern>, presumably because it uses "|" like Prolog.
participants (3)
-
Anthony Clayden
-
Jeff Clites
-
Richard O'Keefe