I need to unwrap a Maybe -value in one of my update functions:
update msg model =
case msg of
UpdateMainContent val ->
Maybe.withDefault 100 (Just 42)
model
This of course is dummy code and the
Maybe.withDefault 100 (Just 42)
is taken straight out of the documentation for Maybe and not supposed to actually do anything. The compiler is complaining and saying:
Detected errors in 1 module.
-- TYPE MISMATCH ----------------------------------- ./src/Review/Form/State.elm
The 1st argument to function `withDefault` is causing a mismatch.
15|> Maybe.withDefault 100 (Just 42))
16| -- Maybe.withDefault 100 (model.activeItem)
17| model
Function `withDefault` is expecting the 1st argument to be:
a -> b
But it is:
number
Why is it saying that "withDefault" is expecting the first argument to be
a -> b
when it is defined as
a -> Maybe a -> a
in the documentation?
You accidentally left in model:
UpdateMainContent val ->
Maybe.withDefault 100 (Just 42)
model -- <-- here
This makes the type inference algorithm think that Maybe.withDefault 100 (Just 42) should evaluate to a function that can take this model argument. For that to make sense, it expects 100 and 42 to be functions, but they aren't, and so it tells you.
It might help to see an example where this works:
f : Int -> Int
f x = x + 1
Maybe.withDefault identity (Just f) 0
This will evaluate to 1.
Related
I have this function
result =
add 1 2 |> \a -> a % 2 == 0)
and I am getting this error
Elm does not use (%) as the remainder operator
When I look at the docs I see I can use modBy, so I tried this.
result =
add 1 2 |> (\a -> a modBy 2 == 0)
But that gives me the following error.
This function cannot handle the argument sent through the (|>) pipe:
The % operator was removed in 0.19 to reduce the confusion between rem and mod.
modBy and remainderBy are regular functions. You use them like:
result = add 1 2 |> (\a -> modBy 2 a == 0)
or, if you prefer a functional composition variant of the code:
result = add 1 2 |> modBy 2 >> (==) 0
As a historical note, there used to be a way to call functions infix using backticks notation:
a `modBy` 2
but this was removed in 0.18
This is a follow up to this question. Thanks to Kwartz I now have a state of the proposition if b divides a then b divides a * c for any integer c, namely:
alsoDividesMultiples : (a, b, c : Integer) ->
DivisibleBy a b ->
DivisibleBy (a * c) b
Now, the goal has been to prove that statement. I realized that I do not understand how to operate on dependent pairs. I tried a simpler problem, which was show that every number is divisible by 1. After a shameful amount of thought on it, I thought I had come up with a solution:
-- All numbers are divisible by 1.
DivisibleBy a 1 = let n = a in
(n : Integer ** a = 1 * n)
This compiles, but I was had doubts it was valid. To verify that I was wrong, it changed it slightly to:
-- All numbers are divisible by 1.
DivisibleBy a 1 = let n = a in
(n : Integer ** a = 2 * n)
This also compiles, which means my "English" interpretation is certainly incorrect, for I would interpret this as "All numbers are divisible by one since every number is two times another integer". Thus, I am not entirely sure what I am demonstrating with that statement. So, I went back and tried a more conventional way of stating the problem:
oneDividesAll : (a : Integer) ->
(DivisibleBy a 1)
oneDividesAll a = ?sorry
For the implementation of oneDividesAll I am not really sure how to "inject" the fact that (n = a). For example, I would write (in English) this proof as:
We wish to show that 1 | a. If so, it follows that a = 1 * n for some n. Let n = a, then a = a * 1, which is true by identity.
I am not sure how to really say: "Consider when n = a". From my understanding, the rewrite tactic requires a proof that n = a.
I tried adapting my fallacious proof:
oneDividesAll : (a : Integer) ->
(DivisibleBy a 1)
oneDividesAll a = let n = a in (n : Integer ** a = b * n)
But this gives:
|
12 | oneDividesAll a = let n = a in (n : Integer ** a = b * n)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When checking right hand side of oneDividesAll with expected type
DivisibleBy a 1
Type mismatch between
Type (Type of DPair a P)
and
(n : Integer ** a = prim__mulBigInt 1 n) (Expected type)
Any help/hints would be appreciated.
First off, if you want to prove properties on number, you should use Nat (or other inductive types). Integer uses primitives that the argument can't argue further than prim__mulBigInt : Integer -> Integer -> Integer; that you pass two Integer to get one. The compiler doesn't know anything how the resulting Integer looks like, so it cannot prove stuff about it.
So I'll go along with Nat:
DivisibleBy : Nat -> Nat -> Type
DivisibleBy a b = (n : Nat ** a = b * n)
Again, this is a proposition, not a proof. DivisibleBy 6 0 is a valid type, but you won't find a proof : Divisible 6 0. So you were right with
oneDividesAll : (a : Nat) ->
(DivisibleBy a 1)
oneDividesAll a = ?sorry
With that, you could generate proofs of the form oneDividesAll a : DivisibleBy a 1. So, what comes into the hole ?sorry? :t sorry gives us sorry : (n : Nat ** a = plus n 0) (which is just DivisibleBy a 1 resolved as far as Idris can). You got confused on the right part of the pair: x = y is a type, but now we need a value – that's what's your last error cryptic error message hints at). = has only one constructor, Refl : x = x. So we need to get both sides of the equality to the same value, so the result looks something like (n ** Refl).
As you thought, we need to set n to a:
oneDividesAll a = (a ** ?hole)
For the needed rewrite tactic we check out :search plus a 0 = a, and see plusZeroRightNeutral has the right type.
oneDividesAll a = (a ** rewrite plusZeroRightNeutral a in ?hole)
Now :t hole gives us hole : a = a so we can just auto-complete to Refl:
oneDividesAll a = (a ** rewrite plusZeroRightNeutral a in Refl)
A good tutorial on theorem proving (where it's also explained why plus a Z does not reduce) is in the Idris Doc.
I was writing a function with user-defined types in OCaml when I encountered an error message that I don't understand.
I'm currently using the OCaml interactive toplevel and also Visual Studio Code to write my code. The strange thing is that when I run the code in Visual Studio Code, it compiles fine but encounters the error in the interactive toplevel.
The OCaml code that I am referring to is as follows:
type loc = int;;
type id = string;;
type value =
| Num of int
| Bool of bool
| Unit
| Record of (id -> loc)
;;
type memory = (loc * value) list;;
exception NotInMemory;;
let rec memory_lookup : (memory * loc) -> value
= fun (mem, l) ->
match mem with
| [] -> raise NotInMemory
| hd :: tl -> (match hd with
| (x, a) -> if x = l then a else (memory_lookup (tl, l))
)
;;
The code that I wrote is basically my rudimentary attempt at implementing/emulating looking up memory and returning corresponding values.
Here's an example input:
memory1 = [ (1, Num 1) ; (2, Bool true) ; (3, Unit) ];;
Here's the expected output:
memory_lookup (memory1, 2);;
- : value = Bool true
However, here's the actual output:
Characters: 179-180:
| (x, a) -> if x = l then "a" else (memory_lookup (tl, l)))
Error: This expression has type value/1076
but an expression was expected of type value/1104
(Just for clarification: the error is regarding character a)
Does anybody know what type value/1076 and type value/1104 mean? Also, if there is anything wrong with the code that I wrote, would anybody be kind enough to point it out?
Thank you.
This kind of error happens in the toplevel when a type is defined multiple times, and some values of the old type are left in scope. A simple example is
type t = A
let x = A;;
type t = A
let y = A;;
x = y;;
Error: This expression has type t/1012 but an expression was expected of type
t/1009
The numerical part after the type name in value/1076 is a binding time for the type value. This binding time is used as a last resort to differentiate between two different types that happens to have the same name. Thus
Error: This expression has type value/1076
but an expression was expected of type value/1104
means that the value memory1 was defined with a type value defined at time 1076, whereas the function memory_lookup expected values of the type value defined at a later date (aka at time 1104). The binding times are a bit arbitrary , so they may be replaced by simply value/1 and value/2 in OCaml 4.08 .
This is literally my first line of Idris code. When I looked up the documentation, all appeared proper:
Idris> data T = Foo Bool | Bar (T -> T)
(input):1:6:
|
1 | data T = Foo Bool | Bar (T -> T)
| ^
unexpected reserved data
expecting dependent type signature
This makes me think I may need to declare T to be a symbol in some fashion?
It works as expected inside an Idris source file. At the REPL however, declarations need to be prefixed with the :let command:
:let data T = Foo Bool | Bar (T -> T)
Thanks for the question. I learned something trying to answer it.
The Beginning Elm - Let Expression page builds on the previous page, but it doesn't cover how to update the main function, written in forward function notation, which was:
main =
time 2 3
|> speed 7.67
|> escapeEarth 11
|> Html.text
to include the new fuelStatus parameter.
The compiler complains about a type mismatch, which is correct, as escapeEarth now has a third argument, which is a string.
As stated on that site "The forward function application operator takes the result from the previous expression and passes it as the last argument to the next function application."
In other words, how do I write this:
Html.text (escapeEarth 11 (speed 7.67 (time 2 3)) "low")
using forward notation?
Also, why doesn't this print "Land on droneship", along with "Stay in orbit"? It only prints "Stay in orbit":
module Playground exposing (..)
import Html
escapeEarth velocity speed fuelStatus =
let
escapeVelocityInKmPerSec =
11.186
orbitalSpeedInKmPerSec =
7.67
whereToLand fuelStatus =
if fuelStatus == "low" then
"Land on droneship"
else
"Land on launchpad"
in
if velocity > escapeVelocityInKmPerSec then
"Godspeed"
else if speed == orbitalSpeedInKmPerSec then
"Stay in orbit"
else
"Come back"
speed distance time =
distance / time
time startTime endTime =
endTime - startTime
main =
Html.text (escapeEarth 11 (speed 7.67 (time 2 3)) "low")
I think what you need is
main =
time 2 3
|> speed 7.67
|> \spd -> escapeEarth 11 spd "low"
|> Html.text
In other words you define a little anonymous function to insert the value correctly. You may want to look at whether the escapeEarth function should be defined with a different order.
An alternative if you love 'point free' would be
main =
time 2 3
|> speed 7.67
|> flip (escapeEarth 11) "low"
|> Html.text
Some would argue that this is less clear though
As for your second question you have defined functions in your let statement but never actually used it