| author | wenzelm | 
| Wed, 20 Apr 2011 22:57:29 +0200 | |
| changeset 42439 | 9efdd0af15ac | 
| parent 41589 | bbd861837ebc | 
| permissions | -rw-r--r-- | 
| 19093 | 1 | (* Title: HOL/Import/mono_seq.ML | 
| 2 | Author: Steven Obua, TU Muenchen | |
| 3 | ||
| 41589 | 4 | Monomorphic sequences. | 
| 19093 | 5 | *) | 
| 6 | ||
| 7 | (* The trouble is that signature / structures cannot depend on type variable parameters ... *) | |
| 8 | ||
| 9 | signature MONO_SCANNER_SEQ = | |
| 10 | sig | |
| 11 | type seq | |
| 12 | type item | |
| 13 | ||
| 14 | val pull : seq -> (item * seq) option | |
| 15 | end | |
| 16 | ||
| 17 | signature MONO_EXTENDED_SCANNER_SEQ = | |
| 18 | sig | |
| 19 | ||
| 20 | include MONO_SCANNER_SEQ | |
| 21 | ||
| 22 | val null : seq -> bool | |
| 23 | val length : seq -> int | |
| 24 | val take_at_most : seq -> int -> item list | |
| 25 | ||
| 26 | end | |
| 27 | ||
| 28 | functor MonoExtendScannerSeq (structure Seq : MONO_SCANNER_SEQ) : MONO_EXTENDED_SCANNER_SEQ = | |
| 29 | struct | |
| 30 | ||
| 31 | type seq = Seq.seq | |
| 32 | type item = Seq.item | |
| 33 | val pull = Seq.pull | |
| 34 | ||
| 35 | fun null s = is_none (pull s) | |
| 36 | ||
| 37 | fun take_at_most s n = | |
| 38 | if n <= 0 then [] else | |
| 39 | case pull s of | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
19093diff
changeset | 40 | NONE => [] | 
| 19093 | 41 | | SOME (x,s') => x::(take_at_most s' (n-1)) | 
| 42 | ||
| 43 | fun length' s n = | |
| 44 | case pull s of | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
19093diff
changeset | 45 | NONE => n | 
| 19093 | 46 | | SOME (_, s') => length' s' (n+1) | 
| 47 | ||
| 48 | fun length s = length' s 0 | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
19093diff
changeset | 49 | |
| 19093 | 50 | end | 
| 51 | ||
| 52 | ||
| 53 | structure StringScannerSeq : | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
19093diff
changeset | 54 | sig | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
19093diff
changeset | 55 | include MONO_EXTENDED_SCANNER_SEQ | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
19093diff
changeset | 56 | val fromString : string -> seq | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
19093diff
changeset | 57 | end | 
| 19093 | 58 | = | 
| 59 | struct | |
| 60 | ||
| 61 | structure Incubator : MONO_SCANNER_SEQ = | |
| 62 | struct | |
| 63 | ||
| 64 | type seq = string * int * int | |
| 65 | type item = string | |
| 66 | ||
| 67 | fun pull (s, len, i) = if i >= len then NONE else SOME (String.substring (s, i, 1), (s, len, i+1)) | |
| 68 | end | |
| 69 | ||
| 70 | structure Extended = MonoExtendScannerSeq (structure Seq = Incubator) | |
| 71 | open Extended | |
| 72 | ||
| 73 | fun fromString s = (s, String.size s, 0) | |
| 74 | ||
| 75 | end | |
| 76 |