Casting int to nat in Isabelle - isabelle

How can I cast an int to nat in Isabelle when I'm sure that it is positive?
value "((-10)::int) mod 3"
for example gives 2 and if I'm correct any application of modulo on an int should result in a nat.
Unfortunately the type signature of modulo is 'a => 'a => 'a so I cannot fix 3 to a nat beforehand and the result will be an int.

You can use value "nat (((-10)::int) mod 3)".

Related

Type Unification failed for bool * bool ⇒ bool * bool

I wrote a simple function which should perform the function of a halfadder.
fun halfadder :: "bool * bool ⇒ bool * bool"
where
"halfadder (a,b) = (
let s = xor a b in
let cout = and a b in
(cout,s))"
However, I get the following error:
Type unification failed: No type arity bool :: semiring_bit_operations
Type error in application: incompatible operand type
Operator: xor :: ??'a ⇒ ??'a ⇒ ??'a
Operand: a :: bool
Why is it not able to perform a XOR operation on a bool? What is going wrong here?
I have tried using the XOR operator with different data types and it still faces the same error
The type bool in Isabelle/HOL is the type for logical formulas (of the object logic HOL) and so not intended to be used as a type for bit datas. For example, a diagnosis
term "P ∧ Q"
gives
"P ∧ Q"
:: "bool"
If you jump to the definition of xor, you find that xor is defined for (or fixed for or being a method of) the class semiring_bit_operations. A possible solution is to define your halfadder inside the context of semiring_bit_operations, e.g.
context semiring_bit_operations
begin
fun halfadder :: "'a * 'a ⇒ 'a * 'a"
where
"halfadder (a, b) = (
let s = xor a b in
let cout = and a b in
(cout,s))"
end
which worked for me.
However, semiring_bit_operations is not a class for one bits but bit sequences. Thus, it's better to define your own boolean datatype, and and xor (or use some built-in type if any (which I don't know)).
(Note: I'm a beginner of Isabelle and so it is probably better to wait for more comprehensive answers from specialists.)

Definition of multiplicative group of a field in Isabelle

theory Scratch
imports
"HOL.Fields"
"HOL.Groups"
begin
locale Field_is_group=
fixes F :: "'a :: field"
begin
typedef 'a mul_group = "{x :: 'a set. x ≠ 0}"
end
end
I'm new to Isabelle, and I have learned something about proving in first-order logic but not about data structures. I tried to formalize the proof of the fact that a field has a multiplicative group, but I don't know how to define the multiplicative group. It is the set of the whole field except zero, and a field in Isabelle is defined as a type. By looking about the references, I know I need use typedef, but it fails:
Type unification failed: No type arity set :: zero
Failed to meet type constraint:
Term: λx. x ≠ 0 :: ??'b ⇒ bool
Type: 'a set ⇒ ??'a
The error(s) above occurred in typedef "mul_group"

Isabelle/HOL error Type unification failed: Clash of types "nat" and "_ set" Type error in application: incompatible operand type

Im really new to isabelle, this is my problem
theory MyTheory3
imports Main
begin
(* 3.1) a) define a type natpair, whose elements are pairs of natural numbers.*)
datatype natpair = Natpair "nat × nat"
(*3.1 b)define a function of type natpair ⇒ nat that returns the sum of the elements of a pair of natural
numbers.*)
definition natpair_sum :: "natpair ⇒ nat" where
"Natpair((a::nat) × (b::nat)) = a + b"
end
i get the error
Type unification failed: Clash of types "nat" and "_ set"
Type error in application: incompatible operand type
Operator: (×) :: ??'a set ⇒ ??'b set ⇒ (??'a × ??'b) set Operand: a
:: nat
in addition, can someone show me an example of natpair with a parameterized
constructor
Thanks in advance
youre nuric
Your code has several issues:
Operator (×) is not a constructor for pairs but the Cartesian product of sets; pairs are constructed using the syntax (a, b) or Pair a b.
The definition natpair_sum does not use its name in the equation, that is, the correct syntax is definition natpair_sum :: "natpair ⇒ nat" where "natpair_sum ... = ...".
Pattern matching is not allowed in the left-hand side of a definition; you can use the case construct in the right-hand side of the definition or you can use functions instead.
There is no need to add type annotations for the components of the pair.
As examples, here are two possible definitions for natpair_sum:
definition natpair_sum :: "natpair ⇒ nat" where
"natpair_sum np = (case np of Natpair (a, b) ⇒ a + b)"
fun natpair_sum :: "natpair ⇒ nat" where
"natpair_sum (Natpair (a, b)) = a + b"
As for your last question, I'm not quite sure what you mean by "an example of natpair with a parameterized constructor". Natpair is already a data constructor which takes a pair of nat's as the only parameter.

Isabelle/Simpl: Type unification failed using hoarestate

I started using Isabelle/Simpl and write the following theory according to the user guide:
theory Scratch
imports Simpl.Simpl
begin
hoarestate newvars =
N :: nat
lemma (in newvars) "Γ ⊢ {} ´N :== ´N + 1 {}"
sorry
end
But Isabelle complains that type unification fails:
Type unification failed
Type error in application: operator not of function type
Operator: N_' :: 'a
Operand: s :: ??'a
Simpl itself (including its user guide) successfully compiles.
How can I make it pass?
As Javier Díaz pointed out, importing only Simpl.Vcg instead of Simpl.Simpl does the trick.
The cause of the error seems a name collision against a record defined in Simpl.SyntaxTest. It contains the following record definition:
record 'g vars = "'g state" +
A_' :: "nat list"
AA_' :: "nat list list"
I_' :: nat
M_' :: nat
N_' :: nat
R_' :: int
S_' :: int
B_' :: bool
Abr_':: string
p_' :: ref
q_' :: ref
Isabelle seems to prefer N_' in the record to N in the hoarestate, although the hoarestate is defined later😥. I don't know why this is the case.

How to merge set of finite maps?

I can merge two finite maps as follows:
value "fmadd
(fmap_of_list [(1::nat,2::nat)])
(fmap_of_list [(2::nat,3::nat)])"
But when I try to merge a set of maps:
value "ffold fmadd fmempty {|
fmap_of_list [(1::nat,2::nat)],
fmap_of_list [(2::nat,3::nat)]|}"
I get the following error:
Wellsortedness error:
Type nat ⇀⇩f nat not of sort finite
No type arity fmap :: finite
According to definition of fmap, it's domain is finite:
typedef ('a, 'b) fmap = "{m. finite (dom m)} :: ('a ⇀ 'b) set"
morphisms fmlookup Abs_fmap
proof
show "Map.empty ∈ {m. finite (dom m)}"
by auto
qed
But why fmap is not finite?
To answer your immediate question:
But why fmap is not finite?
Every fmap has a finite domain, but it is not necessarily the case that there are only finitely many values of type ('a, 'b) fmap. For example, there are infinitely many finitely-sized mappings from nat to nat.
The problem you're observing is deeper than that: I believe there is no proper code setup for ffold. If I try to compute
ffold funion fempty {|
fset_of_list [(1::nat,2::nat)],
fset_of_list [(2::nat,3::nat)]|}
... the error message is similar. For now, I would recommend rewriting it as fold on lists:
fold fmadd [
fmap_of_list [(1::nat,2::nat)],
fmap_of_list [(2::nat,3::nat)]] fmempty
It's not the same but it might be useful for your application.

Resources