From: Sam Halliday <sam.halliday@gmail.com>
To: guile-user@gnu.org
Subject: emulate "sum type" pattern matching?
Date: Wed, 11 Mar 2020 19:58:04 +0000 [thread overview]
Message-ID: <87sgier7eb.fsf@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 2306 bytes --]
Hi all,
I have read the Guile manual as my introduction to Guile. I am very
impressed at how mature this project is and was overwhelmed by the
feature set, which seems to be on-par with heavily invested technologies
like Java and the JVM.
I am considering using Guile for a project because I love Emacs lisp and
know it very well. Emacs lisp has some limitations that I feel Guile
overcomes, e.g. multithreading, a superior regexp engine, a module
system, and parsers.
However, there is one feature that is critical to the development of the
project and I was hoping to be able to implement it through a macro: sum
type pattern matching.
By that, I mean in the sense of Haskell sum types, which I understand
are similar to C++ union types. Roughly translated into GOOP, and using
Scala's encoding of sum types, this would look like record types that
are all children of a superclass. I noticed that Guile has support for
discovering all direct subclasses at runtime, but is this facility
available at compiletime?
An example of how I would want to use this feature can be described in
terms of the XML calculator in the Guile Manual.
https://www.gnu.org/software/guile/manual/html_node/sxml_002dmatch.html#Catamorphisms
which looks like
(define simple-eval
(lambda (x)
(sxml-match x
[,i (guard (integer? i)) i]
[(plus ,x ,y) (+ (simple-eval x) (simple-eval y))]
[(times ,x ,y) (* (simple-eval x) (simple-eval y))]
[(minus ,x ,y) (- (simple-eval x) (simple-eval y))]
[(div ,x ,y) (/ (simple-eval x) (simple-eval y))]
[,otherwise (error "simple-eval: invalid expression" x)])))
If the sxml-match was aware that it was matching over a superclass of
plus, minus, times, div then the "otherwise" line would be redundant and
(most importantly) if I were to forget to match over one of the
subclasses I would get a compiler error.
And that's basically my usecase in a nutshell: exhaustive pattern
matching over a tree-like structure (an AST, in fact). But I'll have
lots of different trees so I don't want to have to manually write a
pattern match macro every time I define a "sum type"... although that
said I do have some ideas on how to abstract that. But I don't really
want to go down a lisp macro rabbit hole at the very beginning...
--
Best regards,
Sam
[-- Attachment #2.1: Type: text/plain, Size: 0 bytes --]
[-- Attachment #2.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 194 bytes --]
next reply other threads:[~2020-03-11 19:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-11 19:58 Sam Halliday [this message]
2020-03-12 14:46 ` emulate "sum type" pattern matching? Chris Vine
2020-03-12 14:56 ` Ricardo Wurmus
2020-03-12 17:16 ` Chris Vine
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87sgier7eb.fsf@gmail.com \
--to=sam.halliday@gmail.com \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).