Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MathCompで文字列を使う

 MathCompで文字列を使う

SUHARA Hiromichi

September 29, 2019
Tweet

More Decks by SUHARA Hiromichi

Other Decks in Programming

Transcript

  1. w .BUIFNBUJDBM$PNQPOFOUT .BUI$PNQ NBUIDPNQ NBUIDPNQ w $PRͷ֦ுύοέʔδɺ$PR443FqFDU.BUI$PNQ 443FqFDUʢͱ$PRͱʣΛؚΉશମͷ໊শͱͯ͠࢖ΘΕΔΑ͏ʹͳͬͨɻ೥͜Ζʙɻ w =>

    : / ͷλΫςΟΧϧΛಋೖ͠ɺapplyɺcaseɺrewriteͳͲͷλΫςΟΫεΛ࠶ఆٛ͢Δɻ 4UBOEBSE$PRͱൺ΂ͯɺՄಡੑΛམͱͣ͞ʹ୹͘ॻ͘͜ͱ͕Ͱ͖Δɻ'PSUSBOWT$ w Α͘੔ཧ͞ΕͨϥΠϒϥϦ͔ΒͳΔɻ w ʮܾఆੑͷ͋Δಉ஋ؔ܎ͷ͋ΔܕʯʢCPPMܕͷ౳͕ࣜ࢖͑Δɺਅِ͕Ұҙʹܾ·ΔʣͷΫϥεΛϧʔτͱ͢Δɻ CPPM஋ͷ໋୊ΛຒΊࠐΜͰ࢖͏ɻຒΊࠐΉ͚ͩͳΒ༰қ͕ͩɻ ෆ౳߸≦ ͸CPPMܕͷؔ਺leqͰ͋Δɻͦͷ··Ͱ͸omega ͳͲͷλΫςΟΫ͕࢖͑ͳ͍ɻ w 4UBOEBSE$PRͷػೳ͸֓Ͷͦͷ··࢖͑Δɻޓ׵ػೳ΍ཪٕ͕͋Δɻ leq͸4UBOEBSE$PRͷෆ౳߸ͷleʹม׵Ͱ͖Δɻ͜ΕʹΑΓomegaͳͲΛ࢖͏͜ͱ΋Մೳɻ<> w ਺ֶͷূ໌ͨΊɺ͚ͩͰ͸ͳ͍ɻ ʢิ଍ʣຊདྷͷ$PR͸ɺ௚؍ओٛ࿦ཧͷ໋୊Λ1SPQܕͱ͢Δɻূ໌Մೳʢ͔Ͳ͏͔ʣɻ .BUI$PNQͱ͸ 
  2. 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͕ɺʮʯͱಉ஋Ͱ͋Δ͜ͱͷূ໌΋͋Δɻ ͜ΕΛͦͷ··࢖ͬͯ΋Α͍͚ΕͲʜ 
  3. ܾఆੑͷ͋Δಉ஋ؔ܎ͷ͋Δܕ 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ܕ΁ͷରԠ͚͕ͮొ࿥͞ΕΔɻΧϊχΧϧɾϓϩδΣΫγϣϯ 
  4. TUSJOH@FR5ZQFΛఆٛ͢Δͱʜ w 1SPQܕͷ౳ࣜʢʮʯɺ-FJCOJ[ͷ౳ࣜʣʹՃ͑ͯʜ Check @eq : forall T : Type,

    T -> T -> Prop. (* = *) Goal "FOO" = "FOO". Proof. reflexivity. Qed. w CPPM஋Λฦ͢౳ࣜʢʮʯɺܾఆੑͷ͋Δಉ஋ؔ܎ʣ͕࢖͑ΔɻલϖʔδͰ /PUBUJPOΛఆ͍ٛͯ͠ͳ͍͜ͱʹ஫ҙ͍ͯͩ͘͠͞ɻ Check @eq_op : forall eT : eqType, eT -> eT -> bool. (* == *) Compute "FOO" == "FOO". (* true *) Compute "FOO" == "BAR". (* false *) w .BUI$PNQͰূ໌͞Εͨิ୊͕࢖͑ΔΑ͏ʹͳΔɻ 
  5. ূ໌ͷྫ ßGoal forall (s : string), ɹs = "FOO" ->

    if (s == "FOO") then true else false. Proof.ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹ s : string move=> s H. ɹɹɹɹɹɹɹɹɹɹɹH : s = "FOO" case: ifP. Goal : (s == "FOO") = true -> true - done. Goal : (s == "FOO") = false -> false - move/eqP. Goal : s <> "FOO" -> false done. Qed.  ࣗ໌ ΰʔϧͷલఏ෦ΛϦϑϨΫγϣϯ ໃ६ JG৚݅ͷਅِͰ৔߹෼͚ ίϯςΩετʹJOUSP͢Δ
  6. Ϧετͱͷ૊Έ߹ΘͤͱɺaJO 㱨 w Ϧετ΍௚ੵͳͲͱͷ૊Έ߹ΘͤͰ΋ɺʮʯ͕࢖͑Δɻ Compute [:: "FOO"; "BAR"] == [::

    "FOO"; "BAZ"]. Check [:: "ABC"; "DEF"] : seq_eqType string_eqType. w aJO 㱨ɺFMFNFOU@PG Λ࢖͏ྫɻQSFE5ZQFܕɻ Compute "FOO" \in [:: "FOO"; "BAR"]. (* true *) Check [:: "ABC"; "DEF"] : seq_predType string_eqType. Goal "FOO" \in [:: "FOO"; "BAR"]. rewrite inE inE. (* Goal : "FOO" == "FOO" || "FOO" == "BAR" *) 
  7. ิ୊ͷ·ͱΊ  ԋࢉࢠ ิ୊ ؔ࿈͢Δܕ λΫςΟΫͷྫ if then else ifP

    CPPMܕ case: ifP. == eqP FR5ZQFܕ move/eqP. apply/eqP. \in 㱨 inE QSFE5ZQFܕ rewrite inE.
  8. จݙ <>.BUI$PNQެࣜIUUQTNBUIDPNQHJUIVCJP <>-JCSBSZ$PR4USJOHT4USJOHIUUQTDPRJOSJBGSMJCSBSZ$PR4USJOHT4USJOHIUNM <>"TTJB.BICPVCJ &OSJDP5BTTJʮ.BUIFNBUJDBM$PNQPOFOUTʯIUUQTNBUIDPNQHJUIVCJPNDC <>ഡݪֶ ΞϑΣϧτɾϨφϧυʮ$PR443FqFDU.BUI$PNQʹΑΔఆཧূ໌ʯ৿๺ग़൛ <>ΞϑΣϧτϨφϧυఆཧূ໌ࢧԉܥ$PRʹΑΔܗࣜݕূ࠷৽൛ IUUQTTUB⒎BJTUHPKQSFZOBMEB⒎FMEUTTSDPRTTSDPRQEG <>*MZB4FSHFZ

    :BMF/64$PMMFHFʮ1SPHSBNTBOE1SPPGTʯIUUQTJMZBTFSHFZOFUQOQ <>ʮ.BUI$PNQͰจࣈྻΛ࢖͏ʰఆཧূ໌खश͍ʱͷূ໌Λͯ͠Έͨʯ IUUQTRJJUBDPNTVIBSBIJSPNJDIJJUFNTGGEDDD⒎ <>MJBNBUIDPNQIUUQTHJUIVCDPNBNBICPVCJMJBNBUIDPNQ <>*/3*" .JDSPTPGU3FTFBSDI 3FTFBSDI3FQPSUO› ʮ"4NBMM4DBMF3FqFDUJPO&YUFOTJPOGPSUIF$PRTZTUFNʯ 
  9. ʢิ଍ʣ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. 
  10. ʢิ଍ʣ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. 
  11. ʢิ଍ʣ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". 
  12. ʢิ଍ʣϦετܕͳͲͱͷ૊Έ߹Θͤ w Ϧετܕ TFR ɺ௚ੵܕɺΦϓγϣϯܕ΋ܾఆੑͷ͋Δಉ ஋ؔ܎ͷ͋Δܕͱͯ͠ఆٛ͞Ε͍ͯΔʢ<>ʣɻ Check [:: "ABC"; "DEF"]

    : seq_eqType string_eqType. w ৽ͨʹఆٛ͢Δ͜ͱ΋Ͱ͖Δɻ -JTQ෩ͷೋ෼໦Λఆٛ͠ɺ-JTQͷϓϩάϥϜͷূ໌Λ͠ ͨྫ<> 
  13. ʢิ଍ʣJG1ิ୊ w (PBMʹCPPM஋Ͱ৔߹෼͚͢ΔJGUIFOFMTF͕͋Δ৔߹ɺ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 JGUIFOFMTFͳΒʮDBTFJG1ʯͱ͓͍͔֮͑ͯͯ·Θͳ͍ɻ w ʮDBTFFR1ʯͰ΋Α͍৔߹΋͋ΔɻJGͷ৚݅ʹCPPMͷ౳͕ࣜݟ͍͑ͯΔ৔߹ɻ w ʮDBTF) O ʯͰ΋Α͍৔߹΋͋Δɻ)͸ϢχʔΫʢϑϨογϡʣͳલఏͷϥϕϧͰɺ৔߹෼͚ͷʮOʯ· ͨ͸ʮ O GBMTFʯ͕֨ೲ͞ΕΔɻ 
  14. ʢิ଍ʣϦϑϨΫγϣϯ w ϏϡʔػೳͰFR1ิ୊Λ͔ͭͬͯɺ1SPQܕͷ౳ࣜͱCPPM஋Λฦ͢౳ࣜͷؒΛ૬ޓม׵͢Δʢ<> Qʣɻ w move/eqPͷ࣮ߦྫΛࣔ͢ɻ w ུ͞Ε͍ͯΔʮUSVFʯ΋ؚΊͯม׵͞ΕΔɻ a =

    b WT(a == b) = true a <> bWT(a != b) = true w ʮGBMTFʯͷ͍ͭͨ৔߹΋ม׵Ͱ͖Δɻ a <> bWT(a == b) = false a = bWT(a != b) = false ʢࢀߟʣϏϡʔώϯτ͔ΒFMJN5'ิ୊͕ࣗಈతʹิΘΕΔ <> ɻ 
  15. ʢิ଍ʣ-JTQ෩ͷೋ෼໦ʢTUBSTUSJOHܕ͕Մೳʣ 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).