From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Chris Vine Newsgroups: gmane.lisp.guile.user Subject: Re: emulate "sum type" pattern matching? Date: Thu, 12 Mar 2020 14:46:22 +0000 Message-ID: <20200312144622.0c660172af70ced0493ce9b1@gmail.com> References: <87sgier7eb.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="91704"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user@gnu.org To: Sam Halliday Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Mar 12 15:46:15 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jCP6E-000NiI-Nl for guile-user@m.gmane-mx.org; Thu, 12 Mar 2020 15:46:14 +0100 Original-Received: from localhost ([::1]:42694 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCP6D-0003YU-Hb for guile-user@m.gmane-mx.org; Thu, 12 Mar 2020 10:46:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37902) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCP60-0003Th-I8 for guile-user@gnu.org; Thu, 12 Mar 2020 10:46:02 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jCP5x-0008Jb-8T for guile-user@gnu.org; Thu, 12 Mar 2020 10:45:58 -0400 Original-Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]:37731) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jCP5w-0008I8-QU for guile-user@gnu.org; Thu, 12 Mar 2020 10:45:57 -0400 Original-Received: by mail-wm1-x32c.google.com with SMTP id a141so6629837wme.2 for ; Thu, 12 Mar 2020 07:45:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wtZVWJW/SxHeXc87yfROZBKAXixKulvUJe+pDUicpB8=; b=hQmqmdHvNkknOZ35WSbU7IN1PI17/t4ZKTAjUQA/gozBtVPU1U6RNt3NauRfWccn2l Qw1O7UKfCc6qhxB2rUIbo8ZK3HMIt53UOE0R5g+tqDqUWVcbJY5+nbqexxUIxk8n/SRQ cmRRvwDXl/vY/6p3q9vMyj2+N+AuqTelqrleacWZS6X0Xka2btCEw/WyVIBShsZUZLlD mXRwkuNUaO5LMnOBX1Iu1nrXmeRFfEZERtfM7fr1ShKPetnm35woQW+cL2fYwnV/aX8T lY7VFxe3OJ0+P8piUNPIpRyFQTolbyVOUhn3VMai1CnvExWG4YfYIWyM/wtBE9HpvzIG mjLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wtZVWJW/SxHeXc87yfROZBKAXixKulvUJe+pDUicpB8=; b=tpW/4jxAIB3Ty+hiGgZqy9icIqBi13RVmKY6DMEyZw5xmPqdfoX/OMVf18QkZxPF6/ JPjtB28MiPhM9mjzEzokxVqqu2orf3JmbN/U1NdbmVntNw/2oX5WEiP5T9aIbtUqpnoI BgNYE5Ggv2FhGqgGNruqA24fzThQ5W+Clczp/NswJGRRXKKAOQpxs+4Ge/a3l5yqJZk1 OGAzLTO6Vw71w4Y9CTQqwHQ7goMM0pir3Q0pKnTdN337kaGlCKyThzMxhlQU0p3P/rFq len988FRedYixnyQ71uAZBrrtXTAwxq/nVQyUFdouDrEY2NRylKHsrhQyUDrze1HMRJq oQaA== X-Gm-Message-State: ANhLgQ2qIMx4rS+70xHr9JnjW4M6X8+UK9CXMrH3l6+kd64lxkOAu4OY uO+NsfULGQoHs2BE6WdzEm4= X-Google-Smtp-Source: ADFU+vvcaUxGLrJGNmmgJ9+gr7XZ2rO69pOuNI6u9XmwZ1IHjjhkfi9Ypqb7OYIQlomjTSXfSS93qg== X-Received: by 2002:a1c:dc55:: with SMTP id t82mr5161395wmg.6.1584024353717; Thu, 12 Mar 2020 07:45:53 -0700 (PDT) Original-Received: from bother.homenet ([2.24.141.99]) by smtp.gmail.com with ESMTPSA id z129sm13190741wmb.7.2020.03.12.07.45.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2020 07:45:53 -0700 (PDT) Original-Received: from bother.homenet (localhost [127.0.0.1]) by bother.homenet (Postfix) with SMTP id 9697C26273B; Thu, 12 Mar 2020 14:46:22 +0000 (GMT) In-Reply-To: <87sgier7eb.fsf@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-unknown-linux-gnu) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::32c X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16291 Archived-At: On Wed, 11 Mar 2020 19:58:04 +0000 Sam Halliday wrote: > 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... guile's built-in pattern matcher (ice-9 match) enables you to match on symbols, literals, pairs, lists, vectors and records, but I don't think it enables you to match on GOOPS objects - someone may contradict me on that, but at least I have never tried doing so (I don't like GOOPS nad I rarely use it). The other problem is that discriminated unions are a better fit with statically typed languages such as Haskell and the MLs; in dynamically typed languages there is a sense in which every variable name represents a union of unlimited extent but which enables its current type to be interrogated.