ocaml

What are row types? Are they algebraic data types?

こ雲淡風輕ζ 提交于 2019-11-27 14:34:13
问题 I often hear that F# lacks support for OCaml row types, that makes the language more powerful than F#. What are they? Are they algebraic data types, such as sum types (discriminated unions) or product types (tuples, records)? And is it possible to write row types in other dialects, such as F#? 回答1: First of all, we need to fix the terminology. There is no such thing as "row type" † , at least in type theory and especially in the type system of OCaml. There exists "row polymorphism" and we

Why does OCaml sometimes require eta expansion?

戏子无情 提交于 2019-11-27 14:20:50
If I have the following OCaml function: let myFun = CCVector.map ((+) 1);; It works fine in Utop, and Merlin doesn't mark it as a compilation error. When I try to compile it, however, I get the following error: Error: The type of this expression, (int, '_a) CCVector.t -> (int, '_b) CCVector.t , contains type variables that cannot be generalized If I eta-expand it however then it compiles fine: let myFun foo = CCVector.map ((+) 1) foo;; So I was wondering why it doesn't compile in eta-reduced form, and also why the eta-reduced form seems to work in the toplevel (Utop) but not when compiling? Oh

OCaml internals: Exceptions

穿精又带淫゛_ 提交于 2019-11-27 11:49:18
问题 I'm curious to know how exceptions are dealt with in OCaml runtime to make them so lightweight. Do they use setjmp/longjmp or do they return a special value in each function, and propagate it? It seems to me that longjmp would put a little strain on the system, but only when an exception is raised, while checking for each function return value would need to check for every and each value after calling a function, which seems to me would put a lot of checks and jumps, and it seems it would

Algorithm for type checking ML-like pattern matching?

旧街凉风 提交于 2019-11-27 10:43:51
问题 How do you determine whether a given pattern is "good", specifically whether it is exhaustive and non-overlapping, for ML-style programming languages? Suppose you have patterns like: match lst with x :: y :: [] -> ... [] -> ... or: match lst with x :: xs -> ... x :: [] -> ... [] -> ... A good type checker would warn that the first is not exhaustive and the second is overlapping. How would the type checker make those kinds of decisions in general, for arbitrary data types? 回答1: Here's a sketch

What is the benefit of purely functional data structure?

淺唱寂寞╮ 提交于 2019-11-27 09:32:05
问题 There are large number of texts on data structures, and libraries of data structures code. I understand that purely functional data structure is easier to reason about. However I have trouble to understand the real world advantage of using purely functional data structure in pragmatic code (using functional programming language or not) over the imperative counterpart. Can somebody provide some real world cases where purely functional data structure has advantage and why? Examples along the

In pure functional languages, is there an algorithm to get the inverse function?

雨燕双飞 提交于 2019-11-27 09:01:57
问题 In pure functional languages like Haskell, is there an algorithm to get the inverse of a function, (edit) when it is bijective? And is there a specific way to program your function so it is? 回答1: In some cases, yes! There's a beautiful paper called Bidirectionalization for Free! which discusses a few cases -- when your function is sufficiently polymorphic -- where it is possible, completely automatically to derive an inverse function. (It also discusses what makes the problem hard when the

transpose of a list of lists

心不动则不痛 提交于 2019-11-27 07:45:29
问题 I'm trying to make a recursive function to get the transpose of a list of lists, n x p to p x n . But i'm unable to do so. I've been able to make a function to transpose a 3 x n list of lists to an n x 3 one: let rec drop1 list= [(match (List.nth list 0) with [] -> [] | a::b -> b); (match (List.nth list 1) with [] -> [] | a::b -> b); (match (List.nth list 2) with [] -> [] | a::b -> b);] let rec transpose list= if List.length (List.nth list 0) == 0 then [] else [(match (List.nth list 0) with [

Frama-C Plugin development: Getting result of value-analysis

强颜欢笑 提交于 2019-11-27 07:11:27
问题 I am working on a Plugin for Frama-C, using the Value-analysis. I simply want to print the state of the variables (values) after each statement (I think the solution is quiet easy, but I couldn't figure it out). I got the current state with Db.Value.get_stmt_state in the vstmt_aux method in the visitor. How can I now get the values of the variables? PS: I found this post, but it didn't help, there is no real solution, and with the help of the description I was not able to do it: How to use

Splitting a list of items into two lists of odd and even indexed items

烂漫一生 提交于 2019-11-27 06:38:20
问题 I would like to make a function that accepts a list and returns two lists: the first contains every odd item, and the second contains every even item. For example, given [1;2;4;6;7;9] , I would like to return [ [1;4;7] ; [2;6;9] ] . I have written this so far and I do not know how to progress. let splitList list = let rec splitOdd oList list1 list2 = match oList with | [] -> [] | head :: tail -> splitEven tail (list1::head) list2 and splitEven oList list1 list2 = match oList with | [] -> [] |

Creating GADT expression in OCaml

╄→гoц情女王★ 提交于 2019-11-27 06:20:06
问题 There is my toy GADT expression: type _ expr = | Num : int -> int expr | Add : int expr * int expr -> int expr | Sub : int expr * int expr -> int expr | Mul : int expr * int expr -> int expr | Div : int expr * int expr -> int expr | Lt : int expr * int expr -> bool expr | Gt : int expr * int expr -> bool expr | And : bool expr * bool expr -> bool expr | Or : bool expr * bool expr -> bool expr Evaluation function: let rec eval : type a. a expr -> a = function | Num n -> n | Add (a, b) -> (eval