{"id":7569,"date":"2022-12-13T13:49:46","date_gmt":"2022-12-13T11:49:46","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7569"},"modified":"2023-04-20T17:26:52","modified_gmt":"2023-04-20T15:26:52","slug":"13-dic-22","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/13-dic-22\/","title":{"rendered":"El tipo de las expresiones aritm\u00e9ticas: Valor de la resta"},"content":{"rendered":"<p>Usando el <a href=\"https:\/\/bit.ly\/40vCQUh\">tipo de las expresiones aritm\u00e9ticas<\/a>, definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   resta :: Expr -> Expr -> Expr\n<\/pre>\n<p>tal que <code>resta e1 e2<\/code> es la expresi\u00f3n correspondiente a la diferencia de <code>e1<\/code> y <code>e2<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   resta (Lit 42) (Lit 2)  ==  Suma (Lit 42) (Op (Lit 2))\n<\/pre>\n<p>Comprobar con QuickCheck que<\/p>\n<pre lang=\"text\">\n   valor (resta x y) == valor x - valor y\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nimport Tipo_expresion_aritmetica (Expr (..))\nimport Valor_de_una_expresion_aritmetica (valor)\nimport Test.QuickCheck\n\nresta :: Expr -> Expr -> Expr\nresta x y = Suma x (Op y)\n\n-- Comprobaci\u00f3n de la propiedad\n-- ============================\n\n-- (exprArbitraria n) es una expresi\u00f3n aleatoria de tama\u00f1o n. Por\n-- ejemplo,\n--    \u03bb> sample (exprArbitraria 3)\n--    Op (Op (Lit 0))\n--    SiCero (Lit 0) (Lit (-2)) (Lit (-1))\n--    Op (Suma (Lit 3) (Lit 0))\n--    Op (Lit 5)\n--    Op (Lit (-1))\n--    Op (Op (Lit 9))\n--    Suma (Lit (-12)) (Lit (-12))\n--    Suma (Lit (-9)) (Lit 10)\n--    Op (Suma (Lit 8) (Lit 15))\n--    SiCero (Lit 16) (Lit 9) (Lit (-5))\n--    Suma (Lit (-3)) (Lit 1)\nexprArbitraria :: Int -> Gen Expr\nexprArbitraria n\n  | n <= 1 = Lit <$> arbitrary\n  | otherwise = oneof\n                [ Lit <$> arbitrary\n                , let m = div n 2\n                  in Suma <$> exprArbitraria m <*> exprArbitraria m\n                , Op <$> exprArbitraria (n - 1)\n                , let m = div n 3\n                  in SiCero <$> exprArbitraria m\n                            <*> exprArbitraria m\n                            <*> exprArbitraria m ]\n\n-- Expr es subclase de Arbitrary\ninstance Arbitrary Expr where\n  arbitrary = sized exprArbitraria\n\n\n-- La propiedad es\nprop_resta :: Expr -> Expr -> Property\nprop_resta x y =\n  valor (resta x y) === valor x - valor y\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_resta\n--    +++ OK, passed 100 tests.\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom random import choice, randint\n\nfrom hypothesis import given\nfrom hypothesis import strategies as st\n\nfrom src.tipo_expresion_aritmetica import Expr, Lit, Op, SiCero, Suma\nfrom src.valor_de_una_expresion_aritmetica import valor\n\n\ndef resta(x: Expr, y: Expr) -> Expr:\n    return Suma(x, Op(y))\n\n# Comprobaci\u00f3n de la propiedad\n# ============================\n\n# exprArbitraria(n) es una expresi\u00f3n aleatoria de tama\u00f1o n. Por\n# ejemplo,\n#    >>> exprArbitraria(3)\n#    Op(x=Op(x=Lit(x=9)))\n#    >>> exprArbitraria(3)\n#    Op(x=SiCero(x=Lit(x=6), y=Lit(x=2), z=Lit(x=6)))\n#    >>> exprArbitraria(3)\n#    Suma(x=Lit(x=8), y=Lit(x=2))\ndef exprArbitraria(n: int) -> Expr:\n    if n <= 1:\n        return Lit(randint(0, 10))\n    m = n \/\/ 2\n    return choice([Lit(randint(0, 10)),\n                   Suma(exprArbitraria(m), exprArbitraria(m)),\n                   Op(exprArbitraria(n - 1)),\n                   SiCero(exprArbitraria(m),\n                          exprArbitraria(m),\n                          exprArbitraria(m))])\n\n# La propiedad es\n@given(st.integers(min_value=1, max_value=10),\n       st.integers(min_value=1, max_value=10))\ndef test_mismaForma(n1: int, n2: int) -> None:\n    x = exprArbitraria(n1)\n    y = exprArbitraria(n2)\n    assert valor(resta(x, y)) == valor(x) - valor(y)\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q valor_de_la_resta.py\n#    1 passed in 0.21s\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Usando el tipo de las expresiones aritm\u00e9ticas, definir la funci\u00f3n resta :: Expr -> Expr -> Expr tal que resta e1 e2 es la expresi\u00f3n correspondiente a la diferencia de e1 y e2. Por ejemplo, resta (Lit 42) (Lit 2) == Suma (Lit 42) (Op (Lit 2)) Comprobar con QuickCheck que valor (resta x y)&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[581],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7569"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=7569"}],"version-history":[{"count":7,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7569\/revisions"}],"predecessor-version":[{"id":8080,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7569\/revisions\/8080"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}