MAPLE, How to convert a string (with my structure) to a set? - maple

i have a text file with 3 lines:
B = 2*Pi/n, true, {B, n}
a = 2*R*sin(B/2), true, {a, R, B}
P = n*a, true, {P, n, a}
i want to read this file in maple. Each line will be a set like this
set1 := {B = 2*Pi/n, true, {B, n}}
set2 := {a = 2*R*sin(B/2), true, {a, R, B}}
set3 := {P = n*a, true, {P, n, a}}
i tried to read the text file line by line (using readline ) and i got 3 string.
str1 := "B = 2*Pi/n, true, {B, n}"
str2 := "a = 2*R*sin(B/2), true, {a, R, B}"
str3 := "P = n*a, true, {P, n, a}"
Is there any sort way to convert these strings to sets?

You can use cat to add braces to the strings and then parse them:
for i to 3 do
(set || i) := parse(cat("{", (str || i), "}"));
end do;
If you have any control over the input file, you could do this a little more easily with the read command and input that is valid Maple commands:
set1 := {B = 2*Pi/n, true, {B, n}};
e.g.

Related

Coq - Assign expression to variable

First and foremost, I'm not yet very familiar with Coq lingo, so I will use terms like e.g. 'expression' and 'variable' loosely, but they are probably not the correct Coq terms.
I'm trying to prove the following subgoal of a Theorem.
1 goal
b : bag
v, b' : nat
b'' : natlist
B : b = b' :: b''
IHb'' : b = b'' -> count v (add v b'') = count v b'' + 1
______________________________________(1/1)
S match v =? b' with
| true => S (count v b'')
| false => count v b''
end = match v =? b' with
| true => S (count v b'')
| false => count v b''
end + 1
You can ignore S and + 1, I'm basically looking for a way to assign
match v =? b' with
| true => S (count v b'')
| false => count v b''
end
to a variable of type nat because it occurs on both sides of the equation.
How can I do this? Or do I need to go through destroying v and b' and proving all cases separately?
Here are two possibilities. There may well be better ones.
You can use set to give a name to a term. All the occurrences of that term are replaced by the variable.
set (x := match v =? b' with
| true => S (count v b'')
| false => count v b''
end).
Sometimes you need to hide the definition of the variable, and only remember it as an equality that you invoke on demand. For that, use remember.
You can match the goal against a pattern using the context form of match goal and give a name to whatever's inside that pattern.
match goal with |- context [S ?_x = ?_x + 1] => set (x := _x) end.
If this is your real goal and not a simplified example, it's a simple arithmetic statement and you can just call lia and let Coq figure it out.
Require Import Lia.
…
lia.
Besides Gilles's suggestions you can use the ssreflect set to achieve this, in at least two ways illustrated here:
Require Import Arith ssreflect.
Variables v b' b'' : nat.
Variable count : nat -> nat -> nat.
Goal
S match v =? b' with
| true => S (count v b'')
| false => count v b''
end
= match v =? b' with
| true => S (count v b'')
| false => count v b''
end + 1.
Proof.
set t := (X in S X = X + _).
Undo.
set t := match _ with true => _ | false => _ end.
Abort.
The latter one also works with the regular set but it needs brackets:
set (t := match _ with true => _ | false => _ end).

how to code in coq a function that sum the integers represented by the 2 lists and boolean representing a holdback?

I want to set the recursive function badd_r: list bool -> list bool -> bool -> list bool that sum the integers represented by the 2 lists and boolean representing a holdback.
I need to use these two functions b1add_r(sum of two lists), bsucc(successor of a binary number represented by a list of booleans).
Here are the two functions:
Fixpoint bsucc (l: list bool): list bool :=
match l with
| [] =>[true]
| true::r => false:: (bsucc r)
| false::r => true::r
end.
Definition b1add_r b1 b2 r :=
match b1,b2 with
true, true => (r,true)
| true,false => (negb r, r)
| false, true => (negb r,r)
| false,false => (r, false)
end.
I have no idea how to code this function in coq and how to prove it...
Lemma badd_rOK: forall l1 l2 r,
value (badd_r l1 l2 r) = value l1 + value l2 + (if r then 1 else 0).
Fixpoint value (l: list bool) :=
match l with
| [] => 0
| true :: r => 1 + 2 * value r
| false :: r => 2 * value r
end.
Any help would be appreciated!
Your function may have the following form:
Fixpoint badd_r (l1 l2:list bool) (r : bool) : list bool :=
match l1, l2 with
nil, nil => (* base case 1 *)
| a::r1, nil => (* base case 2 *)
| nil, b::r2 => (* base case 3 *)
| a::r1, b:: r2 => let (c, r') := b1add_r a b r
in (* recursive case *)
end.
In order to fill the holes/comments, and plan to prove correctness, you may relate arithmetic properties with the possible cases.
For instance, the equality (1+2*x)+(0+2*y)+1= 0+2(x+y+1) corresponds to the case add_r (true::r) (false::s) true = false :: (add_r r s true)

Issue on definition expansion from Coq module system

I have defined a couple of modules in Coq to build a Byte type from a Bit type, recursively, as a three of pairs. But I hit an issue defining a Numeral Notation for the Byte type.
Here is the code:
Require Import ZArith.
(* bit sequence abstracted interface *)
Module Type Numeric.
Parameter T: Set.
Parameter MAX: T. (* sequence of 1...1 = 2^n - 1 *)
Parameter to: T -> Z. (* conversion to Z *)
Parameter of: Z -> option T. (* conversion from Z *)
End Numeric.
(* a single bit *)
Module Bit.
Inductive bit: Set := bit0 | bit1.
Definition T: Set := bit.
Definition MAX: T := bit1.
Definition to (i: T): Z :=
match i with
| bit0 => 0%Z
| bit1 => 1%Z
end.
Definition of (n: Z): option T :=
match n with
| Z0 => Some bit0
| Zpos xH => Some bit1
| _ => None
end.
End Bit.
(* concatenation of two bit sequences *)
Module ConcatNumeric (m1 m2: Numeric).
Definition T: Set := m1.T * m2.T.
Definition MAX: T := (m1.MAX, m2.MAX).
Definition to (x: T): Z :=
let (x1, x2) := x in
let i1 := m1.to x1 in
let i2 := m2.to x2 in
let base := (m2.to m2.MAX + 1)%Z in
(i1 * base + i2)%Z.
Definition of (i: Z): option T :=
let base := (m2.to m2.MAX + 1)%Z in
let i2 := (i mod base)%Z in
let i1 := (i / base)%Z in
match m1.of i1, m2.of i2 with
| Some z1, Some z2 => Some (z1, z2)
| _, _ => None
end.
End ConcatNumeric.
(* hierarchy defining a byte from bits *)
Module Crumb: Numeric := ConcatNumeric Bit Bit.
Module Nibble: Numeric := ConcatNumeric Crumb Crumb.
Module Byte: Numeric := ConcatNumeric Nibble Nibble.
(* boxing Byte.T in an inductive type to make Numeral Notation happy *)
Inductive u8: Set := u8_box (x: Byte.T).
Definition u8_unbox := fun x => match x with u8_box x => x end.
Definition u8_of := fun i => option_map u8_box (Byte.of i).
Definition u8_to := fun x => Byte.to (u8_unbox x).
(* defines the scope and the Numeral Notation *)
Declare Scope u8_scope.
Delimit Scope u8_scope with u8.
Numeral Notation u8 u8_of u8_to: u8_scope.
(* testing the code *)
Open Scope u8_scope.
Definition x: u8 := 1. (* error here! *)
I am getting this error:
Error: Unexpected non-option term
match Byte.of 1 with
| Some a => Some (u8_box a)
| None => None
end while parsing a numeral notation.
which seems not specific to Numeral Notation but rather a more general issue related to the fact that Byte.of cannot be expanded. Can someone shed some light on what is going on, and if there a way to work around this, which appears to be a limitation?
Coq version 8.11.2
When you write Module Byte: Numeric := Foo, you tell Coq to erase all the definitions from Foo and to keep only the signature of Numeric. This causes Byte.of to lose its body.
In your case, you do not want to restrict the content of Byte to Numeric, but only to document that it is compatible with Numeric. You can do so using Module Byte <: Numeric := Foo.
By the way, instead of putting this documentation on Byte, you could instead move it to ConcatNumeric:
Module ConcatNumeric (m1 m2: Numeric) <: Numeric.
...
End ConcatNumeric.
Module Byte := ConcatNumeric Nibble Nibble.

How to make a proc that aply for all variables in maple

for example supposed I want to program a proc that plus 1 to all results that is a real numbers in my program. Is there a way to do that ?
You can use anames('user') to obtain top-level, user-defined variables, and then select those that are real-valued. In the code below, calling incrementReals() will increment all such variables:
restart;
a, b, c := 3, 1 - I, 0.5;
# Procedure to increment by 1 all user-defined variables that are of type 'realcons'.
incrementReals := proc()
local A := select( u -> type( eval(u), 'realcons' ), [ anames( 'user' ) ] ):
local B := map( u -> eval(u) + 1, A ):
assign( A =~ B );
return NULL:
end proc:
# Increment.
incrementReals();
# Confirm that a and b were incremented, but b was not.
'a' = a, 'b' = b, 'c' = c; # a = 4, b = 1 - I, c = 1.5

Pattern matching from a Record type

I have a record type, for example:
Record matrixInt : Type := mkMatrixInt {
const : vector nat dim;
args : vector (matrix dim dim) argCnt
}.
I have a pattern matching where it returns the type of matrixInt, I called it p for example: (where function_name p will return a type of matrixInt. I want to separate p into 2 fields: const and args, for example the draft code I want:
Definition my_function cons arg p :=
match function_name p with
| const => const + cons
| args => args + arg
end.
Could you please help me to write the pattern matching for p that returns 2 fields const; args ?
Thank you very much!
For the record (pun intended):
Record test :=
{ T : Type
; t : T
}.
(* The fields names are indeed accessor functions *)
Definition foo (x : test) : T x := t x.
(* you can destruct a record by matching against its data constructor *)
Definition bar (x : test) : T x :=
match x as _x return T _x with
| Build_test T' t' => t'
end.
(* You can even destruct a record with a let *)
Definition baz (x : test) : T x :=
let (T', t') as _x return T _x := x in t'.
Thank you I have found the answer:(const p) and (args p)