From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Basile Starynkevitch Newsgroups: gmane.lisp.guile.user Subject: Re: using guile like a awk filter in a C program. Date: Tue, 21 May 2024 16:58:47 +0200 Message-ID: References: <60c7c61c6dea289c9a1d8e59829f9c91@univ-nantes.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10045"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla Thunderbird Cc: Pierre LINDENBAUM To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Tue May 21 16:59:22 2024 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1s9QxW-0002Lq-Po for guile-user@m.gmane-mx.org; Tue, 21 May 2024 16:59:22 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s9Qx7-0000U0-Jd; Tue, 21 May 2024 10:58:57 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9Qx6-0000Tl-33 for guile-user@gnu.org; Tue, 21 May 2024 10:58:56 -0400 Original-Received: from relay7-d.mail.gandi.net ([2001:4b98:dc4:8::227]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9Qx2-0006Wu-KE for guile-user@gnu.org; Tue, 21 May 2024 10:58:55 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 96ECA20004; Tue, 21 May 2024 14:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=starynkevitch.net; s=gm1; t=1716303527; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AjI1mlCQmMetUWGac1lI/itfaj/SQ88SVXvu8UUZpHA=; b=Sq69rrni9peFqvuDxt93Jj423ni26RExF057xZKOJNY+1o+5ALxD4x21HsFHyZ6+MFfOko +qSkGJK/nW6BPaX6E2TkWIc7TxRyaxu687V2n2j6GbqKVWt6fc2oCSVsdbWKvXlPCnFBbT 7aYIuzct4neYOjenCCSSa00+YPZcIuy7BF7MwOdbRUvbGBUXPQY21TU0a5D0HiL0k67Azm WrRDjVVIq4vhiYFT3r/59ef/Mz1A7M/F3tTuBUhJyHoG0Ct4jtSGvoXahTkCh7Wxo0cy7q pwnQonGDKqq0gy+eeGPUBv5quy1ou7qLAmarnOjIDpyJaQ14DfTbJs53TS2LkQ== Content-Language: en-US, fr-FR In-Reply-To: <60c7c61c6dea289c9a1d8e59829f9c91@univ-nantes.fr> X-GND-Sasl: basile@starynkevitch.net Received-SPF: pass client-ip=2001:4b98:dc4:8::227; envelope-from=basile@starynkevitch.net; helo=relay7-d.mail.gandi.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19669 Archived-At: Hello Pierre, (if you reply privately, feel free to reply in French, we both are French) You definitely could use libguile in your C program. Since Guile has a conservative garbage collector, and assuming your program is single-threaded, this should be reasonably easy. If your C program needs to be multi-threaded (with each thread doing garbage collection or GUILE object allocation) it would be probably a lot harder. If you need the full power of Scheme call/cc primitive (traversing C primitives and a mixed C/Scheme call stack) you could be in trouble. You probably know about the Bigloo implementation of Guile (compiling to C) by Manuel Serrano https://github.com/manuel-serrano/bigloo (I don't know if it fits better your need, but that code is very well written and robust) BTW, my pet open source project is https://github.com/RefPerSys/RefPerSys/ "REFlexive PERsistent SYStem" - it is not Guile, but provide orthogonal persistence (in JSON files), is GPLv3+ licensed, coded mostly in C++ for Linux, has a precise garbage collector (with some moving/copying of simple immutable data), a multi-threaded agenda mechanism. It is work in progress (aiming to become some open source inference engine). We are seeking some ITEA or ANR or HorizonEurope interested by this open source (or even informal students contributing to it). On 5/10/24 15:55, Pierre LINDENBAUM wrote: > > Hi all, > > I tried to learn guile a few years ago with a side project that went > nowhere. > > I'm now back with guile that I would like to use as a filter, just > like awk, for my data. > I've got question about the general design of such program. > > My program uses a C library ( https://github.com/samtools/htslib ) > scanning mutations/variants in large VCF files ( > https://en.wikipedia.org/wiki/Variant_Call_Format ). > > A typical C program looks like (pseudo code) ; > >     ``` >     header = read_header(input); >     variant = new_variant(); >     while(read_variant(input,header,variant)) { >         do_something(header,variant) >         } >     dispose_variant(variant) >     dispose_header(header) >     ``` > > I would like to use guile to filter VCF using a custom user GUILE > expression/program . So my program would now look like > >     ``` >     header = read_header(input); >     guile_context = my_initialize_guile(header, argc_argv_user_script) >     variant = new_variant(); >     while(read_variant(input,header,variant)) { >         if(!my_guile_test(guile_context,header,variant)) { >             continue; >             } >         do_something(header,variant) >         } >     dispose_variant(variant) >     my_dispose_guile(guile_context) >     dispose_header(header) >     ``` > > and would may be be invoked like: > > ``` > ./a.out -e '(and (variant-is-snp? ) (equals? (variant-allele-count) > 2))' input.vcf > output.vcf > ``` > > where `variant-is-snp` would test if the current variant in the > 'while' loop is a 'single nucleotide polylmorphism' using `bcf_is_snp` > https://github.com/samtools/htslib/blob/develop/htslib/vcf.h#L889 > > > So my questions are: > > - is it possible to use guile for such task ? More precisely, can I > compile the guile script just once in `my_initialize_guile` and use it > in the while loop without having to recompile it. Yes, by using scm_boot_guile and most importantly the internal functions it is calling. It is worth studying the source code of GNU guile. > - furthermore, what's the best practice to include the user's script > in a larger script that would include the definitions of > `variant-is-snp?` , `variant-allele-count`, etc... > - is there any implementation that works like this (something like a > AWK script in guile) ? > - is there any way to make the program stateless ? I mean, any > invocation of `my_guile_test` would erase the definition of the > previous variant Probably by having some Guile global variable. You want to read also https://www.gnu.org/software/guile/manual/html_node/Foreign-Object-Memory-Management.html > > Thanks for your answers, > Regards. -- Basile Starynkevitch (only mine opinions / les opinions sont miennes uniquement) 8 rue de la Faïencerie, 92340 Bourg-la-Reine, France web page: starynkevitch.net/Basile/ See/voir: https://github.com/RefPerSys/RefPerSys