coalton-lang / coalton

Coalton is an efficient, statically typed functional programming language that supercharges Common Lisp.

Home Page:https://coalton-lang.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to call postmodern from coalton ?

devosalain opened this issue · comments

There seems to be a "symbol naming clash" between coalton & postmodern.
So i think i need to wrap , but this looks very ugly :


(load "~/quicklisp/setup.lisp")
(declaim (optimize (speed 3) (safety 3)))
(ql:quickload "fiasco")
(ql:quickload "coalton")
(ql:quickload "postmodern")

(defpackage mytest
  (:use #:coalton #:coalton-prelude)
  (:export cmain ) );defpackage

(in-package :mytest)

(cl:defun mycreatetable ()
  ;(postmodern:query (:create-table 'persons ((name :type cl:string) (age :type cl:integer))))
  );defun

(cl:defun myinsert ()
  (postmodern:query  (:insert-into 'persons :set :name "Alain" :age 20 ));query
  (postmodern:query  (:insert-into 'persons :set :name "Eddy"  :age 30)));defun

(cl:defun myquery ()
    (cl:let 
        ((myname nil)
         (myage  nil))
        (cl:princ (postmodern:doquery (:select 'name 'age :from 'persons) (myname myage) (cl:print myname) (cl:print myage)))
        ))

(cl:defun myconnect ()
    (cl:princ "connect")
    (postmodern:connect-toplevel "x" "y" "z" "127.0.0.1" :port 5432 ))

(cl:defun myprint (s)
    (cl:princ s)
    )

(coalton-toplevel
    
    (declare myprintwrapper ( Unit -> Unit ))
    (define (myprintwrapper z)
       (lisp Unit (z) (myprint (myquery)) Unit)
       );define


    (declare myquerywrapper ( String -> Unit ))
    (define (myquerywrapper y)
       (
       myprintwrapper (lisp Unit (y) (myquery) Unit))
       )
        
    (declare mywrapper ( String -> Unit ))
    (define (mywrapper x)
        (lisp Unit (x) (myconnect)(mycreatetable)(myinsert)Unit)
        (myquerywrapper x)       
        );define

    (declare cmain ( Unit -> Unit ))
    (define (cmain) (mywrapper "") ) );toplevel

(in-package :cl)

(defun main ()
	(mytest:cmain coalton:Unit ) )
(sb-ext:save-lisp-and-die "test.exe" :toplevel #'main :executable t)

Is there a better option ?

If you want to handle this with package management, which may be useful for larger situations:

It might work to have two packages, A lisp package with postmodern free to do what it wants, and then a coalton package importing the cl-package.

(defpackage #:mytest-backend
   (:use #:cl #:postmodern)
   (:export <functions>)

(in-package #:mytest-backend)

<cl function definitions>

(defpackage #:mytest
    (:use #:coalton
             #:coalton-prelude
             #:mytest-backend)

(in-package #:mytest)

<Coalton code>

This will let you avoid all the cl: prefixes and also have a defined space in your project for the postmodern backend
.
It also might be more practical to just add a local-nickname for postmodern, which would avoid the name clash:

(defpackage #:mytest
    (use #:coalton
            #:coalton-prelude)
    (local-nicknames (#:post #:postmodern)))

You'll have to use the prefix post: on postmodern functions but it would be easier than redefining myeverythings.

If you do find that it makes sense to keep the lisp definitions at the top, I'd recommend making two packages, so you can avoid all the

In terms of wrappers, this might be a preferable approach:

(coalton-toplevel

  (define (createtable)
    (Lisp Unit ()
      (post:query (:create-table 'persons ((name :type cl:string) (age :type cl:integer))))))
  
  (define (insert)
    (Lisp Unit ()
      (post:query (:insert-into 'persons :set :name "Alain" :age 20))
      (post:query (:insert-into 'persons :set :name "Alain" :age 30)))))

This way you would have native Coalton functions that perform the sql operations, without any of the lisp predefinitions.

I don't have a database connected right now, so I'm not sure about the return types of these functions, but this frame should work.

Code below works !!!,


(load "~/quicklisp/setup.lisp")
(declaim (optimize (speed 3) (safety 3) (space 0) (debug 3)))
(ql:quickload "serapeum")
(ql:quickload "fiasco")
(ql:quickload "coalton")
(ql:quickload "postmodern")

(defpackage mymain
  (:use #:cl
        #:serapeum)
  (:export main)) ;defpackage

(defpackage mycoalton
  (:use #:coalton 
        #:coalton-prelude)
  (:local-nicknames (#:co #:cl) (#:pm #:postmodern))
  (:export cmain) ) ;defpackage

(in-package :mycoalton)
(coalton-toplevel
    (declare myconnect ( Unit -> Unit ))
    (define (myconnect)
        (Lisp Unit()
            (pm:connect-toplevel "x" "x" "x" "127.0.0.1" :port 5432 ) 
            Unit))
    (declare myquery ( Unit -> String ))
    (define (myquery)
        (Lisp String()
            (co:let ((myname nil)
                       (myage  nil)
                       (mysum  nil))
                (pm:doquery (:select 'name 'age :from 'persons)
                    (myname myage)
                    (co:setf mysum (co:concatenate co::'string mysum (co:format nil "~a:~a ~%" myname myage))))
                mysum)))
    (declare cmain ( Unit -> Unit ))
    (define (cmain)
        (myconnect)
        (Lisp Unit()
            (co:format co:t "~a ~%" (myquery Unit))
            Unit) 
        Unit)) ;toplevel

(in-package :mymain)
(-> main () Integer )
(defun main ()
	(mycoalton:cmain coalton:Unit)
	0 ) ; main
(sb-ext:save-lisp-and-die "test.exe" :toplevel #'main :executable t)