Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

Confused about how to change the type of a function

$
0
0

I am writing a small parser combinator library, currently I am implementing a mapping function that's able to change the contents of the result of a successfully applied combinator

let pmap mapper p =  let inner input =    let res = run p input in    match res with        | Ok (c, rest) -> Ok ((mapper c), rest)        | fail         -> fail  in (Parser inner)

mapper in this code has type ('a -> 'a) but that's of no use since it's the whole purpose of the map to change from something to something else.The explanation I gave myself about this behaviour is: "since c in Ok (c, rest) represents an 'a then mapping it in Ok ((mapper c), rest) and putting it also in an Ok must mean that they are of the same type", if this is correct I'm confused about how to correct the types.

I'd like the type to be ('a -> 'b) but I am unsure on how to achieve that, even if I write the types manually nothing changes.

These are the types involved

type 'a result =  | Ok of 'a  | Fail of stringtype 'a parser = Parser of (char list -> ('a * char list) result)

I've made a small "proof of concept" and here the mapping function has the "right" type, so I'm really confused:

type 'a result =  | Y of 'a  | Nlet mmap f = function  | Y y -> let res = f y in Y res  | N   -> Nlet mapper o = [o]let _ = mmap mapper (Y 1)

Let me know if you need something else, any explanation would help me a lot!


Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>