From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Ricardo Wurmus Newsgroups: gmane.lisp.guile.user Subject: Re: emulate "sum type" pattern matching? Date: Thu, 12 Mar 2020 15:56:47 +0100 Message-ID: <87wo7pmxjk.fsf@elephly.net> References: <87sgier7eb.fsf@gmail.com> <20200312144622.0c660172af70ced0493ce9b1@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="21161"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.2.0; emacs 26.3 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:57:20 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 1jCPGx-0005Jk-Pw for guile-user@m.gmane-mx.org; Thu, 12 Mar 2020 15:57:19 +0100 Original-Received: from localhost ([::1]:42844 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCPGw-00046j-LZ for guile-user@m.gmane-mx.org; Thu, 12 Mar 2020 10:57:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40081) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCPGc-0003yv-2l for guile-user@gnu.org; Thu, 12 Mar 2020 10:56:59 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jCPGa-0005h5-Jx for guile-user@gnu.org; Thu, 12 Mar 2020 10:56:57 -0400 Original-Received: from sender4-of-o51.zoho.com ([136.143.188.51]:21196) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jCPGa-0005g6-6x for guile-user@gnu.org; Thu, 12 Mar 2020 10:56:56 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1584025014; cv=none; d=zohomail.com; s=zohoarc; b=EO3WpM03IwIArETsrpOzBkQrx77mSc5hV0x6yvVVS+SEmHtTwoOatggTIFWyWEo2Ua8dJ2Mn3MXspBEduYkp9/RRinS22H9oEhdApeeq2nk6a0o/LiiqId9SABT8hWnzAg323JBerHfwPumZPLgJ2dG4VYutFVOLdRtoWS6KBmA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584025014; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=+zxC6ILx0d+x2ESBvISjmEoEHYtd7LeGT8drXBpsnc4=; b=a+BRgUs9JQsU78FxTFqdyufXnL624u6W6lsM3k6i6WeNoPc4tKJQrxPdQWS5lx75eUX4w3GoAOVkkJzdbvN53HSZBcAdpS52YzLb91nMJyoyd4k+Eb+gLSLTscdMiEu8xqgMvCxH0p+ejXfVidOjWpVcElMNXd4ODsHwX4EQVzs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1584025014; s=zoho; d=elephly.net; i=rekado@elephly.net; h=References:From:To:Cc:Subject:In-reply-to:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=+zxC6ILx0d+x2ESBvISjmEoEHYtd7LeGT8drXBpsnc4=; b=SFzAefZA4FmqS7Fxa36myeSA5P7zccahDEN7QPhjp0Gh8erRLxJqqAD22lKNLAdh z+2NUaLTl1dOq4EBl/pkVMe1TTvyMs8ssun7pDZuuRBaO0idcJJKgoqy61nwh7AveU2 vls6BYHRX7r7npfCCtO7oElU+0YIhgA7IX8rZnE0= Original-Received: from localhost (p54AD4B4A.dip0.t-ipconnect.de [84.173.75.74]) by mx.zohomail.com with SMTPS id 1584025011050312.7403013708488; Thu, 12 Mar 2020 07:56:51 -0700 (PDT) In-reply-to: <20200312144622.0c660172af70ced0493ce9b1@gmail.com> X-URL: https://elephly.net X-PGP-Key: https://elephly.net/rekado.pubkey X-PGP-Fingerprint: BCA6 89B6 3655 3801 C3C6 2150 197A 5888 235F ACAC X-ZohoMailClient: External X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 136.143.188.51 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:16292 Archived-At: Chris Vine writes: > On Wed, 11 Mar 2020 19:58:04 +0000 > Sam Halliday wrote: >> Hi all, >>=20 >> 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. >>=20 >> 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. >>=20 >> 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. >>=20 >> 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? >>=20 >> 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 >>=20 >> (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)]))) >>=20 >> 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. >>=20 >> 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). You can match on anything as long as there is a predicate for it. For a GOOPS object that has a procedure =E2=80=9Cthing?=E2=80=9D that returns #t = when the argument is of the expected type you can match with (? thing? thing) You cannot, as far as I know, use match to know at compile time that the clauses are exhaustive. --=20 Ricardo