diagrams / diagrams-builder

Utilities for creating diagram-building tools
Other
12 stars 15 forks source link

prettyPrint doesn't respect pattern matching #24

Open david-hoze opened 8 years ago

david-hoze commented 8 years ago

Hi, I have the following code in one of my diagrams in latex:

import Diagrams.TwoD.Text

superScript c = case c of
            '0' -> '\x2070'
            '1' -> '\xb9'
            '2' -> '\xb2'
            '3' -> '\xb3'
            '4' -> '\x2074'
            '5' -> '\x2075'
            '6' -> '\x2076'
            '7' -> '\x2077'
            '8' -> '\x2078'
            '9' -> '\x2079'
            '+' -> '\x207A'
            '-' -> '\x207B'
            '=' -> '\x207C'
            '(' -> '\x207D'
            ')' -> '\x207E'
            'i' -> '\x2071'
            'n' -> '\x207F'
            otherwise -> c

format ('^':'-':d:chars) = [superScript '-',superScript d] ++ format chars
format ('^':d:chars) = [superScript d] ++ format chars
format (c:chars) = [c] ++ format chars
format [] = []

dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)

which doesn't render the diagram at all. I checked the diagrams-builder-cairo's temporary haskell file, and found that it didn't translate the format function's pattern matching as expected:

{-# LANGUAGE NoMonomorphismRestriction, TypeFamilies,
  FlexibleContexts #-}
module Diagram1804289383846930886 where
import Diagrams.Prelude
import Diagrams.Backend.Cairo
import Diagrams.TwoD.Text
superScript c
  = case c of
        '0' -> '\8304'
        '1' -> '\185'
        '2' -> '\178'
        '3' -> '\179'
        '4' -> '\8308'
        '5' -> '\8309'
        '6' -> '\8310'
        '7' -> '\8311'
        '8' -> '\8312'
        '9' -> '\8313'
        '+' -> '\8314'
        '-' -> '\8315'
        '=' -> '\8316'
        '(' -> '\8317'
        ')' -> '\8318'
        'i' -> '\8305'
        'n' -> '\8319'
        otherwise -> c
format ((('^' : '-') : d) : chars)
  = [superScript '-', superScript d] ++ format chars
format (('^' : d) : chars) = [superScript d] ++ format chars
format (c : chars) = [c] ++ format chars
format [] = []
dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)

which in turn causes the following error message (it actually produces something cryptic, see #23):

bug.hs:27:17:
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’
    In the pattern: '-'
    In the pattern: '^' : '-'
    In the pattern: ('^' : '-') : d

bug.hs:28:35:
    Couldn't match expected type ‘Char’ with actual type ‘[[Char]]’
    In the first argument of ‘superScript’, namely ‘d’
    In the expression: superScript d

bug.hs:29:10:
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’
    In the pattern: '^'
    In the pattern: '^' : d
    In the pattern: ('^' : d) : chars

bug.hs:29:43:
    Couldn't match expected type ‘Char’ with actual type ‘[[Char]]’
    In the first argument of ‘superScript’, namely ‘d’
    In the expression: superScript d

bug.hs:30:23:
    Couldn't match expected type ‘Char’ with actual type ‘[[Char]]’
    In the expression: c
    In the first argument of ‘(++)’, namely ‘[c]’

bug.hs:32:20:
    Couldn't match type ‘Char’ with ‘[[Char]]’
    Expected type: [[[Char]]]
      Actual type: [Char]
    In the first argument of ‘format’, namely ‘"A^-3d^2+3A^-1d+3A"’
    In the first argument of ‘text’, namely
      ‘(format "A^-3d^2+3A^-1d+3A")’
    In the first argument of ‘(#)’, namely
      ‘text (format "A^-3d^2+3A^-1d+3A")’

If I replace it by explicitly writing down the parenthesis precedence like so:

\begin{diagram}[width=400,height=75]
import Diagrams.TwoD.Text

superScript c = case c of
            '0' -> '\x2070'
            '1' -> '\xb9'
            '2' -> '\xb2'
            '3' -> '\xb3'
            '4' -> '\x2074'
            '5' -> '\x2075'
            '6' -> '\x2076'
            '7' -> '\x2077'
            '8' -> '\x2078'
            '9' -> '\x2079'
            '+' -> '\x207A'
            '-' -> '\x207B'
            '=' -> '\x207C'
            '(' -> '\x207D'
            ')' -> '\x207E'
            'i' -> '\x2071'
            'n' -> '\x207F'
            otherwise -> c

format ('^':('-':(d:(chars)))) = [superScript '-',superScript d] ++ format chars
format ('^':(d:chars)) = [superScript d] ++ format chars
format (c:chars) = [c] ++ format chars
format [] = []

dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)

it works fine.

Here is a latex document with both examples:

% Created 2015-11-08 Sun 03:21
\documentclass[11pt]{article}
\tolerance=1000
\usepackage{amsmath}
\usepackage[backend=cairo, extension=pdf, outputdir=diagrams]{/home/amitai/github/haskell/diagrams/diagrams-builder/latex/diagrams-latex}
\usepackage{graphicx}
\author{Amitai Hoze}
\date{\today}
\title{Bug reproduction}
\begin{document}

\maketitle

\section{Reproducing the bug}
\label{sec:orgheadline1}
\begin{diagram}[width=400,height=75]
import Diagrams.TwoD.Text

superScript c = case c of
            '0' -> '\x2070'
            '1' -> '\xb9'
            '2' -> '\xb2'
            '3' -> '\xb3'
            '4' -> '\x2074'
            '5' -> '\x2075'
            '6' -> '\x2076'
            '7' -> '\x2077'
            '8' -> '\x2078'
            '9' -> '\x2079'
            '+' -> '\x207A'
            '-' -> '\x207B'
            '=' -> '\x207C'
            '(' -> '\x207D'
            ')' -> '\x207E'
            'i' -> '\x2071'
            'n' -> '\x207F'
            otherwise -> c

format ('^':'-':d:chars) = [superScript '-',superScript d] ++ format chars
format ('^':d:chars) = [superScript d] ++ format chars
format (c:chars) = [c] ++ format chars
format [] = []

dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)
\end{diagram}

\section{Temporary fix}
\label{sec:orgheadline2}
\begin{diagram}[width=400,height=75]
import Diagrams.TwoD.Text

superScript c = case c of
            '0' -> '\x2070'
            '1' -> '\xb9'
            '2' -> '\xb2'
            '3' -> '\xb3'
            '4' -> '\x2074'
            '5' -> '\x2075'
            '6' -> '\x2076'
            '7' -> '\x2077'
            '8' -> '\x2078'
            '9' -> '\x2079'
            '+' -> '\x207A'
            '-' -> '\x207B'
            '=' -> '\x207C'
            '(' -> '\x207D'
            ')' -> '\x207E'
            'i' -> '\x2071'
            'n' -> '\x207F'
            otherwise -> c

format ('^':('-':(d:(chars)))) = [superScript '-',superScript d] ++ format chars
format ('^':(d:chars)) = [superScript d] ++ format chars
format (c:chars) = [c] ++ format chars
format [] = []

dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)
\end{diagram}
\end{document}

versions used diagrams-builder: 0.7.2.0 ghc: 7.10.2 diagrams-core: 1.3.0.3 diagrams-cairo: 1.3.0.5 NixOS: https://github.com/nixos/nixpkgs/commit/819a375382dc86bcc3ee685bb54eafbf449f0c1f

byorgey commented 8 years ago

Thanks for the report! Do you know what version of haskell-src-exts this was built with?