Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. # Comparision of JaneStreet Base and OCaml Stdlib
  2.  
  3.  
  4.  
  5. ## How to use Base
  6.  
  7. Either use Base selectively:
  8.  
  9. ```ocaml
  10. # #show List.hd;;
  11. val hd : 'a list -> 'a
  12.  
  13. # #show Stdlib.List.hd;;
  14. val hd : 'a list -> 'a
  15.  
  16. # #show Base.List.hd;;
  17. val hd : 'a list -> 'a option
  18. ```
  19.  
  20. Opening `Base` shadows existing module, which are still accessible with the fully-qualified path.
  21.  
  22. ```ocaml
  23. # open Base;;
  24.  
  25. # #show List.hd;;
  26. val hd : 'a list -> 'a option
  27.  
  28. # #show Stdlib.List.hd;;
  29. val hd : 'a list -> 'a
  30. ```
  31.  
  32. ```ocaml
  33. # int_of_char;;
  34. Warning 3: deprecated: Base.int_of_char
  35. [2016-09] this element comes from the stdlib distributed with OCaml.
  36. Use [Char.to_int] instead.
  37. - : char -> int = <fun>
  38.  
  39. # Stdlib.int_of_char;;
  40. - : char -> int = <fun>
  41. ```
  42.  
  43. `Base` deprecates:
  44. * Polymorphic comparison functions (next slide)
  45. * I/O functions (more on this later)
  46. * Missplaced functions: `Char.to_int` vs `int_of_char`
  47.  
  48. ## 1. Comparison: monomorphic vs. polymorphic
  49.  
  50. ```ocaml
  51. (* Stdlib *)
  52. val (=): 'a -> 'a -> bool (* Polymorhpic! *)
  53. val (>): 'a -> 'a -> bool (* Polymorphic! *)
  54. val (+): int -> int -> int
  55.  
  56. open Base
  57. val (=): int -> int -> bool
  58. val (>): int -> int -> bool
  59. val (+): int -> int -> int
  60.  
  61. (* Also Base *)
  62. Float.O.(=): float -> float -> bool
  63. Float.O.(>): float -> float -> bool
  64. Float.O.(+): float -> float -> float
  65. ```
  66.  
  67. ## Labelled parameters
  68.  
  69. ```ocaml
  70. # #show Stdlib.String.concat;;
  71. val concat : string -> string list -> string
  72.  
  73. # #show Stdlib.StdLabels.String.concat;;
  74. val concat : ?sep:string -> string list -> string
  75.  
  76. # #show Base.String.concat;;
  77. val concat : ?sep:string -> string list -> string
  78. ```
  79.  
  80. ```ocaml
  81. # Stdlib.List.map;;
  82. - : ('a -> 'b) -> 'a list -> 'b list = <fun>
  83.  
  84. # Stdlib.StdLabels.List.map;;
  85. - : 'a list -> f:('a -> 'b) -> 'b list = <fun>
  86.  
  87. # Base.List.map;;
  88. - : 'a list -> f:('a -> 'b) -> 'b list = <fun>
  89. ```
  90.  
  91. ## Total functions are preffered
  92.  
  93. ```ocaml
  94. # #show Stdlib.List.hd;;
  95. val hd : 'a list -> 'a
  96.  
  97. # #show Base.List.hd;;
  98. val hd : 'a list -> 'a option
  99.  
  100. # #show Base.List.hd_exn;;
  101. val hd : 'a list -> 'a option
  102. ```
  103.  
  104. ## Stack-safe functions
  105.  
  106. ```ocaml
  107. # let big_list = Base.List.init 100_000_000 (fun _ -> 1);;
  108. val big_list : int Base.List.t =
  109. [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
  110. 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
  111. 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
  112. 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
  113. 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; ...]
  114.  
  115. # Base.List.map ~f:succ big_list;;
  116. - : int Base.List.t =
  117. [2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2;
  118. 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2;
  119. 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2;
  120. 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2;
  121. 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; 2; ...]
  122.  
  123. # Stdlib.List.map succ big_list;;
  124. Stack overflow during evaluation (looping recursion?).
  125. Raised by primitive operation at file "//toplevel//", line 5, characters 32-39
  126. Called from file "//toplevel//", line 5, characters 32-39
  127. Called from file "//toplevel//", line 5, characters 32-39
  128. Called from file "//toplevel//", line 5, characters 32-39
  129. Called from file "//toplevel//", line 5, characters 32-39
  130. Called from file "//toplevel//", line 5, characters 32-39
  131. Called from file "//toplevel//", line 5, characters 32-39
  132. Called from file "//toplevel//", line 5, characters 32-39
  133. Called from file "//toplevel//", line 5, characters 32-39
  134. Called from file "//toplevel//", line 5, characters 32-39
  135. Called from file "//toplevel//", line 5, characters 32-39
  136. Called from file "//toplevel//", line 5, characters 32-39
  137. Called from file "//toplevel//", line 5, characters 32-39
  138. Called from file "//toplevel//", line 5, characters 32-39
  139. Called from file "//toplevel//", line 5, characters 32-39
  140. Called from file "//toplevel//", line 5, characters 32-39
  141. Called from file "//toplevel//", line 5, characters 32-39
  142. Called from file "//toplevel//", line 5, characters 32-39
  143. Called from file "//toplevel//", line 5, characters 32-39
  144. Called from file "//toplevel//", line 5, characters 32-39
  145. Called from file "//toplevel//", line 5, characters 32-39
  146. Called from file "//toplevel//", line 5, characters 32-39
  147. Called from file "//toplevel//", line 5, characters 32-39
  148. ...
  149. ```
  150.  
  151. ## Modules common to Base and Stdlib
  152. ### And their type equality
  153.  
  154. ```
  155. ✅Array
  156. ✅Bool
  157. ✅Buffer
  158. ✅Bytes
  159. ✅Char
  160. ✅Float
  161. ➖Fn/Fun
  162. ❌Hashtbl
  163. ✅Int
  164. ✅Int32
  165. ✅Int64
  166. ✅Lazy
  167. ✅List
  168. ❌Map
  169. ✅Nativeint
  170. ✅Option
  171. ✅Printf
  172. ❌Sequence/Seq
  173. ✅Linked_queue/Queue
  174. ➖Random
  175. ✅Result
  176. ❌Set
  177. ❌Stack
  178. ✅String
  179. ➖Sys
  180. ✅Uchar
  181. ✅Unit
  182. ```
  183.  
  184. ## Base-only modules
  185.  
  186. ```
  187. Applicative
  188. Avltree
  189. Backtrace
  190. Binary_search
  191. Binary_searchable
  192. Blit
  193. Comparable
  194. Comparator
  195. Comparisons
  196. Container
  197. Continue_or_stop
  198. Either
  199. Error
  200. Equal
  201. Exn
  202. Field
  203. Floatable
  204. Formatter
  205. Hash
  206. Hash_set
  207. Hashable
  208. Hasher
  209. Identifiable
  210. Indexed_container
  211. Info
  212. Int63
  213. Intable
  214. Invariant
  215. Maybe_bound
  216. Monad
  217. Option_array
  218. Or_error
  219. Ordered_collection_common
  220. Ordering
  221. Poly
  222. Polymorphic_compare
  223. Popcount
  224. Pretty_printer
  225. Queue (array-based queue, unlike the Linked_queue)
  226. Ref
  227. Sexp
  228. Sexpable
  229. Sign
  230. Sign_or_nan
  231. Source_code_position
  232. Staged
  233. Stringable
  234. T
  235. Type_equal
  236. Uniform_array
  237. Validate
  238. Variant
  239. With_return
  240. Word_size
  241. ```
  242.  
  243. ## Stdlib-only modules
  244.  
  245. ```
  246. Arg
  247. Bigarray
  248. Callback
  249. Complex
  250. Condition
  251. Digest
  252. Dynlink
  253. Ephemeron
  254. Event
  255. Filename
  256. Format
  257. Gc
  258. Genlex
  259. Lexing
  260. Marshal
  261. Mutex
  262. Obj
  263. Oo
  264. Parsing
  265. Pervasives
  266. Printexc
  267. Scanf
  268. Spacetime
  269. Str
  270. Stream
  271. Thread
  272. ThreadUnix
  273. Unix
  274. Weak
  275. ```
  276.  
  277. * * *
  278.  
  279. > Note, some private modules like `Stdlib.CamlinternalFormat` and `Base.Not_exposed_properly` were omitted.
  280.  
  281.  
  282.  
  283. ## Functors vs. first-class modules
  284.  
  285. ```ocaml
  286. # module StringMap = Stdlib.Map.Make (Stdlib.String);;
  287. module StringMap : ...
  288.  
  289. # let m = StringMap.empty;;
  290. val m : 'a StringMap.t = <abstr>
  291.  
  292. # StringMap.add "x" 2 m;;
  293. - : int StringMap.t = <abstr>
  294. ```
  295.  
  296. ```ocaml
  297. # let m = Base.Map.empty (module Base.String);;
  298. val m : (string, 'a, Base.String.comparator_witness) Base.Map.t = <abstr>
  299.  
  300. # Base.Map.add m ~key:"x" ~data:1;;
  301. - : (string, int, Base.String.comparator_witness) Base.Map.t Base.Map.Or_duplicate.t = `Ok <abstr>
  302. ```
  303.  
  304. ## Summary
  305.  
  306. * Advantages
  307. * Safety
  308. * Stack safety
  309. * Exceptions safety
  310. * Monomorphic comparison
  311. * Async/blocking IO separation
  312. * Documentation
  313. * API documentation
  314. * Real World OCaml book
  315.  
  316. * Disadvantages
  317. * We have to use it
  318. * We have to get it approved
  319. * We have to audit it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement