SUHARA Hiromichi
September 29, 2019
380

MathCompで文字列を使う

SUHARA Hiromichi

September 29, 2019

Transcript

4. .BUI$PNQͰจࣈྻΛ࢖͏ w ࣗવ਺͚ͩͰͳ͘จࣈྻΛ࢖͍͍͕ͨɺ.BUI$PNQ ʢ<>ʣʹ͸ͳ͍ɻ w 4UBOEBSE$PRͷ4USJOHܕʢ<>ʣΛ࢖͏ɻ w ͦΕΛ.BUI$PNQͷʮܾఆੑͷ͋Δಉ஋ؔ܎ͷ͋Δܕʯ ͱͯ͠૊ΈࠐΉʢ<>ɺ<>Qʣɻ w

ϓϩάϥϜͷূ໌ʹศརʹ࢖͑Δɻ 
5. 4UBOEBSE$PRͷTUSJOHܕ Require Import String. Open Scope string_scope. Check "FOO" : string. w TUSJOHܕ͸BTDJJܕΛίϯετϥΫλͰ૊Έཱͯͨ΋ͷɻ w BTDJJܕ͸CPPMܕΛίϯετϥΫλͰ૊Έཱͯͨ΋ͷɻݸɻ Check String.eqb : string -> string -> bool. w ಉ஋ؔ܎Λද͢CPPMܕͷؔ਺eqb͕ఆٛ͞Ε͍ͯΔɻ'JYFEQPJOUͰී௨ʹఆٛɻ  Lemma String.eqb_spec : forall s1 s2 : string, reflect (s1 = s2) (String.eqb s1 s2) w ಉ஋ؔ܎Λද͢CPPMܕͷؔ਺eqb͕ɺʮ ʯͱಉ஋Ͱ͋Δ͜ͱͷূ໌΋͋Δɻ ͜ΕΛͦͷ··࢖ͬͯ΋Α͍͚ΕͲʜ  6. ܾఆੑͷ͋Δಉ஋ؔ܎ͷ͋Δܕ w FR5ZQFܕΫϥεͷΠϯελϯεͷܕTUSJOH@FR5ZQFܕΛఆٛ͢Δɻ &R.JYJOͱ&R5ZQF͸.BUI$PNQͰ༻ҙ͞Ε͍ͯΔɻ Definition string_eqMixin := @EqMixin string String.eqb

String.eqb_spec. Canonical string_eqType := EqType string string_eqMixin. w 1SPQܕͷ౳ࣜͱCPPMܕͷؔ਺ͱ͕ಉ஋Ͱ͋Δ͜ͱͷূ໌͕ཁΔɻ Lemma String.eqb_spec : forall s1 s2 : string, reflect (s1 = s2) (String.eqb s1 s2) w TUSJOHܕ͔ΒTUSJOH@FR5ZQFܕ΁ͷରԠ͚͕ͮొ࿥͞ΕΔɻΧϊχΧϧɾϓϩδΣΫγϣϯ 

13. ʢิ଍ʣBTDJJHܕͱTUSJOHܕͷఆٛ Inductive ascii : Set := Ascii (_ _ _

_ _ _ _ _ : bool). Definition Ascii.eqb (a b : ascii) : bool := match a, b with | (Ascii a0 a1 a2 a3 a4 a5 a6 a7), (Ascii b0 b1 b2 b3 b4 b5 b6 b7) => Bool.eqb a0 b0 && Bool.eqb a1 b1 && Bool.eqb a2 b2 && Bool.eqb a3 b3ɹ&& Bool.eqb a4 b4 && Bool.eqb a5 b5 && Bool.eqb a6 b6 && Bool.eqb a7 b7 end. Inductive string : Set := | EmptyString : string | String : ascii -> string -> string. Fixpoint String.eqb s1 s2 : bool := match s1, s2 with | EmptyString, EmptyString => true | (String c1 s1'), (String c2 s2') => Ascii.eqb c1 c2 && String.eqb s1' s2' | _,_ => false end. 
14. ʢิ଍ʣFR5ZQFܕΫϥεఆٛ Inductive reflect (P : Prop) : bool -> Prop

:= | ReflectT : P -> reflect P true | ReflectF : ~ P -> reflect P false. Record eqMixin (T : Type) := EqMixin { eq_op : T -> T -> bool; ɹɹɹɹ(* eq_op ͸ bool஋ͷ౳ࣜ *) a : forall x y : T, reflect (x = y) (eq_op x y) }. Record eqType := EqType { sort : Type; m : mixin_of sort }. Check @op_op : forall T : Type, mixin_of T -> T -> T -> bool. 
15. ʢิ଍ʣTUSJOHܕͱTUSJOH@FR5ZQFܕͷؔ܎ จࣈྻ"#$͕TPSU@FR5ZQFܕͰ͋Δ͔ͷΑ͏ʹʜ w ίΞʔγϣϯ TUSJOH@FR5ZQFܕͷจ຺Ͱʹ"#$Λॻ͍ͯ΋ɺܕม׵͕ิΘΕΔɻ Check "ABC" : string_eqTypeɹ: eqType.

Check "ABC" : Equality.sort string_eqType. w ΧϊχΧϧɾϓϩδΣΫγϣϯ ؔ਺FR@PQͷʢলུՄೳͳʣୈҾ਺͸FR5ZQFܕͳͷͰTUSJOH@FR5ZQFͰ͋Δ΂͖ɻ͔͠͠ɺୈҾ਺ʹTUSJOH ܕ"#\$Λॻ͍ͯ΋ɺTUSJOH@FR5ZQF͕࿈૝͞ΕΔɻTUSJOHΛFR5ZQFʹQSPKFDUJPOͨ͠ͱ͖ͷTPMVUJPO͸TUJSJOH@FR5ZQF Fail Check @eq_op string "ABC" "ABC". Check @eq_op string_eqType "ABC" "ABC". Check "ABC" == "ABC". 
16. ʢิ଍ʣϦετܕͳͲͱͷ૊Έ߹Θͤ w Ϧετܕ TFR ɺ௚ੵܕɺΦϓγϣϯܕ΋ܾఆੑͷ͋Δಉ ஋ؔ܎ͷ͋Δܕͱͯ͠ఆٛ͞Ε͍ͯΔʢ<>ʣɻ Check [:: "ABC"; "DEF"]

: seq_eqType string_eqType. w ৽ͨʹఆٛ͢Δ͜ͱ΋Ͱ͖Δɻ -JTQ෩ͷೋ෼໦Λఆٛ͠ɺ-JTQͷϓϩάϥϜͷূ໌Λ͠ ͨྫ<> 
17. ʢิ଍ʣJG1ิ୊ w (PBMʹCPPM஋Ͱ৔߹෼͚͢ΔJGUIFOFMTF͕͋Δ৔߹ɺJG৚͕݅੒ཱ͢Δ৔߹ͱɺ੒ཱ͠ͳ͍৔߹Ͱ৔߹෼͚͢Δ ʢ<><> ɻ w *OEVDUJWFʹఆٛ͞Ε໋ͨ୊JG@TQFDͷ;ͨͭͷίϯετϥΫλͰ৔߹෼͚͢Δʜ Goal forall (n

: nat), n = 42 -> if (n == 42) then true else false  move=> n Hn. move: (@ifP bool (n == 42) true false) => Hif. Check Hif : if_spec (n == 42) true false ((n == 42) = false) (n == 42) (if (n == 42) then true else false). case: Hif => Hcond. )DPOE O USVFͷ৔߹ - Check @IfSpecTrue bool (n == 42) true false ((n == 42) = true): n == 42 -> if_spec (n == 42) true false ((n == 42) = true) true true. done. )DPOE O GBMTFͷ৔߹ - Check @IfSpecTrue bool (n == 42) true false ((n == 42) = false) : n == 42 -> if_spec (n == 42) true false ((n == 42) = false) true true. w JGUIFOFMTFͳΒʮDBTFJG1ʯͱ͓͍͔֮͑ͯͯ·Θͳ͍ɻ w ʮDBTFFR1ʯͰ΋Α͍৔߹΋͋ΔɻJGͷ৚݅ʹCPPMͷ౳͕ࣜݟ͍͑ͯΔ৔߹ɻ w ʮDBTF) O ʯͰ΋Α͍৔߹΋͋Δɻ)͸ϢχʔΫʢϑϨογϡʣͳલఏͷϥϕϧͰɺ৔߹෼͚ͷʮOʯ· ͨ͸ʮ O GBMTFʯ͕֨ೲ͞ΕΔɻ 
18. ʢิ଍ʣϦϑϨΫγϣϯ w ϏϡʔػೳͰFR1ิ୊Λ͔ͭͬͯɺ1SPQܕͷ౳ࣜͱCPPM஋Λฦ͢౳ࣜͷؒΛ૬ޓม׵͢Δʢ<> Qʣɻ w move/eqPͷ࣮ߦྫΛࣔ͢ɻ w ུ͞Ε͍ͯΔʮUSVFʯ΋ؚΊͯม׵͞ΕΔɻ a =

b WT(a == b) = true a <> bWT(a != b) = true w ʮGBMTFʯͷ͍ͭͨ৔߹΋ม׵Ͱ͖Δɻ a <> bWT(a == b) = false a = bWT(a != b) = false ʢࢀߟʣϏϡʔώϯτ͔ΒFMJN5'ิ୊͕ࣗಈతʹิΘΕΔ <> ɻ 
19. ʢิ଍ʣ-JTQ෩ͷೋ෼໦ʢTUBSTUSJOHܕ͕Մೳʣ Fixpoint eqStar (T : eqType) (x y : star

T) : bool := match (x, y) with | (S_ATOM a, S_ATOM b) => a == b (* eqType *) | (S_CONS x1 y1, S_CONS x2 y2) => eqStar x1 x2 && eqStar y1 y2 | _ => false end. Lemma star_eqP : forall (T : eqType) (x y : star T),reflect (x = y)(eqStar x y). Definition star_eqMixin (T : eqType) := @EqMixin (star T)(eqStar T)(star_eqP T). Canonical star_eqType (T : eqType) := EqType (star T)(star_eqMixin T). w ཁૉͱͯ͠จࣈྻΛ࢖͑ΔΑ͏ʹ͠ɺจࣈྻ/*-ΛGBMTF͓Αͼ'BMTFͱͯ͠ʮຒΊࠐΉʯɻ Definition star_exp := star string.ɹ (* Sࣜ *) Coercion is_not_nil (x : star_exp) : bool := x != (S_ATOM "NIL"). Coercion s_quote (s : string) : star_exp := (S_ATOM s). 