1:- module(onepointfour_basics_stringy_and_charylist_type,
2 [
3 charylist_type/2
4 ,stringy_type/2
5 ,stringy_type/3
6 ,stringy_type_with_length/2
7 ,stringy_type_with_length/3
8 ]). 9
10:- use_module(library('onepointfour_basics/checks.pl')). 11
35
86charylist_type(CharyList,var) :-
87 var(CharyList),
88 !.
89
90charylist_type(CharyList,Type) :-
91 list_traversal(CharyList,VarCount,CodeCount,CharCount,TotalCount), 92 assertion(VarCount + CodeCount + CharCount =:= TotalCount),
93 type_decide(VarCount,CodeCount,CharCount,Type).
94
95type_decide(VarCount,CodeCount,CharCount,chars_vars(CharCount,VarCount)) :-
96 VarCount > 0,
97 CharCount > 0,
98 assertion(CodeCount == 0),
99 !.
100
101type_decide(VarCount,CodeCount,CharCount,codes_vars(CodeCount,VarCount)) :-
102 VarCount > 0,
103 CodeCount > 0,
104 assertion(CharCount == 0),
105 !.
106
107type_decide(VarCount,CodeCount,CharCount,vars(VarCount)) :-
108 VarCount > 0,
109 CodeCount == 0,
110 CharCount == 0,
111 !.
112
113type_decide(VarCount,CodeCount,CharCount,vars(VarCount)) :-
114 VarCount > 0,
115 CodeCount == 0,
116 CharCount == 0,
117 !.
118
119type_decide(VarCount,CodeCount,CharCount,chars(CharCount)) :-
120 VarCount == 0,
121 CharCount > 0,
122 assertion(CodeCount == 0),
123 !.
124
125type_decide(VarCount,CodeCount,CharCount,codes(CodeCount)) :-
126 VarCount == 0,
127 CodeCount > 0,
128 assertion(CharCount == 0),
129 !.
130
131type_decide(VarCount,CodeCount,CharCount,empty) :-
132 assertion(VarCount + CodeCount + CharCount =:= 0).
133
134list_traversal(List,VarCount,CodeCount,CharCount,TotalCount) :-
135 list_traversal_2(List,0,0,0,0,VarCount,CodeCount,CharCount,TotalCount).
136
139
140list_traversal_2(List,VarCount,CodeCount,CharCount,TotalCount,VarCountOut,CodeCountOut,CharCountOut,TotalCountOut) :-
141 nonvar(List),
142 List=[X|More],
143 var(X),
144 !,
145 VarCountNext is VarCount+1,
146 TotalCountNext is TotalCount+1,
147 list_traversal_2(More,VarCountNext,CodeCount,CharCount,TotalCountNext,
148 VarCountOut,CodeCountOut,CharCountOut,TotalCountOut).
149
150list_traversal_2(List,VarCount,CodeCount,CharCount,TotalCount,VarCountOut,CodeCountOut,CharCountOut,TotalCountOut) :-
151 nonvar(List),
152 List=[X|More],
153 check_that(X,[smooth(char)]), 154 !,
155 CodeCount=:=0,
156 CharCountNext is CharCount+1,
157 TotalCountNext is TotalCount+1,
158 list_traversal_2(More,VarCount,CodeCount,CharCountNext,TotalCountNext,
159 VarCountOut,CodeCountOut,CharCountOut,TotalCountOut).
160
161list_traversal_2(List,VarCount,CodeCount,CharCount,TotalCount,VarCountOut,CodeCountOut,CharCountOut,TotalCountOut) :-
162 nonvar(List),
163 List=[X|More],
164 check_that(X,[smooth(code)]), 165 !,
166 CharCount=:=0,
167 CodeCountNext is CodeCount+1,
168 TotalCountNext is TotalCount+1,
169 list_traversal_2(More,VarCount,CodeCountNext,CharCount,TotalCountNext,
170 VarCountOut,CodeCountOut,CharCountOut,TotalCountOut).
171
172list_traversal_2(List,VarCount,CodeCount,CharCount,TotalCount,VarCount,CodeCount,CharCount,TotalCount) :-
173 nonvar(List),
174 List=[].
182stringy_type(Stringy,Type) :-
183 stringy_type(Stringy,Type,soft).
184
189
190stringy_type(Stringy,Type,Tuned) :-
191 check_that(Stringy,[break(var),tuned(stringy)],Tuned),
192 check_that(Type,[break(var),tuned(member(var,atom,string))],Tuned),
193 stringy_type_2(Stringy,Type).
194
195stringy_type_2(Stringy,var) :- var(Stringy),!.
196stringy_type_2(Stringy,atom) :- atom(Stringy),!.
197stringy_type_2(Stringy,string) :- string(Stringy),!.
206stringy_type_with_length(Stringy,Type) :-
207 stringy_type_with_length(Stringy,Type,soft).
214stringy_type_with_length(Stringy,Type,Tuned) :-
215 check_that(Stringy,[break(var),tuned(stringy)],Tuned),
216 check_that(Type,[break(var),
217 tuned(
218 forany(
219 [unifies(var),
220 unifies(atom(_)),
221 unifies(string(_))]
222 ))],Tuned),
223 stringy_type_with_length_2(Stringy,Type).
224
225stringy_type_with_length_2(Stringy,var) :- var(Stringy),!.
226stringy_type_with_length_2(Stringy,atom(L)) :- atom(Stringy),!,atom_length(Stringy,L).
227stringy_type_with_length_2(Stringy,string(L)) :- string(Stringy),!,string_length(Stringy,L)
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:
Homepage for this code
https://github.com/dtonhofer/prolog_code/blob/main/unpacked/onepointfour_basics/README_stringy_and_charylist_type.md
History
*/