Zig and Guile for fast code and a REPL
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 542 | |
Author | ||
License | CC Attribution 2.0 Belgium: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor. | |
Identifiers | 10.5446/61793 (DOI) | |
Publisher | ||
Release Date | ||
Language |
Content Metadata
Subject Area | ||
Genre | ||
Abstract |
|
FOSDEM 2023486 / 542
2
5
10
14
15
16
22
24
27
29
31
36
43
48
56
63
74
78
83
87
89
95
96
99
104
106
107
117
119
121
122
125
126
128
130
132
134
135
136
141
143
146
148
152
155
157
159
161
165
166
168
170
173
176
180
181
185
191
194
196
197
198
199
206
207
209
210
211
212
216
219
220
227
228
229
231
232
233
236
250
252
256
258
260
263
264
267
271
273
275
276
278
282
286
292
293
298
299
300
302
312
316
321
322
324
339
341
342
343
344
351
352
354
355
356
357
359
369
370
372
373
376
378
379
380
382
383
387
390
394
395
401
405
406
410
411
413
415
416
421
426
430
437
438
440
441
443
444
445
446
448
449
450
451
458
464
468
472
475
476
479
481
493
494
498
499
502
509
513
516
517
520
522
524
525
531
534
535
537
538
541
00:00
Formal languageOvalComputer programmingTerm (mathematics)Gamma functionBitGastropod shellAxiom of choiceRight angleDiagramLecture/Conference
00:56
Source codeGame theoryRaw image formatLocal ringData bufferComputer configurationComputer fileAerodynamicsEwe languageRandom number generationRandom matrixWorld Wide Web ConsortiumElement (mathematics)Boom (sailing)Online helpGamma functionNumerical digitNewton's law of universal gravitationModemMassLibrary (computing)Computer-generated imageryConvex hullMaxima and minimaLinear multistep methodGraphical user interfaceIntegrated development environmentInformationNormed vector spaceDirectory serviceLie groupDemo (music)Numbering schemeData typeSoftware testingLibrary (computing)Local ringRight angleBoolean algebraFunctional (mathematics)Metropolitan area networkNumbering schemeGastropod shellInterior (topology)Computer fileCodeScripting languageRevision controlProcedural programmingConfiguration spaceObject (grammar)Line (geometry)Structural loadIntegerComputer programmingComputer animation
06:24
Raw image formatHecke operatorMathematicsBookmark (World Wide Web)Hybrid computerSoftwareSoftware configuration managementService (economics)NavigationSystems engineeringIntegrated development environmentLink (knot theory)MaizeFunction (mathematics)IntegerFormal languageSource codeComputer programmingSAP ERPPhysical systemConfiguration spaceData managementExtension (kinesiology)Stack (abstract data type)Buffer overflowScripting languageComputer configurationControl flowRepository (publishing)Real numberIntelView (database)TelecommunicationDisintegrationInterface (computing)Interior (topology)Enterprise resource planningLogarithmNumberNumbering schemeNumerical analysisRational numberTowerType theoryContent (media)Fraction (mathematics)Loop (music)Product (business)Execution unitEquivalence relationNumbering schemeInterior (topology)QuicksortFunctional (mathematics)Web pageComputer animation
06:51
Function (mathematics)Software configuration managementIntegerError messageRange (statistics)SicSocial classGastropod shellRevision controlStructural loadLocal ringSource codeCodeNumbering schemeData typeException handlingVariable (mathematics)Interface (computing)Computer animation
07:13
Software configuration managementFlagFunction (mathematics)Computer configurationBuffer solutionData typeGamma functionComputer fileThetafunktionColor managementStrutTendonLine (geometry)Proper mapComputer fileEquivalence relationNumbering schemeFunctional (mathematics)QuicksortObject (grammar)Right angleInclusion mapComputer animation
09:15
Data typeNumbering schemeDemo (music)Game theoryBootingGastropod shellBacktrackingFunctional (mathematics)Variable (mathematics)CASE <Informatik>IntegerComputer animation
09:52
AerodynamicsIntegrated development environmentConvex hullDemo (music)Data bufferInfinityOnline helpGamma functionNumbering schemeMenu (computing)MassSystem callFunctional (mathematics)Letterpress printingComputer programmingRight angleMultiplicationModule (mathematics)Parameter (computer programming)Interpreter (computing)Repository (publishing)BitSymbol tableMappingSource codeComputer animation
11:13
Manufacturing execution systemReading (process)Gamma functionData typePolygon meshPoint (geometry)Functional programmingFormal languageBefehlsprozessorDegree (graph theory)Type theoryCompilerMixture modelDataflowImperative programmingCoefficient of determinationBitComputer programmingCodeLecture/Conference
13:04
DebuggerComputer fileOvalWorld Wide Web ConsortiumKeilförmige AnordnungMaxima and minimaGame theoryConvex hullCloud computingVector spaceSubject indexingRight angleObject (grammar)Functional (mathematics)Computer programmingRevision controlCodeNumbering schemeIntegerAnalytic continuationBlock (periodic table)Library (computing)Electronic mailing listArray data structurePointer (computer programming)Point (geometry)Semiconductor memoryBitNumberPosition operatorRow (database)Element (mathematics)Computer animation
16:23
UsabilityElectronic meeting systemGamma functionCodeSlide ruleLecture/Conference
16:50
Computer virusGamma functionZeno of EleaEmailCodeSoftwareRight angleStudent's t-testCompilerLine (geometry)Disk read-and-write headFormal languageMultiplication signComputer programmingLecture/Conference
18:25
Projective planeLecture/Conference
18:42
Object (grammar)Numbering schemeInterface (computing)Lecture/Conference
19:28
SoftwareSound effectType theoryAlgebraic numberFormal languageMultiplication signFatou-MengeException handlingSpacetimePoint (geometry)System programmingMemory managementCompileroutputFormal languageFunctional (mathematics)Domain-specific languageState of matterVirtual machineOverhead (computing)Lecture/Conference
22:00
GoogolPoint cloudProgram flowchart
Transcript: English(auto-generated)
00:06
People here organizing the dev room particularly my notice who has done a lot of the work before we organize this room Anyway, I'm gonna talk a little bit about guile and zig so I prepared two talks
00:26
One talk you can download online there was kind of an overview of You know why I made these choices and why we're doing this, but I think it's better just to To hit The command line or a shell right so many people will recognize this Emacs. I mean the letters fall off on the side
00:49
It shouldn't matter too much And then I'm gonna run in a shell
01:05
So I don't know if everyone is aware, but the gig shell is a proper container So only the tools are pulled in that we define Oops, and this is done in the gigs dot scheme file Oh, man in the gig stuff scheme file we define the dependence we want
01:34
Right so guile is there and zig is there so in in the file we find zig
01:42
version So that's in the container right and then guile Minus V is also there But VI for example is not right and in the if this was properly running on Debian It would be it would be visible So what I'm gonna. Do is. I'm gonna run zig to build a my library. It's a dynamic library and
02:16
Then I use package config to pick up the The guile compile switches, and I'm gonna compile it against a little C library. Sorry. There's yeah C file
02:38
Yeah
02:45
Right yeah, I missed the second line I see J Right so guile is is is it's almost designed for linking against C
03:03
right so I wrote a Little C program to show you that and it can It calls guile functions so scheme from int is a guile function, so it switches It turns test into a guile integer essentially
03:21
And then a boolean and then I call this function here in C Right my increment and function, and you can see that it uses guile objects To pass into the function, and there's also a guile object returned right So it's very minimalistic code And I just need to get a compile it
03:42
Now it did compile But now doesn't find the library so I need to add the library path, so I'm just doing this raw So you can see what is happening. You know I mean if you had a proper build script you can
04:03
account for all this But you can see it says hello world from and from three to four right so that's what the C function does Now I want to do the same thing in zig right so I created Well actually what I want to do is I want to call zig from from
04:22
You know this the same library that I'm using I want to call this from from from guile right, so let's try that So I'm in guile now, and I added the local
04:44
search path for the library right Yeah, so here we here we load the the shared object the shared library libmy.so right it loads into guile And I want to try something like and it says it doesn't find the procedure because I haven't defined it
05:10
Yeah, so that doesn't work very well Let me see where we are yeah, so call it ping zig
05:49
With an underscore all right, that's not very guile like is it yeah, so So that's some conventions, and I already ignored it yeah, so
06:04
So guile has a wide range of C functions in the library And these can be called you know if you look for the C function Which one did we use before?
06:20
Yeah, or scheme from int you can see here, right? Scheme from int Yes, in the guile reference manual
06:42
You see in the reference manual almost on every page you see see the sort of the C functions that you could also call us LISP functions You know and that's the scheme from int should be there It's a long list, but that's the idea so when you when you actually use the guile manual You will see the C interface to the C ABI
07:04
now the interesting thing about zig is that It faithfully you know uses the C ABI, so you know you anything you can you define in zig you can essentially Access from guile, so let's look at my zig file, and I say ping
07:26
Yeah, so this is the you know sort of the equivalent C function Sorry zig-ving function that we had defined in C earlier. Yeah, you have ping zig It takes a scheme object as an input, and it returns a scheme object right and it just pings it back
07:43
So how come how come I guess how can I see the scheme object as it is defined now and as a matter of fact? Zig can export or so it can import C include files. Yeah, it's actually one command
08:11
Yeah, so you say zig translate C right then the path to the include file Then include file itself and it turns it to you know it turns into a proper zig file
08:22
And this zig file you can you can just import in it and it will work And so all all the light all the functions that are defined by guile That are exported for for you that you could use from C in principle are now available in zig including the objects Yes, if you look at this
08:41
Zig file it doesn't look very nice, right? But it's all there, and it actually just worked in one go. I didn't have I had to delete one line in it All right, so yeah the other thing of course that is is that I'm using ping zig one right now, okay, so let's try
09:04
Hello, and it pings back hello, right. I mean that's what we see in the in the zig function here and Guile is not a strictly typed language. Yeah, I mean it's it's typed, but it's not in the sense that here
09:21
We have a variable that you can you know in the one case? I'm using the integer in the next case. I'm using a string And and this makes for apart from the fact that I'm using a raffle where I'm actually talking to the you know to the zig back end It also gives me a lot of flexibility and what you know how I how I define these functions and these variables that get passed
09:46
okay, so Let's do something a little bit more complex So the you know this exploration that I had with with zig and and see and and guile
10:02
It's it's also all online you can just read it. It's in a gitlab Repository Yeah, so if you define a function in in In zig you know just naively like ping zig here it won't be
10:22
Immediately visible to guile you need to you need to create a mapping for that Yeah, and this is a this is in the guile documentation. It's exactly the same thing in C So you can find it yeah here it is so when you're in it when you initialize the module Which is mean when you load the shared library, right? it will call this function and
10:40
you will define a Sorry, it will Yeah, define the function call here. So in print zig here is ping zig right ping zig and it has one argument All right, you need to do that to make the symbol feasible to the guile interpreter That's that's basically what you have to do. It's there's nothing more to it, which is kind of boring
11:08
You know So yeah, I'm opinion that you know that we need multiple programming languages, you know, there's there's the When you talk about zig there's often or even C++, you know, there's often the elephant in the room
11:22
I'm not gonna name it But this is a language that tries to be everything, you know, and you end up with a very complex language Also the compiler is dog slow, I don't know if anyone is using the unnamed language And then it has you know, it has a borrow checker which acts like, you know, a nagging wife, you know
11:46
It keeps keeps talking to you and it I tried it, you know I try and I tried to love it because it's a functional language, you know It's a functional programming language, but it kept talking to me and it kept going me out of my flow You know, I just couldn't keep moving so I I think you know, it's probably wiser to have a language like C which is
12:04
You know, you know, you have to realize that most of the code in the world today is written in C++ still If you want a type of performance you will have to end up with a strictly typed language Which is you know? Imperative to some degree because CPUs are imperative, right? We don't have at this point. We don't have really have functional programming
12:25
CPUs so to optimize that stuff you end up, you know with a bit of language that has to cater for that But nobody loves programming in C++, you know, and in C programming is also hard, you know, to shoot yourself in the food language. I call it
12:43
So it's nice if you can have a language that has somewhat stronger guarantees But it's still blazingly fast and still, you know kind of imperative And then have something like Guile which which actually allows you to be, you know, productive, right? And and do functional programming when you want to so you end up with this type of mixture
13:02
I've got five minutes Five minutes to two questions. Okay. So one thing I want one additional thing. I would like you to like to show you Sorry, that's mine
13:22
Yeah, so so you can I mean using the guile libraries you can essentially build up lists lists, you know which is the fundamental for many of the LISP like programming efforts, but You know when you talk about performance you you'd like to deal with with arrays of data
13:42
Yeah, so continuous blocks of memory where you have integers in a row or doubles in a row And you and you're able to address these integers and doubles Of course, of course you can do that from from guile But you know if you want to do if you if you write high performance code like we do You want to be able to you know, use it as a vector in zig
14:03
Yeah, so you have an index the base pointer to the vector and then you have an index You should be able to fetch out the data object that you want So just wrote a little Example here. So this is the list example. Let me see if I can I can the vector array. Yeah, so you have I
14:28
Wrote a little let me move that down a little zig function, which says my increment in Floating point 64 bits vector zig, you know, I'm very good at naming apparently
14:42
You pass in a guile vector, which is again a scheme object It returns a scheme object, which is again a vector right and then it causes guile function Scheme f64. Oh, that's why the way the naming came from Came from guile. Yes, so I said X, you know, so I said I've said in the vector
15:04
I Set At position 1 right in so index 1 I Set the value 3.7. Yeah, so this this is kind of happening in in in guile C code Yeah, so so it's it's calling essentially guile C functions
15:22
Yeah, and I proved that it works you can look it up But here here I'm using a proper index I think let me see Yeah, so you increment the f64 vector, right
15:42
This is the old version Here I get a handle on the on the array And Then I get data so I get a vector here, you know if our data is a vector You know of the elements of the vector array, right? and then I
16:01
Indexed the data points based on the factor, you know for off the floating point and show 0 1 and and and number 2 Yeah, and that's what it what it shows. So I'm not gonna do that live but that's the idea. Yeah, I'm done
16:22
Yeah, so It's in a nutshell the code and the day and the slides are online so you can have a look have a go Any questions?
16:40
Yes I Tried quite long actually, so I read five books I probably took two months And wrote a thousand lines of code or so to decide it wasn't for me
17:05
But yeah, I hear quite a few stories like this which are very similar I think you know, it's it's a language for masochists a Language for masochists you have to be a brilliant person right? Yeah. Well a brilliant masochist. Let's put it that way. Yeah
17:21
You have to be brilliant to keep that all in your head Yeah, so see Yeah, you know, I'm not I'm not complaining about it because People who program in rust They do better than in C++, you know, so the compiler does help a lot and I think it does lead to better code
17:46
I've given students. I know I work in in rust and I They do write better code because the compiler actually helps you. All right But it also takes them a long time to get something done. So it depends on what your goals are, right?
18:01
I mean if you have to if you have to write, you know, perfect software to launch your rocket You might you know want to do it that way But if it's just like me, you know in my job, we write mostly throwaway code. It doesn't pay By informatics, so I mean I'm in science
18:22
Yeah, we do have long-lived code, but it's usually usually by accident right so you write something People start using it and then ten years later, it's it becomes mainstream. It's actually happened to one of our projects. Yeah It's kind of yeah, and then you know, then it's too late to do better
18:51
Necessary No, I mean it's the garlic here's to the CA bi right so it has a calling interface
19:08
But I use the scheme object. Yeah, so I that came from there But actually the scheme object is really simple when you look at it So it could be that you know, just you can just roll your own
19:20
Yeah Didn't design zig I should have been clearer Nor have I any input on guile Unlike other people here, but but
19:44
Yeah, I you know, I dabble in languages, you know, I use a people often ask me what is the language of the year I Think at that point it was scala I'm embarrassed to say But No, I think you know zig does appeal to me, yeah
20:19
So so Fortran is you know, it's a bit difficult because the very different language is Fortran and Julia
20:25
But I think you know Zig tries to be in the space of C. Yeah, and it yeah, it's a general-purpose systems programming and uncompromising speed And it is fast. Yes, the compiler itself compiles itself in 10 minutes about now on the standard machine
20:47
But I think for example, it doesn't have exception handling Yeah, it uses a different approach which is more like a maybe monad You know and even C++ Typically, you'll have exception handling with which you may know every time you call a function
21:04
It has to carry state with it to be able to unroll the stack And this causes overheads That's one thing Now if we see plus plus the other thing is that in the background there's often a lot of Memory allocation going on. Yeah, especially when you I mean, it's kind of unavoidable to use the SEL these days
21:21
best practice And I find that in zig because it's closer to the C philosophy. It's actually much faster so Okay Yeah, we should yeah, we should I think the next speaker needs to go on know
21:44
How much time you have? Yeah, you can it's good to switch