r/ocaml • • May 02 '24

Newbie problem

A few days ago I posted about having to learn Ocaml to do a college assignment. The assignment involves creating a shopping cart system with Ocaml for the backend and Java for the frontend. The professor provided a database "store.pl" and a file "main.ml" that reads this file and returns the following (in "interpreter mode"/ocaml):

# #use "main.ml";;
val read_file : string -> string list = <fun>
val parse_discount : string -> (string * float) option = <fun>
val parse_item : string -> (int * string * string * float * int) option =
  <fun>
val parse_loyalty_discount : string -> (string * float) option = <fun>
val parse_shipping_cost : string -> (string * float) option = <fun>
Discount for category 'potions': 0.100000
Discount for category 'wands': 0.200000
Discount for category 'enchanted_books': 0.300000
Discount for category 'crystals': 0.150000
Discount for category 'amulets': 0.250000
Item: 1, Potion of Healing, potions, 10.000000, 50
Item: 2, Wand of Fireball, wands, 20.000000, 30
Item: 3, Enchanted Spellbook, enchanted_books, 30.000000, 20
Item: 4, Crystal of Clairvoyance, crystals, 15.000000, 40
Item: 5, Amulet of Protection, amulets, 25.000000, 25
Loyalty discount for 1 year(s): 0.050000
Loyalty discount for 2 year(s): 0.100000
Loyalty discount for 3 year(s): 0.150000
Loyalty discount for 4 year(s): 0.200000
Loyalty discount for 5 year(s): 0.250000
Loyalty discount for >5 year(s): 0.300000
Shipping cost to district Aveiro: 5.000000
Shipping cost to district Lisboa: 7.000000
Shipping cost to district Porto: 10.000000

Alright, so I started by creating a file "app.ml" and I would need to receive this list in a variable to then manipulate the strings and so on. The thing is, I couldn't even get past the third line because I don't know how to receive this list.

I have something like:

open Main

let dados = Main() ?????;

How should I go about it?

P.S.: Another question: can I test this code, divided into several files, through the interpreter or do I need to compile it??

Thanks for your attention!

0 Upvotes

4 comments sorted by

3

u/ComanderP May 02 '24 edited May 02 '24

Hi, I'm not an ocaml pro but I think I can try to help ya.

How should I go about it?

For your main question, it's hard for me to tell you how to do it because I don't really know how main is implemented. But reading the type definitions for your function, I assume you can just call read_file from main and then use your parsing functions to convert them to something more usable.

Also, to use a function from module Main in another module you'd write something like this:

open Main
let dados = read_file <file> in (* no need to write Main when using open above *)
...

Regarding the testing part, you can definitely use the interpreter to test your code (a bit more tricky for importing imo), although I'm more used to writing a separate test module for that.

Hope that helps!

P.S. If you have any other question feel free to ask! Boa sorte!

1

u/InternationalFan9915 May 02 '24

Well, your tip was very helpfull but, since you said I could ask new questions, now I can't compile "main.ml". There is an error about Str librarie. Here are my files:

[main.ml]

open Str

let read_file filename =
  let lines = ref [] in
  let channel = open_in filename in
  try
    while true do
      lines := input_line channel :: !lines
    done; []
  with End_of_file ->
    close_in channel;
    List.rev !lines

let parse_discount line =
  let regexp = Str.regexp "discount(\\(.*\\), \\(.*\\))." in
  if Str.string_match regexp line 0 then
    Some (Str.matched_group 1 line, float_of_string (Str.matched_group 2 line))
  else
    None
[...]

[dune]

(executable
 (public_name efolio)
 (name main)
 (libraries str))

So I try to compile with dune build -x windows and it returns:

File "_none_", line 1:              
Alert ocaml_deprecated_auto_include: 
OCaml's lib directory layout changed in 5.0. The str subdirectory has been
automatically added to the search path, but you should add -I +str to the
command-line to silence this alert (e.g. by adding str to the list of
libraries in your dune file, or adding use_str to your _tags file for
ocamlbuild, or using -package str for ocamlfind).

File "bin/main.ml", line 1, characters 0-8:
1 | open Str
    ^^^^^^^^
Error (warning 33 [unused-open]): unused open Str.

If I cut "open Str" off it also doesn't works. I don't get what should I do by the debugger message. Is it automatically added or not? If it is, why do I need to reference it in dune file?

2

u/ComanderP May 02 '24 edited May 02 '24

I copied the initial part of your main.ml and it compiled fine for me (with a small exception below), although I'm running OCaml on Ubuntu through WSL (I think you're running it on windows?). I didn't get the deprecated message you're getting.

Dune shows warnings as errors by default and prevents you from compiling because of that. I'm assuming the open Str isn't actually doing anything as I see you have qualified calls to Str in the code (e.g. Str.string_match). Usually the solution would be removing the open Str at the top, but since you said it didn't change anything, I don't really know.

Another alternative is disabling warnings as errors in your dune file altogether using the (flags (-w -A)) stanza (it's good for quick prototyping). Example dune file below:

(executable
 (public_name testing)
 (name main)
 (libraries str)
 (flags
  (-w -A)))

Tell me if this changes anything, although I doubt it.

P.S. What error do you get when you remove the open Str at the top?

1

u/InternationalFan9915 May 02 '24

What error do you get when you remove the open Str at the top?

File "_none_", line 1:              
Alert ocaml_deprecated_auto_include: 
OCaml's lib directory layout changed in 5.0. The str subdirectory has been
automatically added to the search path, but you should add -I +str to the
command-line to silence this alert (e.g. by adding str to the list of
libraries in your dune file, or adding use_str to your _tags file for
ocamlbuild, or using -package str for ocamlfind).
File "_none_", line 1:             
Error: No implementations provided for the following modules:
         Str referenced from bin/.main.eobjs/native/dune__exe__Main.cmx
File "_none_", line 1:                       
Alert ocaml_deprecated_auto_include: 
OCaml's lib directory layout changed in 5.0. The str subdirectory has been
automatically added to the search path, but you should add -I +str to the
command-line to silence this alert (e.g. by adding str to the list of
libraries in your dune file, or adding use_str to your _tags file for
ocamlbuild, or using -package str for ocamlfind).

Unfortunelly, it didn't work. I'm almost giving up of it. Thank you for your help.