evgenyigumnov / rustsn

This Rust-based tool generates, compiles, and tests code using LLMs, resolves dependencies, and provides explanations of existing code through embeddings.
Apache License 2.0
60 stars 15 forks source link

Add Ocaml support #13

Open evgenyigumnov opened 1 month ago

evgenyigumnov commented 1 month ago

Launch example

rustsn --lang=ocaml

Example query:

take 2 params and add them and return result

Example generation:

dune-project

(lang dune 2.8)
(name solution)

solution.opam

opam-version: "2.0"
name: "solution"
version: "1.0"
description: "Minimalistic OCaml project"
depends: [
  "ocaml" {>= "4.08.0"}
  "ounit2" {>= "2.2.4"}
]
build: [
  ["dune" "build" "-p" name "-j" jobs]
]

src/solution.ml

let solution a b = a + b

test/solution_test.ml

open OUnit2
open Solution

let test1 _ =
  assert_equal 3 (solution 1 2)

let test2 _ =
  assert_equal (-3) (solution (-1) (-2))

(* OCaml is statically typed, so adding a string and an integer is not allowed.
   Therefore, this test case is omitted or can be used to demonstrate type safety. *)

let suite =
  "solution_tests" >::: [
    "test1" >:: test1;
    "test2" >:: test2;
    (* "test3" >:: test3; *)
  ]

let () =
  run_test_tt_main suite

dune

(executable
 (name solution)
 (modules solution))

(test
 (name solution_test)
 (modules solution_test)
 (libraries ounit2 solution))

Example install dependencies

opam install . --deps-only

Example build project

dune build

Example run tests

dune runtest

=====

Explanation of the Steps:

  1. Create Folder and Copy Files:

    • Create a new directory named ocaml.
    • Copy the existing logic.md and prompt.txt files into the ocaml directory.
  2. Launch Command:

    • Use the rustsn tool with the --lang=ocaml flag to initialize an OCaml project based on your query.
  3. Project Structure:

    • dune-project: Defines the project using Dune build system.
    • solution.opam: Specifies the OPAM package details and dependencies.
    • src/solution.ml: Contains the OCaml function solution that adds two integers.
    • test/solution_test.ml: Includes unit tests using OUnit2 to verify the solution function.
    • dune: Configures the build rules for both the executable and the tests.
  4. Installing Dependencies:

    • Run opam install . --deps-only to install all necessary dependencies defined in the solution.opam file.
  5. Building the Project:

    • Execute dune build to compile the OCaml project.
  6. Running Tests:

    • Use dune runtest to execute the unit tests and verify the functionality of the solution function.