Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml syntax error in type declaration

Tags:

ocaml

I'm new to OCaml and I have no clue why this is giving me a syntax error:

type ('nonterminal, 'terminal) pe =
| Empty
| T of t 
| N of n
| Seq of list
| Choose of list
| Star of e
| Not of e;;

type ('nonterminal, 'terminal) pe_tree = 
| Is_empty 
| Leaf of t 
| Node of (n,tree) 
| Sequence of list 
| Repeat of list 
| Is_not of e;;

All it's saying is that there's a syntax error on line 14 characters 0-1 (which is where | Sequence of list is) and I can't figure out why!

like image 839
robocop Avatar asked Feb 26 '23 07:02

robocop


1 Answers

type ('nonterminal, 'terminal) pe_tree = 
  | Is_empty 
  | Leaf of t 
  | Node of (n * tree) 
  | Sequence of list 
  | Repeat of list 
  | Is_not of e;;

You use * to define product types, as in 'a * 'b. Although probably not too important now, you should know that Node of 'a * 'b and Node of ('a * 'b) are different. You can think of them as a variant type with two parameters, and the other a variant type with one parameter (a tuple), respectively.

There are a few other things,

  • you need to define what Sequence and Repeat are a list of.
  • 'nonterminal and 'terminal aren't used; unless they are for phantom types, which i doubt it, they probably should be used in part of the signature.
like image 108
nlucaroni Avatar answered Mar 03 '23 05:03

nlucaroni