asteroid-lang / asteroid

Asteroid is a modern, multi-paradigm programming language that supports first-class patterns.
https://asteroid-lang.org
MIT License
40 stars 10 forks source link

parameterized patterns #283

Open lutzhamel opened 1 year ago

lutzhamel commented 1 year ago

An idea on how to look at parameterized patterns. Currently Asteroid supports parameterized patterns via variables read from the environment,

Asteroid Version 2.0.1alpha
(c) University of Rhode Island
Type "asteroid -h" for help
Press CTRL-D to exit
ast> let p = pattern %integer.
ast> let pair = pattern (x:*p,y:*p).
ast> let q:*pair = (1,2). -- here pair is a pair of integer values
ast> q
(1,2)
ast> let p = pattern %real. -- now pair is a pair of real values
ast> let q:*pair = (1,2).
error: pattern match failed: conditional pattern match failed
ast> let q:*pair = (1.1,2.2).
ast> 

This is very clumsy and prone to programming errors...think dynamically scoped functions. A nice way would be to actually have have parameters on the pattern. Something like this perhaps,

ast> let p = pattern %integer.
ast> let pair = pattern with a match (x:*a,y:*a).
ast> let q:*pair(p) = (1,2). -- here pair is a pair of integer values
ast> q
(1,2)

or in more concise form,


ast> let pair = pattern with a match (x:*a,y:*a).
ast> let q:*pair(pattern %integer) = (1,2). -- here pair is a pair of integer values
ast> q
(1,2)
ast> let q:*pair(pattern %real) = (1.1,2.2). -- here pair is a pair of real values
ast> q
(1.1,2.2)
lutzhamel commented 1 year ago

We cannot use juxtaposition as a way to parameterize patterns, it makes the language too ambiguous due to the confusion with function application. We can adopt the generics notation here,

ast> let pair = pattern<a> (x:*a,y:*a).
ast> let q:*pair<pattern %integer> = (1,2). -- here pair is a pair of integer values
ast> q
(1,2)
ast> let q:*pair<pattern %real> = (1.1,2.2). -- here pair is a pair of real values
ast> q
(1.1,2.2)