unfy.3 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. .TH Unfy 3 "October 2017" "Version 1.0" "libunfy API"
  2. .SH Name
  3. unfy \- unification algorithm library using rids.
  4. .SH SYNOPSIS
  5. .B #include <rid.h>
  6. .br
  7. .B #include <stdint.h>
  8. .sp
  9. .B #include <nit/list.h>
  10. .br
  11. .B #include <nit/set.h>
  12. .br
  13. .B #include <nit/map.h>
  14. .sp
  15. .B #include <unfy.h>
  16. .sp
  17. Link with \fI\-lrid\fP, \fI\-lnit\fP and \fI\-lbsd\fP.
  18. .SH DESCRIPTION
  19. A library to create terms and unify them if possible.
  20. .SS Types
  21. .B Unfy_type
  22. denotes a type for a
  23. .B Unfy_term
  24. it is defined as:
  25. .sp
  26. .nf
  27. typedef enum {
  28. UNFY_CONST = 0,
  29. UNFY_LIST,
  30. UNFY_VAR,
  31. } Unfy_type;
  32. .fi
  33. .sp
  34. .B Unfy_stat
  35. denotes the result of unification in terms of success of not, it is defined as:
  36. .sp
  37. .nf
  38. typedef enum {
  39. UNFY_YES,
  40. UNFY_NO,
  41. UNFY_ERR,
  42. } Unfy_stat;
  43. .fi
  44. .sp
  45. .B Unfy_term
  46. is a term that can be unified. It is defined as:
  47. .sp
  48. .nf
  49. typedef struct {
  50. Unfy_type type;
  51. union {
  52. Rid id;
  53. Unfy_list *list;
  54. void *dat;
  55. } u;
  56. } Unfy_term;
  57. .fi
  58. .sp
  59. .B Unfy_list
  60. is a list of terms. It is defined as:
  61. .sp
  62. .nf
  63. struct Unfy_list {
  64. Nit_list next;
  65. Unfy_term *term;
  66. };
  67. .fi
  68. .sp
  69. .B Unfy_recycle
  70. stores information for recycling terms and lists. You should not depend on how it is implemented.
  71. .SS Functions
  72. .BI "void unfy_recycle_init(Unfy_recycle *" rec ");"
  73. .br
  74. .BI "void unfy_recycle_empty(Unfy_recycle *" rec ");"
  75. .br
  76. .BI "void unfy_recycle_term(Unfy_recycle *" rec ", Unfy_term *" term ");"
  77. .br
  78. .BI "void unfy_recycle_list(Unfy_recycle *" rec ", Unfy_list *" list ");"
  79. .sp
  80. .BI "Unfy_term * unfy_term(Unfy_type " type ", void *" dat ", Unfy_recycle *" rec ");"
  81. .br
  82. .BI "Unfy_term * unfy_term_copy(const Unfy_term *" term ", Unfy_recycle *" rec ");"
  83. .br
  84. .BI "void unfy_term_init(Unfy_term *" term ", Unfy_type " type ", void *" dat ");"
  85. .br
  86. .BI "int unfy_term_init_copy(Unfy_term *" des ", const Unfy_term *" src ", Unfy_recycle *" rec ");"
  87. .br
  88. .BI "Unfy_stat unfy_term_same(const Unfy_term *" des ", const Unfy_term *" src ", Nit_map *" binds ", Nit_entry **" entries ", Unfy_recycle *" rec ");"
  89. .sp
  90. .BI "Unfy_list * unfy_list(Unfy_term *" term ", Unfy_list *" next ", Unfy_recycle *" rec ");"
  91. .br
  92. .BI "Unfy_list * unfy_list_copy(const Unfy_list *" list ", Unfy_recycle *" rec ");"
  93. .br
  94. .BI "void unfy_list_init(Unfy_list *" list ", Unfy_term *" term ", Unfy_list *" next ");"
  95. .br
  96. .BI "int unfy_list_init_copy(Unfy_list *" copy ", const Unfy_list *" list ", Unfy_recycle *" rec ");"
  97. .br
  98. .BI "Unfy_stat unfy_list_same(const Unfy_list *" des ", const Unfy_list *" src ", Nit_map *" binds ", Nit_entry **" entries ", Unfy_recycle *" rec ");"
  99. .br
  100. .BI "Unfy_list ** unfy_list_append(Unfy_list **" tail ", Unfy_term *" term ", Unfy_recycle *" rec ");"
  101. .sp
  102. .BI "Unfy_term * unfy_term_bind(const Unfy_term *" term ", const Nit_map *" binds ", Unfy_recycle *" rc ");"
  103. .br
  104. .BI "int unfy_term_bind_set(Unfy_term *" term ", const Nit_map *" binds ", Unfy_recycle *" rec ");"
  105. .br
  106. .BI "void unfy_binds_free(void *" key ", void *" val ", void *" rec ");"
  107. .sp
  108. .BI "Unfy_stat unfy_unify(const Unfy_term *" left ", const Unfy_term *" right ", Nit_map *" lbinds ", Nit_map *" rbinds ", Nit_entry **" entries ", Unfy_recycle *" rec ");"
  109. .SS Function Explanations
  110. .B unfy_recycle_init ()
  111. initializes
  112. .I rec
  113. to be used in other functions.
  114. .sp
  115. .B unfy_recycle_empty ()
  116. frees all memory (recycled terms and lists) that are held by
  117. .IR "rec" .
  118. .sp
  119. .B unfy_recycle_term ()
  120. recycles memory used by
  121. .I term
  122. and stores it in
  123. .IR "rec" .
  124. .sp
  125. .B unfy_recycle_list ()
  126. recycles memory used by
  127. .I list
  128. and stores it in
  129. .IR "rec" .
  130. .sp
  131. .B unfy_term ()
  132. return a term where
  133. .I type
  134. denotes the type of the term and
  135. .I dat
  136. sets what value the term should have, in the case of
  137. .I type
  138. being
  139. .B UNFY_CONST
  140. or
  141. .B UNFY_VAR
  142. this should be an Rid, but if the type is
  143. .B UNFY_LIST
  144. this should be a Unfy_list.
  145. .sp
  146. .B unfy_term_copy ()
  147. returns a copy of
  148. .IR term "."
  149. .sp
  150. .B unfy_term_init ()
  151. and
  152. .B unfy_term_init_copy ()
  153. work similar to the functions with the same name minus "init", except instead of returning a term they initialize the first argument passed to them.
  154. .sp
  155. .B unfy_term_same ()
  156. determines if the two input terms have the same structure (if everything is between them is the same except specific variable names).
  157. .I binds
  158. is set so that variables in
  159. .I des
  160. can be changed to have the same names as variables in
  161. .I src
  162. allowing for
  163. .I des
  164. to effectively become the same as
  165. .IR src ","
  166. if it is possible. The return value is UNFY_YES if the two terms are the same in structure, UNFY_NO if they are not, and UNFY_ERR if an error occurred.
  167. .I entries
  168. refers to recycled values of type Nit_entry, a reference to a NULL pointer of that type can be used here and freed later using the nit function
  169. .B nit_entry_stack_free ()
  170. .sp
  171. .B unfy_list ()
  172. returns a list initialized using the arguments passed to the function.
  173. .sp
  174. .B unfy_list_copy ()
  175. returns a copy of
  176. .IR list "."
  177. .sp
  178. .B unfy_list_init ()
  179. and
  180. .B unfy_list_init_copy ()
  181. work similar to the functions with the same name minus "init", except instead of returning a list they initialize the first argument passed to them.
  182. .sp
  183. .B unfy_list_same ()
  184. is similar to
  185. .B unfy_term_same ()
  186. except it is on lists instead of terms.
  187. .sp
  188. .B unfy_list_append ()
  189. takes
  190. .IR tail ","
  191. a reference to the next value of the last element of a Unfy_list (which should be NULL), and a term
  192. .RI "(" term ")."
  193. It then uses
  194. .I tail
  195. to append to the end of the list a Unfy_list using
  196. .I term
  197. as the term. It then returns a reference to the next value of the new last element of the list (which can be used to append another element to the end of the list which makes this function useful in loops).
  198. .sp
  199. .B unfy_term_bind ()
  200. returns a copy of
  201. .I term
  202. replaces its variables using their corresponding bindings from
  203. .IR binds "."
  204. .sp
  205. .B unfy_term_bind_set ()
  206. works like
  207. .B unfy_term_bind ()
  208. except instead of making a copy, it changes
  209. .I term
  210. directly.
  211. .sp
  212. .B unfy_binds_free ()
  213. should not be used directly, but instead passed to a function for freeing binds and used for the argument of type Nit_map_free. The argument
  214. .I extra
  215. should be a of type Unfy_recycle.
  216. .sp
  217. .B unfy_unify ()
  218. tries to unify the terms
  219. .I left
  220. and
  221. .I right
  222. where bound variables for
  223. .I left
  224. are stored in
  225. .I lbinds
  226. and bound variables for
  227. .I right
  228. are stored in
  229. .IR rbinds "."
  230. The return value is UNFY_YES if the two terms can be unified, UNFY_NO if they cannot and UNFY_ERR if an error occurred.
  231. .SH "Return Values"
  232. All functions returning a value can fail, if they do it is due to running out of memory. On failure if the return value is a pointer (of any type) it will be NULL, if the value is an int it will be less than 0, and if they type is Unfy_stat it will be UNFY_ERR.
  233. .SH EXAMPLES
  234. See the
  235. .I examples
  236. folder in the repository of unfy for an example repl that takes two terms and tries to unify them.
  237. .SH AUTHOR
  238. Uladox (coming up with terms)
  239. .SH "AVAILABILITY AND SOURCE"
  240. .B unfy
  241. can be found in its repository at https://www.notabug.org/Uladox/unfy
  242. .SH "SEE ALSO"
  243. .BR rid (3)