Ullman's book, Elements of ML Programming, introduces a simple version of the map function defined as follows:
fun simpleMap(F, nil) = nil
| simpleMap(F, x::xs) = F(x)::simpleMap(F, xs);
val map = fn : ('a -> 'b) * 'a list -> 'b list
Given that -> is right associative, wouldn't you parenthesize like this:
('a -> 'b) * ('a list -> 'b list) ?
But this is incorrect since the domain type is a tuple consisting of the function and a list and the range type is just a list.
Where am I going wrong?
Thanks!
In SML, the type operator * binds more tightly than ->: it has a higher precedence just like * has a higher precedence than + in arithmetic.
This is why string * string -> string is the same as (string * string) -> string and not string * (string -> string). To read your example, we'd need to put parentheses around the * before worrying about how -> associates:
(('a -> 'b) * 'a list) -> 'b list
Related
So I have two custom datatypes:
datatype ('a, 't) action = ACTION (name: "'a") (args: "'t list") ("⌈_ _⌋")
and
datatype ('a, 't) multiaction = MULTIACTION "('a, 't) action multiset" ("⟨(_)⟩")
Both of them work with their given notations, however whenever I want to use these data structures in their pretty-printed format, it looks a little redundant. For example:
value "⟨{#⌈a b⌋, ⌈c d⌋#}⟩"
What I'd like to do is have the above typed without the multiset brackets, so that it looks like this:
value "⟨⌈a b⌋, ⌈c d⌋⟩"
What I've tried is using syntax:
syntax
"_maction" :: "args ⇒ ('a, 't) multiaction" ("⟨_⟩" [0] 60)
translations
"⟨x⟩" == "CONST MULTIACTION {#x#}"
But when I try it out with some datatypes:
value "⟨⌈''x'' [1,2,3::int]⌋⟩"
But I get a wellsortedness error, which I have no idea why it occurs. Can I please have some help with this?
Thanks in advance!
Perhaps, you are looking to lift the multiset syntax to multiaction? In this case, you may also wish to lift the relevant definitions for the multiset (as noted in the comments, ideally, you will wish to use the datatype/lift_definition infrastructure for this):
datatype ('a, 't) action = ACTION (name: "'a") (args: "'t list") ("⌈_ _⌋")
datatype ('a, 't) multiaction = MULTIACTION "('a, 't) action multiset"
(*this should not be lifted manually: use the integration of
the datatype and lifting infrastructure*)
fun add_multiaction ::
"('a, 'b) action ⇒ ('a, 'b) multiaction ⇒ ('a, 'b) multiaction"
where "add_multiaction x (MULTIACTION xs) = MULTIACTION (add_mset x xs)"
abbreviation MAempty :: "('a, 't) multiaction" ("⟨#⟩") where
"MAempty ≡ MULTIACTION {#}"
syntax
"_multiaction" :: "args ⇒ ('a, 't) multiaction" ("⟨(_)⟩")
translations
"⟨x, xs⟩" == "CONST add_multiaction x ⟨xs⟩"
"⟨x⟩" == "CONST add_multiaction x ⟨#⟩"
value "⟨⌈''x'' [1,2,3::int]⌋, ⌈''x'' [1,2,3::int]⌋⟩"
I have to admit I am not entirely certain if I fully understood what exactly you are trying to achieve: I am providing the first idea that came to my mind based on not-very-thorough understanding of the question.
Isabelle version: Isabelle2021-RC6
I have a function f: 'a -> Async<'b option> that I want to call with 'a option instead of 'a. Now, if I had a function g that returned Async<'b> instead of f's Async<'b option>, I could write a standard traverse implementation and I would then have Async<'b option> at the end. But if I use traverse with f, the result is Async<'b option option>.
It seems to me I need something like this:
module Option =
let traverseBindAsync (f: 'a -> Async<'b option>) (opt: 'a option) : Async<'b option> =
async {
match opt with
| None -> return None
| Some x -> return! f x
}
I don't think traverseBind is a recognized concept (no search results). Is it known under another name, or alternatively, is there another way to think about this in terms of recognized FP concepts one can express in F#? If so, could I use those concepts in a way that is more or less as syntactically concise as the above Option.traverseBindAsync?
I'm not aware of any standard name for a function of a type like this. However, there are two changes you can do to the code that might be somewhat revealing. First, you can implement it without the use of the async block, just by using either f x or by returning async.Unit(None), i.e. an asynchronous computation that immediately returns None:
let traverseBindAsync (f: 'a -> Async<'b option>) (opt: 'a option) : Async<'b option> =
match opt with
| None -> async.Return None
| Some x -> f x
Now you can also rewrite this as doing Option.map on the input and using async.Return(None) as the default value when the input option (and therefore also the result of the map operation) is None:
let traverseBindAsync (f: 'a -> Async<'b option>) (opt: 'a option) : Async<'b option> =
opt |> Option.map f |> Option.defaultValue (async.Return None)
I don't think this has any name, but you can think of your function as Option.map with a particular default value when the input is not available.
In a learning environment, what are my options to provide type signatures for functions?
Standard ML doesn't have top-level type signatures like Haskell. Here are the alternatives I have considered:
Module signatures, which require either a separate signature file, or the type signature being defined in a separate block inside the same file as the module itself. This requires the use of modules, and in any production system that would be a sane choice.
Modules may seem a little verbose in a stub file when the alternative is a single function definition. They both introduce the concept of modules, perhaps a bit early,
Using val and val rec I can have the complete type signature in one line:
val incr : int -> int =
fn i => i + 1
val rec map : ('a -> 'b) -> 'a list -> 'b list =
fn f => fn xs => case xs of
[] => []
| x::ys => f x :: map f ys
Can I have this and also use fun?
If this is possible, I can't seem to get the syntax right.
Currently the solution is to embed the argument types and the result type as such:
fun map (f : 'a -> 'b) (xs : 'a list) : 'b list =
raise Fail "'map' is not implemented"
But I have experienced that this syntax gives the novice ML programmer the impression that the solution either cannot or should not be updated to the model solution:
fun map f [] = []
| map f (x::xs) = f x :: map f xs
It seems then that the type signatures, which are supposed to aid the student, prevents them from pattern matching. I cannot say if this is because they think that the type signatures cannot be removed or if they should not be removed. It is, of course, a matter of style whether they should (and where), but the student should be enabled to explore a style of type inference.
By using a let or local bound function, and shadowing
you can declare the function, and then assign it to a value.
using local for this is more convenient, since it has the form:
local decl in decl end, rather than let decl in expr end,
meaning let's expr, wants a top-level argument f
val map = fn f => let fun map = ... in map end
I don't believe people generally use local, anymore primarily because modules can do anything that local can, and more, but perhaps it is worth considering it as an anonymous module, when you do not want to explain modules yet.
local
fun map (f : 'a -> 'b) (x::rest : 'a list) : 'b list
= f x :: map f rest
| map _ ([]) = []
in
val (map : ('a -> 'b) -> 'a list -> 'b list) = map;
end
Then when it comes time to explain modules, you can declare the structure inside the local, around all of the declarations,
and then remove the local, and try to come up with a situation, where they have coded 2 functions, and it's more appropriate to replace 2 locals, with 1 structure.
local
structure X = struct
fun id x = x
end
in val id = X.id
end
perhaps starting them off with something like the following:
exception ReplaceSorryWithYourAnswer
fun sorry () = raise ReplaceSorryWithYourAnswer
local
(* Please fill in the _'s with the arguments
and the call to sorry() with your answer *)
fun map _ _ = sorry ()
in
val map : ('a -> 'b) -> ('a list) -> ('b list) = map
end
I am attempting to change the value of a key in a map I made in OCaml:
module TestMap = Map.Make(String);;
let m = TestMap.empty;;
let m = TestMap.add "Chris" 1 m ;;
let m = TestMap.add "Julie" 4 m;;
This compiles file, but when I try to update the value at key Julie with:
let m = TestMap.update "Julie" 10 m;;
I get an error from the compiler:
Error: This expression has type int but an expression was expected of type
'a option -> 'a option
I'm guessing that I'm maybe using the function incorrectly. I'm finding the documentation for Map.update pretty hard to understand:
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
Is my syntax or are my arguments incorrect?
The update function works in a way different from what you think
key -> ('a option -> 'a option) -> 'a t -> 'a t
You see that second argument is a function which takes an 'a option and returns an 'a option so you don't directly update with a new value but rather pass a function which returns the new value, according to the previous one, eg:
let m = TestMap.update "Julie" (fun _ -> Some 10) m;;
This because, as documentation states, the passed 'a option tells you if there was a mapping for the key and the returned 'a option allows you to change it or even remove it (through None).
If you need just to update a mapping you can use Map.add again, there's no need to use more advanced Map.update.
Hi this is my first time posting on Stack Overflow and I've run into a problem while trying to construct a type in OCaml
I'm trying to construct a type tree that has nodes/leafs/etc. This is what I have so far.
type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a * tree) | ....
My node is supposed to be a type that contains the its name and another tree as a tuple. But when I tried to compile this it said tree required two arguments. So I tried:
type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a * tree ('a*'b))
and I was still getting an error. Anything that you notice I was doing wrong? Thanks!
type ('a, 'b) tree = Empty | Leaf of 'b | Node of 'a * ('a, 'b) tree
You probably want your Nodes two have more than one child, though
type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a, 'b) tree * 'a * ('a, 'b) tree
PS : Beware than in a type declaration, Foo of bar * baz and Foo of (bar * baz) are not the same : the first is a constructor Foo with two fields, the second has only one field, which is of type (bar * baz).