Warning from guile when using procedure from external file - module

I have procedure that generate sha1 hash from string:
(define (string->sha1 string)
(let ((port (open-input-string string)))
(hex (sha1 port))))
the function hex and sha1 are taken from A "pure" scheme implementation (R5RS) of SHA256? I've put the code in sha.scm and load using:
(load "sha.scm")
and I've got warnings like this:
;;; /home/kuba/projects/jcubic/notes/./rpc.scm:66:6: warning: possibly unbound variable `hex'
;;; /home/kuba/projects/jcubic/notes/./rpc.scm:66:11: warning: possibly unbound variable `sha1'
How can I resolve those warnings?

Related

Why clojure.repl/source does not works for my custom functtion

When I execute the clojure.repl/source activate in repl, it gives me the source of the activate function.
Now, I defined a custom function in namespace tutorial.test
(ns tutorial.test)
(defn return_type_of_arg
[x]
(type x)
)
After switching the repl namespace to tutorial.test and loading this file in repl, I tried to execute clojure.repl/source return_type_of_arg.
It's giving me the output as Source not found
What's going wrong?
EDITS:
I have confirmed that the namespace is shifted to the one required. Please see the following image:
These are the exact steps I followed:
Created a file test.clj and added above fucntion into it.
Left clicked this file in Intellij Idea and selected option Switch REPL namespace to current file.
Left clicked this file in Intellij Idea and selected option
Load file in REPL.
Below is a transcript of a REPL session that shows the steps to make this work.
dorabs-imac:tmp dorab$ cat tutorial.clj
(ns tutorial)
(defn return_type_of_arg
[x]
(type x))
dorabs-imac:tmp dorab$ clj -Sdeps '{:paths ["."]}'
Clojure 1.10.3
user=> (require 'tutorial)
nil
user=> (in-ns 'tutorial)
#object[clojure.lang.Namespace 0x187eb9a8 "tutorial"]
tutorial=> (clojure.repl/source return_type_of_arg)
(defn return_type_of_arg
[x]
(type x))
nil
tutorial=>
dorabs-imac:tmp dorab$
Added in edit:
First I created a file called tutorial.clj with the ns form and the defn of the function.
Then I started a REPL with the current directory (.) in the CLASSPATH so Clojure knows where to load the file from.
Then I loaded up the file using require. This loads the function definition into the tutorial namespace.
Then I changed the current namespace of the REPL to be tutorial using the in-ns function.
Then I ran the clojure.repl/source function.
Further edit:
I think I have identified the problem you are facing. It seems that the way you are using IntelliJ, it is sending the contents of the file to the REPL, rather than requireing the file. According to the docs for source, "Prints the source code for the given symbol, if it can find it.
This requires that the symbol resolve to a Var defined in a
namespace for which the .clj is in the classpath." [Emphasis mine]. So, when the file contents are sent to the REPL directly (without using require) there is no .clj file for source to look for the source. The following transcript demonstrates that. Compare the following transcript with the transcript above that does work.
dorabs-imac:tmp dorab$ clj
Clojure 1.10.3
user=> (ns tutorial)
(defn return_type_of_arg
[x]
(type x))
nil
tutorial=> tutorial=> #'tutorial/return_type_of_arg
tutorial=> tutorial=> (clojure.repl/source return_type_of_arg)
Source not found
nil
tutorial=>

I use Simply Fortran, but cannot include modules there [duplicate]

This question already has an answer here:
The mysterious nature of Fortran 90 modules
(1 answer)
Closed 2 years ago.
I have written the following Fortran Code in the System called "Simply Fortran":
program math2
use prec, only: print_kind_info
implicit none
call print_kind_info
end program math2
module prec
implicit none
integer, parameter :: dp = selected_real_kind(P=10,R=30)
integer, parameter :: sp = selected_real_kind(P=5,R=15)
integer, parameter :: dp_alt = kind(0.d0)
public :: dp, sp, print_kind_info
private
contains
! Subroutine comes here
subroutine print_kind_info()
real(sp) :: sing_prec
real(dp) :: double_prec
print*,'Single precision is kind ',sp
print*,'Double precision is kind ',dp
print*,'Kind of double precision number is ',dp_alt
end subroutine print_kind_info
end module prec
However, the module which I call 'prec' is not found. More precisely, the error message is the following:
use prec, only: print_kind_info
1
Fatal Error: Can't open module file 'prec.mod' for reading at (1): No such file or directory
compilation terminated.
Error(E42): Last command making (build\prec.o) returned a bad status
Error(E02): Make execution terminated
* Failed *
What I have to do that the module (see above code) is recognized properly? It is recognized in File Outline, but it is not inside the "module" folder. And I really don't have a plan here what to do. Moreover I have no experience with the Makefile environment (this BASHrc-like things I am not familar with).
Simply said: The problem is that the compiler doesn't know the module prec.mod at is defined after the main program but is used already in the main program.
So first define the module and after that the main program.
Better would be to place the module in a separate files and the main program in another file and compile these (in the right order) and link everything together.

Fortran .mod file doesn't update when compiling [duplicate]

I am working with GFortran and CodeBlocks but I'm having an issue about Modules and Multiple files.
i keep getting this error:
Fatal Error: Can't open module file 'mesh.mod' for reading at (1): No such file or directory
For some reason, GFortran is not building the 'mesh.mod' file.
This problem does not occur when I put all the code in a single .f90 file.
Bellow is an example of code that this error happens.
main.f90
MODULE MESH
IMPLICIT NONE
INTEGER :: IMAX,JMAX,NMAX
REAL(8), ALLOCATABLE :: XD(:),YD(:),FX(:,:),FY(:,:)
REAL(8) :: PI,E,DX,DY,H,L,RHO,MU
PARAMETER (PI = ACOS(-1.D0))
PARAMETER (E = 2.718)
END MODULE MESH
!**************************************************************
program Cavity
Use Mesh
implicit none
Real(8), Allocatable :: func(:)
Real(8) :: Der,DfDx
integer :: i
IMAX=10
DX=1./10
Allocate(xd(IMAX),func(IMAX))
Do i=1,IMAX
xd(i)=i*DX
End Do
Do i=1,IMAX
func(i) = xd(i)**2
End Do
Der=Dfdx(func,2)
Write(*,*) Der
End program Cavity
Derivatives.f90
Real(8) Function DfDx(f,i)
Use Mesh
implicit none
Real(8) :: f(1:Imax)
integer :: i
DfDx=(f(i+1)-f(i-1))/(2d0*dx)
return
end function DfDx
When I use console command line compilation instead of CodeBlocks interface I already solved this problem (Compiling Multiple Files with modules) but I'm still getting this problem with CodeBlocks.
Does anyone know how to solve this issue?
Assuming what you have written is how your code is, then it appears that the problem is that the module mesh is inside the main program and not a separate file. You should have three files: Mesh.f90, Derivatives.f90 and Main.f90.
Mesh.f90 is exactly as you have it,
module Mesh
implicit none
integer :: IMAX,JMAX,NMAX
real(8), allocatable :: XD(:),YD(:),FX(:,:),FY(:,:)
real(8) :: PI,E,DX,DY,H,L,RHO,MU
parameter (PI = ACOS(-1.D0))
parameter (E = 2.718)
end module Mesh
Derivatives.f90 should be written as another module, using contains:
module Derivatives
use mesh
contains
real(8) function dfdx(f,i)
real(8) :: f(i:imax)
integer :: i
DfDx=(f(i+1)-f(i-1))/(2d0*dx)
end function dfdx
end module Derivatives
and the Main.f90 will then use both modules. Note that I had to eliminate the variable DfDx; this is because it conflicts with the function DfDx in module Derivatives
program Cavity
Use Mesh
use Derivatives
implicit none
Real(8), Allocatable :: func(:)
Real(8) :: Der
integer :: i
IMAX=10
DX=1./10
Allocate(xd(IMAX),func(IMAX))
Do i=1,IMAX
xd(i)=i*DX
End Do
Do i=1,IMAX
func(i) = xd(i)**2
End Do
Der=Dfdx(func,2)
Write(*,*) Der
End program Cavity
I do not know how CodeBlocks works, but I would presume it lets you choose the compilation order. If that is the case, you should compile Mesh.f90 first, then Derivatives.f90, then compile Main.f90 before linking them to an executable.
When I compiled & linked them, I got an answer of 0.200000002980232; hopefully that links up to what you have as well.
On codeblock, you may go to Project properties > Build targets
Then select the file you want to build first (say mod.f90).
In the "Selected file properties" go to "Build"
Here,change the priority weight. Lower weight implies the file will be built first.
The problem is that in CodeBlocks "projects are built in the order of appearence, from top to bottom" (CodeBlocks Wiki), in other words, the files are compiled alphabetically.
Which means that in my case, Derivatives.f90 was being compiled before than Main.f90 causing the error.
A way to circumvent the problem is to set only the Main.f90 file as build target in CodeBlocks:
Menu Project/Properties...
In Build Target Files at the tab Build targets check only Main.f90
And use the command Include 'File_Name.f90' inside the Main.f90 code to include the other f90 files for compilation in the right order.

Invoke `racket` in a Racket script

General question:
Can I invoke the current racket executable from within a running Racket script?
Basically, I'd like a replacement for (system "racket ...") in the case that (find-executable-path "racket") does not return a path to the Racket executable I'm currently using.
Context:
What I really want is to try compiling a few expressions and assert that they raise compilation errors. This is for unit testing.
I don't believe you need to step outside of the executable here. Try this:
#lang racket
(require syntax/modread)
;; define a namespace anchor to attach a namespace to:
(define-namespace-anchor anchor)
;; define a namespace for expansion:
(define target-namespace (namespace-anchor->namespace anchor))
(define program-to-compile
"#lang racket
(+ 3 4)")
;; go ahead and expand
(with-module-reading-parameterization
(λ()
(parameterize ([current-namespace target-namespace])
(expand
(read-syntax
"bogus-filename"
(open-input-string program-to-compile))))))
I think I'm correct when I say that Racket is singularly clean in its ability to provide the compiler to running programs in a disciplined way.
If your goal is just to compile some racket expressions, you can do that just with either compile or compile-syntax. An example file would be:
#lang racket
(require rackunit)
(define tests
(list #'(+ 1 "3")
#'(void void)
#'(string-append 4)))
(for/list ([t (in-list test)])
(check-exn exn:fail?
(lambda () (compile t))))
Where exn:fail? is whatever exception you are looking for.
Furthermore, if you have some common syntax context you want to run your test in, you can use #` #,. So your code would end up something like this:
#lang racket
(require rackunit)
(define tests
(list #'(+ 1 "3")
#'(void void)
#'(string-append 4)))
(for/list ([t (in-list test)])
(check-exn exn:fail?
(lambda () (compile #`(module anonymous racket
#,t)))))
Finally, if your code is stored on your computer, you can use John's solution, while using file->string to convert the file into a string.
For small tests, you can also use convert-compile-time-error from the syntax/macro-testing library. It turns an expression that causes a compile-time error into an expression that raises a run-time error when evaluated. The expression uses the environment where it occurs in the module, including local bindings; you don't have to fiddle with namespaces and eval.
(check-exn #rx"bad syntax"
(lambda () (convert-compile-time-error (lambda))))
There's also convert-syntax-error (on the same page).

Preprocessors and use association

In summary, is it possible to access via use association a preprocessor directive defined in a Fortran module?
Context
I use preprocessor statements to define subroutines to print warning and error messages. For example, I use the following module/subroutine, in the file errors.f, to print warning messages
module errors
use, intrinsic :: iso_fortran_env, only : error_unit=>stderr
implicit none
contains
!> Print formatted warning message.
subroutine warn_print( file, line, mesg )
implicit none
character(len=*), intent(in) :: file
integer, intent(in) :: line
character(len=*), intent(in) :: mesg
write(stderr,'(a,a,a,i4,a,a)') "WARNING::", file, ":", line, ": ", mesg
end subroutine warn_print
end module errors
and, in a separate file errors.h, I use the above module and define a preprocessor macro
use errors
#define warn( text )warn_print(__FILE__,__LINE__,text)
I then #include the file errors.h in whichever file/module I wish to use the warning print routine which allows me to simply write
call warn("Some warning message")
and the compiler will automatically include the file and line number at which the warning message was called.
Question
The use of #include 'errors.h' is rather idiosyncratic in Fortran code and it hides the use of the errors module. Ideally I would prefer to define the above preprocessor in the errors module itself. However, then when using that module, this preprocessor directive is not available to the program/module which uses this module.
Is there a way to make a preprocessor directive accessible via use association?
The only other way I can think of doing it is to just have the errors module and define the preprocessor directive in my call to the compiler (using, for example, the -D flag with ifort). Any suggestions for any alternative way of achieving the above would be greatly appreciated.
No, it is simply not possible, since the preprocessing and the compilation stages are completely separate one from each other and the C preprocessor does not know anything about the Fortran USE statement.
I use to #include 'config.h' (from autoconf) in most of my .F90 sources, without problems.
This may not be what you are looking for, but if you are using ifort, you can use traceback functionality to achieve something similar (a bit more powerful, but also more ugly), e.g.
program tracetest
call sub(5)
write(*,*) '=== DONE ==='
end program tracetest
subroutine sub(n)
use ifcore
integer :: n
character(len=60) :: str
write(str,*) '=== TROUBLE DETECTED: n =',n ! code -1 means "do not abort"
call tracebackqq(str,-1)
end subroutine sub
Then, compile with -traceback to see the source file, line, and stack trace. The stack trace and line may be obscured because of inlining; to avoid that, you can specify -traceback -O0 to get smth like this:
=== TROUBLE DETECTED: n = 5
Image PC Routine Line Source
a.out 0000000000473D0D Unknown Unknown Unknown
a.out 0000000000472815 Unknown Unknown Unknown
a.out 0000000000423260 Unknown Unknown Unknown
a.out 0000000000404BD6 Unknown Unknown Unknown
a.out 0000000000402C14 sub_ 12 tracetest.f90
a.out 0000000000402B18 MAIN__ 2 tracetest.f90
a.out 0000000000402ADC Unknown Unknown Unknown
libc.so.6 000000323201EC5D Unknown Unknown Unknown
a.out 00000000004029D9 Unknown Unknown Unknown
=== DONE ===
Alternatively, if want to keep the optimizations, and also want to see the correct line (12), you can compile with (for example) -fast -traceback -debug all,inline_debug_info. Something similar may be available in other compilers, but I am not sure.