Browse thread
Re: declaration of an infix operator in a .mli file
- Pierre Weis
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 1998-09-08 (07:53) |
From: | Pierre Weis <Pierre.Weis@i...> |
Subject: | Re: declaration of an infix operator in a .mli file |
> Dans foo.ml, j'ai les définitions suivantes de nouveaux opérateurs infixes : > > (***) > let epsilon = 1.0e-4 ;; > > let (prefix =?) = fun x y -> abs_float (x -. y) <. epsilon > and (prefix <?) = fun x y -> x <. y +. epsilon > and (prefix >?) = fun x y -> y <. x +. epsilon ;; > (***) > > Quel fichier foo.mli dois-je écrire pour les exporter ? value prefix =? : float -> float -> bool and prefix <? : float -> float -> bool and prefix >? : float -> float -> bool;; > Et, au fait, pourquoi > > let (prefix =?) x y = abs_float (x -. y) <. epsilon ;; > > est-il incorrect ? (J'obtiens une Erreur de syntaxe.) Il n'y a pas de bonne raison évidemment. Mais l'idée est que le mot-clé prefix suffit à introduire la version prefixe d'un opérateur infixe et que les parenthèses sont inutiles. On écrirait donc plutôt let prefix =? = fun x y -> abs_float (x -. y) <. epsilon and prefix <? = fun x y -> x <. y +. epsilon and prefix >? = fun x y -> y <. x +. epsilon ;; et donc let prefix =? x y = abs_float (x -. y) <. epsilon and prefix <? x y = x <. y +. epsilon and prefix >? x y = y <. x +. epsilon ;; ----- > I wrote this litte file foo.ml : > > (***) > let epsilon = 1.0e-4 ;; > > let (prefix =?) = fun x y -> abs_float (x -. y) <. epsilon > and (prefix <?) = fun x y -> x <. y +. epsilon > and (prefix >?) = fun x y -> y <. x +. epsilon ;; > (***) > > I do'nt know write the file foo.mli. value prefix =? : float -> float -> bool and prefix <? : float -> float -> bool and prefix >? : float -> float -> bool;; > Another question : why is the following wrong ? > > let (prefix =?) x y = abs_float (x -. y) <. epsilon ;; > > (I obtain a Syntax error) No really good reason. In fact the prefix keyword if used to introduce the prefix version of an infix operator. Hence the parens are useless and redundant. If you remove them then you can write let prefix =? = fun x y -> abs_float (x -. y) <. epsilon and prefix <? = fun x y -> x <. y +. epsilon and prefix >? = fun x y -> y <. x +. epsilon;; and thus let prefix =? x y = abs_float (x -. y) <. epsilon and prefix <? x y = x <. y +. epsilon and prefix >? x y = y <. x +. epsilon;; Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/