| 1 |
Require Import PrimInt63 PrimString. |
| 2 |
|
| 3 |
Inductive smallstring := SmallString (_:string). |
| 4 |
|
| 5 |
Declare Scope smallstring_scope. |
| 6 |
Open Scope smallstring_scope. |
| 7 |
|
| 8 |
Module V1. |
| 9 |
|
| 10 |
Definition of_smallstring '(SmallString v) := v. |
| 11 |
|
| 12 |
|
| 13 |
Definition to_smallstring v := |
| 14 |
if PrimInt63.leb (length v) 10 then Ok (SmallString v) |
| 15 |
else Error (cat "not a small string: " v). |
| 16 |
|
| 17 |
String Notation smallstring to_smallstring of_smallstring : smallstring_scope. |
| 18 |
|
| 19 |
Check "hello". |
| 20 |
|
| 21 |
Fail Check "too long string". |
| 22 |
|
| 23 |
(* printing function doesn't check so we print something that doesn't parse *) |
| 24 |
Check SmallString "too long string". |
| 25 |
|
| 26 |
End V1. |
| 27 |
|
| 28 |
Module V2. |
| 29 |
Inductive smallstring_error := NotSmallString (_:string). |
| 30 |
|
| 31 |
Definition to_smallstring v := |
| 32 |
if PrimInt63.leb (length v) 10 then Ok (SmallString v) |
| 33 |
else Error (NotSmallString v). |
| 34 |
|
| 35 |
Definition of_smallstring '(SmallString v) := |
| 36 |
match to_smallstring v with |
| 37 |
| Ok _ => Ok v |
| 38 |
| Error e => Error e |
| 39 |
end. |
| 40 |
|
| 41 |
String Notation smallstring to_smallstring of_smallstring : smallstring_scope. |
| 42 |
|
| 43 |
Check "hello". |
| 44 |
|
| 45 |
Fail Check "too long string". |
| 46 |
|
| 47 |
(* printing function produces an error message but its content is ignored *) |
| 48 |
Check SmallString "too long string". |
| 49 |
End V2. |