-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymbol.sml
60 lines (49 loc) · 1.82 KB
/
symbol.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(* $Id$
*
* Copyright (c) 2008 Timothy Bourke (University of NSW and NICTA)
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the "BSD License" which is distributed with the
* software in the file LICENSE.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD
* License for more details.
*)
structure Symbol : SYMBOL = struct
infix <+ <- ++ <\ \ =:=
type symbol = Atom.atom
type symbolset = AtomSet.set
fun set <+ item = AtomSet.add (set, item)
fun set <\ item = AtomSet.difference (set, AtomSet.singleton item)
fun setA \ setB = AtomSet.difference (setA, setB)
fun i <- set = AtomSet.member (set, i)
val op++ = AtomSet.union
val emptyset = AtomSet.empty
val ` = Atom.atom
fun s1 =:= s2 = Atom.compare (s1, s2) = EQUAL
local structure SS = Substring in
fun getNewName (base, names) =
(*{{{1*)
if not (AtomSet.member (names, base)) then base
else let
fun getPrefix s = SS.string (SS.dropr Char.isDigit (SS.full s))
fun getSuffix s = let
val suf = SS.taker Char.isDigit (SS.full s)
in
if SS.isEmpty suf then 0
else valOf (Int.fromString (SS.string suf))
end
val prefix = getPrefix (Atom.toString base)
fun maxSuffix (a, m) = let val s = Atom.toString a in
if String.isPrefix prefix s
then Int.max (m, getSuffix s) else m
end
val suffix = (AtomSet.foldl maxSuffix 0 names) + 1
in
Atom.atom (prefix ^ Int.toString suffix)
end (*}}}1*)
end (*local*)
end