-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringy_and_charylist_type.pl
229 lines (185 loc) · 8.58 KB
/
stringy_and_charylist_type.pl
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
:- module(onepointfour_basics_stringy_and_charylist_type,
[
charylist_type/2
,stringy_type/2
,stringy_type/3
,stringy_type_with_length/2
,stringy_type_with_length/3
]).
:- use_module(library('onepointfour_basics/checks.pl')).
/* MIT License Follows (https://opensource.org/licenses/MIT)
Copyright 2021 David Tonhofer <ronerycoder@gluino.name>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* pldoc ==================================================================== */
/** <module> Analyze "chary" or "stringy" terms
This code is specific to SWI-Prolog, as that Prolog provides the traditional
"atom" and the non-traditional "string" as two distinct representations of
"sequences of characters".
We introduce the following additional vocabulary:
- A stringy term is a term that is either an atom or a string.
In SWI-Prolog, the string is a distinct representation of a sequence
of characters, distinct from the atom and mean to be used in text
processing rather than as basis for identifiers.
- A chary term is a term that is either a char (an atom of length 1) or a
code (an integer and, more precisely in SWI-Prolog, a Unicode code point).
- A charylist is less precise: it is a proper list of either codes or chars.
It may or may not contain uninstantiated elements. An empty list is a
charylist but we cannot know whether it is supposed to be composed of
codes or chars. A list containing only uninstantiated variables is also
a charylist and again we don't know what it is supposed to contain, at
least not yet.
## Homepage for this code
https://github.com/dtonhofer/prolog_code/blob/main/unpacked/onepointfour_basics/README_stringy_and_charylist_type.md
## History
1. 2020-07-XX: First code elements created.
1. 2021-06-08: Re-created from existing code lying around.
1. 2021-06-11: Back up on github.
*/
%! charylist_type(@CharyList,?Type)
%
% Determine the type of a charylist. It will be one of the following atoms
% or compound terms **or else the predicate will fail** (it doesn't throw,
% i.e. behaves "smoothly"). Note that CharyList must be proper list, partial
% lists are rejected.
%
% - =|var|= : charylist is uninstantiated
% - =|empty|= : charylist is the empty list (no info whether chars or codes)
% - =|chars(N)|= : charylist is nonempty and consists of N chars
% - =|codes(N)|= : charylist is nonempty and consists of N codes
% - =|chars_vars(N,V)|= : charylist is nonempty and consists of N chars and V vars
% - =|codes_vars(N,V)|= : charylist is nonempty and consists of N codes and V vars
% - =|vars(V)|= : charylist is nonempty and consists of V vars
charylist_type(CharyList,var) :-
var(CharyList),
!.
charylist_type(CharyList,Type) :-
list_traversal(CharyList,VarCount,CodeCount,CharCount,TotalCount), % fails on malformed/non-list
assertion(VarCount + CodeCount + CharCount =:= TotalCount),
type_decide(VarCount,CodeCount,CharCount,Type).
type_decide(VarCount,CodeCount,CharCount,chars_vars(CharCount,VarCount)) :-
VarCount > 0,
CharCount > 0,
assertion(CodeCount == 0),
!.
type_decide(VarCount,CodeCount,CharCount,codes_vars(CodeCount,VarCount)) :-
VarCount > 0,
CodeCount > 0,
assertion(CharCount == 0),
!.
type_decide(VarCount,CodeCount,CharCount,vars(VarCount)) :-
VarCount > 0,
CodeCount == 0,
CharCount == 0,
!.
type_decide(VarCount,CodeCount,CharCount,vars(VarCount)) :-
VarCount > 0,
CodeCount == 0,
CharCount == 0,
!.
type_decide(VarCount,CodeCount,CharCount,chars(CharCount)) :-
VarCount == 0,
CharCount > 0,
assertion(CodeCount == 0),
!.
type_decide(VarCount,CodeCount,CharCount,codes(CodeCount)) :-
VarCount == 0,
CodeCount > 0,
assertion(CharCount == 0),
!.
type_decide(VarCount,CodeCount,CharCount,empty) :-
assertion(VarCount + CodeCount + CharCount =:= 0).
list_traversal(List,VarCount,CodeCount,CharCount,TotalCount) :-
list_traversal_2(List,0,0,0,0,VarCount,CodeCount,CharCount,TotalCount).
% we need to beware of being handed an open list, so we
% can't unify solely in the head
list_traversal_2(List,VarCount,CodeCount,CharCount,TotalCount,VarCountOut,CodeCountOut,CharCountOut,TotalCountOut) :-
nonvar(List),
List=[X|More],
var(X),
!,
VarCountNext is VarCount+1,
TotalCountNext is TotalCount+1,
list_traversal_2(More,VarCountNext,CodeCount,CharCount,TotalCountNext,
VarCountOut,CodeCountOut,CharCountOut,TotalCountOut).
list_traversal_2(List,VarCount,CodeCount,CharCount,TotalCount,VarCountOut,CodeCountOut,CharCountOut,TotalCountOut) :-
nonvar(List),
List=[X|More],
check_that(X,[smooth(char)]), % is a char, fail if not
!,
CodeCount=:=0,
CharCountNext is CharCount+1,
TotalCountNext is TotalCount+1,
list_traversal_2(More,VarCount,CodeCount,CharCountNext,TotalCountNext,
VarCountOut,CodeCountOut,CharCountOut,TotalCountOut).
list_traversal_2(List,VarCount,CodeCount,CharCount,TotalCount,VarCountOut,CodeCountOut,CharCountOut,TotalCountOut) :-
nonvar(List),
List=[X|More],
check_that(X,[smooth(code)]), % is a code, fail if not
!,
CharCount=:=0,
CodeCountNext is CodeCount+1,
TotalCountNext is TotalCount+1,
list_traversal_2(More,VarCount,CodeCountNext,CharCount,TotalCountNext,
VarCountOut,CodeCountOut,CharCountOut,TotalCountOut).
list_traversal_2(List,VarCount,CodeCount,CharCount,TotalCount,VarCount,CodeCount,CharCount,TotalCount) :-
nonvar(List),
List=[].
%! stringy_type(@Stringy,?Type)
%
% Determine the type of Stringy. Type can be =|string|= or =|atom|= or =|var|=, the
% latter indicating that Stringy is uninstantiated. This predicates behaves _softly_,
% i.e. preferentially fails on bad input.
stringy_type(Stringy,Type) :-
stringy_type(Stringy,Type,soft).
% !stringy_type(@Stringy,?Type,@Tuned)
%
% As stringy_type/2, but setting Tuned to =|hard|= will make the
% predicate throw on bad input (contrary is =|soft|=)
stringy_type(Stringy,Type,Tuned) :-
check_that(Stringy,[break(var),tuned(stringy)],Tuned),
check_that(Type,[break(var),tuned(member(var,atom,string))],Tuned),
stringy_type_2(Stringy,Type).
stringy_type_2(Stringy,var) :- var(Stringy),!.
stringy_type_2(Stringy,atom) :- atom(Stringy),!.
stringy_type_2(Stringy,string) :- string(Stringy),!.
%! stringy_type_with_length(@Stringy,Type)
%
% Determine an atom or compound-term representation for the actual type of Stringy.
% It will be one of the atom =|var|= or one of the compound terms =|atom(L)|= or
% =|string(L)|=, where L is the length of Stringy. This predicates behaves _softly_,
% i.e. preferentially fails on bad input.
stringy_type_with_length(Stringy,Type) :-
stringy_type_with_length(Stringy,Type,soft).
%! stringy_type_with_length(@Stringy,Type,Tuned)
%
% As stringy_type_with_length/2, but setting Tuned to either =|true|= or =|throw|=
% will make the predicate throw on bad input.
stringy_type_with_length(Stringy,Type,Tuned) :-
check_that(Stringy,[break(var),tuned(stringy)],Tuned),
check_that(Type,[break(var),
tuned(
forany(
[unifies(var),
unifies(atom(_)),
unifies(string(_))]
))],Tuned),
stringy_type_with_length_2(Stringy,Type).
stringy_type_with_length_2(Stringy,var) :- var(Stringy),!.
stringy_type_with_length_2(Stringy,atom(L)) :- atom(Stringy),!,atom_length(Stringy,L).
stringy_type_with_length_2(Stringy,string(L)) :- string(Stringy),!,string_length(Stringy,L).