File: Bool.sml

package info (click to toggle)
polyml 5.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 40,616 kB
  • sloc: cpp: 44,142; ansic: 26,963; sh: 22,002; asm: 13,486; makefile: 602; exp: 525; python: 253; awk: 91
file content (73 lines) | stat: -rw-r--r-- 2,757 bytes parent folder | download | duplicates (4)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(*
    Title:      Standard Basis Library: Bool Structure
    Author:     David Matthews
    Copyright   David Matthews 1999, 2005, 2016

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License version 2.1 as published by the Free Software Foundation.
    
    This library 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 GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*)

(* G&R status: Minor change to signature.  Done. *)
signature BOOL =
  sig
    datatype bool = datatype bool
    val not : bool -> bool
    val fromString : string -> bool option
    val scan : (char, 'a) StringCvt.reader -> (bool, 'a) StringCvt.reader
    val toString : bool -> string
  end;

structure Bool : BOOL =
struct
    open Bool (* Defined in Initialise.  Contains bool datatype and "not". *)
        
    local
        val explode_true = Text.String.explode "true"
        and explode_false = Text.String.explode "false"
    in
    fun scan (getc: (char, 'a) StringCvt.reader) (str: 'a) : (bool * 'a) option =
        let
        (* Skip leading white space. *)
        val strm = StringCvt.skipWS getc str
        (* Test for a match between a reader and a list of lower case chars. *)
        fun matchNC _    strm [] = (strm, true )(* Reached end of list - succeeded *)
          | matchNC getc strm (ch::rest) =
                case getc strm of
                    NONE => (strm, false) (* Couldn't read it - fail. *)
                  | SOME(ch', strm') =>
                      if ch = Char.toLower ch' (* N.B. ch is already lower case. *)
                      then matchNC getc strm' rest
                      else (strm', false)
        in
            (* If it matches "true" or "false" we have a valid match,
               otherwise return NONE. *)
            case matchNC getc strm explode_true of
                (strm', true) => SOME(true, strm')
              | _ =>
                (
                case matchNC getc strm explode_false of
                    (strm', true) => SOME(false, strm')
                  | _ => NONE
                )
        end
    end
    
    (* Convert from a string. *)
    (* TODO: More efficient conversion? *)
    val fromString = StringCvt.scanString scan
    
    (* Convert to a string. *)
    fun toString true = "true"
      | toString false = "false"

end;