this pattern-matching is not exhaustive. Ocaml - functional-programming

can someone tell me why does this code# let func [g] a = g a ::[] ;;
output this warning
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]

The argument [g] will match a single-element list, and bind that element to g. func [f] a will work, but what if the function is applied with an empty list, func [] a, or a list with multiple elements, func [f, g] a? What can it do other than raise an exception? That's what the warning is telling you.

Related

Elm - fold on recursive type with list does not compile

I'm following this article on catamorphism and I'm trying to define a fold function for a recursive data type like this
type Node anyType
= Leaf Id (Maybe anyType)
| Tree Id (List (Node anyType))
What i wrote is this:
foldTree fLeaf fTree acc node =
let
recurse =
foldTree fLeaf fTree
in
case node of
Leaf id v ->
let
newAcc = fLeaf acc (id, v)
in
newAcc
Tree id l ->
let
newAcc = fTree acc id
in
l |> List.foldl recurse newAcc
If i don't infer the types for foldTree the function compiles, but it seems to not be usable:
collectIds node =
let
fLeaf acc (id,v) = id :: acc
fTree acc id = id :: acc
in
foldTree fLeaf fTree [] node
throws the following:
TYPE MISMATCH - The 1st argument to `foldTree` is not what I expect:
173| foldTree fLeaf fTree [] node
#^^^^^#
This `fLeaf` value is a:
#List a# -> ( a, b ) -> #List a#
But `foldTree` needs the 1st argument to be:
#Node anyType# -> ( Id, Maybe anyType ) -> #Node anyType#
Auto inferring the types for foldTree makes it not compilable and throws the following:
-- Auto Inferred
foldTree : (c -> (Id, Maybe anyType) -> a) -> (c -> Id -> b) -> c -> Node anyType -> d
TYPE MISMATCH - Something is off with the 1st branch of this `case` expression:
126| newAcc
#^^^^^^#
This `newAcc` value is a:
#a#
But the type annotation on `foldTree` says it should be:
#d#
#Hint#: Your type annotation uses `a` and `d` as separate type variables. Your
code seems to be saying they are the same though. Maybe they should be the same
in your type annotation? Maybe your code uses them in a weird way?
and if I try to follow the hint, still does not compile
foldTree : (c -> (Id, Maybe anyType) -> a) -> (c -> Id -> b) -> c -> Node anyType -> a
TYPE MISMATCH - This function cannot handle the argument sent through the (|>) pipe:
134| l |> List.foldl recurse newAcc
#^^^^^^^^^^^^^^^^^^^^^^^^^#
The argument is:
List #(Node anyType)#
But (|>) is piping it to a function that expects:
List #c#
#Hint#: Your type annotation uses type variable `c` which means ANY type of value
can flow through, but your code is saying it specifically wants a `Node` value.
Maybe change your type annotation to be more specific? Maybe change the code to
be more general?
Read <https://elm-lang.org/0.19.1/type-annotations> for more advice!Elm
TYPE MISMATCH - The 1st argument to `foldl` is not what I expect:
134| l |> List.foldl recurse newAcc
#^^^^^^^#
This `recurse` value is a:
c -> Node anyType -> #a#
But `foldl` needs the 1st argument to be:
c -> Node anyType -> #Node anyType#
#Hint#: Your type annotation uses type variable `a` which means ANY type of value
can flow through, but your code is saying it specifically wants a `Node` value.
Maybe change your type annotation to be more specific? Maybe change the code to
be more general?
I'm stuck. Following the types on the article precisely also does not seem to work. I understand that the code on the article is F# and I'm working on Elm but I thought that in this case it would have been 100% translatable.
Where am I going wrong?
Thanks in advance!
You've flipped the arguments to List.foldl. The fold function takes the value first and the accumulator second, while your recurse function takes the accumulator first and the value second.
The simple fix to this is to eta expand the recurse function and flip the arguments when passing it to foldTree:
recurse v a = foldTree fLeaf fTree a v
Also, interestingly, annotating the type of recurse will make it compile, but obviously produces the wrong result. I didn't pursue this further to understand why, as it was wrong, but the lesson you should take from this is to always annotate your top level functions. This will give you better error messages, but also prevents your code from accidentally compiling but producing the wrong result.

Correctly using 'in' keyword in OCAML

I have recently started working on OCAML. I am working from the book Modern Programming Languages, 2nd ed. The first chapter on ML has an exercise requiring the definition of a function max of type int list -> int to return the largest element from a list of integers. There is additionally a hint which suggests the inclusion of a helper function maxhelper which as a second parameter takes the current largest element. Then max is defined as :
fun max x = maxhelper (tl x, hd x)
I am trying to implement this in OCAML. Here is my code :
let max x =
let rec maxhelper x cur_max =
match x with
| [] -> cur_max
| h::t ->
if cur_max < h then maxhelper t h
else maxhelper t cur_max
in maxhelper List.tl(x) List.hd(x)
;;
This results in an error I cannot understand : This expression, i.e. List.tl(x) on the last line has type 'a list -> 'a list
but an expression was expected of type ('b -> 'c -> 'd) list
What puzzles me is when I write the maxhelper function separately and give it arguments [2;3;4] 1 (original list being [1;2;3;4]) it works correctly. Further, if I replace the arguments provided under in as
in maxhelper x 0
The code compiles and works correctly (for non-negative numbers). I am not sure what I have missed regarding passing arguments to in, or the error message I received. Mainly, why does the additional of a List() call cause an error?
You're writing function calls as in an Algolic language, that's the basic problem I think.
Instead of
maxhelper List.tl(x) List.hd(x)
You should have
maxhelper (List.tl x) (List.hd x)
The way you wrote it, there are 4 parameters being passed to maxhelper: List.tl, x, List.hd, x.

SML Create function receives list of tuples and return list with sum each pair

I'm studying Standard ML and one of the exercices I have to do is to write a function called opPairs that receives a list of tuples of type int, and returns a list with the sum of each pair.
Example:
input: opPairs [(1, 2), (3, 4)]
output: val it = [3, 7]
These were my attempts, which are not compiling:
ATTEMPT 1
type T0 = int * int;
fun opPairs ((h:TO)::t) = let val aux =(#1 h + #2 h) in
aux::(opPairs(t))
end;
The error message is:
Error: unbound type constructor: TO
Error: operator and operand don't agree [type mismatch]
operator domain: {1:'Y; 'Z}
operand: [E]
in expression:
(fn {1=1,...} => 1) h
ATTEMPT 2
fun opPairs2 l = map (fn x => #1 x + #2 x ) l;
The error message is: Error: unresolved flex record (need to know the names of ALL the fields
in this context)
type: {1:[+ ty], 2:[+ ty]; 'Z}
The first attempt has a typo: type T0 is defined, where 0 is zero, but then type TO is referenced in the pattern, where O is the letter O. This gets rid of the "operand and operator do not agree" error, but there is a further problem. The pattern ((h:T0)::t) does not match an empty list, so there is a "match nonexhaustive" warning with the corrected type identifier. This manifests as an exception when the function is used, because the code needs to match an empty list when it reaches the end of the input.
The second attempt needs to use a type for the tuples. This is because the tuple accessor #n needs to know the type of the tuple it accesses. To fix this problem, provide the type of the tuple argument to the anonymous function:
fun opPairs2 l = map (fn x:T0 => #1 x + #2 x) l;
But, really it is bad practice to use #1, #2, etc. to access tuple fields; use pattern matching instead. Here is a cleaner approach, more like the first attempt, but taking full advantage of pattern matching:
fun opPairs nil = nil
| opPairs ((a, b)::cs) = (a + b)::(opPairs cs);
Here, opPairs returns an empty list when the input is an empty list, otherwise pattern matching provides the field values a and b to be added and consed recursively onto the output. When the last tuple is reached, cs is the empty list, and opPairs cs is then also the empty list: the individual tuple sums are then consed onto this empty list to create the output list.
To extend on exnihilo's answer, once you have achieved familiarity with the type of solution that uses explicit recursion and pattern matching (opPairs ((a, b)::cs) = ...), you can begin to generalise the solution using list combinators:
val opPairs = map op+

Ocaml Error Unbound Value when using Recursion

My code is very basic as I am pretty new to ocaml
I am trying to call a function recursively but I am getting an unbound value error message on the name of the function
let count_help x a lst = match lst with
[] -> a
| (s,i)::t -> if s = x then count_help x a+1 t else count_help x a t
;;
let count_assoc lst x =
count_help x 0 lst
;;
The error is Unbound value count_help on the line that calls count_help inside of count_help
This code is simply suppose to count the number times an association appears for the given character x
You need to say
let rec count_help ...
to allow the name count_help to be used recursively within its definition.

Combinator Reduction Wolfram Mathematica

How can I implement the normal reduction strategy for Combinators {S,K,I} in Mathematica?
Here are the rules:
S[x][y][z]->x[y][z[y]]
K[x][y][z]->x
Also we have an Y combinator ( fixed point) thus Y[a]=a[Ya]].
And we must "evaluate" the expression like ,for instance, S[K][K][a] = K[a][K[a]]=a
It is highly important to have a ONE step of reduction. Thus, that the result will be in application one step many times..
Thanks in anticipation for any suggestions!!!
Let's define a function called eval that will perform one step of combinator evaluation. First, we need to consider what exactly constitutes a single step. Arbitrarily, we will evaluate the left-outermost expression first and work inwards from there.
ClearAll[eval]
eval[e_] := Module[{r = eval1[e]}, r /; r =!= e]
eval[e:f_[g_]] := Module[{r = eval[f][g]}, r /; r =!= e]
eval[e:f_[g_]] := Module[{r = f[eval[g]]}, r /; r =!= e]
eval[e_] := e
Note that the rules are only applied if they actually change the expression. The awkwardness of these definitions is due to Mathematica's lack of a pattern expression that will match arbitrarily long curried argument lists.
Now we can define the recognized combinators, S, K and I:
ClearAll[eval1]
eval1[s[f_][g_][h_]] := f[h][g[h]]
eval1[k[f_][_]] := f
eval1[i[f_]] := f
The symbols are in lower case here mainly to avoid the way that Mathematica renders the built-in symbol I, the imaginary unit.
Any other combinators will be taken as variables and left unevaluated:
eval1[e_] := e
Now we can try out the basic cases:
i[f] // eval
(* f *)
k[f][g] // eval
(* f *)
s[f][g][h] // eval
(* f[h][g[h]] *)
For more interesting cases, let's define evalAll that shows all steps in an evaluation chain:
ClearAll[evalAll]
evalAll[e_, n_:Infinity] := FixedPointList[eval, e, n+1] // Most // Column
s[k][k][a] // evalAll
(*
s[k][k][a]
k[a][k[a]]
a
*)
s[s[m][n][r]][k[a][b]][c] // evalAll
(*
s[s[m][n][r]][k[a][b]][c]
s[m][n][r][c][k[a][b][c]]
m[r][n[r]][c][k[a][b][c]]
m[r][n[r]][c][a[c]]
*)
f[s[i[k]][k][g][i[f]]] // evalAll
(*
f[s[i[k]][k][g][i[f]]]
f[i[k][g][k[g]][i[f]]]
f[k[g][k[g]][i[f]]]
f[g[i[f]]]
f[g[f]]
*)
evalAll takes an optional "count" argument to limit the number of steps shown. This is useful for divergent expressions -- like a direct expression of the Y combinator:
eval1[y[f_]] := f[y[f]]
y[f] // evalAll[#, 4]&
(*
y[f]
f[y[f]]
f[f[y[f]]]
f[f[f[y[f]]]]
f[f[f[f[y[f]]]]]
*)
... but it is more fun to express such sequences in terms of SKI:
Module[{y = s[k[s[i][i]]][s[s[k[s]][k]][k[s[i][i]]]]}
, evalAll[y[f], 28]
]
(*
s[k[s[i][i]]][s[s[k[s]][k]][k[s[i][i]]]][f]
k[s[i][i]][f][s[s[k[s]][k]][k[s[i][i]]][f]]
s[i][i][s[s[k[s]][k]][k[s[i][i]]][f]]
i[s[s[k[s]][k]][k[s[i][i]]][f]][i[s[s[k[s]][k]][k[s[i][i]]][f]]]
s[s[k[s]][k]][k[s[i][i]]][f][i[s[s[k[s]][k]][k[s[i][i]]][f]]]
s[k[s]][k][f][k[s[i][i]][f]][i[s[s[k[s]][k]][k[s[i][i]]][f]]]
k[s][f][k[f]][k[s[i][i]][f]][i[s[s[k[s]][k]][k[s[i][i]]][f]]]
s[k[f]][k[s[i][i]][f]][i[s[s[k[s]][k]][k[s[i][i]]][f]]]
k[f][i[s[s[k[s]][k]][k[s[i][i]]][f]]][k[s[i][i]][f][i[s[s[k[s]][k]][k[s[i][i]]][f]]]]
f[k[s[i][i]][f][i[s[s[k[s]][k]][k[s[i][i]]][f]]]] <-- f[...]
f[s[i][i][i[s[s[k[s]][k]][k[s[i][i]]][f]]]]
f[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]
f[i[s[s[k[s]][k]][k[s[i][i]]][f]][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]
f[s[s[k[s]][k]][k[s[i][i]]][f][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]
f[s[k[s]][k][f][k[s[i][i]][f]][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]
f[k[s][f][k[f]][k[s[i][i]][f]][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]
f[s[k[f]][k[s[i][i]][f]][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]
f[k[f][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]][k[s[i][i]][f][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]
f[f[k[s[i][i]][f][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]] <-- f[f[...]]
f[f[s[i][i][i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]
f[f[i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]
f[f[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]
f[f[i[s[s[k[s]][k]][k[s[i][i]]][f]][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]
f[f[s[s[k[s]][k]][k[s[i][i]]][f][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]
f[f[s[k[s]][k][f][k[s[i][i]][f]][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]
f[f[k[s][f][k[f]][k[s[i][i]][f]][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]
f[f[s[k[f]][k[s[i][i]][f]][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]
f[f[k[f][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]][k[s[i][i]][f][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]]
f[f[f[k[s[i][i]][f][i[i[i[s[s[k[s]][k]][k[s[i][i]]][f]]]]]]]] <-- f[f[f[...]]]
*)

Resources