How to use the infix operator from the SML module?

I have code for the code and I use SML / NJ:

signature STACK=
sig

    type 'a Stack

    val empty :'a Stack
    val isEmpty : 'a Stack -> bool

    val cons : 'a*'a Stack -> 'a Stack
    val head : 'a Stack ->'a
    val tail : 'a Stack -> 'a Stack
    val ++ : 'a Stack * 'a Stack -> 'a Stack
end
structure List : STACK = 
 struct
 infix 9 ++
type 'a Stack = 'a list

val empty = []
fun isEmpty s = null s

fun cons (x,s) = x::s
fun head s = hd s
fun tail s = tl s
fun xs ++ ys = if isEmpty xs then ys else cons(head xs, tail xs ++ ys)    

end

I want to use the ++ operator from the interpreter, but when I write s1 List. ++ s2, where the stack types are s1 and s2, I get a message that the operator is not a function.

Thank.

+3
source share
1 answer

You declared it ++as an infix within a structure, and this declaration is limited to the area of ​​the structure (inside struct...end). You can declare it as infix at the top level or use it as a prefix, but in increment declarations, SML are not included in the signature.

- List.++ ([1], [2,3]);
val it = [1,2,3] : int Stack

- infix 9 ++;
infix 9 ++
- open List;
...
- [1] ++ [2,3];
val it = [1,2,3] : int Stack

Take a look at some interesting hacks: http://www.mlton.org/InfixingOperators

+3
source

All Articles