jerome.martin.dev
2020-1-22 13:01:12

Hey Racketeers, long time no see! I’m currently struggling trying to provide a #lang as a web service… I’m looking for a way to execute an arbitrary string of text (I get from the network) through a custom reader, then expander, as #lang usually does. I found make-evaluator and make-module-evaluator, but they don’t seem to handle readers correctly. I tried to summarize everything in this stackoverflow question : https://stackoverflow.com/questions/59855704/how-to-tell-make-module-evaluator-to-use-a-custom-reader-like-lang-does I’m currently trying every possible combination of make-evaluator parameters, with no success… Can you help me out?


gknauth
2020-1-22 14:04:09

@notjack This is what I came up with that worked for my immediate purposes.


soegaard2
2020-1-22 14:14:30

@jerome.martin.dev Have you tried read-lang-module to turn the string into a syntax object representing a module ?


soegaard2
2020-1-22 14:26:45

At the moment, I am dog-fooding urlang. The frontend simply sends standard http requests to the server, which responds with json.


jerome.martin.dev
2020-1-22 14:35:22

I usually get an evaluator, but then when I try to (eval 'meal) it complains about missing #%top-interaction.


jerome.martin.dev
2020-1-22 14:38:29

Then if I provide #%top and co… (which I don’t really want to, but, anyways…) It just complains about the variable meal missing, and I’m stuck there.


jerome.martin.dev
2020-1-22 14:39:15

It’s weird because it works perfectly when I require a file the usual way.


jerome.martin.dev
2020-1-22 14:39:36

It’s like the reader submod is never called.


soegaard2
2020-1-22 14:39:58

Are you using a load or the read-eval-loop ? (I am curious about where #%top-interaction came from)


soegaard2
2020-1-22 14:40:42

I guess I am curious about what read-lang-module returns?


jerome.martin.dev
2020-1-22 14:40:44

So I tried providing sandbox-reader. But then the sandbox passes 'program as the first argument to my read-syntax function! Weird!


jerome.martin.dev
2020-1-22 14:42:29

read-lang-module returns a correct module parsed by my reader: #<syntax:private/reader.rkt:41:8 (module program broccoli/private/expander (program (sentence "Hello" "world")))> But the evaluator fails to call the expander on it.


soegaard2
2020-1-22 14:43:37

Fails how?


jerome.martin.dev
2020-1-22 14:44:16

Either #%top missing or ’meal missing


jerome.martin.dev
2020-1-22 14:45:48

It’s supposed to expand into (#%module-begin (provide meal) (define meal (list 1 2))) But I never get to that meal…


soegaard2
2020-1-22 14:47:01

And the broccoli program runs fine on its own (I mean directly in DrRacket)


jerome.martin.dev
2020-1-22 14:47:07

yep


jerome.martin.dev
2020-1-22 14:47:37

if I do: > (require "my-file.meal") > meal '(1 2)


jerome.martin.dev
2020-1-22 14:49:03

with the file being something like #lang broccoli Hello world!


soegaard2
2020-1-22 14:49:46

What does your (make-module-evaluator ...) expression look like?


jerome.martin.dev
2020-1-22 14:50:57

> (define module (read-lang-module (open-input-string "#lang broccoli\nHello world!\n"))) > module #<syntax:private/reader.rkt:41:8 (module program broccoli/private/expander (program (sentence "Hello" "world")))> > (define eva (make-module-evaluator module)) > (eva 'meal) ; meal: undefined; ; cannot reference an identifier before its definition ; in module: 'program ; [,bt for context] >


soegaard2
2020-1-22 14:52:28

I think you need to use #:lang ...something here... in order to get the correct namespace.


jerome.martin.dev
2020-1-22 14:54:00

Well, there’s a #:language parameter, but it just puts a restriction on the module, it doesn’t do anything particular


soegaard2
2020-1-22 14:56:15

I was reading this section on make-evaluator and now I am confused whether it also works like this for make-module-evaluator:


jerome.martin.dev
2020-1-22 14:57:02

Oh, hold on, I got it working! > (eva '(require 'program)) > (eva 'meal) '(1 2)


jerome.martin.dev
2020-1-22 14:57:21

I need to require the created module inside the evaluator…


soegaard2
2020-1-22 14:57:57

Ah!


jerome.martin.dev
2020-1-22 15:06:45

It’s a bit sad because I wanted to avoid having to provide #%app, #%top, require, quote…


jerome.martin.dev
2020-1-22 15:07:02

But I guess it’s okay


jerome.martin.dev
2020-1-22 15:27:59

I may find a way to simplify and secure this further, but for now, it’ll be okay. I’ll just put some statement in my presentation like “please call me if you broke into my server by executing some racket code through my app” and call it a day :stuck_out_tongue:


jerome.martin.dev
2020-1-22 15:29:07

@soegaard2 As always, thanks for helping me!


soegaard2
2020-1-22 15:29:55

Glad to help.


hj5855586
2020-1-22 17:16:21

@soegaard2 I see- so the you write your front end with urlang; in turn, urlang sends requests to the server, which responds with json; json is the mechanism by which the front end is displayed?


soegaard2
2020-1-22 17:17:41

No the json just contains the data. I have chosen react-bootstrap for the dynamic parts of the gui (and plain bootstrap for the static parts).


soegaard2
2020-1-22 17:18:28

But honestly it might be easier to use plain JavaScript in the frontend.


soegaard2
2020-1-22 17:20:00

The reason I picked react-bootstrap is that the “hooks” in modern react feels like functional programming.


soegaard2
2020-1-22 17:20:38

(The old approach in React used objects.)


hj5855586
2020-1-22 17:21:02

@soegaard2 I see — do you have the project you’re building with urlang on github?


soegaard2
2020-1-22 17:21:56

Sorry no. At some point I might change http://racket-stories.com\|racket-stories.com to use Urlang for the gui, but I haven’t got any examples on Github yet.


soegaard2
2020-1-22 17:22:51


soegaard2
2020-1-22 17:24:27

(the last one might be a bad example - it was an experiment to see how close I could get to the beginner language)


hj5855586
2020-1-22 17:26:46

@soegaard2 thanks a lot. My background is Structure and Interpretation of Computer Programs. Now I want to get into web development but I want to use Racket or Haskell. Do you know any good tutorials or books where I can learn the fundamentals of web development?


soegaard2
2020-1-22 17:28:49

There are so many options in web programming, so it can be confusing. The JavaScript plethora of libraries is still confusing to me.

A good place to start: https://docs.racket-lang.org/continue/index.html


soegaard2
2020-1-22 17:30:17

Also worth a look is https://github.com/soegaard/web-tutorial There are 4 versions of a web-site: listit, listit2, listit3 and listit4. The first one listit is very basic. The code is heavily commented and was meant to be read.

Haven’t written an actual tutorial yet…


hj5855586
2020-1-22 17:32:06

Ok thanks. How did you build http://racket-stories.com\|racket-stories.com? It’s pretty cool.


soegaard2
2020-1-22 17:34:14

Racket Stories used the Racket web-server. It uses a database to store all information. Everything happens server-side (i.e. no Javascript is used).

The code is available here (but it is worth reading the simpler listit sites first): https://github.com/soegaard/racket-stories


soegaard2
2020-1-22 17:37:29

If you need an overview of how the web-site works, then this (very old) blog post on gives some insight. The actual implementation from back then you can ignore. http://www.scheme.dk/blog/2007/01/introduction-to-web-development-with.html


hj5855586
2020-1-22 17:41:51

Wow- so a server side application or project can look like http://racket-stories.com\|racket-stories.com?


soegaard2
2020-1-22 17:42:09

Yes.


soegaard2
2020-1-22 17:42:51

Look in “view.rkt” to see how the html is produced.


hj5855586
2020-1-22 17:47:35

That’s so cool. I’m going to take a look at it. Sorry for my ignorance but how did you make it look nice; for example it’s purple and it looks nice. I’m operating on the idea that html alone can’t do that.


soegaard2
2020-1-22 17:52:56

I used Bootstrap. When you generate the html, you mark the elements with a class. The browser looks up the classses in the style sheet (css) and draws them accordingly. Bootstrap provides some nice themes - they are way better at design than I am.

https://getbootstrap.com/


soegaard2
2020-1-22 17:55:24

I stuck to the default theme, but there are lots of other Bootstrap themes to choose from: https://themes.getbootstrap.com/


hj5855586
2020-1-22 18:03:55

This is pretty cool. Inside “view.rkt” you refer to bootstrap by providing a link. Do you get this link when you buy the theme or is this the link for the default theme?


soegaard2
2020-1-22 18:08:08

That’s just the default theme. I got the link here: https://getbootstrap.com/docs/4.4/getting-started/introduction/


soegaard2
2020-1-22 18:09:05

There are other free themes for Bootstrap, if you look for it.


hj5855586
2020-1-22 18:10:29

So I can use racket to build a simple html blog right?


soegaard2
2020-1-22 18:10:48

That would be a great project.


hj5855586
2020-1-22 18:15:54

You have been really helpful. I can find your email at http://racket-stories.com\|racket-stories.com right?


soegaard2
2020-1-22 18:16:21

Yes.


hj5855586
2020-1-22 18:19:19

I see in your github that you have written a computer algebra system. I wrote a few toy computer algebra programs. If you have time could you give some feedback on my work?


soegaard2
2020-1-22 18:22:34

Sure, what kind of feedback are you looking for?


hj5855586
2020-1-22 18:26:19

Well, I would like some feedback to get a better idea of my skill level. and I would like to know if they are any good. here’s one: https://github.com/xsquared93/generic-arithmetic


soegaard2
2020-1-22 18:33:51

The code is clear and follows the spirit from SICP.

If I were to change something it would be the representation of values. The SICP authors were forced to use tagged lists, because the Standard Scheme back then didn’t have structures/records/objects.


soegaard2
2020-1-22 18:35:33

Another thing to look into, is pattern matching (pattern matching weren’t available in Scheme back then either).


hj5855586
2020-1-22 18:40:57

okay, I will look into that. thank you very much.


deactivateduser60718
2020-1-22 18:55:20

@mflatt Thanks!

@notjack Entire collection. I want to rename one of my projects without needing users to change their code.


deactivateduser60718
2020-1-22 18:56:27

@mflatt Would it be reasonable for me to add code to raco pkg ’s implementation to recognize collection aliases as defined in info.rkt, or would that overcomplicate things?


soegaard2
2020-1-22 18:58:25

hj5855586
2020-1-22 19:03:41

I will check them out. what inspired your computer algebra system?


notjack
2020-1-22 19:06:20

@deactivateduser60718 How many user-facing modules are there?


deactivateduser60718
2020-1-22 19:19:53

@notjack 9.


deactivateduser60718
2020-1-22 19:20:14

https://github.com/zyrolasting/polyglot << Counting every module, minus those in private/ and info.rkt


soegaard2
2020-1-22 19:22:01

The main structure of racket-cas is inspired by Cohen’s books. But I have also read and enjoyed the algebra section of SICP. There is also a nice chapter in PAIP (by Peter Norvig).


soegaard2
2020-1-22 19:23:55

Dusty’s Terminal Phase is on the Hacker News front page: https://news.ycombinator.com/item?id=22095092


mflatt
2020-1-22 19:30:22

@deactivateduser60718 Possibly ok. My first thought was that raco setup might have some race condition on the filesystem if a module can be accessed by two different names, but I think it’s ok because the compilation lock is based on the resolved path. But I wouldn’t be surprised if I’m overlooking something else like that.


deactivateduser60718
2020-1-22 19:33:50

@mflatt I’d have to set aside time to read what’s going on anyway. I imagine this is the kind of thing where whatever I first try will have unexpected side-effects.


hj5855586
2020-1-22 19:35:00

yea, I have heard of PAIP. I haven’t read it, though. Maybe, PAIP is good book to read after SICP? you agree? is PAIP relevant if I want to get into the software industry? I believe it will make you a better Lisp programmer and that should transfer to a more popular language. what do you think?


soegaard2
2020-1-22 19:35:47

If you liked SICP then I the odds are high, you’ll like PAIP.


hj5855586
2020-1-22 19:36:59

speaking of books, have you tackled Purely Functional Data Structures by Chris Okasaki?


soegaard2
2020-1-22 19:37:21

Yes, that’s also a great book.


hj5855586
2020-1-22 19:39:08

do you feel like it made you a better programmer?


soegaard2
2020-1-22 19:42:27

That’s difficult to say. Okasaki’s book is (of course) very focused on data structures, where as Norvig’s book introduces you to a range of topics.

But Okasaki and “Introduction to Algorithms” by Cormen et. al are the first place I look when it comes to data structures.


soegaard2
2020-1-22 19:43:33

PAIP is old and Amazon tells me that Norvig soon releases a new book on AI, so maybe it is wise to borrow PAIP from a library to check out rather than buying it.


soegaard2
2020-1-22 19:43:45

I don’t think the new book uses Common Lisp though.


hj5855586
2020-1-22 19:48:49

I think the new book uses Python. And with respect to data structures, I read that data structures and algorithms are tightly linked. A good choice of data structures will make your algorithms simple, maintainable, and faster. What is your take?


soegaard2
2020-1-22 19:52:01

That’s true. And a the choice of data strucure does to some degree how your code will like. However it’s rare that you need to actual implement complicated data structures from scratch. Most often you find a library that provides, say, splay trees and just use them.


samdphillips
2020-1-22 20:06:21

You can also use record-merge2 to build a record from arbitrary fields.

(define (record-add-field a-record kw value) (record-merge2 a-record (build-record (lambda (ignore) value) (keyset-add empty-keyset kw))))


samdphillips
2020-1-22 20:06:32

apologies for the formatting


samdphillips
2020-1-22 20:07:00

Ooh or for your usecase build-record may be better.


hj5855586
2020-1-22 20:07:20

yea that is the reason people give against studying CLRS. I think studying CLRS will make you a better programmer because it seems like in industry, sometimes, you need to write code that is efficient. you can lose customers, for example, if your system isn’t quick enough. but Dr. Felleisen, in his blog, has written that programming in industry is not algorithmically heavy. He says it is more similar to the things you learn in PL -oriented courses. take a look: https://felleisen.org/matthias/Thoughts/what-is-pl.html


soegaard2
2020-1-22 20:13:00

I am sure Felleisen would say CLRS is worth studying :wink: . The context of that blog post seems to be a question of internal politics.


hj5855586
2020-1-22 20:15:00

Yea I agree, Dr. Felleisen would say that.


sorawee
2020-1-22 21:15:27

Is there a way to use tech as contract in defproc? I felt I have seen this before with some weird quoting tricks but not sure how.


samdphillips
2020-1-22 21:22:07

Does anyone have a blog post about making “minimal” docker images for Racket? I’m using rebellion and rash and they both want to (transitively) pull in all of DrRacket.


soegaard2
2020-1-22 21:23:16

@notjack perhaps?


soegaard2
2020-1-22 21:24:05

If a single package uses docs, then …


samdphillips
2020-1-22 21:24:07

Really part of the problem is pulling in everything to build scribble documentation


sorawee
2020-1-22 21:24:09

Ah, it’s #,(tech ...)


samdphillips
2020-1-22 21:25:20

(s/part/most/)


notjack
2020-1-22 21:28:30

Can you install them in the docker container with raco pkg install --binary-lib? The docker images are configured to use the built-package catalog so that option should download bytecode instead of source code, and it should completely skip installing build-only dependencies.


notjack
2020-1-22 21:35:38

Also, I should definitely write a post about this for the Racket blog


samdphillips
2020-1-22 21:54:28

Ok I was missing the --binary-lib argument. I’ll give that a try.


notjack
2020-1-22 23:37:18

Let me know if it actually works. I haven’t tried that myself yet.


zalp.rogue
2020-1-22 23:49:07

@zalp.rogue has joined the channel


zalp.rogue
2020-1-22 23:52:59

I am trying to play around with namespaces and I was running this example from doc https://docs.racket-lang.org/reference/Namespaces.html?q=namespace#%28def._%28%28quote._~23~25kernel%29._namespace-attach-module%29%29 but I am having trouble. (module food racket/base (provide apple) (define apple (list "pie"))) (namespace-require ''food) this gives an error require: unknown module where it’s the exact same thing from the example. Did something change with namespace-require?


notjack
2020-1-22 23:57:19

@zalp.rogue The examples from the doc are meant to be run at a REPL. You might get different results if you put the same code in a file and run the whole file. Is that how you ran it?


zalp.rogue
2020-1-22 23:58:29

oh yes it runs correctly in repl. is there a way to do similar thing in a file?


soegaard2
2020-1-23 00:00:27

Yes, you need to change the current namespace to the one “inside” the file (I think). Let me see if I can find an example.


soegaard2
2020-1-23 00:03:32

I think, you need something like this: #lang racket (define-namespace-anchor here) (define ns (namespace-anchor-&gt;namespace here)) (current-namespace ns)


zalp.rogue
2020-1-23 00:07:07

this still gives an error #lang racket (module food racket/base (provide apple) (define apple (list "pie"))) (define-namespace-anchor here) (define ns (namespace-anchor-&gt;namespace here)) (current-namespace ns) (namespace-require ''food)


samdphillips
2020-1-23 00:18:25

IIRC there was a libc problem with Alpine and Racket? (or I’m remembering incorrectly.)


soegaard2
2020-1-23 00:19:22

#lang racket (module food racket/base (provide apple) (define apple (list "pie"))) (require (submod "." food)) apple


zalp.rogue
2020-1-23 00:22:09

yes I tried that but I am trying to do the full example of attaching a module to another namespace and instantiating there. Essentially I am trying to instantiate a module separately in a different namespace. namespace-require says it needs a quoted-raw-require-spec so '(submod "." food) should work but that gives an error ; standard-module-name-resolver: no base path for relative submodule path: (submod "." food)


soegaard2
2020-1-23 00:22:54

What about ''(submod "." food) ?


soegaard2
2020-1-23 00:23:14

The quoted-raw-require-spec always confuses me.


zalp.rogue
2020-1-23 00:23:20

that’s just ?: bad require spec


zalp.rogue
2020-1-23 00:24:24

my understanding was if it works with #%require and it should work the same after quoting, but apparently that’s not the case.


soegaard2
2020-1-23 00:32:51

If we save this in “c.rkt” then I get pie as output: #lang racket (module food racket/base (provide apple) (define apple (list "pie"))) (require (submod "." food)) (define-namespace-anchor here) (define ns (make-base-namespace)) (namespace-attach-module (namespace-anchor-&gt;namespace here) '(submod "c.rkt" food) ns) ; ns now knows the same instance (namespace-require '(submod "c.rkt" food) ns) (eval 'apple ns)


zalp.rogue
2020-1-23 00:34:48

yeah I was able to do that with the filename as well but I don’t have access to the file name all the time. (you don’t even need to anchor in this case just (namespace-require '(submod "c.rkt" food) will work)


soegaard2
2020-1-23 00:37:48

Won’t that require into the wrong namespace?


zalp.rogue
2020-1-23 00:39:20

It will require into the current-namespace which can be just used in same way.


notjack
2020-1-23 00:49:54

Yeah there was a problem getting racket to compile with musl libc. I think some progress was made on that though, and it might even be fixed. There’s a GitHub issue thread somewhere with the details.


notjack
2020-1-23 01:18:44

@deactivateduser60718 You could use #lang reprovide. You’d have to write one file per module, but each file would only have one line of code (not counting the #lang line). So aliasing oldfoo/bar/baz to newfoo/bar/baz would require writing an oldfoo/bar/baz.rkt file that looks like this: #lang reprovide newfoo/bar/baz



shawsumma060
2020-1-23 05:23:44

what could cause racket so that it would not quit with an interrupt. it will not stop with killall either. only killall -9 worked


shawsumma060
2020-1-23 05:23:55

usually things just take a sec to shutdown


notjack
2020-1-23 06:42:41

I’m writing some documentation for Rebellion that gives advice on when to use different kinds of collections. Does this table seem helpful?


fedreg
2020-1-23 07:48:05

@fedreg has joined the channel