├── .github ├── actions │ └── install-rhombus │ │ └── action.yml └── workflows │ └── test.yml ├── .gitignore ├── LICENSE ├── rhombus-compat ├── compat │ ├── custodian.rhm │ ├── dict.rhm │ ├── extra.rhm │ ├── format.rhm │ ├── function.rhm │ ├── logging.rhm │ ├── port.rhm │ ├── stream-test.rhm │ ├── stream.rhm │ ├── string.rhm │ ├── tcp.rhm │ ├── thread.rhm │ ├── time.rhm │ ├── url.rhm │ └── xml.rhm └── info.rkt ├── rhombus-examples-echo ├── echo_client.rhm ├── echo_server.rhm └── info.rkt ├── rhombus-examples-heatbugs ├── heatbugs.rhm ├── info.rkt └── showbug.rhm ├── rhombus-examples-kanren ├── info.rkt └── microkanren.rhm ├── rhombus-examples-sheepdogs ├── README.md ├── info.rkt └── sheepdogs.rhm └── rhombus-examples-web ├── README.md ├── info.rkt ├── web.rkt └── web └── private ├── disposable.rkt ├── http.rkt ├── keyring.rkt ├── racket.rkt ├── rhombus-names.rkt └── url.rkt /.github/actions/install-rhombus/action.yml: -------------------------------------------------------------------------------- 1 | name: "Install Rhombus prototype" 2 | runs: 3 | using: "composite" 4 | steps: 5 | - name: Current Date 6 | id: get-date 7 | run: echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT 8 | shell: bash 9 | - name: Cache base install 10 | id: cache 11 | uses: actions/cache@v3.2.4 12 | with: 13 | key: ${{ steps.get-date.outputs.date }} 14 | path: | 15 | /opt/racket 16 | - name: install racket 17 | if: steps.cache.outputs.cache-hit != 'true' 18 | uses: Bogdanp/setup-racket@v1.9.1 19 | with: 20 | architecture: 'x64' 21 | distribution: 'full' 22 | variant: 'CS' 23 | version: "current" 24 | dest: "/opt/racket" 25 | - name: install rhombus prototype 26 | if: steps.cache.outputs.cache-hit != 'true' 27 | run: sudo /opt/racket/bin/raco pkg install -i --batch --auto https://github.com/racket/rhombus-prototype.git 28 | shell: bash 29 | - name: update PATH 30 | if: steps.cache.outputs.cache-hit == 'true' 31 | run: echo "PATH=/opt/racket/bin:$PATH" >> $GITHUB_ENV 32 | shell: bash 33 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | on: 3 | pull_request: 4 | schedule: 5 | - cron: "17 1 * * *" 6 | jobs: 7 | test_compat: 8 | runs-on: "ubuntu-latest" 9 | steps: 10 | - name: checkout 11 | uses: actions/checkout@master 12 | - name: Install Rhombus prototype 13 | uses: ./.github/actions/install-rhombus 14 | - name: install Racket compat library 15 | run: raco pkg install --batch --auto ./rhombus-compat 16 | - name: check dependencies 17 | run: raco setup --check-pkg-deps --unused-pkg-deps --pkgs rhombus-compat 18 | 19 | test_web: 20 | needs: test_compat 21 | runs-on: "ubuntu-latest" 22 | steps: 23 | - name: checkout 24 | uses: actions/checkout@master 25 | - name: Install Rhombus prototype 26 | uses: ./.github/actions/install-rhombus 27 | - name: install Racket compat library 28 | run: raco pkg install --batch --auto ./rhombus-compat 29 | - name: install web example 30 | run: raco pkg install --batch --auto ./rhombus-examples-web 31 | - name: check dependencies 32 | run: raco setup --check-pkg-deps --unused-pkg-deps --pkgs rhombus-examples-web 33 | 34 | test_kanren: 35 | needs: test_compat 36 | runs-on: "ubuntu-latest" 37 | steps: 38 | - name: checkout 39 | uses: actions/checkout@master 40 | - name: Install Rhombus prototype 41 | uses: ./.github/actions/install-rhombus 42 | - name: install Racket compat library 43 | run: raco pkg install --batch --auto ./rhombus-compat 44 | - name: install kanren example 45 | run: raco pkg install --batch --auto ./rhombus-examples-kanren 46 | - name: check dependencies 47 | run: raco setup --check-pkg-deps --unused-pkg-deps --pkgs rhombus-examples-kanren 48 | 49 | test_sheepdog: 50 | needs: test_compat 51 | runs-on: "ubuntu-latest" 52 | steps: 53 | - name: checkout 54 | uses: actions/checkout@master 55 | - name: Install Rhombus prototype 56 | uses: ./.github/actions/install-rhombus 57 | - name: install Racket compat library 58 | run: raco pkg install --batch --auto ./rhombus-compat 59 | - name: install sheepdogs example 60 | run: raco pkg install --batch --auto ./rhombus-examples-sheepdogs 61 | - name: check dependencies 62 | run: raco setup --check-pkg-deps --unused-pkg-deps --pkgs rhombus-examples-sheepdogs 63 | 64 | test_echo: 65 | needs: test_compat 66 | runs-on: "ubuntu-latest" 67 | steps: 68 | - name: checkout 69 | uses: actions/checkout@master 70 | - name: Install Rhombus prototype 71 | uses: ./.github/actions/install-rhombus 72 | - name: install Racket compat library 73 | run: raco pkg install --batch --auto ./rhombus-compat 74 | - name: install echo example 75 | run: raco pkg install --batch --auto ./rhombus-examples-echo 76 | - name: check dependencies 77 | run: raco setup --check-pkg-deps --unused-pkg-deps --pkgs rhombus-examples-echo 78 | 79 | test_heatbugs: 80 | needs: test_compat 81 | runs-on: "ubuntu-latest" 82 | steps: 83 | - name: checkout 84 | uses: actions/checkout@master 85 | - name: Install Rhombus prototype 86 | uses: ./.github/actions/install-rhombus 87 | - name: install Racket compat library 88 | run: raco pkg install --batch --auto ./rhombus-compat 89 | - name: install heatbugs example 90 | run: raco pkg install --batch --auto ./rhombus-examples-heatbugs 91 | - name: check dependencies 92 | run: raco setup --check-pkg-deps --unused-pkg-deps --pkgs rhombus-examples-heatbugs 93 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .*.swp 2 | compiled/ 3 | *~ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /rhombus-compat/compat/custodian.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | rhombus/meta open 5 | 6 | lib("racket/base.rkt") as r 7 | lib("racket/base.rkt") as custodian: 8 | rename: 9 | #{current-custodian} as current 10 | #{custodian-shutdown-all} as shutdown 11 | #{make-custodian} as _make 12 | #{custodian-limit-memory} as limit_memory 13 | 14 | export: 15 | custodian.current 16 | custodian.shutdown 17 | custodian.limit_memory 18 | make 19 | Custodian 20 | custodian_dot_provider 21 | 22 | annot.macro 'Custodian': 23 | annot_meta.pack_predicate('r.#{custodian?}', 24 | '(($(statinfo_meta.dot_provider_key), 25 | custodian_dot_provider))') 26 | 27 | dot.macro 'custodian_dot_provider $left $dot $right': 28 | match right 29 | | 'limit_memory': 'fun(arg): custodian.limit_memory($left, arg)' 30 | | 'shutdown': 'fun(): custodian.shutdown($left)' 31 | 32 | fun make(c :: Custodian = custodian.current()) :~ Custodian: 33 | custodian._make(c) 34 | -------------------------------------------------------------------------------- /rhombus-compat/compat/dict.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | rhombus/meta open 5 | lib("racket/dict.rkt"): 6 | rename: 7 | #{dict-ref} as ref 8 | #{dict-set} as set 9 | #{dict?} as is_dict 10 | 11 | export: 12 | all_from(.dict) 13 | dict_dot_provider 14 | Dict 15 | 16 | annot.macro 'Dict': 17 | annot_meta.pack_predicate('dict.is_dict', 18 | '(($(statinfo_meta.dot_provider_key), 19 | dict_dot_provider))') 20 | 21 | dot.macro 'dict_dot_provider $left $dot $right': 22 | match right 23 | // One argument functions 24 | | 'ref': 'fun(arg): dict.ref($left, arg)' 25 | | 'set': 'fun(key, val): dict.set($left, key, val)' 26 | -------------------------------------------------------------------------------- /rhombus-compat/compat/extra.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | 4 | import: lib("racket/base.rkt") as r 5 | rhombus/meta open 6 | 7 | export: 8 | letcc 9 | 10 | expr.macro 'letcc $k: $body': 11 | 'r.#{call/cc}(fun($k): $body)' 12 | 13 | -------------------------------------------------------------------------------- /rhombus-compat/compat/format.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | lib("racket/format.rkt"): 5 | rename: 6 | #{~a} as a 7 | #{~r} as r 8 | 9 | export: 10 | all_from(.format) 11 | -------------------------------------------------------------------------------- /rhombus-compat/compat/function.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | export: 4 | |> 5 | thunk 6 | loop 7 | 8 | macro 'thunk: $body': 9 | 'fun (): $body' 10 | 11 | macro 'loop: $body': 12 | '(block: 13 | fun lp(): 14 | $body 15 | lp() 16 | lp())' 17 | 18 | operator (a |> b): b(a) 19 | -------------------------------------------------------------------------------- /rhombus-compat/compat/logging.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus/static/and_meta 2 | 3 | import: 4 | lib("racket/base.rkt") as r 5 | 6 | export: 7 | def_logger 8 | 9 | decl.macro 10 | | 'def_logger $(name :: Identifier)': 11 | 'def_logger $name ~topic: #'$name' 12 | | 'def_logger $(name :: Identifier) ~topic: $topic_expr': 13 | 'def $name :~ Logger = r.#{make-logger}($topic_expr, r.#{current-logger}())' 14 | 15 | annot.macro 'Logger': 16 | annot_meta.pack_predicate('r.#{logger?}', 17 | '(($statinfo_meta.dot_provider_key, 18 | logger_dot_provider))') 19 | 20 | meta: 21 | syntax_class LogLevel 22 | | 'fatal' 23 | | 'error' 24 | | 'warning' 25 | | 'info' 26 | | 'debug' 27 | 28 | dot.macro 29 | | 'logger_dot_provider $lt . when': 30 | ~tail '($(level :: LogLevel)): 31 | $body 32 | ...' 33 | values('when r.#{log-level?}($lt, #'$level) 34 | | $body 35 | ...', 36 | '') 37 | | 'logger_dot_provider $lt . log_message': 38 | ~tail '($level, $msg, $vals, ...)' 39 | values('r.#{log-message}($lt, $level, r.format($msg, $vals, ...))', '') 40 | | 'logger_dot_provider $lt . $(rt :: LogLevel)': 41 | ~tail '($msg, $vals, ...)' 42 | values('($lt :~ Logger).when($rt): 43 | ($lt :~ Logger).log_message(#'$rt, $msg, $vals, ...)', 44 | '') 45 | -------------------------------------------------------------------------------- /rhombus-compat/compat/port.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | rhombus/meta open 5 | lib("racket/base.rkt") as r 6 | lib("racket/port.rkt") as p 7 | 8 | export: 9 | port_dot_provider 10 | Port 11 | input_port_dot_provider 12 | InputPort 13 | output_port_dot_provider 14 | OutputPort 15 | 16 | fun close_port(p): 17 | if r.#{input-port?}(p) 18 | | r.#{close-input-port}(p) 19 | | r.#{close-output-port}(p) 20 | 21 | fun is_port(p): r.#{input-port?}(p) || r.#{output-port?}(p) 22 | 23 | annot.macro 'Port': 24 | annot_meta.pack_predicate('is_port', 25 | '(($(statinfo_meta.dot_provider_key), 26 | port_dot_provider))') 27 | 28 | dot.macro 'port_dot_provider $left $dot $right': 29 | match right 30 | | 'close': 'fun (): close_port($left)' 31 | 32 | annot.macro 'InputPort': 33 | annot_meta.pack_predicate('r.#{input-port?}', 34 | '(($(statinfo_meta.dot_provider_key), 35 | input_port_dot_provider))') 36 | 37 | dot.macro 'input_port_dot_provider $left $dot $right': 38 | match right 39 | | 'match': 'fun(arg): r.#{regexp-match}(arg, $left)' 40 | | 'readline': 'fun (): r.#{read-line}($left)' 41 | | 'readline_evt': 'fun (): p.#{read-line-evt}($left)' 42 | | 'close': 'fun (): r.#{close-input-port}($left)' 43 | | 'close_evt': 'fun (): r.#{port-closed-evt}($left)' 44 | 45 | annot.macro 'OutputPort': 46 | annot_meta.pack_predicate('r.#{output-port?}', 47 | '(($(statinfo_meta.dot_provider_key), 48 | output_port_dot_provider))') 49 | 50 | dot.macro 'output_port_dot_provider $left $dot $right': 51 | match right 52 | | 'print': 'fun(arg): print(arg, $left)' 53 | | 'display': 'fun(arg): r.display(arg, $left)' 54 | | 'println': 'fun(arg): println(arg, $left)' 55 | | 'displayln': 'fun(arg): r.displayln(arg, $left)' 56 | | 'flush': 'fun(): r.#{flush-output}($left)' 57 | | 'close': 'fun (): r.#{close-output-port}($left)' 58 | | 'close_evt': 'fun (): r.#{port-closed-evt}($left)' 59 | -------------------------------------------------------------------------------- /rhombus-compat/compat/stream-test.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | module test: 4 | import: 5 | "stream.rhm": open 6 | 7 | check [1,2,3] ~is_a Stream 8 | 9 | check Stream(1, 2, 3) ~is_a Stream 10 | 11 | check Stream() ~is_a Stream.empty 12 | 13 | check Stream(~rest: Stream.empty) ~is_a Stream.empty 14 | 15 | check Stream.empty ~is_a Stream.empty 16 | 17 | check: 18 | use_static 19 | let v :: Stream = [1, 2, 3] 20 | v.first 21 | ~is 1 22 | 23 | check: 24 | use_static 25 | let v :: Stream.of(Int) = [1, 2, 3] 26 | v.first 27 | ~is 1 28 | 29 | check: 30 | use_static 31 | Stream.empty.is_empty 32 | ~is #true 33 | 34 | check: 35 | use_static 36 | let v :: Stream.of(String) = ["abc"] 37 | v.first.length() 38 | ~is 3 39 | 40 | check: 41 | use_static 42 | let v :: Stream.of(String) = ["abc"] 43 | Stream.first(v).length() 44 | ~is 3 45 | 46 | check: 47 | use_static 48 | let v :: Stream.of(String) = ["abc", "defg"] 49 | v.rest 50 | ~is ["defg"] 51 | 52 | check: 53 | use_static 54 | let v :: Stream.of(String) = ["abc", "defg"] 55 | v.rest.first.length() 56 | ~is 4 57 | 58 | check: 59 | use_static 60 | let v :: Stream.of(String) = ["abc", "defg"] 61 | v.take(1).first 62 | ~is "abc" 63 | 64 | check: 65 | use_static 66 | let v :: Stream.of(String) = ["abc", "defg"] 67 | v.take(1).rest 68 | ~is_a Stream.empty 69 | 70 | check: 71 | use_static 72 | let v :: Stream.of(String) = Stream("defg", "hijkl") 73 | let u :: Stream.of(String) = Stream("abc", ~rest: v) 74 | u.rest.first.length() 75 | ~is 4 76 | -------------------------------------------------------------------------------- /rhombus-compat/compat/stream.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus/static/and_meta 2 | 3 | import: 4 | lib("racket/stream.rkt") 5 | 6 | export: 7 | Stream 8 | 9 | statinfo.key #%stream_of_info: 10 | ~union: statinfo_meta.union 11 | ~intersect: statinfo_meta.intersect 12 | 13 | meta: 14 | namespace stream_meta: 15 | export infos_key make_statinfo 16 | def infos_key = '#%stream_of_info' 17 | 18 | fun make_statinfo(of_info :: Syntax = '()'): 19 | '(($(statinfo_meta.dot_provider_key), stream_dot_provider), 20 | ($(infos_key), $of_info))' 21 | 22 | expr.macro 23 | | 'Stream($e, ..., ~rest: $r)': 24 | statinfo_meta.wrap('stream.#{stream*}($e, ..., $r)', 25 | stream_meta.make_statinfo()) 26 | | 'Stream($e, ...)': 27 | statinfo_meta.wrap('stream.#{stream*}($e, ..., Stream.empty)', 28 | stream_meta.make_statinfo()) 29 | 30 | namespace Stream: 31 | export: 32 | of 33 | is_stream 34 | is_empty 35 | empty 36 | first 37 | rest 38 | take 39 | to_list 40 | 41 | annot.macro 'of($(ann :: annot_meta.Parsed))': 42 | def (ann_pred, ann_infos) = annot_meta.unpack_predicate(ann) 43 | annot_meta.pack_predicate('Stream.is_stream', stream_meta.make_statinfo(ann_infos)) 44 | 45 | def is_stream = stream.#{stream?} 46 | def is_empty = stream.#{stream-empty?} 47 | // TODO add static info to `to_list` 48 | def to_list = stream.#{stream->list} 49 | 50 | def empty = stream.#{empty-stream} 51 | statinfo.macro 'empty': stream_meta.make_statinfo() 52 | 53 | annot.macro 'empty': 54 | annot_meta.pack_predicate('is_empty', stream_meta.make_statinfo()) 55 | 56 | expr.macro 57 | | 'first($(e :: expr_meta.Parsed))': 58 | let infos = statinfo_meta.lookup(e, stream_meta.infos_key) || '()' 59 | statinfo_meta.wrap('stream.#{stream-first}($e)', infos) 60 | | 'first': 'stream.#{stream-first}' 61 | 62 | expr.macro 63 | | 'rest($(e :: expr_meta.Parsed))': 64 | let infos = statinfo_meta.lookup(e, stream_meta.infos_key) || '()' 65 | statinfo_meta.wrap('stream.#{stream-rest}($e)', stream_meta.make_statinfo(infos)) 66 | | 'rest': 67 | let infos = stream_meta.make_statinfo() 68 | statinfo_meta.wrap('stream.#{stream-rest}', 69 | '(($(statinfo_meta.call_result_key), 70 | $(statinfo_meta.pack(infos))))') 71 | 72 | expr.macro 73 | | 'take($(e :: expr_meta.Parsed))': 74 | let of_infos = statinfo_meta.lookup(e, stream_meta.infos_key) || '()' 75 | let infos = stream_meta.make_statinfo(of_infos) 76 | statinfo_meta.wrap('fun (n :: NonnegInt): take($e, n)', 77 | '(($(statinfo_meta.call_result_key), 78 | $(statinfo_meta.pack(infos))))') 79 | | 'take($(e :: expr_meta.Parsed), $n)': 80 | let infos = statinfo_meta.lookup(e, stream_meta.infos_key) || '()' 81 | statinfo_meta.wrap('stream.#{stream-take}($e, $n)', 82 | stream_meta.make_statinfo(infos)) 83 | | 'take': 84 | let result_info = stream_meta.make_statinfo() 85 | statinfo_meta.wrap('stream.#{stream-take}', 86 | '(($(statinfo_meta.call_result_key), 87 | $(statinfo_meta.pack(result_info))))') 88 | 89 | annot.macro 'Stream': 90 | annot_meta.pack_predicate('Stream.is_stream', stream_meta.make_statinfo()) 91 | 92 | dot.macro 'stream_dot_provider $left $dot $right': 93 | match right 94 | | 'is_empty': 'Stream.is_empty($left)' 95 | | 'first': 'Stream.first($left)' 96 | | 'rest': 'Stream.rest($left)' 97 | | 'take': 'Stream.take($left)' 98 | | 'to_list': 'Stream.to_list($left)' 99 | -------------------------------------------------------------------------------- /rhombus-compat/compat/string.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | // FIXME: Steal @soegaard's code instead 4 | // https://github.com/soegaard/rhombus-experiments/blob/main/spelling-corrector/string.rkt 5 | 6 | import: 7 | lib("racket/base.rkt") as string: 8 | rename: 9 | #{string->number} as to_number 10 | #{string-append} as sappend 11 | 12 | 13 | operator(a ++ b): 14 | string.sappend(a, b) 15 | 16 | export: string ++ 17 | -------------------------------------------------------------------------------- /rhombus-compat/compat/tcp.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | rhombus/meta open 5 | "port.rhm" open 6 | lib("racket/tcp.rkt"): 7 | rename: 8 | #{tcp-connect} as connect 9 | #{tcp-listen} as listen 10 | #{tcp-accept} as accept 11 | #{tcp-close} as close 12 | 13 | export: 14 | all_from(.tcp) 15 | Listener 16 | tcp_listener_dot_provider 17 | 18 | annot.macro 'Listener': 19 | annot_meta.pack_predicate('tcp.#{tcp-listener?}', 20 | '(($(statinfo_meta.dot_provider_key), 21 | tcp_listener_dot_provider))') 22 | 23 | dot.macro 'tcp_listener_dot_provider $left $dot $right': 24 | match right 25 | // One argument functions 26 | | 'close': 'fun(): tcp.close($left)' 27 | | 'accept': 'fun() :~ (InputPort, OutputPort) : tcp.accept($left)' 28 | | 'accept_evt': 'fun (): tcp.#{tcp-accept-evt}($left)' 29 | -------------------------------------------------------------------------------- /rhombus-compat/compat/thread.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus/static/and_meta 2 | 3 | import: 4 | lib("racket/base.rkt") as r 5 | 6 | export: 7 | Thread 8 | thread_dot_provider 9 | thread 10 | rename: 11 | r.#{current-thread} as current 12 | 13 | Evt 14 | Channel 15 | 16 | 17 | annot.macro 'Thread': 18 | annot_meta.pack_predicate('r.#{thread?}', 19 | '(($(statinfo_meta.dot_provider_key), 20 | thread_dot_provider))') 21 | 22 | dot.macro 'thread_dot_provider $lt $dot $rt': 23 | match rt 24 | | 'dead_evt': 'fun () :~ Evt: r.{thread-dead-evt}($lt)' 25 | | 'kill': 'fun (): r.#{kill-thread}($lt)' 26 | | 'wait': 'fun (): r.#{thread-wait}($lt)' 27 | | _: #false 28 | 29 | expr.macro 'thread: $body': 30 | 'r.thread(fun (): $body) :~ Thread' 31 | 32 | 33 | meta: 34 | def evt_statinfo = '(($(statinfo_meta.dot_provider_key), evt_dot_provider))' 35 | def evt_result_statinfo: 36 | '(($(statinfo_meta.call_result_key), 37 | $(statinfo_meta.pack(evt_statinfo))))' 38 | 39 | namespace Evt: 40 | export: 41 | handle 42 | wrap 43 | 44 | def handle = r.#{handle-evt} 45 | statinfo.macro 'handle': evt_result_statinfo 46 | 47 | def wrap = r.#{wrap-evt} 48 | statinfo.macro 'wrap': evt_result_statinfo 49 | 50 | def guard = r.#{guard-evt} 51 | statinfo.macro 'guard': evt_result_statinfo 52 | 53 | annot.macro 'Evt': 54 | annot_meta.pack_predicate('r.#{evt?}', evt_statinfo) 55 | 56 | dot.macro 'evt_dot_provider $lt . $rt': 57 | match rt 58 | | 'handle': 'fun(f) :~ Evt: Evt.handle($lt, f)' 59 | | 'wrap': 'fun(f) :~ Evt: Evt.wrap($lt, f)' 60 | | _: #false 61 | 62 | 63 | namespace Channel: 64 | export: 65 | get 66 | put 67 | put_evt 68 | 69 | def get = r.#{channel-get} 70 | def put = r.#{channel-put} 71 | 72 | def put_evt = r.#{channel-put-evt} 73 | statinfo.macro 'put_evt': evt_result_statinfo 74 | 75 | annot.macro 'Channel': 76 | annot_meta.pack_predicate('r.#{channel?}', 77 | '(($(statinfo_meta.dot_provider_key), 78 | channel_dot_provider))') 79 | 80 | dot.macro 'channel_dot_provider $lt . $rt': 81 | match rt 82 | | 'get': 'fun(): Channel.get($lt)' 83 | | 'put': 'fun(v): Channel.put($lt, v)' 84 | | 'put_evt': 'fun (v) :~ Evt: Channel.put_evt($lt, v)' 85 | | _: #false 86 | 87 | fun Channel() :~ Channel: 88 | r.#{make-channel}() 89 | 90 | -------------------------------------------------------------------------------- /rhombus-compat/compat/time.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | lib("racket/base.rkt") as r 5 | 6 | export: 7 | sleep; now; now_inexact 8 | 9 | fun sleep(n :: Number): 10 | r.sleep(n) 11 | 12 | fun now() :: Int: 13 | r.#{current-seconds}() 14 | 15 | fun now_inexact(): 16 | r.#{current-inexact-milliseconds}() 17 | -------------------------------------------------------------------------------- /rhombus-compat/compat/url.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | lib("net/url.rkt"): 5 | rename: 6 | #{string->url} as from_string 7 | #{url-path} as path 8 | #{path/param-path} as param_path 9 | 10 | export: 11 | query 12 | all_from(.url) 13 | 14 | fun query(u): 15 | for Map: 16 | each Pair(k,v): url.#{url-query}(u) 17 | values(k, v && to_string(v)) 18 | -------------------------------------------------------------------------------- /rhombus-compat/compat/xml.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | export: xml 4 | import: 5 | lib("xml/main.rkt") as xml: 6 | rename: 7 | #{xexpr->string} as to_string 8 | -------------------------------------------------------------------------------- /rhombus-compat/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define name "rhombus-compat") 4 | (define collection "rhombus") 5 | (define deps 6 | '("base" 7 | "rhombus-lib")) 8 | (define module-suffixes '(#"rhm")) 9 | 10 | -------------------------------------------------------------------------------- /rhombus-examples-echo/echo_client.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus/static 2 | 3 | import: 4 | lib("racket/base.rkt"): 5 | expose: 6 | random 7 | sleep 8 | void 9 | rhombus/compat/function: 10 | expose: 11 | thunk 12 | rhombus/compat/port 13 | rhombus/compat/tcp 14 | 15 | fun run(count, inp :~ port.InputPort, outp :~ port.OutputPort): 16 | unless (count == 0) 17 | | outp.println("ping " +& count) 18 | outp.flush() 19 | println("sent: " +& count) 20 | println("received: " +& inp.readline()) 21 | sleep(random(5)) 22 | run(count - 1, inp, outp) 23 | 24 | fun start(count = 10): 25 | def values(inp :: port.InputPort, outp :: port.OutputPort): 26 | tcp.connect("localhost", 12777) 27 | try: 28 | run(count, inp, outp) 29 | ~finally: 30 | inp.close() 31 | outp.close() 32 | 33 | start() 34 | -------------------------------------------------------------------------------- /rhombus-examples-echo/echo_server.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus/static 2 | 3 | import: 4 | rhombus/meta open 5 | lib("racket/base.rkt"): 6 | rename: 7 | #{eof-object?} as is_eof 8 | #{choice-evt} as choice_evt 9 | #{guard-evt} as guard_evt 10 | #{handle-evt} as handle_evt 11 | #{make-channel} as make_channel 12 | expose: 13 | choice_evt 14 | guard_evt 15 | handle_evt 16 | make_channel 17 | sync 18 | void 19 | rhombus/compat/port 20 | rhombus/compat/tcp 21 | rhombus/compat/thread 22 | 23 | expr.macro 'select_evt 24 | | $evt_expr ... ($evt_args, ...): 25 | $evt_handler 26 | | ...': 27 | 'sync(handle_evt($evt_expr ..., fun ($evt_args, ...): $evt_handler), ...)' 28 | 29 | def shutdown_ch: make_channel() 30 | def shutdown_evt: guard_evt(fun (): shutdown_ch) 31 | def listener :: tcp.Listener: tcp.listen(12777) 32 | def listener_evt: listener.accept_evt() 33 | 34 | fun handle_client_connection(n, inp :~ port.InputPort, outp :~ port.OutputPort): 35 | def close_evt: choice_evt(inp.close_evt(), outp.close_evt()) 36 | fun echo(): 37 | select_evt 38 | | close_evt (_ignore): 39 | println(n +& " closing on closed port") 40 | inp.close() 41 | outp.close() 42 | | inp.readline_evt() (line): 43 | println(n +& " received: " +& line) 44 | if (base.is_eof(line)) 45 | | println(n +& " closing on eof") 46 | inp.close() 47 | outp.close() 48 | | outp.println(line) 49 | outp.flush() 50 | echo() 51 | echo() 52 | println(n +& " exiting client thread") 53 | 54 | fun run(n = 0): 55 | select_evt 56 | | shutdown_evt (_ignore): void() 57 | | listener_evt ([inp :: port.InputPort, outp :: port.OutputPort]): 58 | thread.thread: 59 | handle_client_connection(n, inp, outp) 60 | run(n + 1) 61 | 62 | run() 63 | -------------------------------------------------------------------------------- /rhombus-examples-echo/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define name "rhombus-examples-echo") 4 | (define collection "rhombus-examples") 5 | (define deps 6 | '("base" 7 | "rhombus-prototype" 8 | "rhombus-compat")) 9 | (define module-suffixes '(#"rhm")) 10 | 11 | -------------------------------------------------------------------------------- /rhombus-examples-heatbugs/heatbugs.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus/static 2 | 3 | import: 4 | lib("racket/base.rkt").sort 5 | lib("racket/list.rkt").shuffle 6 | 7 | export: 8 | max_target_temp 9 | world_width 10 | world_height 11 | Posn 12 | Heatbug 13 | Environment 14 | 15 | def world_width = 20 16 | def world_height = 20 17 | 18 | def min_target_temp = 10 19 | def max_target_temp = 20 20 | 21 | def min_emit_temp = 0.5 22 | def max_emit_temp = 2.0 23 | 24 | def diffuse_amount = 0.2 25 | def evaporate_amount = 0.01 26 | 27 | fun random_range(lo :: Real, hi :: Real) :~ Real: 28 | math.random() * (hi - lo) + lo 29 | 30 | class ArrayView(arr :: Array, x_off :: Int, y_off :: Int): 31 | method at(i :: NonnegInt): 32 | let x = i rem world_width 33 | let y = i div world_width 34 | let x1 = (x + this.x_off) mod world_width 35 | let y1 = (y + this.y_off) mod world_height 36 | let j = x1 + y1 * world_width 37 | this.arr[j] 38 | 39 | class Posn(x, y): 40 | export random 41 | fun random(): 42 | Posn(math.random(world_width), 43 | math.random(world_height)) 44 | 45 | property index :: NonnegInt: 46 | (this.y * world_width) + this.x 47 | 48 | method neighbors() :: List.of(Posn): 49 | for List: 50 | each i: -1..2 51 | each j: -1..2 52 | //skip_when i == 0 && j == 0 53 | Posn((x + i) mod world_width, (y + j) mod world_height) 54 | 55 | class Heatbug(~posn: mutable posn :: Posn, 56 | ~target_temp: target_temp :: Real, 57 | ~emit_temp: emit_temp :: Real): 58 | constructor 59 | | (): 60 | super(~posn: Posn.random(), 61 | ~target_temp: random_range(min_target_temp, max_target_temp), 62 | ~emit_temp: random_range(min_emit_temp, max_emit_temp)) 63 | | (~posn, ~target_temp, ~emit_temp): 64 | super(~posn: posn, ~target_temp: target_temp, ~emit_temp: emit_temp) 65 | 66 | property index :: NonnegInt: 67 | this.posn.index 68 | 69 | method current_happiness(env :: Environment): 70 | def t = env.temp_at(this.posn) 71 | this.happiness(t) 72 | 73 | method happiness(cell_temp): 74 | math.abs(cell_temp - this.target_temp) 75 | 76 | method best_moves(env :: Environment): 77 | let neighbors = shuffle(this.posn.neighbors()) 78 | sort(neighbors, 79 | fun(a,b): a Stream.of(State), this implementation cheats since 28 | // there isn't an arrow annotation. 29 | dot.macro 'goal_dot_provider $left $dot $right': 30 | match right 31 | | 'apply': 'fun (state :~ State) :~ Stream: $left(state)' 32 | | 'apply_empty': 'fun () :~ Stream: $left.apply(empty_state)' 33 | 34 | annot.macro 'Goal': 35 | annot_meta.pack_predicate('r.#{procedure?}', 36 | '(($(statinfo_meta.dot_provider_key), 37 | goal_dot_provider))') 38 | 39 | class Var (i :: Int) 40 | def is_var: #{Var?} 41 | 42 | annot.macro 'Subst': 'Map.of(Var, Any)' 43 | class State (counter :: Int, subst :: Subst) 44 | 45 | def empty_state :: State: State(0, {}) 46 | 47 | 48 | // walk: finds the current value (if any) for a variable in the Subst 49 | fun walk(var, subst :~ Subst) :~ Any: 50 | def value: r.hash_ref(subst, var, #false) 51 | cond 52 | | is_var(value): walk(value, subst) 53 | | r.not(value): var 54 | | ~else: value 55 | 56 | 57 | // unify: attempts to unify two terms 58 | fun unify(u, v, subst :~ Subst): 59 | def u1: walk(u, subst) 60 | def v1: walk(v, subst) 61 | cond 62 | | is_var(u1) && is_var(v1) && u1 === v1: subst 63 | | is_var(u1): subst ++ {u1: v1} 64 | | is_var(v1): subst ++ {v1: u1} 65 | | r.is_pair(u1) && r.is_pair(v1): 66 | def s1: unify(r.car(u1), r.car(v1), subst) 67 | s1 && unify(r.cdr(u1), r.cdr(v1), s1) 68 | | ~else: 69 | u1 === v1 && subst 70 | 71 | 72 | // unify_goal: construct a goal unifying two terms 73 | fun unify_goal(u, v) :~ Goal: 74 | fun (state :~ State) :~ Stream: 75 | def subst: unify(u, v, state.subst) 76 | if subst 77 | | [State(state.counter, subst)] 78 | | [] 79 | 80 | 81 | // sugar for unify_goal 82 | operator (u ≡ v): 83 | ~stronger_than: ∧ ∨ 84 | ~weaker_than: + - * / 85 | unify_goal(u, v) 86 | 87 | 88 | // call_with_fresh_vars: allocate some fresh variables and pass them to 89 | // the procedure argument 90 | fun call_with_fresh_vars(proc) :~ Goal: 91 | def num_vars :: Int: r.procedure_arity(proc) 92 | fun (state :~ State) :~ Stream: 93 | fun make_var(i): 94 | Var(state.counter + i) 95 | def vars: r.build_list(num_vars, make_var) 96 | def new_state: State(state.counter + num_vars, state.subst) 97 | r.apply(proc, vars)(new_state) 98 | 99 | 100 | // sugar for `call_with_fresh_vars` 101 | expr.macro 'fresh ($u, $v, ...): 102 | $body ... 103 | ...': 104 | '(call_with_fresh_vars(fun ($u, $v, ...): 105 | $body ... 106 | ...)) :~ Goal' 107 | 108 | 109 | // mplus: Monadic plus for the Stream of answers. Similar to the paper this 110 | // implementation alternates the two input streams allowing for breadth first 111 | // search. 112 | fun mplus(st0 :~ Stream, st1 :~ Stream) :~ Stream: 113 | cond 114 | | st0.is_empty: st1 115 | | ~else: 116 | Stream(st0.first, ~rest: mplus(st1, st0.rest)) 117 | 118 | 119 | // bind: Monadic bind for the Stream of answers. 120 | fun bind(st :~ Stream, goal :~ Goal) :~ Stream: 121 | cond 122 | | st.is_empty: [] 123 | | ~else: 124 | mplus(goal(st.first), bind(st.rest, goal)) 125 | 126 | 127 | // disj: construct a disjunction goal from two goals 128 | fun disj(g0 :~ Goal, g1 :~ Goal) :~ Goal: 129 | fun (state :~ State) :~ Stream: 130 | mplus(g0(state), g1(state)) 131 | 132 | 133 | // conj: construct a conjunction goal from two goals 134 | fun conj(g0 :~ Goal, g1 :~ Goal) :~ Goal: 135 | fun (state :~ State) :~ Stream: 136 | bind(g0(state), g1) 137 | 138 | 139 | // zzz: snooze operator, delays creating a goal 140 | expr.macro 'zzz($g ...)': 141 | 'fun (state :~ State) :~ Stream: Stream(~rest: ($g ...)(state))' 142 | 143 | 144 | operator ((g0 :~ Goal) ∨ (g1 :~ Goal)) :~ Goal: 145 | disj(g0, g1) 146 | 147 | 148 | operator ((g0 :~ Goal) ∧ (g1 :~ Goal)) :~ Goal: 149 | ~stronger_than: ∨ 150 | conj(g0, g1) 151 | 152 | 153 | /* 154 | expr.macro '($g0 ∨ $g1): 155 | '(disj($g0, zzz($g1))) 156 | 157 | expr.macro '($g0 ∧ $g1 ...): 158 | ~stronger_than: ∨ 159 | '(conj($g0, zzz($g1 ...))) 160 | */ 161 | 162 | // --------- Examples ----------- 163 | 164 | #// 165 | def g: 166 | fresh (x, y): 167 | x ≡ #'red ∧ y ≡ #'beans 168 | ∨ x ≡ #'split ∧ y ≡ #'pea 169 | 170 | #// 171 | fun fives(x :: Var) :~ Goal: 172 | x ≡ 5 ∨ zzz(fives(x)) 173 | 174 | #// 175 | fun sixes(x :: Var) :~ Goal: 176 | x ≡ 6 ∨ zzz(sixes(x)) 177 | 178 | #// 179 | def fives_and_sixes :~ Goal: 180 | fresh (v): 181 | fives(v) ∨ sixes(v) 182 | -------------------------------------------------------------------------------- /rhombus-examples-sheepdogs/README.md: -------------------------------------------------------------------------------- 1 | # Rhombus Sheepdogs 2 | 3 | This is an incomplete implementation of a sheep and sheepdog agent simulation 4 | in Rhombus. 5 | 6 | **NOTE** The Rhombus proposal is not stable. This example should be considered 7 | a point in time example. I may or may not update this to reflect newer versions 8 | of the proposal. 9 | 10 | -------------------------------------------------------------------------------- /rhombus-examples-sheepdogs/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define name "rhombus-examples-sheepdogs") 4 | (define collection "rhombus-examples") 5 | (define deps 6 | '("base" 7 | "rhombus-prototype" 8 | "rhombus-compat")) 9 | (define module-suffixes '(#"rhm")) 10 | 11 | -------------------------------------------------------------------------------- /rhombus-examples-sheepdogs/sheepdogs.rhm: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | rhombus/meta open 5 | lib("racket/base.rkt") as r: 6 | rename: 7 | #{null?} as is_null 8 | #{inexact->exact} as exact 9 | #{random-seed} as random_seed 10 | lib("racket/list.rkt") as l 11 | 12 | class Posn(x,y) 13 | class Sheep(p :: Posn) 14 | class Sheepdog(p :: Posn) 15 | class World(width :: Int, height :: Int, entities) 16 | 17 | def sheep_view: 10 18 | def sheepdog_view: 25 19 | r.random_seed(0xDEC777) 20 | 21 | operator (a ^ b): 22 | ~stronger_than: * / + - <= 23 | ~associativity: ~right 24 | math.expt(a, b) 25 | 26 | operator (a % b): 27 | ~weaker_than: * / 28 | ~stronger_than: + - 29 | r.modulo(a, b) 30 | 31 | // make_world :: width height num_sheep num_sheepdogs -> World 32 | fun make_world(width :: Int, 33 | height :: Int, 34 | num_sheep :: Int, 35 | num_sheepdogs :: Int):: World: 36 | def sheep: make_sheep(num_sheep, width, height) 37 | def sheepdogs: make_sheepdogs(num_sheepdogs, width, height) 38 | World(width, height, l.shuffle(r.append(sheep, sheepdogs))) 39 | 40 | // make_things :: make num_things width height -> Listof thing 41 | fun make_things(make, 42 | num_things :: Int, 43 | width :: Int, 44 | height :: Int): 45 | fun make_random(i): 46 | def w: r.random(0, width) 47 | def h: r.random(0, height) 48 | make(Posn(w, h)) 49 | // XXX: this begs the question: what does `for` syntax look like in Rhombus. 50 | // The shrubbery proposal has some possible `for` syntax. 51 | r.map(make_random, l.range(num_things)) 52 | 53 | // make_sheep :: num_sheep width height -> Listof Sheep 54 | fun make_sheep(num_sheep :: Int, 55 | width :: Int, 56 | height :: Int): 57 | make_things(fun(p): Sheep(p), num_sheep, width, height) 58 | 59 | // make_sheepdogs :: num_sheepdogs width height -> Listof Sheepdog 60 | fun make_sheepdogs(num_sheepdog :: Int, 61 | width :: Int, 62 | height :: Int): 63 | make_things(fun(p): Sheepdog(p), num_sheepdog, width, height) 64 | 65 | def d: [Posn(-1,-1), Posn(0, -1), Posn(1, -1), 66 | Posn(-1, 0), Posn(0, 0), Posn(1, 0), 67 | Posn(-1, 1), Posn(0, 1), Posn(1, 1)] 68 | 69 | fun distance(width, height, Posn(x0, y0), Posn(x1, y1)): 70 | fun step(d, cur_distsq, cur_d): 71 | cond 72 | | r.is_null(d): 73 | def dist: math.sqrt(cur_distsq) 74 | values(dist, 75 | Posn (r.exact(math.round(cur_d.x / dist)), 76 | r.exact(math.round(cur_d.y / dist)))) 77 | | ~else: 78 | def dx: (x1 + d[0].x * width) - x0 79 | def dy: (y1 + d[0].y * height) - y0 80 | def distsq: dx ^ 2 + dy ^ 2 81 | if r.not(cur_distsq) || distsq < cur_distsq 82 | | step(r.cdr(d), distsq, Posn(dx, dy)) 83 | | step(r.cdr(d), cur_distsq, cur_d) 84 | step(d, #false, #false) 85 | 86 | def width: 100 87 | def height: 100 88 | def entity: Sheep(Posn(50, 50)) 89 | def sight: match entity 90 | | Sheep(_): sheep_view 91 | | Sheepdog(_): sheepdog_view 92 | def posn: match entity 93 | | Sheep(p): p 94 | | Sheepdog(p): p 95 | def scores: Array(0, 0, 0, 0, 0, 0, 0, 0, 0) 96 | fun update_score(d :: Posn, v :: Number): 97 | def i: d.x + 1 + 3 * (d.y + 1) 98 | scores[i] := scores[i] + v 99 | 100 | fun calculate_scores(others): 101 | cond 102 | | r.is_null(others): scores 103 | | ~else: 104 | def (dist :: Number, d :: Posn, score :: Int): 105 | match r.car(others) 106 | | Sheep(p): 107 | def (dist, d): distance(width, height, posn, p) 108 | values(dist, d, 1) 109 | | Sheepdog(p): 110 | def (dist, d): distance(width, height, posn, p) 111 | values(dist, d, -1) 112 | when dist <= sight 113 | | update_score(d, score) 114 | calculate_scores(r.cdr(others)) 115 | 116 | def the_world: make_world(100,100,100,100) 117 | -------------------------------------------------------------------------------- /rhombus-examples-web/README.md: -------------------------------------------------------------------------------- 1 | # Kicking the tires of Rhombus 2 | 3 | **NOTE** The Rhombus proposal is not stable. This example should be considered 4 | a point in time example. I may or may not update this to reflect newer 5 | versions of the proposal. 6 | 7 | ## 2021/08/04 8 | This is a translation of a kind of scraping script I've written in Racket. 9 | 10 | Some notes: 11 | - I'm avoiding making new syntax, some things (`with-handlers*`, 12 | `with-disposable`) could look nicer with special syntax. I'm not quite up to 13 | speed on the macro system, and I wanted to get something working. 14 | - `shim.rkt` avoids the problem where requiring `racket/base` will shadow the 15 | rhombus names and then Bad Things(TM) start happening. Like none of the 16 | infix operators work and syntax like `if` and `define` get shadowed. 17 | - Racket identifiers are not the same as Rhombus identifiers so a lot has to be 18 | renamed. 19 | 20 | ## 2021/08/05 21 | - Based on update to the implementation changed: 22 | - Name changes: 23 | - `value` -> `val` 24 | - `define` -> `def` 25 | - `function` -> `fun` 26 | - `require` -> `import` 27 | - Imports places names into a namespace object so now `import`ed names are dotted. 28 | - Made a shim like module for each `import` from Racket code. 29 | - Wrote a mostly automatic renaming function to use with 30 | [`filtered-out`](https://docs.racket-lang.org/reference/require.html#%28form._%28%28lib._racket%2Fprovide..rkt%29._filtered-out%29%29) 31 | from the shim modules. 32 | 33 | ## 2021/10/29 34 | Getting back into some Rhombus. 35 | 36 | Changes (from new prototype): 37 | - New keyword syntax 38 | - `import` prefix syntax has changed 39 | - Syntax for alt forms has changed 40 | 41 | -------------------------------------------------------------------------------- /rhombus-examples-web/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define name "rhombus-examples-web") 4 | (define collection "rhombus-examples") 5 | (define deps 6 | '("base" 7 | "rhombus-prototype" 8 | "http-easy" 9 | "disposable" 10 | "keyring-lib")) 11 | (define module-suffixes '(#"rhm")) 12 | 13 | -------------------------------------------------------------------------------- /rhombus-examples-web/web.rkt: -------------------------------------------------------------------------------- 1 | #lang rhombus 2 | 3 | import: 4 | "web/private/disposable.rkt" as d 5 | "web/private/racket.rkt" as rkt 6 | "web/private/http.rkt" 7 | "web/private/keyring.rkt" 8 | "web/private/url.rkt" 9 | 10 | def user_name: rkt.argv[0] 11 | def service_name: rkt.argv[1] 12 | def request_url_s: rkt.argv[2] 13 | 14 | def request_url: 15 | url.string_to_url(request_url_s) 16 | 17 | // keyring_auth :: 18 | // (Url Headers Query -> String String) AuthProc -> AuthProc 19 | // AuthProc: 20 | // https://docs.racket-lang.org/http-easy/index.html#%28def._%28%28lib._net%2Fhttp-easy..rkt%29._auth-procedure%2Fc%29%29 21 | // 22 | // This function handles credential lookup in a keyring 23 | fun keyring_auth(service_user_lookup, auth_proc): 24 | fun(a_url, headers, query): 25 | def (service_name, user_name): service_user_lookup(a_url, headers, query) 26 | def password: keyring.get_password(service_name, user_name) 27 | auth_proc(user_name, password)(a_url, headers, query) 28 | 29 | def service_auth: 30 | keyring_auth(fun(u, h, q): values(service_name, user_name), http.basic_auth) 31 | 32 | // A disposable value (https://docs.racket-lang.org/disposable/index.html) for 33 | // making http requests with http-easy and closing the responses. 34 | def http_request: 35 | rkt.make_keyword_procedure( 36 | // XXX: in a general case use a rest-arg but there is only one positional 37 | // argument we care about here. 38 | fun(kws, kwargs, uri): 39 | def max_tries: 10 40 | def pos_args: rkt.list(http.current_session(), uri) 41 | fun request_retry(tries_left): 42 | try: 43 | rkt.keyword_apply(http.session_request, kws, kwargs, pos_args) 44 | ~catch e :: Exn.Fail: 45 | if tries_left == 0 46 | | throw e 47 | | request_retry(tries_left - 1) 48 | d.disposable( 49 | fun(): request_retry(max_tries), 50 | http.response_close)) 51 | 52 | #// 53 | keyring.default_keyring( 54 | keyring.make_keyring_from_string("keychain://")) 55 | 56 | def job_doc: 57 | d.call_with_disposable( 58 | http_request(request_url, 59 | ~auth: service_auth), 60 | http.response_json) 61 | 62 | job_doc 63 | -------------------------------------------------------------------------------- /rhombus-examples-web/web/private/disposable.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | "rhombus-names.rkt") 5 | disposable 6 | racket/provide) 7 | 8 | (provide 9 | (filtered-out 10 | (rhombus-rename) 11 | (all-from-out disposable))) 12 | -------------------------------------------------------------------------------- /rhombus-examples-web/web/private/http.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | "rhombus-names.rkt") 5 | net/http-easy 6 | racket/provide) 7 | 8 | (provide 9 | (filtered-out 10 | (rhombus-rename) 11 | (all-from-out net/http-easy))) 12 | -------------------------------------------------------------------------------- /rhombus-examples-web/web/private/keyring.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | "rhombus-names.rkt") 5 | keyring 6 | racket/provide) 7 | 8 | (provide 9 | (filtered-out 10 | (rhombus-rename) 11 | (all-from-out keyring))) 12 | -------------------------------------------------------------------------------- /rhombus-examples-web/web/private/racket.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | "rhombus-names.rkt") 5 | racket/provide) 6 | 7 | (provide 8 | argv 9 | proc_with_handlers 10 | 11 | (filtered-out 12 | (rhombus-rename) 13 | (combine-out 14 | exn:fail? 15 | list 16 | keyword-apply 17 | make-keyword-procedure))) 18 | 19 | (define argv (current-command-line-arguments)) 20 | 21 | ;; one case handling exception handling 22 | (define (proc_with_handlers exn? exn-handle body) 23 | (with-handlers* ([exn? exn-handle]) 24 | (body))) 25 | -------------------------------------------------------------------------------- /rhombus-examples-web/web/private/rhombus-names.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | ;; My completely unofficial rules for renaming Racket names to Rhombus 4 | 5 | (provide rhombus-rename) 6 | 7 | (define ((rhombus-rename [override null]) name) 8 | (define (rename name) 9 | (cond 10 | [(assoc name override) 11 | => (lambda (p) (cdr p))] 12 | [(regexp-match #px"^(.*)\\?$" name) 13 | => (lambda (m) (rename (string-append "is_" (cadr m))))] 14 | [(regexp-match #px"^(.*)!$" name) 15 | => (lambda (m) (rename (cadr m)))] 16 | [(regexp-match #px"->" name) 17 | (rename (regexp-replace* #px"->" name "_to_"))] 18 | [(regexp-match #px"/" name) 19 | (rename (regexp-replace* #px"/" name "_with_"))] 20 | [(regexp-match #px":" name) 21 | (rename (regexp-replace* #px":" name "_"))] 22 | [(regexp-match #px"-" name) 23 | (rename (regexp-replace* #px"-" name "_"))] 24 | [else name])) 25 | (rename name)) 26 | -------------------------------------------------------------------------------- /rhombus-examples-web/web/private/url.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | "rhombus-names.rkt") 5 | net/url 6 | racket/provide) 7 | 8 | (provide 9 | (filtered-out 10 | (rhombus-rename 11 | '(["file://->path" . "file_url_to_path"])) 12 | (all-from-out net/url))) 13 | --------------------------------------------------------------------------------