Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings/haskell): init haskell binding #2463

Merged
merged 5 commits into from
Jun 15, 2023
Merged

feat(bindings/haskell): init haskell binding #2463

merged 5 commits into from
Jun 15, 2023

Conversation

silver-ymz
Copy link
Member

@silver-ymz silver-ymz commented Jun 15, 2023

This is a demo for haskell binding.

Explanation

I researched 3 ways to complete haskell bindings.

  1. Using hs-bindgen
  2. According completed C binding
  3. Self exporting C-compatible API

The first way is the simplest way. However, the function of hs-bindgen is limited. Moreover, hs-bindgen seems still a working experiment. I raised 2 issues about it which did not receive much attention from the author.
yvan-sraka/hs-bindgen-attribute#2, https://github.com/yvan-sraka/reflexive/issues/1

As for the second way, using the existing C bindings can simplify the process of creating Haskell bindings. However, there are also some potential downsides.

  • Some C APIs are difficult to interact with haskell.
    For example, haskell can't deal with returned struct directly. struct opendal_result_read opendal_operator_blocking_read(struct opendal_operator_ptr ptr, const char *path); will be hard to use in haskell. We need to write some C codes to make it become void opendal_operator_blocking_read(struct opendal_operator_ptr ptr, const char *path, struct opendal_result_read* result);
  • Less flexible
    If we want some features about haskell binding, we must wait it in C bindings.

Because of these reasons, I choose to create a C-compatible interface from the Rust library.

Also ask advices from maintainers.

Example

Exported API will like this.

import OpenDAL
import qualified Data.HashMap.Strict as HashMap

main :: IO ()
main = do
  op <- createOp "memory" HashMap.empty
    case op of
      Left e -> putStrLn $ "Failed to create operator, " ++ e
      Right op' -> do
        _ <- writeOp op' "key1" "value1"
        _ <- writeOp op' "key2" "value2"
        value1 <- readOp op' "key1"
        value2 <- readOp op' "key2"
        print value1
        print value2

Tests

I write a simple test testReadAndWriteToMemory, which can pass now.

Signed-off-by: silver-ymz <[email protected]>
bindings/haskell/Cargo.toml Outdated Show resolved Hide resolved
bindings/haskell/haskell-src/OpenDAL.hs Show resolved Hide resolved
Signed-off-by: silver-ymz <[email protected]>
@silver-ymz silver-ymz marked this pull request as draft June 15, 2023 06:54
Signed-off-by: silver-ymz <[email protected]>
bindings/haskell/Cargo.toml Outdated Show resolved Hide resolved
bindings/haskell/Cargo.toml Outdated Show resolved Hide resolved
Signed-off-by: silver-ymz <[email protected]>
@silver-ymz silver-ymz marked this pull request as ready for review June 15, 2023 07:13
Copy link
Member

@Xuanwo Xuanwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM! This PR is good start! Are you willing to create a tracking issue of ongoing tasks? The first thing we should address is building the develop loop:

  • Add contributing docs on how to setup the develop environment
  • Add CI jobs on building and testing hs binding

And than we will need to implement more features, finally, we will handle the release process.

@Xuanwo Xuanwo merged commit 25f2ce4 into apache:main Jun 15, 2023
@silver-ymz
Copy link
Member Author

issue #2465 is opened for tracking.

@silver-ymz silver-ymz deleted the feat/haskell-binding branch June 15, 2023 07:26
@PsiACE PsiACE mentioned this pull request Jun 27, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants