Agda:解析嵌套列表

时间:2012-09-12 01:54:37

标签: parsing haskell agda dependent-type

我正在尝试解析Agda中的嵌套列表。我在google上搜索过,我发现最接近的是在Haskell中解析,但是通常使用像“parsec”这样的库,这些库在Agda中不可用。

所以我想用结果类型"((1,2,3),(4,5,6))"解析(List (List Nat))

应支持更多嵌套列表(深度为5),例如,深度3为(List (List (List Nat)))

我的代码非常冗长且繁琐,它只适用于(List (List Nat)),但不适用于其他嵌套列表。我自己没有取得任何进展。

如果有帮助,我想重新使用我之前发布的帖子first answer中的splitBy

NesList : ℕ → Set
NesList 0 = ℕ -- this case is easy
NesList 1 = List ℕ -- this case is easy
NesList 2 = List (List ℕ) 
NesList 3 = List (List (List ℕ))
NesList 4 = List (List (List (List ℕ)))
NesList 5 = List (List (List (List (List ℕ)))) -- I am only interested to list depth 5
NesList _ = ℕ -- this is a hack, but I think okay for now


-- My implementation is *not* shown here
--
--
-- (it's about 80 lines long and uses 3 different functions
parseList2 : List Char → Maybe (List (List ℕ))
parseList2 _ = nothing -- dummy result


parseList : (dept : ℕ) → String → Maybe (NesList dept)
parseList 2 s = parseList2 (toList s)
parseList _ _ = nothing



-- Test Cases that are working (in my version)

p1 : parseList 2 "((1,2,3),(4,5,6))" ≡ just ((1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ [])
p1 = refl


p2 : parseList 2 "((1,2,3),(4,5,6),(7,8,9,10))" ≡ just ((1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ (7 ∷ 8 ∷ 9 ∷ 10 ∷ []) ∷ [])
p2 = refl

p3 : parseList 2 "((1),(2))" ≡ just ((1 ∷ []) ∷ (2 ∷ []) ∷ [])
p3 = refl

p4 : parseList 2 "((1,2))" ≡ just ((1 ∷ 2 ∷ []) ∷ [])
p4 = refl

-- Test Cases that are not working 
-- i.e., List (List (List Nat))

lp5 : parseList 3 "(((1,2),(3,4)),((5,6),(7,8)))" ≡ just (  ((1 ∷ 2 ∷ []) ∷ (3 ∷ 4 ∷ []) ∷ []) ∷ ((5 ∷ 6 ∷ []) ∷ (7 ∷ 8 ∷ []) ∷ []) ∷ [])
lp5 = refl

EDIT1 **

Connor在ICFP上的演讲是在线 - 标题是“Agda好奇吗?” 它是从两天前开始的。看看!!

观看视频:
http://www.youtube.com/watch?v=XGyJ519RY6Y

-
EDIT2:
我发现一个链接似乎几乎是我解析所需的代码 提供了tokenize功能:
https://github.com/fkettelhoit/agda-prelude/blob/master/Examples/PrefixCalculator.agda

-
EDIT3:
我终于找到了一个简单的组合库,应该足够快。库中没有示例,因此我仍然需要了解如何解决问题 这是链接:

https://github.com/crypto-agda/agda-nplib/blob/master/lib/Text/Parser.agda

Nicolas Pouillard在线提供更多agda代码:
https://github.com/crypto-agda

3 个答案:

答案 0 :(得分:2)

我现在无法访问agda实现,所以我无法检查语法,但这就是我要解决的问题。

首先,可以简化NesList。

NesList 0 = ℕ
NesList (succ n) = List (NesList n)

然后你需要一个通用的列表解析功能。而不是可以使用List来指定替代解析。返回值是一个成功的解析和字符串的其余部分。

Parser : Set -> Set
Parser a = List Char -> Maybe (Pair a (List Char))

给定x类型的解析器例程,解析括号 - 描述的以逗号分隔的x列表。

parseGeneralList : { a : Set } Parser a -> Parser (List a)
parseGeneralList = ...implement me!...

解析一般的NesList。

parseNesList : (a : ℕ) -> Parser (NesList a)
parseNesList 0 = parseNat
parseNesList (succ n) = parseGeneralList (parseNesList n)

编辑:正如评论中指出的那样,使用这种解析器的代码不会通过agda的终止检查程序。我想如果你想做解析器组合器,你需要一个基于流的设置。

答案 1 :(得分:1)

我参加派对有点晚了,但我目前正在编写一个完整的解析器组合库,我有一个相当紧凑的解决方案,重新使用@NovaDenizen建议的整齐NesList类型。

我使用差异列表,但基本列表也是如此(我们只需将DList.toList替换为List.reverse,因为chainl1会从左到右聚合值。

NList : Set → ℕ → Set
NList A zero    = A
NList A (suc n) = List (NList A n)

NList′ : {A : Set} → [ Parser A ] →
         (n : ℕ) → [ Parser (NList A n) ]
NList′ A zero    = A
NList′ A (suc n) = parens $ return $ DList.toList <$>
                   chainl1 (DList.[_] <$> NList′ A n)
                           (return $ DList._++_ <$ char ',')

所有测试用例都成功通过。我已将示例添加到the (monolithic) poc file,以便您自行检查

_ : "((1,2,3),(4,5,6))" ∈ NList′ decimal 2
_ = (1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ [] !

_ : "((1,2,3),(4,5,6),(7,8,9,10))" ∈ NList′ decimal 2
_ = (1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ (7 ∷ 8 ∷ 9 ∷ 10 ∷ []) ∷ [] !

_ : "((1),(2))" ∈ NList′ decimal 2
_ = (1 ∷ []) ∷ (2 ∷ []) ∷ [] !

_ : "((1,2))" ∈ NList′ decimal 2
_ = (1 ∷ 2 ∷ []) ∷ [] !

_ : "(((1,2),(3,4)),((5,6),(7,8)))" ∈ NList′ decimal 3
_ = ((1 ∷ 2 ∷ []) ∷ (3 ∷ 4 ∷ []) ∷ []) ∷
    ((5 ∷ 6 ∷ []) ∷ (7 ∷ 8 ∷ []) ∷ []) ∷ [] !

答案 2 :(得分:0)

我在这里使用解析器组合器发布我的解决方案。 它使用agda-nplib library is on github。 代码远非最佳,但它的工作原理。

module NewParser where

-- dummy
open import Data.Maybe
open import Data.Bool

-- includes
open import Data.List hiding (map)

-- ***
-- WAS PRELUDE IMPORTS
open import StringHelpers using (charToℕ; stringToℕ)
open import Data.String hiding (_==_; _++_)
open import Data.Char
open import Function
open import Data.Nat
open import Data.Unit
open import Data.Maybe


-- https://github.com/crypto-agda/agda-nplib/tree/master/lib/Text
open import Text.Parser
open import ParserHelpers



--- ****
--- Lessons Learned, this is the key:
--- (was a basic error that tyeps where too specific, generalisation not possible)

-- parseList : {A : Set} → Parser (Maybe A) → Parser (Maybe A) → ℕ → Parser (List (Maybe A))
-- converted to
-- parseList : {A : Set} → Parser A → Parser A → ℕ → Parser (List A)



-- *****
-- General ... Normal List (depth 1)

parseList : {A : Set} → Parser A → Parser A → ℕ → Parser (List A)
parseList oneMatcher manyMatcher n = ⟪ _++_ · (map toL oneMatcher) · (many n manyMatcher) ⟫

parseBracketList : {A : Set} → Parser A → Parser A → ℕ → Parser (List A)
parseBracketList oneMatcher manyMatcher n = bracket '(' (parseList oneMatcher manyMatcher n) ')'

parseCommaListConvert : {A : Set} → (List Char → A) → (Parser (List Char)) → ℕ → Parser (List A)
parseCommaListConvert convert parser = parseBracketList (⟪ convert · parser ⟫) (⟪ convert · parseOne "," *> parser ⟫) 


-- For Numbers

number : Parser (List Char)
number = manyOneOf (toList "1234567890")

parseNumList : ℕ → Parser (List (Maybe ℕ))
parseNumList = parseCommaListConvert charsToℕ number


-- Nested List (depth 2)
--

parseListListNum : ℕ → Parser (List (List (Maybe ℕ)))
parseListListNum n = parseList (parseNumList n) ((parseOne ",") *> (parseNumList n)) n


parseManyLists : ℕ → Parser (List (List (Maybe ℕ)))
parseManyLists n = bracket '(' (parseListListNum n) ')'



-- Run the Parsers
--

open import MaybeEliminatorHelper

-- max number of terms is the number of characters in the string
-- this is for the termination checker
runParseList' : String → Maybe (List (Maybe ℕ))
runParseList' s = runParser (parseNumList (strLength s)) (toList s)

runParseList : String → Maybe (List ℕ)
runParseList = maybe-list-maybe-eliminate ∘ runParseList'

-- nested list
runParseNesList' : String → Maybe (List (List( Maybe ℕ)))
runParseNesList' s = runParser (parseManyLists (length (toList s))) (toList s)

runParseNesList : String → Maybe (List (List ℕ))
runParseNesList = maybe-list-list-maybe-eliminate ∘ runParseNesList' 

这是我的助手功能:

module MaybeEliminatorHelper where

open import Data.Maybe
open import Category.Monad
open import Function

open import Data.List

open import Category.Functor


sequence-maybe : ∀ {a} {A : Set a} → List (Maybe A) → Maybe (List A)
sequence-maybe = sequence Data.Maybe.monad


join : {A : Set} → Maybe (Maybe A) → Maybe A
join m = m >>= id
  where 
    open RawMonad Data.Maybe.monad


maybe-list-elem : {A : Set} → Maybe (List (Maybe A)) → Maybe (List A)
maybe-list-elem mlm = join (sequence-maybe <$> mlm)
  where open RawFunctor functor

{-
sequence-maybe : [Maybe a] -> Maybe [a]

join :: Maybe (Maybe a) -> Maybe a


Maybe (List (List (Maybe A))
  Maybe.fmap (List.fmap sequenc-maybe)
Maybe (List (Maybe (List A))
  Maybe.fmap sequence-maybe
Maybe (Maybe (List (List A)))
  join
Maybe (List (List A))



join . Maybe.fmap sequence-maybe . Maybe.fmap (List.fmap sequenc-maybe)

join . Maybe.fmap (sequence-maybe . List.fmap sequenc-maybe)
(short form)

-}

maybe-list-elem2 : {A : Set} → Maybe (List (List (Maybe A))) → Maybe (List (List A)) 
maybe-list-elem2 = join ∘ Mfmap (sequence-maybe ∘ Lfmap sequence-maybe)
  where
    open RawMonad Data.Maybe.monad hiding (join) renaming (_<$>_ to Mfmap)
    open RawMonad Data.List.monad hiding (join) renaming (_<$>_ to Lfmap)


maybe-list-maybe-eliminate = maybe-list-elem

maybe-list-list-maybe-eliminate = maybe-list-elem2

其他助手功能:

-- ***
-- WAS PRELUDE IMPORTS
open import StringHelpers using (charToℕ; stringToℕ)

open import Data.String hiding (_==_)
open import Data.Char
open import Function
open import Data.Nat
open import Data.Unit
open import Data.Maybe



open import Text.Parser

open import Data.List


-- mini helpers
--

parseOne : String → Parser Char
parseOne = oneOf ∘ toList 

strLength : String → ℕ
strLength = length ∘ toList 


-- misc helpers
--

charsToℕ : List Char → Maybe ℕ
charsToℕ [] = nothing
charsToℕ xs = stringToℕ (fromList xs)

toL : ∀ {a} {A : Set a} → A → List A
toL x = x ∷ []


-- test
l : List (Maybe ℕ)
l = (just 3) ∷ (just 3) ∷ []


-- Parser Helpers Nicolas
--
isSpace : Char → Bool
isSpace = (_==_ ' ')

spaces : Parser ⊤
spaces = manySat isSpace *> pure _

-- decide if seperator before after brackets is spaces
someSpaces : Parser ⊤
someSpaces = someSat isSpace *> pure _

tok : Char → Parser ⊤
tok c = spaces *> char c *> pure _

bracket : ∀ {A} → Char → Parser A → Char → Parser A
bracket start p stop = tok start *> p <* tok stop

以及一些测试用例:

tn09 : pList "12,13,,14" ≡ nothing
tn09 = refl

tn08 : pList "" ≡ nothing
tn08 = refl

tn07 : pList "12,13,14" ≡ nothing
tn07 = refl

-- not working tn06 : pList "(12,13,14,17)," ≡ nothing
-- not working tn06 = refl

tn05 : pList "aa,bb,cc" ≡ nothing
tn05 = refl

tn04 : pList "11" ≡ nothing 
tn04 = refl

tn03 : pList "(11,12,13)" ≡ just (11 ∷ 12 ∷ 13 ∷ [])
tn03 = refl


-- new testcases
tn11 : pList2 "((1,2,3),(4,5,6),(7,8,9))" ≡ just ((1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ (7 ∷ 8 ∷ 9 ∷ []) ∷ [])
tn11 = refl

-- old testcases
p1 : pList2 "((1,2,3),(4,5,6))" ≡ just ((1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ [])
p1 = refl


p2 : pList2 "((1,2,3),(4,5,6),(7,8,9,10))" ≡ just ((1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ (7 ∷ 8 ∷ 9 ∷ 10 ∷ []) ∷ [])
p2 = refl

p3 : pList2 "((1),(2))" ≡ just ((1 ∷ []) ∷ (2 ∷ []) ∷ [])
p3 = refl

p4 : pList2 "((1,2))" ≡ just ((1 ∷ 2 ∷ []) ∷ [])
p4 = refl

我愿意接受改进代码的建议。