osa1 github gitlab twitter cv rss

Shen.hs FFI

June 29, 2013 - Tagged as: en, haskell, shen.

Shen.hs now has a simple way to call Haskell functions. As an example, I removed some non-standard KLambda functions from Fun.hs(standard primitive functions are defined there). Instead, they live in a separate module now, and I load them in run-time when I’m debugging.

How it works

Shen.hs is now exports it’s modules as a library. This is required to allow writing Shen.hs functions in Haskell.

KLambda.Types module export all required type information to start writing Shen.hs functions in Haskell. All your functions should have the type KlFun1. To make converting Haskell functions with higher arity(ie. KlFun2, KlFun3) to KlFun1, all KlFun instances now have a mkFun1 :: a -> KlFun1 method.

As an example, Debug.hs module functions with types KlFun2, KlFun3 etc. To call them from within Shen.hs, they are all wrapped with mkFun1 function.

After defining functions with type KlFun1 and compiling it, you should call dynload primitive to load functions. Here’s an example session that demonstrates loading and using debugging functions:

$ Shen.hs --shen "/home/omer/Shen/hs_yeni/K Lambda"
loading file: /home/omer/Shen/hs_yeni/K Lambda/toplevel.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/core.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/sys.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/sequent.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/yacc.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/reader.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/prolog.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/track.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/load.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/writer.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/macros.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/declarations.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/t-star.kl ...loaded.
loading file: /home/omer/Shen/hs_yeni/K Lambda/types.kl ...loaded.

Shen 2010, copyright (C) 2010 Mark Tarver
released under the Shen license
www.shenlanguage.org, version 12
running under Haskell, implementation: GHC
port 0.1 ported by Ömer Sinan Ağacan


(0-) (cd "/home/omer/Shen/hs_yeni/dynload_test")
"/home/omer/Shen/hs_yeni/dynload_test/"

(1-) (dynload "Debug.o" "debug1F")
<function>

(2-) (debug1F "calling haskell function -- " 1 2)
calling haskell function -- 1 :: TyNum

2

(3-)

Note that you need both .hi and .o files to be able to load the library dynamically.

Status of Shen.hs

Before this additional feature I had fixed a bug and now Shen.hs fails in 8 tests in Test Suite distributed with Shen source. All errors are related and probably there is only one bug. Unfortunately narrowing bug’s scope is very hard at this point. Pull requests are welcome.

If you encounter any bugs please open an issue in Github repository.