From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id 88F9F3858CD1 for ; Fri, 8 Dec 2023 15:04:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 88F9F3858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 88F9F3858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::532 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702047868; cv=none; b=l4baOrmLHlOYjGZ9Y9legjWc8KWG2Z7IzrP02nxI3J+GY/q/USnI/Sv0FdsoTX/fJ6b85XncB1ELv+RIx89au7AMelUA7MgE0QwglIoXV3Zn8mP+nwC1JHv/A+94J9Neq6pJxtUMikLAIfUG1mdWzl72XMLsQ5PaNvWpY1mdL1I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702047868; c=relaxed/simple; bh=9x+gZnuh+B2MwrQViEkDnpT2+Co7hzDnSBSseKON6js=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=WGKcriTGt7cR06ItlrQjOOEvFqp4FbDPkbj+pHaHOnTkeEsIntk5kggllIU1lJbMG5XSloeVSjxgQpHHVePpB/xDm/v8x490CkgPc9IvuApqGmWv+1eOfWVaDxxQeX3SVCTxMfmm5dAlM6yfKtY1+0zh9YkZHZR8Pe18V1JGjVQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-54c67b0da54so2925856a12.0 for ; Fri, 08 Dec 2023 07:04:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702047865; x=1702652665; darn=sourceware.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=A6jzP/N20F91pZL5x7bM77zV5XTw/V+wJiPZgg+OI/U=; b=QHESKAO3J8nbML8PKY5QQcf9WdgQ85NG//GCKqvegNxdKaikQiz7JGm/tPzl97a2jN CZRxHIx0X4w4/V7dKwQWZKENooAwKlAqJQPeVjdJXbet6w2A/s1bkwZqsevNBI08BD/b Z/wv4EE8iiQKN1geluHyHdEZlEJWT1SyU5/0CAAtzLE1LW8mFsD9bGQt8oyhzvUa3p0D K8XQlso4SW6YYGgPoYur0/TsnPq1SVzVY0Y1L2dX7RUk4hzK78lmG/1NCLFXWPZHAPQf 3pMK3v5cFEdrZ0l/Ko0hsPaU1p9c4GuHAc+1YpB/3fD2XenRX2Nu5K13XPAwxNP8sks5 2jIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702047865; x=1702652665; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=A6jzP/N20F91pZL5x7bM77zV5XTw/V+wJiPZgg+OI/U=; b=UhVAdoyZgLtdh3kqrmF7zABkJD4/lZsgL1fymsd7iYeHPAi6jmJqKlKpOzvmJu8239 u+0StPiXfeYKp5B5NWX1v2rDpu5w0jl8RagqXrsj+Af8/6A1E6as2HjzlnP1u9plqHbd rWBNDTNVlCRRc/c7OjYVnfRJAyli0L5XgUJnUUHwuPyeGOQTpfkWOAV0rudjm3hCq2wt D+YjHzHvn5lGuj5L1ShDlrtvP+1bZnVdv/Tj+3nT8W10CT6N7aol4Uk45lsUrmylyf0V m7fo4ma8yvmOCNhAdDZ4MHlF2KRRyiFlIwiLvlDKwynshICYXX7HvMhtcI/KVIcEueq5 MftA== X-Gm-Message-State: AOJu0Yy5H8h2RboR974Nh1RKCaXW5WY6perXXqgbvEXQa7ilo0wwtpfn MflZjGEfo1kMWeCeyGItfebU13Cw146Qybn+pjS4/ccYgyI= X-Google-Smtp-Source: AGHT+IEI7t/iGtwXbekzjcp5C2ZZtz8gUPFa2G2ciX88uapUs+nBij2dtlrlrXFuT9l5smIaO8/GmheGAiIKs43zbls= X-Received: by 2002:a17:906:b2d2:b0:a1c:a12d:e8e4 with SMTP id cf18-20020a170906b2d200b00a1ca12de8e4mr82985ejb.2.1702047864607; Fri, 08 Dec 2023 07:04:24 -0800 (PST) MIME-Version: 1.0 From: Damien Mattei Date: Fri, 8 Dec 2023 16:04:13 +0100 Message-ID: Subject: Kawa parser for SRFI 105 curly infix and operator precedence optimization To: kawa mailing list Content-Type: multipart/alternative; boundary="0000000000005c55aa060c00e6f0" X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --0000000000005c55aa060c00e6f0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable hello, i have written a SRFI 105 optimizer for curly infix with operator precedence. It allows optimization of infix mathematics expression that are translated in the fastest prefix scheme expressions. For example: {M_i_o[j {i + 1}] <- M_i_o[j {i + 1}] - (- =CE=B7) * z_input[i] * =E1=83=9Bz=E2=B3=86=E1=83=9Bz=CC=83(z_output[j] z=CC=83_output[j]) * =E1=90= =81_i_o[j]} is normally translated in SRFI 105 curly infix in: ($nfx$ (bracket-apply M_i_o j (+ i 1)) <- (bracket-apply M_i_o j (+ i 1)) - (- =CE=B7) * (bracket-apply z_input i) * (=E1=83=9Bz=E2=B3=86=E1=83=9Bz=CC=83 (bracket-apply z_output j) (bracket-= apply z=CC=83_output j)) * (bracket-apply =E1=90=81_i_o j)) but the $nfx$ which should be an infix operator precedence evaluator is a long recursive algorithm that slows down scheme infix evaluation. The parser curly-infix2prefix4kawa.scm precompute symbolically any infix mathematics expression and replace them by prefix scheme expressions before being compiled or interpreted by your favorite scheme implementation. Then the SRFI 105 could be used with any scheme implementation Kawa or other and more than curly infix, infix expression with operator precedence could be used. the expression above: {M_i_o[j {i + 1}] <- M_i_o[j {i + 1}] - (- =CE=B7) * z_input[i] * =E1=83=9Bz=E2=B3=86=E1=83=9Bz=CC=83(z_output[j] z=CC=83_output[j]) * =E1=90= =81_i_o[j]} is then translated in: (<- sum (+ sum (* (=E1=83=9Bz=E2=B3=86=E1=83=9Bz=CC=83 (bracket-applynext (bracket-applynext z (list (+ i 1))) (list k)) (bracket-applynext (bracket-applynext z=CC=83 (list (+ i 1))) (list k))) (* (bracket-applynext (bracket-applynext M (list i)) (list k (+ j 1))) (bracket-applynext (bracket-applynext =E1=90=81 (list (+ i 1))) (list k))))) you will notice there is then no more $nfx$ (from SRFI 105) call and expression is then normal scheme (bracket-applynext is only for [ ] specialisation) . It is more than 4 times faster than without optimization. The optimiser works also for specialisation of [ ] that allow the Python sliced array notation but it is implemented outside the parser then, only optimization is done in the parser: example where the slice notation with : in Python is replaced by $ in scheme (note that Kawa as also its own 'ranges' features for containers see: https://www.gnu.org/software/kawa/Ranges.html ): echo "{#(1 2 3 4 5 6 7)[2 * 5 - 8 $ 3 * 5 - 10 $ 2 * 4 - 6]}" > sliced-array+.scm cat sliced-array+.scm {#(1 2 3 4 5 6 7)[2 * 5 - 8 $ 3 * 5 - 10 $ 2 * 4 - 6]} parsing of the file : kawa curly-infix2prefix4kawa.scm --infix-optimize --infix-optimize-slice sliced-array+.scm curly-infix2prefix4kawa.scm:421:5: warning - unreachable code (bracket-applynext #(1 2 3 4 5 6 7) (list (- (* 2 5) 8) $ (- (* 3 5) 10) $ (- (* 2 4) 6))) testing of the expression : kawa -Dkawa.import.path=3D".:/Users/mattei/Scheme-PLUS-for-Kawa:./kawa" #|kawa:1|# (require Scheme+) #|kawa:3|# bracket-applynext # #|kawa:4|# (bracket-applynext #(1 2 3 4 5 6 7) #|.....5|# (list (- (* 2 5) 8) $ (- (* 3 5) 10) $ (- (* 2 4) 6))) #(3 5) the result is #(3 5) because the slicing of {#(1 2 3 4 5 6 7)[2 * 5 - 8 $ 3 * 5 - 10 $ 2 * 4 - 6]} is equal to the slicing of {#(1 2 3 4 5 6 7)[2 $ 5 $ 2]} the parser is available (with its includes files in the same directory) here: https://github.com/damien-mattei/Scheme-PLUS-for-Kawa/blob/main/curly-infix= 2prefix4kawa.scm Damien --0000000000005c55aa060c00e6f0--