From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id C14153896805 for ; Sat, 17 Apr 2021 10:33:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C14153896805 Received: by mail-ej1-x62c.google.com with SMTP id w23so29911604ejb.9 for ; Sat, 17 Apr 2021 03:33:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=K0904td9AmDEAf6bIifTK0RpgGB1evfeQzIE8FNN6pM=; b=fJBL0JE68z+2LYqwDal/2ndS5U05dz5Avbor5FSmWeWb0tUTe7xO5C8htLpgnAl67y BfR0ZKMOfQutP07qaWix5qsWGKbW9n2hsShFeFzTS9e+E4WuV6AUYTRCck/ttCBttW73 P3Au+7om/dRzSMlyU0aH/U5CXAidMYdZPDcY1V95UEIfY4klguR9uOsBIYOUyxbFOdm1 3p9/aEC4izgpkcUcyz7u1pGABJ08ifPPHbrEv8yQ/KiNFNSLp2EsjJb7LSfM/I0lD70D 2EtYLwYwiNyLpqaR3+a0mKJq5en1CgLnjCf1AC2u2GsncVl+il0TCD6MTVOuREfZPJK9 mqUg== X-Gm-Message-State: AOAM531xQev+Ma+yIhCHGgsf6VSZ1VMH3gpi6WS+x0whHMe89b5wwiOv AZx/I7SfPC4nbllhA52Qq+amGZLZBq2gLErRAGAr8hvxFaI= X-Google-Smtp-Source: ABdhPJymxGrDOtxoiaUFyysB58odAAI0qxPgQ1VRAbTesznHIUnY81/ttSluSgyOUsAheHlqGnnBtKRdXVQ5uqkyzPM= X-Received: by 2002:a17:906:26c9:: with SMTP id u9mr12362348ejc.520.1618655589476; Sat, 17 Apr 2021 03:33:09 -0700 (PDT) MIME-Version: 1.0 From: "J. Vincent Toups" Date: Sat, 17 Apr 2021 06:32:58 -0400 Message-ID: Subject: Extensible Macros and define-library To: kawa@sourceware.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: kawa@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Kawa mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Apr 2021 10:33:12 -0000 I'm seeking ideas on how to implement an extensible pattern matcher. You can see the code as it currently is here: https://github.com/VincentToups/kawa-lib/blob/main/lib/shadchen.scm Usage look like this (define (example arg) (match arg ((string? x) x) ((number? x) x) ((list a b c) (list c b a)) (anything-else (error "Fail")))) (example 10) -> 10 (example '(1 2 3)) -> (3 2 1) When matching against a pattern which is a list, the matcher uses the head of the list to determine what behavior to implement. The list head is interpreted as being in its own namespace (the pattern namespace, if you'd like). If you examine the source code you can see that we dispatch presently on the symbolic value of the head rather than its syntax. The "pattern namespace" is totally static and flat. I'm trying to figure out how to let users extend this pattern matcher. A user defined pattern is naturally thought of as a special kind of syntax-transformer. It takes some syntax in and returns a new pattern using primitive patterns as an implementation language. eg: (define-pattern list-of-two (lambda (expr) (syntax-case expr () ((_ a b) #'(list a b))))) How can I set this system up so that 1. patterns can be exported/imported/etc using define-library 2. they are neither regular functions nor syntax-transformers a. they don't have run time values b. they also aren't interpreted as regular syntax-transformers? I'm not sure such a thing can be defined in pure R7RS/R6RS scheme? I _think_ the essence of this question is whether there is any clever way to set up a separate namespace that I can interact with at syntax-transformation time. I think I could hack something together if there was a way to do a macro-expand-once but in that case I think I'd still have to define patterns as regular syntax-transformations. And it doesn't seem like there is such a thing as "expand-once" in R(6,7)RS or in Kawa. A cute use of such a thing would be to implement something like scala's "unapply" idiom where a specific static method of a class describes how to perform a pattern match for instances. Any advice would be appreciated!